1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* m_kline.c: Bans a user. |
4 |
* |
5 |
* Copyright (C) 2005 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
#include "stdinc.h" |
26 |
#include "channel.h" |
27 |
#include "client.h" |
28 |
#include "common.h" |
29 |
#include "ircd.h" |
30 |
#include "hostmask.h" |
31 |
#include "numeric.h" |
32 |
#include "s_conf.h" |
33 |
#include "parse_aline.h" |
34 |
#include "send.h" |
35 |
#include "hash.h" |
36 |
#include "handlers.h" |
37 |
#include "s_serv.h" |
38 |
#include "msg.h" |
39 |
#include "s_gline.h" |
40 |
#include "parse.h" |
41 |
#include "conf/modules.h" |
42 |
|
43 |
static void me_rkline(struct Client *, struct Client *, int, char *[]); |
44 |
static void mo_rkline(struct Client *, struct Client *, int, char *[]); |
45 |
static void ms_rkline(struct Client *, struct Client *, int, char *[]); |
46 |
|
47 |
static void me_unrkline(struct Client *, struct Client *, int, char *[]); |
48 |
static void mo_unrkline(struct Client *, struct Client *, int, char *[]); |
49 |
static void ms_unrkline(struct Client *, struct Client *, int, char *[]); |
50 |
/* Local function prototypes */ |
51 |
static int already_placed_rkline(struct Client *, const char *, const char *); |
52 |
static void apply_rkline(struct Client *, struct ConfItem *, const char *, time_t); |
53 |
static void apply_trkline(struct Client *, struct ConfItem *, int); |
54 |
|
55 |
static char buffer[IRCD_BUFSIZE]; |
56 |
static int remove_trkline_match(const char *, const char *); |
57 |
|
58 |
struct Message rkline_msgtab = { |
59 |
"RKLINE", 0, 0, 2, 0, MFLG_SLOW, 0, |
60 |
{m_unregistered, m_not_oper, ms_rkline, me_rkline, mo_rkline, m_ignore} |
61 |
}; |
62 |
|
63 |
struct Message unrkline_msgtab = { |
64 |
"UNRKLINE", 0, 0, 2, 0, MFLG_SLOW, 0, |
65 |
{m_unregistered, m_not_oper, ms_unrkline, me_unrkline, mo_unrkline, m_ignore} |
66 |
}; |
67 |
|
68 |
INIT_MODULE(m_rkline, "$Revision$") |
69 |
{ |
70 |
mod_add_cmd(&rkline_msgtab); |
71 |
mod_add_cmd(&unrkline_msgtab); |
72 |
} |
73 |
|
74 |
CLEANUP_MODULE |
75 |
{ |
76 |
mod_del_cmd(&unrkline_msgtab); |
77 |
mod_del_cmd(&rkline_msgtab); |
78 |
} |
79 |
|
80 |
/* mo_rkline() |
81 |
* |
82 |
* inputs - pointer to server |
83 |
* - pointer to client |
84 |
* - parameter count |
85 |
* - parameter list |
86 |
* output - |
87 |
* side effects - rk line is added |
88 |
*/ |
89 |
static void |
90 |
mo_rkline(struct Client *client_p, struct Client *source_p, |
91 |
int parc, char *parv[]) |
92 |
{ |
93 |
pcre *exp_user = NULL, *exp_host = NULL; |
94 |
const char *errptr = NULL; |
95 |
char *reason = NULL; |
96 |
char *oper_reason = NULL; |
97 |
char *user = NULL; |
98 |
char *host = NULL; |
99 |
const char *current_date = NULL; |
100 |
char *target_server = NULL; |
101 |
struct ConfItem *conf = NULL; |
102 |
struct AccessItem *aconf = NULL; |
103 |
time_t tkline_time = 0; |
104 |
time_t cur_time = 0; |
105 |
|
106 |
if (!IsAdmin(source_p) || !IsOperK(source_p)) |
107 |
{ |
108 |
sendto_one(source_p, form_str(ERR_NOPRIVS), |
109 |
me.name, source_p->name, "rkline"); |
110 |
return; |
111 |
} |
112 |
|
113 |
if (parse_aline("RKLINE", source_p, parc, parv, NOUSERLOOKUP, &user, |
114 |
&host, &tkline_time, &target_server, &reason) < 0) |
115 |
return; |
116 |
|
117 |
if (target_server != NULL) |
118 |
{ |
119 |
if (HasID(source_p)) |
120 |
{ |
121 |
sendto_server(NULL, source_p, NULL, CAP_KLN|CAP_TS6, NOCAPS, |
122 |
":%s RKLINE %s %lu %s %s :%s", |
123 |
source_p->id, target_server, (unsigned long)tkline_time, |
124 |
user, host, reason); |
125 |
sendto_server(NULL, source_p, NULL, CAP_KLN, CAP_TS6, |
126 |
":%s RKLINE %s %lu %s %s :%s", |
127 |
source_p->name, target_server, (unsigned long)tkline_time, |
128 |
user, host, reason); |
129 |
} |
130 |
else |
131 |
sendto_server(NULL, source_p, NULL, CAP_KLN, NOCAPS, |
132 |
":%s RKLINE %s %lu %s %s :%s", |
133 |
source_p->name, target_server, (unsigned long)tkline_time, |
134 |
user, host, reason); |
135 |
|
136 |
/* Allow ON to apply local kline as well if it matches */ |
137 |
if (!match(target_server, me.name)) |
138 |
return; |
139 |
} |
140 |
#if 0 |
141 |
else |
142 |
cluster_a_line(source_p, "RKLINE", CAP_KLN, SHARED_KLINE, |
143 |
"%d %s %s :%s", tkline_time, user, host, reason); |
144 |
#endif |
145 |
|
146 |
if (already_placed_rkline(source_p, user, host)) |
147 |
return; |
148 |
|
149 |
/* Look for an oper reason */ |
150 |
if ((oper_reason = strchr(reason, '|')) != NULL) |
151 |
*oper_reason++ = '\0'; |
152 |
|
153 |
if (!(exp_user = ircd_pcre_compile(user, &errptr)) || |
154 |
!(exp_host = ircd_pcre_compile(host, &errptr))) |
155 |
{ |
156 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
157 |
"Failed to add regular expression based K-Line: %s", errptr); |
158 |
return; |
159 |
} |
160 |
|
161 |
cur_time = CurrentTime; |
162 |
current_date = smalldate(cur_time); |
163 |
conf = make_conf_item(RKLINE_TYPE); |
164 |
aconf = &conf->conf.AccessItem; |
165 |
|
166 |
DupString(aconf->host, host); |
167 |
DupString(aconf->user, user); |
168 |
|
169 |
aconf->regexuser = exp_user; |
170 |
aconf->regexhost = exp_host; |
171 |
|
172 |
if (tkline_time != 0) |
173 |
{ |
174 |
ircsprintf(buffer, "Temporary RK-line %d min. - %s (%s)", |
175 |
(int)(tkline_time/60), reason, current_date); |
176 |
DupString(aconf->reason, buffer); |
177 |
|
178 |
if (oper_reason != NULL) |
179 |
DupString(aconf->oper_reason, oper_reason); |
180 |
apply_trkline(source_p, conf, tkline_time); |
181 |
} |
182 |
else |
183 |
{ |
184 |
ircsprintf(buffer, "%s (%s)", reason, current_date); |
185 |
DupString(aconf->reason, buffer); |
186 |
|
187 |
if (oper_reason != NULL) |
188 |
DupString(aconf->oper_reason, oper_reason); |
189 |
apply_rkline(source_p, conf, current_date, cur_time); |
190 |
} |
191 |
} |
192 |
|
193 |
/* me_rkline - handle remote rkline. no propagation */ |
194 |
static void |
195 |
me_rkline(struct Client *client_p, struct Client *source_p, |
196 |
int parc, char *parv[]) |
197 |
{ |
198 |
struct ConfItem *conf = NULL; |
199 |
struct AccessItem *aconf = NULL; |
200 |
int tkline_time; |
201 |
const char *current_date = NULL; |
202 |
time_t cur_time; |
203 |
char *kuser, *khost, *kreason, *oper_reason; |
204 |
|
205 |
if (parc != 6 || EmptyString(parv[5])) |
206 |
return; |
207 |
|
208 |
if (!match(parv[1], me.name)) |
209 |
return; |
210 |
|
211 |
tkline_time = valid_tkline(parv[2], TK_SECONDS); |
212 |
kuser = parv[3]; |
213 |
khost = parv[4]; |
214 |
kreason = parv[5]; |
215 |
|
216 |
if ((oper_reason = strchr(kreason, '|')) != NULL) |
217 |
*oper_reason++ = '\0'; |
218 |
|
219 |
cur_time = CurrentTime; |
220 |
current_date = smalldate(cur_time); |
221 |
|
222 |
if (find_matching_name_conf(ULINE_TYPE, source_p->servptr->name, |
223 |
source_p->username, source_p->host, |
224 |
SHARED_KLINE)) |
225 |
{ |
226 |
pcre *exp_user = NULL, *exp_host = NULL; |
227 |
const char *errptr = NULL; |
228 |
|
229 |
if (!IsClient(source_p) || |
230 |
already_placed_rkline(source_p, kuser, khost)) |
231 |
return; |
232 |
|
233 |
if (!(exp_user = ircd_pcre_compile(kuser, &errptr)) || |
234 |
!(exp_host = ircd_pcre_compile(khost, &errptr))) |
235 |
{ |
236 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
237 |
"Failed to add regular expression based K-Line: %s", errptr); |
238 |
return; |
239 |
} |
240 |
|
241 |
conf = make_conf_item(RKLINE_TYPE); |
242 |
aconf = &conf->conf.AccessItem; |
243 |
DupString(aconf->host, khost); |
244 |
DupString(aconf->user, kuser); |
245 |
|
246 |
aconf->regexuser = exp_user; |
247 |
aconf->regexhost = exp_host; |
248 |
|
249 |
if (tkline_time) |
250 |
{ |
251 |
ircsprintf(buffer, "Temporary RK-line %d min. - %s (%s)", |
252 |
(int)(tkline_time/60), kreason, current_date); |
253 |
DupString(aconf->reason, buffer); |
254 |
|
255 |
if (oper_reason != NULL) |
256 |
DupString(aconf->oper_reason, oper_reason); |
257 |
apply_trkline(source_p, conf, tkline_time); |
258 |
} |
259 |
else |
260 |
{ |
261 |
ircsprintf(buffer, "%s (%s)", kreason, current_date); |
262 |
DupString(aconf->reason, buffer); |
263 |
|
264 |
if (oper_reason != NULL) |
265 |
DupString(aconf->oper_reason, oper_reason); |
266 |
apply_rkline(source_p, conf, current_date, cur_time); |
267 |
} |
268 |
} |
269 |
} |
270 |
|
271 |
static void |
272 |
ms_rkline(struct Client *client_p, struct Client *source_p, |
273 |
int parc, char *parv[]) |
274 |
{ |
275 |
if (parc != 6 || EmptyString(parv[5])) |
276 |
return; |
277 |
|
278 |
/* parv[0] parv[1] parv[2] parv[3] parv[4] parv[5] */ |
279 |
/* oper target_server tkline_time user host reason */ |
280 |
sendto_match_servs(source_p, parv[1], CAP_KLN, |
281 |
"RKLINE %s %s %s %s :%s", |
282 |
parv[1], parv[2], parv[3], parv[4], parv[5]); |
283 |
|
284 |
me_rkline(client_p, source_p, parc, parv); |
285 |
} |
286 |
|
287 |
/* apply_rkline() |
288 |
* |
289 |
* inputs - |
290 |
* output - NONE |
291 |
* side effects - kline as given, is added to the hashtable |
292 |
* and conf file |
293 |
*/ |
294 |
static void |
295 |
apply_rkline(struct Client *source_p, struct ConfItem *conf, |
296 |
const char *current_date, time_t cur_time) |
297 |
{ |
298 |
write_conf_line(source_p, conf, current_date, cur_time); |
299 |
/* Now, activate kline against current online clients */ |
300 |
rehashed_klines = 1; |
301 |
} |
302 |
|
303 |
/* apply_trkline() |
304 |
* |
305 |
* inputs - |
306 |
* output - NONE |
307 |
* side effects - tkline as given is placed |
308 |
*/ |
309 |
static void |
310 |
apply_trkline(struct Client *source_p, struct ConfItem *conf, |
311 |
int tkline_time) |
312 |
{ |
313 |
struct AccessItem *aconf = &conf->conf.AccessItem; |
314 |
|
315 |
aconf->hold = CurrentTime + tkline_time; |
316 |
add_temp_line(conf); |
317 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
318 |
"%s added temporary %d min. RK-Line for [%s@%s] [%s]", |
319 |
get_oper_name(source_p), tkline_time/60, |
320 |
aconf->user, aconf->host, |
321 |
aconf->reason); |
322 |
sendto_one(source_p, ":%s NOTICE %s :Added temporary %d min. RK-Line [%s@%s]", |
323 |
MyConnect(source_p) ? me.name : ID_or_name(&me, source_p->from), |
324 |
source_p->name, tkline_time/60, aconf->user, aconf->host); |
325 |
ilog(L_TRACE, "%s added temporary %d min. RK-Line for [%s@%s] [%s]", |
326 |
source_p->name, tkline_time/60, |
327 |
aconf->user, aconf->host, aconf->reason); |
328 |
rehashed_klines = 1; |
329 |
} |
330 |
|
331 |
/* already_placed_rkline() |
332 |
* inputs - user to complain to, username & host to check for |
333 |
* outputs - returns 1 on existing K-line, 0 if doesn't exist |
334 |
* side effects - notifies source_p if the K-line already exists |
335 |
*/ |
336 |
static int |
337 |
already_placed_rkline(struct Client *source_p, const char *user, const char *host) |
338 |
{ |
339 |
const dlink_node *ptr = NULL; |
340 |
|
341 |
DLINK_FOREACH(ptr, rkconf_items.head) |
342 |
{ |
343 |
struct AccessItem *aptr = &((struct ConfItem *)(ptr->data))->conf.AccessItem; |
344 |
|
345 |
if (!strcmp(user, aptr->user) && |
346 |
!strcmp(aptr->host, host)) |
347 |
{ |
348 |
sendto_one(source_p, |
349 |
":%s NOTICE %s :[%s@%s] already RK-Lined by [%s@%s] - %s", |
350 |
me.name, source_p->name, user, host, aptr->user, |
351 |
aptr->host, aptr->reason ? aptr->reason : "No reason"); |
352 |
return 1; |
353 |
} |
354 |
} |
355 |
|
356 |
return 0; |
357 |
} |
358 |
|
359 |
/* |
360 |
* mo_unrkline |
361 |
* parv[0] = sender |
362 |
* parv[1] = address to remove |
363 |
*/ |
364 |
static void |
365 |
mo_unrkline(struct Client *client_p,struct Client *source_p, |
366 |
int parc, char *parv[]) |
367 |
{ |
368 |
char *target_server = NULL; |
369 |
char *user, *host; |
370 |
|
371 |
if (!IsAdmin(source_p) || !IsOperUnkline(source_p)) |
372 |
{ |
373 |
sendto_one(source_p, form_str(ERR_NOPRIVS), |
374 |
me.name, source_p->name, "unrkline"); |
375 |
return; |
376 |
} |
377 |
|
378 |
if (parc < 2 || *parv[1] == '\0') |
379 |
{ |
380 |
sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), |
381 |
me.name, source_p->name, "UNRKLINE"); |
382 |
return; |
383 |
} |
384 |
|
385 |
if (parse_aline("UNRKLINE", source_p, parc, parv, NOUSERLOOKUP, &user, |
386 |
&host, NULL, &target_server, NULL) < 0) |
387 |
return; |
388 |
|
389 |
if (target_server != NULL) |
390 |
{ |
391 |
sendto_match_servs(source_p, target_server, CAP_UNKLN, |
392 |
"UNRKLINE %s %s %s", |
393 |
target_server, user, host); |
394 |
|
395 |
/* Allow ON to apply local unkline as well if it matches */ |
396 |
if (!match(target_server, me.name)) |
397 |
return; |
398 |
} |
399 |
#if 0 |
400 |
else |
401 |
cluster_a_line(source_p, "UNRKLINE", CAP_UNKLN, SHARED_UNKLINE, |
402 |
"%s %s", user, host); |
403 |
#endif |
404 |
|
405 |
if (remove_trkline_match(host, user)) |
406 |
{ |
407 |
sendto_one(source_p, |
408 |
":%s NOTICE %s :Un-klined [%s@%s] from temporary RK-Lines", |
409 |
me.name, source_p->name, user, host); |
410 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
411 |
"%s has removed the temporary RK-Line for: [%s@%s]", |
412 |
get_oper_name(source_p), user, host); |
413 |
ilog(L_NOTICE, "%s removed temporary RK-Line for [%s@%s]", |
414 |
source_p->name, user, host); |
415 |
return; |
416 |
} |
417 |
|
418 |
if (remove_conf_line(RKLINE_TYPE, source_p, user, host) > 0) |
419 |
{ |
420 |
sendto_one(source_p, ":%s NOTICE %s :RK-Line for [%s@%s] is removed", |
421 |
me.name, source_p->name, user,host); |
422 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
423 |
"%s has removed the RK-Line for: [%s@%s]", |
424 |
get_oper_name(source_p), user, host); |
425 |
ilog(L_NOTICE, "%s removed RK-Line for [%s@%s]", |
426 |
source_p->name, user, host); |
427 |
} |
428 |
else |
429 |
sendto_one(source_p, ":%s NOTICE %s :No RK-Line for [%s@%s] found", |
430 |
me.name, source_p->name, user, host); |
431 |
} |
432 |
|
433 |
/* me_unrkline() |
434 |
* |
435 |
* inputs - server |
436 |
* - client |
437 |
* - parc |
438 |
* - parv |
439 |
* outputs - none |
440 |
* side effects - if server is authorized, rkline is removed |
441 |
* does not propagate message |
442 |
*/ |
443 |
static void |
444 |
me_unrkline(struct Client *client_p, struct Client *source_p, |
445 |
int parc, char *parv[]) |
446 |
{ |
447 |
const char *user = NULL, *host = NULL; |
448 |
|
449 |
if (parc != 4 || EmptyString(parv[3])) |
450 |
return; |
451 |
|
452 |
user = parv[2]; |
453 |
host = parv[3]; |
454 |
|
455 |
if (!IsClient(source_p) || !match(parv[1], me.name)) |
456 |
return; |
457 |
|
458 |
if (find_matching_name_conf(ULINE_TYPE, source_p->servptr->name, |
459 |
source_p->username, source_p->host, |
460 |
SHARED_UNKLINE)) |
461 |
{ |
462 |
if (remove_trkline_match(host, user)) |
463 |
{ |
464 |
sendto_one(source_p, |
465 |
":%s NOTICE %s :Un-klined [%s@%s] from temporary RK-Lines", |
466 |
me.name, source_p->name, user, host); |
467 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
468 |
"%s has removed the temporary RK-Line for: [%s@%s]", |
469 |
get_oper_name(source_p), user, host); |
470 |
ilog(L_NOTICE, "%s removed temporary RK-Line for [%s@%s]", |
471 |
source_p->name, user, host); |
472 |
return; |
473 |
} |
474 |
|
475 |
if (remove_conf_line(RKLINE_TYPE, source_p, user, host) > 0) |
476 |
{ |
477 |
sendto_one(source_p, ":%s NOTICE %s :RK-Line for [%s@%s] is removed", |
478 |
me.name, source_p->name, user, host); |
479 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
480 |
"%s has removed the RK-Line for: [%s@%s]", |
481 |
get_oper_name(source_p), user, host); |
482 |
|
483 |
ilog(L_NOTICE, "%s removed RK-Line for [%s@%s]", |
484 |
source_p->name, user, host); |
485 |
} |
486 |
else |
487 |
sendto_one(source_p, ":%s NOTICE %s :No RK-Line for [%s@%s] found", |
488 |
me.name, source_p->name, user, host); |
489 |
} |
490 |
} |
491 |
|
492 |
/* ms_unrkline - propagates and handles a remote unrkline message */ |
493 |
static void |
494 |
ms_unrkline(struct Client *client_p, struct Client *source_p, |
495 |
int parc, char *parv[]) |
496 |
{ |
497 |
if (parc != 4 || EmptyString(parv[3])) |
498 |
return; |
499 |
|
500 |
sendto_match_servs(source_p, parv[1], CAP_UNKLN, |
501 |
"UNRKLINE %s %s %s", |
502 |
parv[1], parv[2], parv[3]); |
503 |
|
504 |
me_unrkline(client_p, source_p, parc, parv); |
505 |
} |
506 |
|
507 |
/* static int remove_rtkline_match(const char *host, const char *user) |
508 |
* Input: A hostname, a username to unrkline. |
509 |
* Output: returns YES on success, NO if no trkline removed. |
510 |
* Side effects: Any matching trklines are removed. |
511 |
*/ |
512 |
static int |
513 |
remove_trkline_match(const char *const host, |
514 |
const char *const user) |
515 |
{ |
516 |
dlink_node *ptr = NULL; |
517 |
|
518 |
DLINK_FOREACH(ptr, temporary_rklines.head) |
519 |
{ |
520 |
struct ConfItem *conf = ptr->data; |
521 |
struct AccessItem *aptr = &conf->conf.AccessItem; |
522 |
|
523 |
if (!strcmp(user, aptr->user) && |
524 |
!strcmp(aptr->host, host)) |
525 |
{ |
526 |
dlinkDelete(ptr, &temporary_rklines); |
527 |
free_dlink_node(ptr); |
528 |
delete_conf_item(conf); |
529 |
return 1; |
530 |
} |
531 |
} |
532 |
|
533 |
return 0; |
534 |
} |