InspIRCd  3.0
users.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 linuxdaemon <[email protected]>
5  * Copyright (C) 2019 Matt Schatz <[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-2019 Sadie Powell <[email protected]>
9  * Copyright (C) 2012, 2018-2019 Robby <[email protected]>
10  * Copyright (C) 2012 DjSlash <[email protected]>
11  * Copyright (C) 2012 ChrisTX <[email protected]>
12  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
13  * Copyright (C) 2009 Uli Schlachter <[email protected]>
14  * Copyright (C) 2008 Thomas Stagner <[email protected]>
15  * Copyright (C) 2008 John Brooks <[email protected]>
16  * Copyright (C) 2007-2009 Robin Burchell <[email protected]>
17  * Copyright (C) 2007, 2009 Dennis Friis <[email protected]>
18  * Copyright (C) 2006-2008 Craig Edwards <[email protected]>
19  *
20  * This file is part of InspIRCd. InspIRCd is free software: you can
21  * redistribute it and/or modify it under the terms of the GNU General Public
22  * License as published by the Free Software Foundation, version 2.
23  *
24  * This program is distributed in the hope that it will be useful, but WITHOUT
25  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
27  * details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 
34 #pragma once
35 
36 #include "socket.h"
37 #include "inspsocket.h"
38 #include "mode.h"
39 #include "membership.h"
40 
43 enum ClassTypes {
45  CC_ALLOW = 0,
47  CC_DENY = 1,
49  CC_NAMED = 2
50 };
51 
55 enum RegistrationState {
56 
57 #ifndef _WIN32 // Burlex: This is already defined in win32, luckily it is still 0.
58  REG_NONE = 0, /* Has sent nothing */
59 #endif
60 
61  REG_USER = 1, /* Has sent USER */
62  REG_NICK = 2, /* Has sent NICK */
63  REG_NICKUSER = 3, /* Bitwise combination of REG_NICK and REG_USER */
64  REG_ALL = 7 /* REG_NICKUSER plus next bit along */
65 };
66 
67 enum UserType {
68  USERTYPE_LOCAL = 1,
69  USERTYPE_REMOTE = 2,
70  USERTYPE_SERVER = 3
71 };
72 
75 struct CoreExport ConnectClass : public refcountbase
76 {
77  reference<ConfigTag> config;
80  char type;
81 
83  bool fakelag;
84 
87  std::string name;
88 
91  unsigned int registration_timeout;
92 
95  std::string host;
96 
99  unsigned int pingtime;
100 
104  unsigned long softsendqmax;
105 
109  unsigned long hardsendqmax;
110 
113  unsigned long recvqmax;
114 
117  unsigned int penaltythreshold;
118 
120  unsigned int commandrate;
121 
124  unsigned long maxlocal;
125 
128  unsigned long maxglobal;
129 
133 
136  unsigned int maxchans;
137 
141  unsigned long limit;
142 
146 
151 
154  ConnectClass(ConfigTag* tag, char type, const std::string& mask);
157  ConnectClass(ConfigTag* tag, char type, const std::string& mask, const ConnectClass& parent);
158 
160  void Update(const ConnectClass* newSettings);
161 
162  const std::string& GetName() { return name; }
163  const std::string& GetHost() { return host; }
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 
233 class CoreExport User : public Extensible
234 {
235  private:
238  std::string cached_fullhost;
239 
242  std::string cached_hostip;
243 
246  std::string cached_makehost;
247 
250  std::string cached_fullrealhost;
251 
254  std::string cachedip;
255 
257  std::string displayhost;
258 
260  std::string realhost;
261 
263  std::string realname;
264 
271  std::bitset<ModeParser::MODEID_MAX> modes;
272 
273  public:
278  {
279  public:
284  virtual void Execute(LocalUser* user) = 0;
285  };
286 
290 
293  time_t age;
294 
299  time_t signon;
300 
306 
311  std::string nick;
312 
316  const std::string uuid;
317 
321  std::string ident;
322 
326  std::bitset<64> snomasks;
327 
331 
335 
339  std::string awaymsg;
340 
344  time_t awaytime;
345 
349 
354  unsigned int registered:3;
355 
361  unsigned int quitting:1;
362 
364  const UserType usertype:2;
365 
369  const std::string& GetIPString();
370 
374  const std::string& GetHost(bool uncloak) const;
375 
377  const std::string& GetDisplayedHost() const;
378 
380  const std::string& GetRealHost() const;
381 
383  const std::string& GetRealName() const;
384 
387  irc::sockets::cidr_mask GetCIDRMask();
388 
392  virtual bool SetClientIP(const std::string& address);
393 
394  virtual void SetClientIP(const irc::sockets::sockaddrs& sa);
395 
399  User(const std::string& uid, Server* srv, UserType objtype);
400 
406  virtual const std::string& GetFullHost();
407 
414  virtual const std::string& GetFullRealHost();
415 
419  void InvalidateCache();
420 
425  bool IsAway() const { return (!awaymsg.empty()); }
426 
431  bool IsOper() const { return oper; }
432 
437  bool IsNoticeMaskSet(unsigned char sm);
438 
444  std::string GetModeLetters(bool includeparams = false) const;
445 
450  bool IsModeSet(unsigned char m) const;
451  bool IsModeSet(const ModeHandler* mh) const;
452  bool IsModeSet(const ModeHandler& mh) const { return IsModeSet(&mh); }
453  bool IsModeSet(UserModeReference& moderef) const;
454 
459  void SetMode(ModeHandler* mh, bool value);
460  void SetMode(ModeHandler& mh, bool value) { SetMode(&mh, value); }
461 
468  virtual bool HasCommandPermission(const std::string& command);
469 
477  virtual bool HasPrivPermission(const std::string& privstr);
478 
485  virtual bool HasModePermission(const ModeHandler* mh) const;
486 
491  const std::string& MakeHost();
492 
497  const std::string& MakeHostIP();
498 
502  void Oper(OperInfo* info);
503 
507  void UnOper();
508 
512  void WriteNotice(const std::string& text);
513 
517  virtual void WriteRemoteNotice(const std::string& text);
518 
519  virtual void WriteRemoteNumeric(const Numeric::Numeric& numeric);
520 
521  template <typename T1>
522  void WriteRemoteNumeric(unsigned int numeric, T1 p1)
523  {
524  Numeric::Numeric n(numeric);
525  n.push(p1);
526  WriteRemoteNumeric(n);
527  }
528 
529  template <typename T1, typename T2>
530  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2)
531  {
532  Numeric::Numeric n(numeric);
533  n.push(p1);
534  n.push(p2);
535  WriteRemoteNumeric(n);
536  }
537 
538  template <typename T1, typename T2, typename T3>
539  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
540  {
541  Numeric::Numeric n(numeric);
542  n.push(p1);
543  n.push(p2);
544  n.push(p3);
545  WriteRemoteNumeric(n);
546  }
547 
548  template <typename T1, typename T2, typename T3, typename T4>
549  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
550  {
551  Numeric::Numeric n(numeric);
552  n.push(p1);
553  n.push(p2);
554  n.push(p3);
555  n.push(p4);
556  WriteRemoteNumeric(n);
557  }
558 
559  template <typename T1, typename T2, typename T3, typename T4, typename T5>
560  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
561  {
562  Numeric::Numeric n(numeric);
563  n.push(p1);
564  n.push(p2);
565  n.push(p3);
566  n.push(p4);
567  n.push(p5);
568  WriteRemoteNumeric(n);
569  }
570 
571  void WriteNumeric(const Numeric::Numeric& numeric);
572 
573  template <typename T1>
574  void WriteNumeric(unsigned int numeric, T1 p1)
575  {
576  Numeric::Numeric n(numeric);
577  n.push(p1);
578  WriteNumeric(n);
579  }
580 
581  template <typename T1, typename T2>
582  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2)
583  {
584  Numeric::Numeric n(numeric);
585  n.push(p1);
586  n.push(p2);
587  WriteNumeric(n);
588  }
589 
590  template <typename T1, typename T2, typename T3>
591  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
592  {
593  Numeric::Numeric n(numeric);
594  n.push(p1);
595  n.push(p2);
596  n.push(p3);
597  WriteNumeric(n);
598  }
599 
600  template <typename T1, typename T2, typename T3, typename T4>
601  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
602  {
603  Numeric::Numeric n(numeric);
604  n.push(p1);
605  n.push(p2);
606  n.push(p3);
607  n.push(p4);
608  WriteNumeric(n);
609  }
610 
611  template <typename T1, typename T2, typename T3, typename T4, typename T5>
612  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
613  {
614  Numeric::Numeric n(numeric);
615  n.push(p1);
616  n.push(p2);
617  n.push(p3);
618  n.push(p4);
619  n.push(p5);
620  WriteNumeric(n);
621  }
622 
627  void WriteCommonRaw(ClientProtocol::Event& protoev, bool include_self = true);
628 
637  void ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true);
638 
643  bool SharesChannelWith(User *other);
644 
649  bool ChangeDisplayedHost(const std::string& host);
650 
655  void ChangeRealHost(const std::string& host, bool resetdisplay);
656 
664  bool ChangeIdent(const std::string& newident);
665 
670  bool ChangeRealName(const std::string& real);
671 
677  bool ChangeNick(const std::string& newnick, time_t newts = 0);
678 
682  void PurgeEmptyChannels();
683 
686  virtual ~User();
687  CullResult cull() CXX11_OVERRIDE;
688 
690  bool Deserialize(Data& data) CXX11_OVERRIDE;
691 
693  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
694 };
695 
696 class CoreExport UserIOHandler : public StreamSocket
697 {
698  private:
699  size_t checked_until;
700  public:
701  LocalUser* const user;
703  : StreamSocket(StreamSocket::SS_USER)
704  , checked_until(0)
705  , user(me)
706  {
707  }
708  void OnDataReady() CXX11_OVERRIDE;
709  bool OnSetEndPoint(const irc::sockets::sockaddrs& local, const irc::sockets::sockaddrs& remote) CXX11_OVERRIDE;
710  void OnError(BufferedSocketError error) CXX11_OVERRIDE;
711 
717  void AddWriteBuf(const std::string &data);
718 
722  void SwapInternals(UserIOHandler& other);
723 };
724 
725 typedef unsigned int already_sent_t;
726 
727 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
728 {
732  void Write(const ClientProtocol::SerializedMessage& serialized);
733 
739  void Send(ClientProtocol::Event& protoev, ClientProtocol::MessageList& msglist);
740 
743  static ClientProtocol::MessageList sendmsglist;
744 
745  public:
747  LocalUser(int fd, const std::string& uuid, Serializable::Data& data);
748 
749  CullResult cull() CXX11_OVERRIDE;
750 
751  UserIOHandler eh;
752 
756 
759  unsigned int bytes_in;
760 
763  unsigned int bytes_out;
764 
767  unsigned int cmds_in;
768 
771  unsigned int cmds_out;
772 
777  std::string password;
778 
782 
786  ConnectClass* GetClass() const { return MyClass; }
787 
790  void CheckClass(bool clone_count = true);
791 
795 
800  unsigned int quitting_sendq:1;
801 
804  unsigned int lastping:1;
805 
808  unsigned int exempt:1;
809 
811  time_t nextping;
812 
815  time_t idle_lastmsg;
816 
820  unsigned int CommandFloodPenalty;
821 
822  already_sent_t already_sent;
823 
828  bool CheckLines(bool doZline = false);
829 
833  void FullConnect();
834 
839  void SetClass(const std::string &explicit_name = "");
840 
841  bool SetClientIP(const std::string& address) CXX11_OVERRIDE;
842 
843  void SetClientIP(const irc::sockets::sockaddrs& sa) CXX11_OVERRIDE;
844 
849  void WriteRemoteNotice(const std::string& text) CXX11_OVERRIDE;
850 
857  bool HasCommandPermission(const std::string& command) CXX11_OVERRIDE;
858 
866  bool HasPrivPermission(const std::string& privstr) CXX11_OVERRIDE;
867 
874  bool HasModePermission(const ModeHandler* mh) const CXX11_OVERRIDE;
875 
880  void OverruleNick();
881 
885  void Send(ClientProtocol::Event& protoev);
886 
891  void Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg);
892 
894  bool Deserialize(Data& data) CXX11_OVERRIDE;
895 
897  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
898 };
899 
900 class RemoteUser : public User
901 {
902  public:
903  RemoteUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_REMOTE)
904  {
905  }
906 };
907 
908 class CoreExport FakeUser : public User
909 {
910  public:
911  FakeUser(const std::string& uid, Server* srv)
912  : User(uid, srv, USERTYPE_SERVER)
913  {
914  nick = srv->GetName();
915  }
916 
917  FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
918  : User(uid, new Server(uid, sname, sdesc), USERTYPE_SERVER)
919  {
920  nick = sname;
921  }
922 
923  CullResult cull() CXX11_OVERRIDE;
924  const std::string& GetFullHost() CXX11_OVERRIDE;
925  const std::string& GetFullRealHost() CXX11_OVERRIDE;
926 };
927 
928 /* Faster than dynamic_cast */
930 inline LocalUser* IS_LOCAL(User* u)
931 {
932  return (u != NULL && u->usertype == USERTYPE_LOCAL) ? static_cast<LocalUser*>(u) : NULL;
933 }
935 inline RemoteUser* IS_REMOTE(User* u)
936 {
937  return (u != NULL && u->usertype == USERTYPE_REMOTE) ? static_cast<RemoteUser*>(u) : NULL;
938 }
940 inline FakeUser* IS_SERVER(User* u)
941 {
942  return (u != NULL && u->usertype == USERTYPE_SERVER) ? static_cast<FakeUser*>(u) : NULL;
943 }
944 
945 inline bool User::IsModeSet(const ModeHandler* mh) const
946 {
947  return ((mh->GetId() != ModeParser::MODEID_MAX) && (modes[mh->GetId()]));
948 }
949 
950 inline bool User::IsModeSet(UserModeReference& moderef) const
951 {
952  if (!moderef)
953  return false;
954  return IsModeSet(*moderef);
955 }
956 
957 inline void User::SetMode(ModeHandler* mh, bool value)
958 {
959  if (mh && mh->GetId() != ModeParser::MODEID_MAX)
960  modes[mh->GetId()] = value;
961 }
User::chans
ChanList chans
Definition: users.h:330
ConnectClass::limit
unsigned long limit
Definition: users.h:141
User::Serialize
bool Serialize(Serializable::Data &data) CXX11_OVERRIDE
Definition: serializable.cpp:174
ClientProtocol::EventProvider
Definition: clientprotocol.h:625
OperInfo
Definition: configreader.h:179
ConnectClass::name
std::string name
Definition: users.h:87
Extensible
Definition: extensible.h:139
FakeUser
Definition: users.h:908
LocalUser::bytes_out
unsigned int bytes_out
Definition: users.h:763
irc::sockets::cidr_mask
Definition: socket.h:82
ConnectClass::fakelag
bool fakelag
Definition: users.h:83
User::signon
time_t signon
Definition: users.h:299
ConnectClass
Definition: users.h:75
User::ident
std::string ident
Definition: users.h:321
Serializable
Definition: serialize.h:23
insp::flat_set< int >
RemoteUser
Definition: users.h:900
Server
Definition: server.h:24
ClientProtocol::Event
Definition: clientprotocol.h:462
User::IsAway
bool IsAway() const
Definition: users.h:425
UserIOHandler
Definition: users.h:696
ConnectClass::GetPenaltyThreshold
unsigned int GetPenaltyThreshold()
Definition: users.h:203
Serializable::Data
Definition: serialize.h:30
Server::GetName
const std::string & GetName() const
Definition: server.h:76
User::awaymsg
std::string awaymsg
Definition: users.h:339
ConnectClass::commandrate
unsigned int commandrate
Definition: users.h:120
ConnectClass::penaltythreshold
unsigned int penaltythreshold
Definition: users.h:117
LocalUser::idle_lastmsg
time_t idle_lastmsg
Definition: users.h:815
reference< ConfigTag >
ConnectClass::maxchans
unsigned int maxchans
Definition: users.h:136
User::HasModePermission
virtual bool HasModePermission(const ModeHandler *mh) const
Definition: users.cpp:179
LocalUser::cmds_in
unsigned int cmds_in
Definition: users.h:767
ConnectClass::GetRecvqMax
unsigned long GetRecvqMax()
Definition: users.h:196
UserModeReference
Definition: dynref.h:130
refcountbase
Definition: base.h:90
ConnectClass::type
char type
Definition: users.h:80
User::cull
CullResult cull() CXX11_OVERRIDE
Definition: users.cpp:349
LocalUser::serializer
ClientProtocol::Serializer * serializer
Definition: users.h:755
User::age
time_t age
Definition: users.h:293
StreamSocket
Definition: inspsocket.h:112
User::oper
reference< OperInfo > oper
Definition: users.h:348
ConnectClass::ports
insp::flat_set< int > ports
Definition: users.h:150
irc::sockets::sockaddrs
Definition: socket.h:62
User::IsOper
bool IsOper() const
Definition: users.h:431
ConnectClass::resolvehostnames
bool resolvehostnames
Definition: users.h:145
User::HasPrivPermission
virtual bool HasPrivPermission(const std::string &privstr)
Definition: users.cpp:219
User::ForEachNeighborHandler
Definition: users.h:277
ConnectClass::GetSendqSoftMax
unsigned long GetSendqSoftMax()
Definition: users.h:182
LocalUser::exempt
unsigned int exempt
Definition: users.h:808
ConnectClass::softsendqmax
unsigned long softsendqmax
Definition: users.h:104
User::GetFullHost
virtual const std::string & GetFullHost()
Definition: users.cpp:161
ConnectClass::maxlocal
unsigned long maxlocal
Definition: users.h:124
ConnectClass::maxconnwarn
bool maxconnwarn
Definition: users.h:132
ConnectClass::hardsendqmax
unsigned long hardsendqmax
Definition: users.h:109
User::client_sa
irc::sockets::sockaddrs client_sa
Definition: users.h:305
User::nick
std::string nick
Definition: users.h:311
LocalUser::MyClass
reference< ConnectClass > MyClass
Definition: users.h:781
ConnectClass::GetMaxGlobal
unsigned long GetMaxGlobal()
Definition: users.h:222
User::snomasks
std::bitset< 64 > snomasks
Definition: users.h:326
insp::intrusive_list_node
Definition: intrusive_list.h:33
ConfigTag
Definition: configreader.h:43
User::SetMode
void SetMode(ModeHandler *mh, bool value)
Definition: users.h:957
insp::intrusive_list< Membership >
User::IsModeSet
bool IsModeSet(unsigned char m) const
Definition: users.cpp:50
LocalUser::cmds_out
unsigned int cmds_out
Definition: users.h:771
ConnectClass::host
std::string host
Definition: users.h:95
User::usertype
const UserType usertype
Definition: users.h:364
ModeHandler::GetId
Id GetId() const
Definition: mode.h:248
User::User
User(const std::string &uid, Server *srv, UserType objtype)
Definition: users.cpp:80
LocalUser::password
std::string password
Definition: users.h:777
User::WriteRemoteNotice
virtual void WriteRemoteNotice(const std::string &text)
Definition: users.cpp:872
User::SetClientIP
virtual bool SetClientIP(const std::string &address)
Definition: users.cpp:740
ConnectClass::recvqmax
unsigned long recvqmax
Definition: users.h:113
ConnectClass::GetRegTimeout
time_t GetRegTimeout()
Definition: users.h:167
User
Definition: users.h:233
User::uuid
const std::string uuid
Definition: users.h:316
CullResult
Definition: base.h:36
ConnectClass::GetPingTime
unsigned int GetPingTime()
Definition: users.h:174
ConnectClass::GetMaxLocal
unsigned long GetMaxLocal()
Definition: users.h:215
LocalUser::server_sa
irc::sockets::sockaddrs server_sa
Definition: users.h:794
User::awaytime
time_t awaytime
Definition: users.h:344
ClientProtocol::Message
Definition: clientprotocol.h:181
LocalUser::nextping
time_t nextping
Definition: users.h:811
Extensible::cull
CullResult cull() CXX11_OVERRIDE
Definition: base.cpp:204
User::server
Server * server
Definition: users.h:334
User::ChanList
insp::intrusive_list< Membership > ChanList
Definition: users.h:289
ConnectClass::maxglobal
unsigned long maxglobal
Definition: users.h:128
LocalUser::lastping
unsigned int lastping
Definition: users.h:804
ConnectClass::pingtime
unsigned int pingtime
Definition: users.h:99
LocalUser::bytes_in
unsigned int bytes_in
Definition: users.h:759
Numeric::Numeric
Definition: numeric.h:30
User::quitting
unsigned int quitting
Definition: users.h:361
LocalUser::CommandFloodPenalty
unsigned int CommandFloodPenalty
Definition: users.h:820
ConnectClass::registration_timeout
unsigned int registration_timeout
Definition: users.h:91
LocalUser::quitting_sendq
unsigned int quitting_sendq
Definition: users.h:800
ClientProtocol::Serializer
Definition: clientprotocol.h:679
User::Deserialize
bool Deserialize(Data &data) CXX11_OVERRIDE
Definition: serializable.cpp:108
LocalUser::GetClass
ConnectClass * GetClass() const
Definition: users.h:786
ConnectClass::GetSendqHardMax
unsigned long GetSendqHardMax()
Definition: users.h:189
ModeParser::MODEID_MAX
static const ModeHandler::Id MODEID_MAX
Definition: mode.h:560
ModeHandler
Definition: mode.h:99
User::GetFullRealHost
virtual const std::string & GetFullRealHost()
Definition: users.cpp:170
LocalUser
Definition: users.h:727
User::registered
unsigned int registered
Definition: users.h:354
User::HasCommandPermission
virtual bool HasCommandPermission(const std::string &command)
Definition: users.cpp:203