]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/class.c
Streamline handling of connection rejects (bad password, G/K-line)
[ngircd-alex.git] / src / ngircd / class.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * User class management.
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <string.h>
22
23 #include "defines.h"
24 #include "array.h"
25 #include "conn.h"
26 #include "client.h"
27 #include "lists.h"
28 #include "match.h"
29 #include "stdio.h"
30
31 #include "exp.h"
32 #include "class.h"
33
34 struct list_head My_Classes[CLASS_COUNT];
35
36 char Reject_Reason[COMMAND_LEN];
37
38 GLOBAL void
39 Class_Init(void)
40 {
41         memset(My_Classes, 0, sizeof(My_Classes));
42 }
43
44 GLOBAL void
45 Class_Exit(void)
46 {
47         int i;
48
49         for (i = 0; i < CLASS_COUNT; Lists_Free(&My_Classes[i++]));
50 }
51
52 GLOBAL char *
53 Class_GetMemberReason(const int Class, CLIENT *Client)
54 {
55         char *reason;
56
57         assert(Class < CLASS_COUNT);
58         assert(Client != NULL);
59
60         reason = Lists_CheckReason(&My_Classes[Class], Client);
61         if (!reason)
62                 return NULL;
63
64         if (!*reason)
65                 reason = "listed";
66
67         switch(Class) {
68                 case CLASS_GLINE:
69                         snprintf(Reject_Reason, sizeof(Reject_Reason),
70                                  "\"%s\" (G-Line)", reason);
71                         return Reject_Reason;
72                 case CLASS_KLINE:
73                         snprintf(Reject_Reason, sizeof(Reject_Reason),
74                                  "\"%s\" (K-Line)", reason);
75                         return Reject_Reason;
76         }
77         return reason;
78 }
79
80 /**
81  * Check if a client is banned from this server: GLINE, KLINE.
82  *
83  * If a client isn't allowed to connect, it will be disconnected again.
84  *
85  * @param Client The client to check.
86  * @return CONNECTED if client is allowed to join, DISCONNECTED if not.
87  */
88 GLOBAL bool
89 Class_HandleServerBans(CLIENT *Client)
90 {
91         char *rejectptr;
92
93         assert(Client != NULL);
94
95         rejectptr = Class_GetMemberReason(CLASS_GLINE, Client);
96         if (!rejectptr)
97                 rejectptr = Class_GetMemberReason(CLASS_KLINE, Client);
98         if (rejectptr) {
99                 Client_Reject(Client, rejectptr, true);
100                 return DISCONNECTED;
101         }
102
103         return CONNECTED;
104 }
105
106
107 GLOBAL bool
108 Class_AddMask(const int Class, const char *Mask, time_t ValidUntil,
109               const char *Reason)
110 {
111         assert(Class < CLASS_COUNT);
112         assert(Mask != NULL);
113         assert(Reason != NULL);
114
115         return Lists_Add(&My_Classes[Class], Mask, ValidUntil, Reason);
116 }
117
118 GLOBAL void
119 Class_DeleteMask(const int Class, const char *Mask)
120 {
121         assert(Class < CLASS_COUNT);
122         assert(Mask != NULL);
123
124         Lists_Del(&My_Classes[Class], Mask);
125 }
126
127 GLOBAL struct list_head *
128 Class_GetList(const int Class)
129 {
130         assert(Class < CLASS_COUNT);
131
132         return &My_Classes[Class];
133 }
134
135 GLOBAL void
136 Class_Expire(void)
137 {
138         Lists_Expire(&My_Classes[CLASS_GLINE], "G-Line");
139         Lists_Expire(&My_Classes[CLASS_KLINE], "K-Line");
140 }
141
142 /* -eof- */