]> jfr.im git - irc/rqf/shadowircd.git/blob - src/reject.c
[svn] Fix some cases where the size argument to strlcpy()
[irc/rqf/shadowircd.git] / src / reject.c
1 /*
2 * ircd-ratbox: A slightly useful ircd
3 * reject.c: reject users with prejudice
4 *
5 * Copyright (C) 2003 Aaron Sethman <androsyn@ratbox.org>
6 * Copyright (C) 2003-2005 ircd-ratbox development team
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 *
23 * $Id: reject.c 849 2006-02-15 01:33:43Z jilles $
24 */
25
26 #include "stdinc.h"
27 #include "config.h"
28 #include "patricia.h"
29 #include "client.h"
30 #include "s_conf.h"
31 #include "event.h"
32 #include "tools.h"
33 #include "reject.h"
34 #include "s_stats.h"
35 #include "msg.h"
36
37 static patricia_tree_t *reject_tree;
38 dlink_list delay_exit;
39 static dlink_list reject_list;
40
41 struct reject_data
42 {
43 dlink_node rnode;
44 time_t time;
45 unsigned int count;
46 };
47
48 static void
49 reject_exit(void *unused)
50 {
51 struct Client *client_p;
52 dlink_node *ptr, *ptr_next;
53
54 DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head)
55 {
56 client_p = ptr->data;
57 if(IsDead(client_p))
58 continue;
59
60 /* this MUST be here, to prevent the possibility
61 * sendto_one() generates a write error, and then a client
62 * ends up on the dead_list and the abort_list --fl
63 *
64 * new disconnect notice stolen from ircu --nenolod
65 * no, this only happens when someone's IP has some
66 * ban on it and rejects them rather longer than the
67 * ircu message suggests --jilles
68 */
69 if(!IsIOError(client_p))
70 sendto_one(client_p, "ERROR :Closing Link: %s (*** Banned (cache))", client_p->host);
71
72 close_connection(client_p);
73 SetDead(client_p);
74 dlinkAddAlloc(client_p, &dead_list);
75 }
76
77 delay_exit.head = delay_exit.tail = NULL;
78 delay_exit.length = 0;
79 }
80
81 static void
82 reject_expires(void *unused)
83 {
84 dlink_node *ptr, *next;
85 patricia_node_t *pnode;
86 struct reject_data *rdata;
87
88 DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
89 {
90 pnode = ptr->data;
91 rdata = pnode->data;
92
93 if(rdata->time + ConfigFileEntry.reject_duration > CurrentTime)
94 continue;
95
96 dlinkDelete(ptr, &reject_list);
97 MyFree(rdata);
98 patricia_remove(reject_tree, pnode);
99 }
100 }
101
102 void
103 init_reject(void)
104 {
105 reject_tree = New_Patricia(PATRICIA_BITS);
106 eventAdd("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME);
107 eventAdd("reject_expires", reject_expires, NULL, 60);
108 }
109
110
111 void
112 add_reject(struct Client *client_p)
113 {
114 patricia_node_t *pnode;
115 struct reject_data *rdata;
116
117 /* Reject is disabled */
118 if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0)
119 return;
120
121 if((pnode = match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL)
122 {
123 rdata = pnode->data;
124 rdata->time = CurrentTime;
125 rdata->count++;
126 }
127 else
128 {
129 int bitlen = 32;
130 #ifdef IPV6
131 if(client_p->localClient->ip.ss_family == AF_INET6)
132 bitlen = 128;
133 #endif
134 pnode = make_and_lookup_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen);
135 pnode->data = rdata = MyMalloc(sizeof(struct reject_data));
136 dlinkAddTail(pnode, &rdata->rnode, &reject_list);
137 rdata->time = CurrentTime;
138 rdata->count = 1;
139 }
140 }
141
142 int
143 check_reject(struct Client *client_p)
144 {
145 patricia_node_t *pnode;
146 struct reject_data *rdata;
147
148 /* Reject is disabled */
149 if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0 ||
150 ConfigFileEntry.reject_duration == 0)
151 return 0;
152
153 pnode = match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip);
154 if(pnode != NULL)
155 {
156 rdata = pnode->data;
157
158 rdata->time = CurrentTime;
159 if(rdata->count > ConfigFileEntry.reject_after_count)
160 {
161 ServerStats->is_rej++;
162 SetReject(client_p);
163 comm_setselect(client_p->localClient->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0);
164 SetClosing(client_p);
165 dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit);
166 return 1;
167 }
168 }
169 /* Caller does what it wants */
170 return 0;
171 }
172
173 void
174 flush_reject(void)
175 {
176 dlink_node *ptr, *next;
177 patricia_node_t *pnode;
178 struct reject_data *rdata;
179
180 DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
181 {
182 pnode = ptr->data;
183 rdata = pnode->data;
184 dlinkDelete(ptr, &reject_list);
185 MyFree(rdata);
186 patricia_remove(reject_tree, pnode);
187 }
188 }
189
190 int
191 remove_reject(const char *ip)
192 {
193 patricia_node_t *pnode;
194
195 /* Reject is disabled */
196 if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0 ||
197 ConfigFileEntry.reject_duration == 0)
198 return -1;
199
200 if((pnode = match_string(reject_tree, ip)) != NULL)
201 {
202 struct reject_data *rdata = pnode->data;
203 dlinkDelete(&rdata->rnode, &reject_list);
204 MyFree(rdata);
205 patricia_remove(reject_tree, pnode);
206 return 1;
207 }
208 return 0;
209 }
210