ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_dline.c
Revision: 3929
Committed: Mon Jun 9 14:56:25 2014 UTC (11 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 11149 byte(s)
Log Message:
- Greatly speedup d-line lookup. Instead of testing every single client against
  every existing d-line just check the just added ban against connected clients.
  Also now check d-line _before_ looking for a matching exempt{}

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 */
21
22 /*! \file m_dline.c
23 * \brief Includes required functions for processing the DLINE command.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "client.h"
30 #include "irc_string.h"
31 #include "conf.h"
32 #include "ircd.h"
33 #include "hostmask.h"
34 #include "numeric.h"
35 #include "log.h"
36 #include "misc.h"
37 #include "send.h"
38 #include "hash.h"
39 #include "server.h"
40 #include "parse.h"
41 #include "modules.h"
42 #include "conf_db.h"
43 #include "memory.h"
44
45
46 static void
47 check_dline(struct AddressRec *arec)
48 {
49 dlink_node *ptr = NULL, *ptr_next = NULL;
50
51 DLINK_FOREACH_SAFE(ptr, ptr_next, local_client_list.head)
52 {
53 struct Client *client_p = ptr->data;
54
55 if (IsDead(client_p))
56 continue;
57
58 switch (arec->masktype)
59 {
60 case HM_IPV4:
61 if (client_p->localClient->aftype == AF_INET)
62 if (match_ipv4(&client_p->localClient->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits))
63 conf_try_ban(client_p, arec->conf);
64 break;
65 case HM_IPV6:
66 if (client_p->localClient->aftype == AF_INET6)
67 if (match_ipv6(&client_p->localClient->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits))
68 conf_try_ban(client_p, arec->conf);
69 break;
70 default: break;
71 }
72 }
73
74 DLINK_FOREACH_SAFE(ptr, ptr_next, unknown_list.head)
75 {
76 struct Client *client_p = ptr->data;
77
78 if (IsDead(client_p))
79 continue;
80
81 switch (arec->masktype)
82 {
83 case HM_IPV4:
84 if (client_p->localClient->aftype == AF_INET)
85 if (match_ipv4(&client_p->localClient->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits))
86 conf_try_ban(client_p, arec->conf);
87 break;
88 case HM_IPV6:
89 if (client_p->localClient->aftype == AF_INET6)
90 if (match_ipv6(&client_p->localClient->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits))
91 conf_try_ban(client_p, arec->conf);
92 break;
93 default: break;
94 }
95 }
96 }
97
98
99 /* apply_tdline()
100 *
101 * inputs -
102 * output - NONE
103 * side effects - tkline as given is placed
104 */
105 static void
106 apply_dline(struct Client *source_p, struct MaskItem *conf,
107 time_t tkline_time)
108 {
109 if (tkline_time)
110 {
111 conf->until = CurrentTime + tkline_time;
112 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
113 "%s added temporary %d min. D-Line for [%s] [%s]",
114 get_oper_name(source_p), tkline_time/60,
115 conf->host, conf->reason);
116 sendto_one_notice(source_p, &me, ":Added temporary %d min. D-Line [%s]",
117 tkline_time/60, conf->host);
118 ilog(LOG_TYPE_DLINE, "%s added temporary %d min. D-Line for [%s] [%s]",
119 get_oper_name(source_p), tkline_time/60, conf->host, conf->reason);
120 }
121 else
122 {
123 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
124 "%s added D-Line for [%s] [%s]",
125 get_oper_name(source_p), conf->host, conf->reason);
126 sendto_one_notice(source_p, &me, ":Added D-Line [%s]", conf->host);
127 ilog(LOG_TYPE_DLINE, "%s added D-Line for [%s] [%s]",
128 get_oper_name(source_p), conf->host, conf->reason);
129
130 }
131
132 SetConfDatabase(conf);
133 conf->setat = CurrentTime;
134 check_dline(add_conf_by_address(CONF_DLINE, conf));
135 }
136
137 /* mo_dline()
138 *
139 * inputs - pointer to server
140 * - pointer to client
141 * - parameter count
142 * - parameter list
143 * output -
144 * side effects - D line is added
145 *
146 */
147 static int
148 mo_dline(struct Client *source_p, int parc, char *parv[])
149 {
150 char def_reason[] = CONF_NOREASON;
151 char *dlhost = NULL, *reason = NULL;
152 char *target_server = NULL;
153 const char *creason;
154 const struct Client *target_p = NULL;
155 struct irc_ssaddr daddr;
156 struct MaskItem *conf=NULL;
157 time_t tkline_time=0;
158 int bits = 0, aftype = 0, t = 0;
159 const char *current_date = NULL;
160 char hostip[HOSTIPLEN + 1];
161 char buffer[IRCD_BUFSIZE];
162
163 if (!HasOFlag(source_p, OPER_FLAG_DLINE))
164 {
165 sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "dline");
166 return 0;
167 }
168
169 if (parse_aline("DLINE", source_p, parc, parv, AWILD, &dlhost,
170 NULL, &tkline_time, &target_server, &reason) < 0)
171 return 0;
172
173 if (target_server)
174 {
175 sendto_match_servs(source_p, target_server, CAP_DLN, "DLINE %s %lu %s :%s",
176 target_server, (unsigned long)tkline_time,
177 dlhost, reason);
178
179 /* Allow ON to apply local kline as well if it matches */
180 if (match(target_server, me.name))
181 return 0;
182 }
183 else
184 cluster_a_line(source_p, "DLINE", CAP_DLN, SHARED_DLINE,
185 "%d %s :%s", tkline_time, dlhost, reason);
186
187 if ((t = parse_netmask(dlhost, NULL, &bits)) == HM_HOST)
188 {
189 if ((target_p = find_chasing(source_p, dlhost)) == NULL)
190 return 0; /* find_chasing sends ERR_NOSUCHNICK */
191
192 if (!MyConnect(target_p))
193 {
194 sendto_one_notice(source_p, &me, ":Cannot DLINE nick on another server");
195 return 0;
196 }
197
198 if (IsExemptKline(target_p))
199 {
200 sendto_one_notice(source_p, &me, ":%s is E-lined",
201 target_p->name);
202 return 0;
203 }
204
205 getnameinfo((struct sockaddr *)&target_p->localClient->ip,
206 target_p->localClient->ip.ss_len, hostip,
207 sizeof(hostip), NULL, 0, NI_NUMERICHOST);
208 dlhost = hostip;
209 t = parse_netmask(dlhost, NULL, &bits);
210 assert(t == HM_IPV4 || t == HM_IPV6);
211 }
212
213 if (bits < 8)
214 {
215 sendto_one_notice(source_p, &me, ":For safety, bitmasks less than 8 require conf access.");
216 return 0;
217 }
218
219 #ifdef IPV6
220 if (t == HM_IPV6)
221 aftype = AF_INET6;
222 else
223 #endif
224 aftype = AF_INET;
225
226 parse_netmask(dlhost, &daddr, NULL);
227
228 if ((conf = find_dline_conf(&daddr, aftype)))
229 {
230 creason = conf->reason ? conf->reason : def_reason;
231
232 if (IsConfExemptKline(conf))
233 sendto_one_notice(source_p, &me, ":[%s] is (E)d-lined by [%s] - %s",
234 dlhost, conf->host, creason);
235 else
236 sendto_one_notice(source_p, &me, ":[%s] already D-lined by [%s] - %s",
237 dlhost, conf->host, creason);
238 return 0;
239 }
240
241 current_date = smalldate(0);
242
243 if (!valid_comment(source_p, reason, 1))
244 return 0;
245
246 conf = conf_make(CONF_DLINE);
247 conf->host = xstrdup(dlhost);
248
249 if (tkline_time)
250 snprintf(buffer, sizeof(buffer), "Temporary D-line %d min. - %s (%s)",
251 (int)(tkline_time/60), reason, current_date);
252 else
253 snprintf(buffer, sizeof(buffer), "%s (%s)", reason, current_date);
254
255 conf->reason = xstrdup(buffer);
256 apply_dline(source_p, conf, tkline_time);
257 return 0;
258 }
259
260 static int
261 ms_dline(struct Client *source_p, int parc, char *parv[])
262 {
263 char def_reason[] = CONF_NOREASON;
264 char *dlhost, *reason;
265 const char *creason;
266 const struct Client *target_p = NULL;
267 struct irc_ssaddr daddr;
268 struct MaskItem *conf=NULL;
269 time_t tkline_time=0;
270 int bits = 0, aftype = 0, t = 0;
271 const char *current_date = NULL;
272 char hostip[HOSTIPLEN + 1];
273 char buffer[IRCD_BUFSIZE];
274
275 if (parc != 5 || EmptyString(parv[4]))
276 return 0;
277
278 /* parv[0] parv[1] parv[2] parv[3] parv[4] */
279 /* oper target_server tkline_time host reason */
280 sendto_match_servs(source_p, parv[1], CAP_DLN,
281 "DLINE %s %s %s :%s",
282 parv[1], parv[2], parv[3], parv[4]);
283
284 if (match(parv[1], me.name))
285 return 0;
286
287 tkline_time = valid_tkline(parv[2], TK_SECONDS);
288 dlhost = parv[3];
289 reason = parv[4];
290
291 if (HasFlag(source_p, FLAGS_SERVICE) ||
292 find_matching_name_conf(CONF_ULINE, source_p->servptr->name,
293 source_p->username, source_p->host,
294 SHARED_DLINE))
295 {
296 if (!IsClient(source_p))
297 return 0;
298
299 if ((t = parse_netmask(dlhost, NULL, &bits)) == HM_HOST)
300 {
301 if ((target_p = find_chasing(source_p, dlhost)) == NULL)
302 return 0; /* find_chasing sends ERR_NOSUCHNICK */
303
304 if (!MyConnect(target_p))
305 {
306 sendto_one_notice(source_p, &me, ":Cannot DLINE nick on another server");
307 return 0;
308 }
309
310 if (IsExemptKline(target_p))
311 {
312 sendto_one_notice(source_p, &me, ":%s is E-lined", target_p->name);
313 return 0;
314 }
315
316 getnameinfo((struct sockaddr *)&target_p->localClient->ip,
317 target_p->localClient->ip.ss_len, hostip,
318 sizeof(hostip), NULL, 0, NI_NUMERICHOST);
319 dlhost = hostip;
320 t = parse_netmask(dlhost, NULL, &bits);
321 assert(t == HM_IPV4 || t == HM_IPV6);
322 }
323
324 if (bits < 8)
325 {
326 sendto_one_notice(source_p, &me, ":For safety, bitmasks less than 8 require conf access.");
327 return 0;
328 }
329
330 #ifdef IPV6
331 if (t == HM_IPV6)
332 aftype= AF_INET6;
333 else
334 #endif
335 aftype = AF_INET;
336
337 parse_netmask(dlhost, &daddr, NULL);
338
339 if ((conf = find_dline_conf(&daddr, aftype)))
340 {
341 creason = conf->reason ? conf->reason : def_reason;
342 if (IsConfExemptKline(conf))
343 sendto_one_notice(source_p, &me, ":[%s] is (E)d-lined by [%s] - %s",
344 dlhost, conf->host, creason);
345 else
346 sendto_one_notice(source_p, &me, ":[%s] already D-lined by [%s] - %s",
347 dlhost, conf->host, creason);
348 return 0;
349 }
350
351 current_date = smalldate(0);
352
353 if (!valid_comment(source_p, reason, 1))
354 return 0;
355
356 conf = conf_make(CONF_DLINE);
357 conf->host = xstrdup(dlhost);
358
359 if (tkline_time)
360 snprintf(buffer, sizeof(buffer), "Temporary D-line %d min. - %s (%s)",
361 (int)(tkline_time/60), reason, current_date);
362 else
363 snprintf(buffer, sizeof(buffer), "%s (%s)", reason, current_date);
364
365 conf->reason = xstrdup(buffer);
366 apply_dline(source_p, conf, tkline_time);
367 }
368
369 return 0;
370 }
371
372 static struct Message dline_msgtab =
373 {
374 "DLINE", 0, 0, 2, MAXPARA, MFLG_SLOW, 0,
375 { m_unregistered, m_not_oper, ms_dline, m_ignore, mo_dline, m_ignore }
376 };
377
378 static void
379 module_init(void)
380 {
381 mod_add_cmd(&dline_msgtab);
382 add_capability("DLN", CAP_DLN, 1);
383 }
384
385 static void
386 module_exit(void)
387 {
388 mod_del_cmd(&dline_msgtab);
389 delete_capability("DLN");
390 }
391
392 struct module module_entry =
393 {
394 .node = { NULL, NULL, NULL },
395 .name = NULL,
396 .version = "$Revision$",
397 .handle = NULL,
398 .modinit = module_init,
399 .modexit = module_exit,
400 .flags = 0
401 };

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision