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 
76 struct CoreExport ConnectClass : public refcountbase
77 {
78  reference<ConfigTag> config;
81  char type;
82 
84  bool fakelag;
85 
88  std::string name;
89 
92  unsigned int registration_timeout;
93 
95  std::string host;
96 
98  std::vector<std::string> hosts;
99 
102  unsigned int pingtime;
103 
107  unsigned long softsendqmax;
108 
112  unsigned long hardsendqmax;
113 
116  unsigned long recvqmax;
117 
120  unsigned int penaltythreshold;
121 
123  unsigned int commandrate;
124 
127  unsigned long maxlocal;
128 
131  unsigned long maxglobal;
132 
136 
139  unsigned int maxchans;
140 
144  unsigned long limit;
145 
149 
154 
156  std::string password;
157 
159  std::string passwordhash;
160 
163  ConnectClass(ConfigTag* tag, char type, const std::string& mask);
166  ConnectClass(ConfigTag* tag, char type, const std::string& mask, const ConnectClass& parent);
167 
169  void Update(const ConnectClass* newSettings);
170 
171  const std::string& GetName() const { return name; }
172  const std::string& GetHost() const { return host; }
173  const std::vector<std::string>& GetHosts() const { return hosts; }
174 
177  time_t GetRegTimeout()
178  {
179  return (registration_timeout ? registration_timeout : 90);
180  }
181 
184  unsigned int GetPingTime()
185  {
186  return (pingtime ? pingtime : 120);
187  }
188 
192  unsigned long GetSendqSoftMax()
193  {
194  return (softsendqmax ? softsendqmax : 4096);
195  }
196 
199  unsigned long GetSendqHardMax()
200  {
201  return (hardsendqmax ? hardsendqmax : 0x100000);
202  }
203 
206  unsigned long GetRecvqMax()
207  {
208  return (recvqmax ? recvqmax : 4096);
209  }
210 
213  unsigned int GetPenaltyThreshold()
214  {
215  return penaltythreshold ? penaltythreshold : (fakelag ? 10 : 20);
216  }
217 
218  unsigned int GetCommandRate()
219  {
220  return commandrate ? commandrate : 1000;
221  }
222 
225  unsigned long GetMaxLocal()
226  {
227  return maxlocal;
228  }
229 
232  unsigned long GetMaxGlobal()
233  {
234  return maxglobal;
235  }
236 };
237 
239 typedef unsigned int already_sent_t;
240 
246 class CoreExport User : public Extensible
247 {
248  private:
251  std::string cached_fullhost;
252 
255  std::string cached_hostip;
256 
259  std::string cached_makehost;
260 
263  std::string cached_fullrealhost;
264 
267  std::string cachedip;
268 
270  std::string displayhost;
271 
273  std::string realhost;
274 
276  std::string realname;
277 
284  std::bitset<ModeParser::MODEID_MAX> modes;
285 
286  public:
291  {
292  public:
297  virtual void Execute(LocalUser* user) = 0;
298  };
299 
303 
306  time_t age;
307 
312  time_t signon;
313 
319 
324  std::string nick;
325 
329  const std::string uuid;
330 
334  std::string ident;
335 
339  std::bitset<64> snomasks;
340 
344 
348 
352  std::string awaymsg;
353 
357  time_t awaytime;
358 
362 
367  unsigned int registered:3;
368 
374  unsigned int quitting:1;
375 
377  const unsigned int usertype:2;
378 
382  const std::string& GetIPString();
383 
387  const std::string& GetHost(bool uncloak) const;
388 
390  const std::string& GetDisplayedHost() const;
391 
393  const std::string& GetRealHost() const;
394 
396  const std::string& GetRealName() const;
397 
400  irc::sockets::cidr_mask GetCIDRMask();
401 
405  virtual bool SetClientIP(const std::string& address);
406 
407  virtual void SetClientIP(const irc::sockets::sockaddrs& sa);
408 
412  User(const std::string& uid, Server* srv, UserType objtype);
413 
419  virtual const std::string& GetFullHost();
420 
427  virtual const std::string& GetFullRealHost();
428 
432  void InvalidateCache();
433 
438  bool IsAway() const { return (!awaymsg.empty()); }
439 
444  bool IsOper() const { return oper; }
445 
450  bool IsNoticeMaskSet(unsigned char sm);
451 
457  std::string GetModeLetters(bool includeparams = false) const;
458 
463  bool IsModeSet(unsigned char m) const;
464  bool IsModeSet(const ModeHandler* mh) const;
465  bool IsModeSet(const ModeHandler& mh) const { return IsModeSet(&mh); }
466  bool IsModeSet(UserModeReference& moderef) const;
467 
472  void SetMode(ModeHandler* mh, bool value);
473  void SetMode(ModeHandler& mh, bool value) { SetMode(&mh, value); }
474 
481  virtual bool HasCommandPermission(const std::string& command);
482 
490  virtual bool HasPrivPermission(const std::string& privstr);
491 
498  virtual bool HasModePermission(const ModeHandler* mh) const;
499 
504  virtual bool HasSnomaskPermission(char chr) const;
505 
510  const std::string& MakeHost();
511 
516  const std::string& MakeHostIP();
517 
521  void Oper(OperInfo* info);
522 
526  void UnOper();
527 
531  void WriteNotice(const std::string& text);
532 
536  virtual void WriteRemoteNotice(const std::string& text);
537 
538  virtual void WriteRemoteNumeric(const Numeric::Numeric& numeric);
539 
540  template <typename T1>
541  void WriteRemoteNumeric(unsigned int numeric, T1 p1)
542  {
543  Numeric::Numeric n(numeric);
544  n.push(p1);
545  WriteRemoteNumeric(n);
546  }
547 
548  template <typename T1, typename T2>
549  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2)
550  {
551  Numeric::Numeric n(numeric);
552  n.push(p1);
553  n.push(p2);
554  WriteRemoteNumeric(n);
555  }
556 
557  template <typename T1, typename T2, typename T3>
558  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
559  {
560  Numeric::Numeric n(numeric);
561  n.push(p1);
562  n.push(p2);
563  n.push(p3);
564  WriteRemoteNumeric(n);
565  }
566 
567  template <typename T1, typename T2, typename T3, typename T4>
568  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
569  {
570  Numeric::Numeric n(numeric);
571  n.push(p1);
572  n.push(p2);
573  n.push(p3);
574  n.push(p4);
575  WriteRemoteNumeric(n);
576  }
577 
578  template <typename T1, typename T2, typename T3, typename T4, typename T5>
579  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
580  {
581  Numeric::Numeric n(numeric);
582  n.push(p1);
583  n.push(p2);
584  n.push(p3);
585  n.push(p4);
586  n.push(p5);
587  WriteRemoteNumeric(n);
588  }
589 
590  void WriteNumeric(const Numeric::Numeric& numeric);
591 
592  template <typename T1>
593  void WriteNumeric(unsigned int numeric, T1 p1)
594  {
595  Numeric::Numeric n(numeric);
596  n.push(p1);
597  WriteNumeric(n);
598  }
599 
600  template <typename T1, typename T2>
601  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2)
602  {
603  Numeric::Numeric n(numeric);
604  n.push(p1);
605  n.push(p2);
606  WriteNumeric(n);
607  }
608 
609  template <typename T1, typename T2, typename T3>
610  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
611  {
612  Numeric::Numeric n(numeric);
613  n.push(p1);
614  n.push(p2);
615  n.push(p3);
616  WriteNumeric(n);
617  }
618 
619  template <typename T1, typename T2, typename T3, typename T4>
620  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
621  {
622  Numeric::Numeric n(numeric);
623  n.push(p1);
624  n.push(p2);
625  n.push(p3);
626  n.push(p4);
627  WriteNumeric(n);
628  }
629 
630  template <typename T1, typename T2, typename T3, typename T4, typename T5>
631  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
632  {
633  Numeric::Numeric n(numeric);
634  n.push(p1);
635  n.push(p2);
636  n.push(p3);
637  n.push(p4);
638  n.push(p5);
639  WriteNumeric(n);
640  }
641 
646  void WriteCommonRaw(ClientProtocol::Event& protoev, bool include_self = true);
647 
656  already_sent_t ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true);
657 
662  bool SharesChannelWith(User *other);
663 
668  bool ChangeDisplayedHost(const std::string& host);
669 
674  void ChangeRealHost(const std::string& host, bool resetdisplay);
675 
683  bool ChangeIdent(const std::string& newident);
684 
689  bool ChangeRealName(const std::string& real);
690 
696  bool ChangeNick(const std::string& newnick, time_t newts = 0);
697 
701  void PurgeEmptyChannels();
702 
705  virtual ~User();
706  CullResult cull() CXX11_OVERRIDE;
707 
709  bool Deserialize(Data& data) CXX11_OVERRIDE;
710 
712  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
713 };
714 
715 class CoreExport UserIOHandler : public StreamSocket
716 {
717  private:
718  size_t checked_until;
719  public:
720  LocalUser* const user;
722  : StreamSocket(StreamSocket::SS_USER)
723  , checked_until(0)
724  , user(me)
725  {
726  }
727  void OnDataReady() CXX11_OVERRIDE;
728  bool OnSetEndPoint(const irc::sockets::sockaddrs& local, const irc::sockets::sockaddrs& remote) CXX11_OVERRIDE;
729  void OnError(BufferedSocketError error) CXX11_OVERRIDE;
730 
736  void AddWriteBuf(const std::string &data);
737 
741  void SwapInternals(UserIOHandler& other);
742 };
743 
744 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
745 {
749  void Write(const ClientProtocol::SerializedMessage& serialized);
750 
756  void Send(ClientProtocol::Event& protoev, ClientProtocol::MessageList& msglist);
757 
760  static ClientProtocol::MessageList sendmsglist;
761 
762  public:
764  LocalUser(int fd, const std::string& uuid, Serializable::Data& data);
765 
766  CullResult cull() CXX11_OVERRIDE;
767 
768  UserIOHandler eh;
769 
773 
776  unsigned int bytes_in;
777 
780  unsigned int bytes_out;
781 
784  unsigned int cmds_in;
785 
788  unsigned int cmds_out;
789 
794  std::string password;
795 
799 
803  ConnectClass* GetClass() const { return MyClass; }
804 
807  void CheckClass(bool clone_count = true);
808 
812 
817  unsigned int quitting_sendq:1;
818 
821  unsigned int lastping:1;
822 
825  unsigned int exempt:1;
826 
828  time_t nextping;
829 
832  time_t idle_lastmsg;
833 
837  unsigned int CommandFloodPenalty;
838 
839  already_sent_t already_sent;
840 
845  bool CheckLines(bool doZline = false);
846 
850  void FullConnect();
851 
855  void SetClass(const std::string &explicit_name = "");
856 
857  bool SetClientIP(const std::string& address) CXX11_OVERRIDE;
858 
859  void SetClientIP(const irc::sockets::sockaddrs& sa) CXX11_OVERRIDE;
860 
865  void WriteRemoteNotice(const std::string& text) CXX11_OVERRIDE;
866 
873  bool HasCommandPermission(const std::string& command) CXX11_OVERRIDE;
874 
882  bool HasPrivPermission(const std::string& privstr) CXX11_OVERRIDE;
883 
890  bool HasModePermission(const ModeHandler* mh) const CXX11_OVERRIDE;
891 
893  bool HasSnomaskPermission(char chr) const CXX11_OVERRIDE;
894 
899  void OverruleNick();
900 
904  void Send(ClientProtocol::Event& protoev);
905 
910  void Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg);
911 
913  bool Deserialize(Data& data) CXX11_OVERRIDE;
914 
916  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
917 };
918 
919 class RemoteUser : public User
920 {
921  public:
922  RemoteUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_REMOTE)
923  {
924  }
925 };
926 
927 class CoreExport FakeUser : public User
928 {
929  public:
930  FakeUser(const std::string& uid, Server* srv)
931  : User(uid, srv, USERTYPE_SERVER)
932  {
933  nick = srv->GetName();
934  }
935 
936  FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
937  : User(uid, new Server(uid, sname, sdesc), USERTYPE_SERVER)
938  {
939  nick = sname;
940  }
941 
942  CullResult cull() CXX11_OVERRIDE;
943  const std::string& GetFullHost() CXX11_OVERRIDE;
944  const std::string& GetFullRealHost() CXX11_OVERRIDE;
945 };
946 
947 /* Faster than dynamic_cast */
949 inline LocalUser* IS_LOCAL(User* u)
950 {
951  return (u != NULL && u->usertype == USERTYPE_LOCAL) ? static_cast<LocalUser*>(u) : NULL;
952 }
954 inline RemoteUser* IS_REMOTE(User* u)
955 {
956  return (u != NULL && u->usertype == USERTYPE_REMOTE) ? static_cast<RemoteUser*>(u) : NULL;
957 }
959 inline FakeUser* IS_SERVER(User* u)
960 {
961  return (u != NULL && u->usertype == USERTYPE_SERVER) ? static_cast<FakeUser*>(u) : NULL;
962 }
963 
964 inline bool User::IsModeSet(const ModeHandler* mh) const
965 {
966  return ((mh->GetId() != ModeParser::MODEID_MAX) && (modes[mh->GetId()]));
967 }
968 
969 inline bool User::IsModeSet(UserModeReference& moderef) const
970 {
971  if (!moderef)
972  return false;
973  return IsModeSet(*moderef);
974 }
975 
976 inline void User::SetMode(ModeHandler* mh, bool value)
977 {
978  if (mh && mh->GetId() != ModeParser::MODEID_MAX)
979  modes[mh->GetId()] = value;
980 }
Definition: clientprotocol.h:465
Definition: clientprotocol.h:628
Definition: clientprotocol.h:182
Definition: clientprotocol.h:682
Definition: configreader.h:38
Definition: base.h:36
Definition: extensible.h:142
Definition: users.h:928
Definition: users.h:745
ConnectClass * GetClass() const
Definition: users.h:803
time_t idle_lastmsg
Definition: users.h:832
unsigned int bytes_in
Definition: users.h:776
unsigned int cmds_in
Definition: users.h:784
unsigned int exempt
Definition: users.h:825
unsigned int bytes_out
Definition: users.h:780
unsigned int CommandFloodPenalty
Definition: users.h:837
reference< ConnectClass > MyClass
Definition: users.h:798
unsigned int cmds_out
Definition: users.h:788
irc::sockets::sockaddrs server_sa
Definition: users.h:811
unsigned int quitting_sendq
Definition: users.h:817
ClientProtocol::Serializer * serializer
Definition: users.h:772
time_t nextping
Definition: users.h:828
unsigned int lastping
Definition: users.h:821
std::string password
Definition: users.h:794
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:920
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:291
virtual void Execute(LocalUser *user)=0
Definition: users.h:247
bool IsOper() const
Definition: users.h:444
ChanList chans
Definition: users.h:343
irc::sockets::sockaddrs client_sa
Definition: users.h:318
time_t signon
Definition: users.h:312
void SetMode(ModeHandler *mh, bool value)
Definition: users.h:976
bool IsAway() const
Definition: users.h:438
std::string nick
Definition: users.h:324
unsigned int quitting
Definition: users.h:374
unsigned int registered
Definition: users.h:367
time_t age
Definition: users.h:306
Server * server
Definition: users.h:347
std::string ident
Definition: users.h:334
reference< OperInfo > oper
Definition: users.h:361
std::bitset< 64 > snomasks
Definition: users.h:339
const unsigned int usertype
Definition: users.h:377
const std::string uuid
Definition: users.h:329
std::string awaymsg
Definition: users.h:352
time_t awaytime
Definition: users.h:357
insp::intrusive_list< Membership > ChanList
Definition: users.h:302
bool IsModeSet(unsigned char m) const
Definition: users.cpp:50
Definition: users.h:716
Definition: dynref.h:131
Definition: intrusive_list.h:34
Definition: base.h:90
Definition: users.h:77
bool maxconnwarn
Definition: users.h:135
unsigned int maxchans
Definition: users.h:139
std::string passwordhash
Definition: users.h:159
unsigned int commandrate
Definition: users.h:123
insp::flat_set< int > ports
Definition: users.h:153
unsigned long GetSendqSoftMax()
Definition: users.h:192
unsigned long GetSendqHardMax()
Definition: users.h:199
bool resolvehostnames
Definition: users.h:148
unsigned int pingtime
Definition: users.h:102
unsigned long GetMaxLocal()
Definition: users.h:225
unsigned long recvqmax
Definition: users.h:116
std::string host
Definition: users.h:95
bool fakelag
Definition: users.h:84
unsigned long limit
Definition: users.h:144
std::string password
Definition: users.h:156
unsigned long maxlocal
Definition: users.h:127
unsigned int penaltythreshold
Definition: users.h:120
unsigned int GetPenaltyThreshold()
Definition: users.h:213
unsigned long maxglobal
Definition: users.h:131
unsigned int registration_timeout
Definition: users.h:92
std::vector< std::string > hosts
Definition: users.h:98
unsigned int GetPingTime()
Definition: users.h:184
unsigned long GetMaxGlobal()
Definition: users.h:232
time_t GetRegTimeout()
Definition: users.h:177
unsigned long softsendqmax
Definition: users.h:107
unsigned long hardsendqmax
Definition: users.h:112
char type
Definition: users.h:81
unsigned long GetRecvqMax()
Definition: users.h:206
std::string name
Definition: users.h:88
Definition: socket.h:83
Definition: socket.h:63