]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
Improve behaviour when running as root.
[user/henk/code/inspircd.git] / include / xline.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2004-2007 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #pragma once
24
25 /** XLine is the base class for ban lines such as G-lines and K-lines.
26  * Modules may derive from this, and their xlines will automatically be
27  * handled as expected by any protocol modules (e.g. m_spanningtree will
28  * propogate them using AddLine). The process of translating a type+pattern
29  * to a known line type is done by means of an XLineFactory object (see
30  * below).
31  */
32 class CoreExport XLine : public classbase
33 {
34  protected:
35
36         /** Default 'apply' action. Quits the user.
37          * @param u User to apply the line against
38          * @param line The line typed, used for display purposes in the quit message
39          * @param bancache If true, the user will be added to the bancache if they match. Else not.
40          */
41         void DefaultApply(User* u, const std::string &line, bool bancache);
42
43  public:
44
45         /** Create an XLine.
46          * @param s_time The set time
47          * @param d The duration of the xline
48          * @param src The sender of the xline
49          * @param re The reason of the xline
50          * @param t The line type, should be set by the derived class constructor
51          */
52         XLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& t)
53                 : set_time(s_time)
54                 , duration(d)
55                 , source(src)
56                 , reason(re)
57                 , type(t)
58                 , from_config(false)
59         {
60                 expiry = set_time + duration;
61         }
62
63         /** Destructor
64          */
65         virtual ~XLine()
66         {
67         }
68
69         /** Change creation time of an xline. Updates expiry
70          * to be after the creation time.
71          */
72         virtual void SetCreateTime(time_t created)
73         {
74                 set_time = created;
75                 expiry = created + duration;
76         }
77
78         /** Returns true whether or not the given user is covered by this line.
79          * @param u The user to match against. The mechanics of the match
80          * are defined by the derived class.
81          * @return True if there is a match.
82          */
83         virtual bool Matches(User *u) = 0;
84
85         /** Returns true whether or not the given string is covered by this line.
86          * @param str The string to match against. The details of what must be
87          * in this string and the mechanics of the match are defined by the
88          * derived class.
89          * @return True if there is a match
90          */
91         virtual bool Matches(const std::string &str) = 0;
92
93         /** Apply a line against a user. The mechanics of what occurs when
94          * the line is applied are specific to the derived class.
95          * @param u The user to apply against
96          */
97         virtual void Apply(User* u);
98
99         /** Called when the line is unset either via expiry or
100          * via explicit removal.
101          */
102         virtual void Unset() { }
103
104         /** Called when the expiry message is to be displayed for the
105          * line. Usually a line in the form 'expiring X-line blah, set by...'
106          * see the DisplayExpiry methods of GLine, ELine etc.
107          */
108         virtual void DisplayExpiry();
109
110         /** Returns the displayable form of the pattern for this xline,
111          * e.g. '*\@foo' or '*baz*'. This must always return the full pattern
112          * in a form which can be used to construct an entire derived xline,
113          * even if it is stored differently internally (e.g. GLine stores the
114          * ident and host parts separately but will still return ident\@host
115          * for its Displayable() method).
116          */
117         virtual const std::string& Displayable() = 0;
118
119         /** Called when the xline has just been added.
120          */
121         virtual void OnAdd() { }
122
123         /** The time the line was added.
124          */
125         time_t set_time;
126
127         /** The duration of the ban, or 0 if permenant
128          */
129         unsigned long duration;
130
131         /** Source of the ban. This can be a servername or an oper nickname
132          */
133         std::string source;
134
135         /** Reason for the ban
136          */
137         std::string reason;
138
139         /** Expiry time. Does not contain useful data if the duration is 0.
140          */
141         time_t expiry;
142
143         /** "Q", "K", etc. Set only by derived classes constructor to the
144          * type of line this is.
145          */
146         const std::string type;
147
148         // Whether this XLine was loaded from the server config.
149         bool from_config;
150
151         virtual bool IsBurstable();
152 };
153
154 /** KLine class
155  */
156 class CoreExport KLine : public XLine
157 {
158   public:
159
160         /** Create a K-line.
161          * @param s_time The set time
162          * @param d The duration of the xline
163          * @param src The sender of the xline
164          * @param re The reason of the xline
165          * @param ident Ident to match
166          * @param host Host to match
167          */
168         KLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
169                 : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
170         {
171                 matchtext = this->identmask;
172                 matchtext.append("@").append(this->hostmask);
173         }
174
175         /** Destructor
176          */
177         ~KLine()
178         {
179         }
180
181         bool Matches(User* u) CXX11_OVERRIDE;
182
183         bool Matches(const std::string& str) CXX11_OVERRIDE;
184
185         void Apply(User* u) CXX11_OVERRIDE;
186
187         const std::string& Displayable() CXX11_OVERRIDE;
188
189         bool IsBurstable() CXX11_OVERRIDE;
190
191         /** Ident mask (ident part only)
192          */
193         std::string identmask;
194         /** Host mask (host part only)
195          */
196         std::string hostmask;
197
198         std::string matchtext;
199 };
200
201 /** GLine class
202  */
203 class CoreExport GLine : public XLine
204 {
205   public:
206         /** Create a G-line.
207          * @param s_time The set time
208          * @param d The duration of the xline
209          * @param src The sender of the xline
210          * @param re The reason of the xline
211          * @param ident Ident to match
212          * @param host Host to match
213          */
214         GLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
215                 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
216         {
217                 matchtext = this->identmask;
218                 matchtext.append("@").append(this->hostmask);
219         }
220
221         /** Destructor
222          */
223         ~GLine()
224         {
225         }
226
227         bool Matches(User* u) CXX11_OVERRIDE;
228
229         bool Matches(const std::string& str) CXX11_OVERRIDE;
230
231         void Apply(User* u)  CXX11_OVERRIDE;
232
233         const std::string& Displayable() CXX11_OVERRIDE;
234
235         /** Ident mask (ident part only)
236          */
237         std::string identmask;
238         /** Host mask (host part only)
239          */
240         std::string hostmask;
241
242         std::string matchtext;
243 };
244
245 /** ELine class
246  */
247 class CoreExport ELine : public XLine
248 {
249   public:
250         /** Create an E-line.
251          * @param s_time The set time
252          * @param d The duration of the xline
253          * @param src The sender of the xline
254          * @param re The reason of the xline
255          * @param ident Ident to match
256          * @param host Host to match
257          */
258         ELine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
259                 : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
260         {
261                 matchtext = this->identmask;
262                 matchtext.append("@").append(this->hostmask);
263         }
264
265         ~ELine()
266         {
267         }
268
269         bool Matches(User* u) CXX11_OVERRIDE;
270
271         bool Matches(const std::string& str) CXX11_OVERRIDE;
272
273         void Unset() CXX11_OVERRIDE;
274
275         void OnAdd() CXX11_OVERRIDE;
276
277         const std::string& Displayable() CXX11_OVERRIDE;
278
279         /** Ident mask (ident part only)
280          */
281         std::string identmask;
282         /** Host mask (host part only)
283          */
284         std::string hostmask;
285
286         std::string matchtext;
287 };
288
289 /** ZLine class
290  */
291 class CoreExport ZLine : public XLine
292 {
293   public:
294         /** Create a Z-line.
295          * @param s_time The set time
296          * @param d The duration of the xline
297          * @param src The sender of the xline
298          * @param re The reason of the xline
299          * @param ip IP to match
300          */
301         ZLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ip)
302                 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
303         {
304         }
305
306         /** Destructor
307          */
308         ~ZLine()
309         {
310         }
311
312         bool Matches(User* u) CXX11_OVERRIDE;
313
314         bool Matches(const std::string& str) CXX11_OVERRIDE;
315
316         void Apply(User* u) CXX11_OVERRIDE;
317
318         const std::string& Displayable() CXX11_OVERRIDE;
319
320         /** IP mask (no ident part)
321          */
322         std::string ipaddr;
323 };
324
325 /** QLine class
326  */
327 class CoreExport QLine : public XLine
328 {
329   public:
330         /** Create a Q-line.
331          * @param s_time The set time
332          * @param d The duration of the xline
333          * @param src The sender of the xline
334          * @param re The reason of the xline
335          * @param nickname Nickname to match
336          */
337         QLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& nickname)
338                 : XLine(s_time, d, src, re, "Q"), nick(nickname)
339         {
340         }
341
342         /** Destructor
343          */
344         ~QLine()
345         {
346         }
347         bool Matches(User* u) CXX11_OVERRIDE;
348
349         bool Matches(const std::string& str) CXX11_OVERRIDE;
350
351         void Apply(User* u) CXX11_OVERRIDE;
352
353         const std::string& Displayable() CXX11_OVERRIDE;
354
355         /** Nickname mask
356          */
357         std::string nick;
358 };
359
360 /** XLineFactory is used to generate an XLine pointer, given just the
361  * pattern, timing information and type of line to create. This is used
362  * for example in the spanningtree module which will call an XLineFactory
363  * to create a new XLine when it is inbound on a server link, so that it
364  * does not have to know the specifics of the internals of an XLine class
365  * and/or how to call its constructor.
366  */
367 class CoreExport XLineFactory
368 {
369  protected:
370
371         std::string type;
372
373  public:
374
375         /** Create an XLine factory
376          * @param t Type of XLine this factory generates
377          */
378         XLineFactory(const std::string &t) : type(t) { }
379
380         /** Return the type of XLine this factory generates
381          * @return The type of XLine this factory generates
382          */
383         virtual const std::string& GetType() { return type; }
384
385         /** Generate a specialized XLine*.
386          * @param set_time Time this line was created
387          * @param duration Duration of the line
388          * @param source The sender of the line, nickname or server
389          * @param reason The reason for the line
390          * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
391          * @return A specialized XLine class of the given type for this factory.
392          */
393         virtual XLine* Generate(time_t set_time, unsigned long duration, const std::string& source, const std::string& reason, const std::string& xline_specific_mask) = 0;
394
395         virtual bool AutoApplyToUserList(XLine* x) { return true; }
396
397         /** Destructor
398          */
399         virtual ~XLineFactory() { }
400 };
401
402 /** XLineManager is a class used to manage G-lines, K-lines, E-lines, Z-lines and Q-lines,
403  * or any other line created by a module. It also manages XLineFactory classes which
404  * can generate a specialized XLine for use by another module.
405  */
406 class CoreExport XLineManager
407 {
408  protected:
409         /** Used to hold XLines which have not yet been applied.
410          */
411         std::vector<XLine *> pending_lines;
412
413         /** Current xline factories
414          */
415         XLineFactMap line_factory;
416
417         /** Container of all lines, this is a map of maps which
418          * allows for fast lookup for add/remove of a line, and
419          * the shortest possible timed O(n) for checking a user
420          * against a line.
421          */
422         XLineContainer lookup_lines;
423
424  public:
425
426         /** Constructor
427          */
428         XLineManager();
429
430         /** Destructor
431          */
432         ~XLineManager();
433
434         /** Split an ident and host into two seperate strings.
435          * This allows for faster matching.
436          */
437         IdentHostPair IdentSplit(const std::string &ident_and_host);
438
439         /** Checks what users match E-lines and sets their ban exempt flag accordingly.
440          */
441         void CheckELines();
442
443         /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
444          * NOTE: When this function runs any expired items are removed from the list before it
445          * is returned to the caller.
446          * @param type The type to look up
447          * @return A list of all XLines of the given type.
448          */
449         XLineLookup* GetAll(const std::string &type);
450
451         /** Remove all lines of a certain type.
452          */
453         void DelAll(const std::string &type);
454
455         /** Return all known types of line currently stored by the XLineManager.
456          * @return A vector containing all known line types currently stored in the main list.
457          */
458         std::vector<std::string> GetAllTypes();
459
460         /** Add a new XLine
461          * @param line The line to be added
462          * @param user The user adding the line or NULL for the local server
463          * @return True if the line was added successfully
464          */
465         bool AddLine(XLine* line, User* user);
466
467         /** Delete an XLine
468          * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
469          * @param type The type of xline
470          * @param reason The xline reason, if it is being removed successfully
471          * @param user The user removing the line or NULL if its the local server
472          * @param simulate If this is true, don't actually remove the line, just return
473          * @return True if the line was deleted successfully
474          */
475         bool DelLine(const char* hostmask, const std::string& type, std::string& reason, User* user, bool simulate = false);
476
477         /** Registers an xline factory.
478          * An xline factory is a class which when given a particular xline type,
479          * will generate a new XLine specialized to that type. For example if you
480          * pass the XLineFactory that handles G-lines some data it will return a
481          * pointer to a GLine, polymorphically represented as XLine. This is used where
482          * you do not know the full details of the item you wish to create, e.g. in a
483          * server protocol module like m_spanningtree, when you receive xlines from other
484          * servers.
485          * @param xlf XLineFactory pointer to register
486          */
487         bool RegisterFactory(XLineFactory* xlf);
488
489         /** Unregisters an xline factory.
490          * You must do this when your module unloads.
491          * @param xlf XLineFactory pointer to unregister
492          */
493         bool UnregisterFactory(XLineFactory* xlf);
494
495         /** Get the XLineFactory for a specific type.
496          * Returns NULL if there is no known handler for this xline type.
497          * @param type The type of XLine you require the XLineFactory for
498          */
499         XLineFactory* GetFactory(const std::string &type);
500
501         /** Check if a user matches an XLine
502          * @param type The type of line to look up
503          * @param user The user to match against (what is checked is specific to the xline type)
504          * @return The reason for the line if there is a match, or NULL if there is no match
505          */
506         XLine* MatchesLine(const std::string &type, User* user);
507
508         /** Check if a pattern matches an XLine
509          * @param type The type of line to look up
510          * @param pattern A pattern string specific to the xline type
511          * @return The matching XLine if there is a match, or NULL if there is no match
512          */
513         XLine* MatchesLine(const std::string &type, const std::string &pattern);
514
515         /** Expire a line given two iterators which identify it in the main map.
516          * @param container Iterator to the first level of entries the map
517          * @param item Iterator to the second level of entries in the map
518          * @param silent If true, doesn't send an expiry SNOTICE.
519          */
520         void ExpireLine(ContainerIter container, LookupIter item, bool silent = false);
521
522         /** Apply any new lines that are pending to be applied.
523          * This will only apply lines in the pending_lines list, to save on
524          * CPU time.
525          */
526         void ApplyLines();
527
528         /** Handle /STATS for a given type.
529          * NOTE: Any items in the list for this particular line type which have expired
530          * will be expired and removed before the list is displayed.
531          * @param type The type of stats to show
532          * @param numeric The numeric to give to each result line
533          * @param stats Stats context
534          */
535         void InvokeStats(const std::string& type, unsigned int numeric, Stats::Context& stats);
536
537         /** Expire X-lines which were added by the server configuration and have been removed. */
538         void ExpireRemovedConfigLines(const std::string& type, const insp::flat_set<std::string>& configlines);
539 };