]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_kline.c
Document BAN message.
[irc/rqf/shadowircd.git] / modules / m_kline.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_kline.c: Bans/unbans a user.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
d8a4c5f6 24 * $Id$
212380e3 25 */
26
27#include "stdinc.h"
212380e3 28#include "channel.h"
29#include "class.h"
30#include "client.h"
31#include "common.h"
13ae2f4b 32#include "match.h"
212380e3 33#include "ircd.h"
34#include "hostmask.h"
35#include "numeric.h"
212380e3 36#include "s_conf.h"
37#include "s_newconf.h"
d3455e2c 38#include "logger.h"
212380e3 39#include "send.h"
40#include "hash.h"
41#include "s_serv.h"
42#include "msg.h"
43#include "parse.h"
44#include "modules.h"
35f6f850 45#include "reject.h"
d8a4c5f6 46#include "bandbi.h"
a0f4c418 47#include "operhash.h"
212380e3 48
49static int mo_kline(struct Client *, struct Client *, int, const char **);
50static int ms_kline(struct Client *, struct Client *, int, const char **);
51static int me_kline(struct Client *, struct Client *, int, const char **);
52static int mo_unkline(struct Client *, struct Client *, int, const char **);
53static int ms_unkline(struct Client *, struct Client *, int, const char **);
54static int me_unkline(struct Client *, struct Client *, int, const char **);
55
56struct Message kline_msgtab = {
57 "KLINE", 0, 0, 0, MFLG_SLOW,
58 {mg_unreg, mg_not_oper, {ms_kline, 5}, {ms_kline, 5}, {me_kline, 5}, {mo_kline, 3}}
59};
60
61struct Message unkline_msgtab = {
62 "UNKLINE", 0, 0, 0, MFLG_SLOW,
63 {mg_unreg, mg_not_oper, {ms_unkline, 4}, {ms_unkline, 4}, {me_unkline, 3}, {mo_unkline, 2}}
64};
65
66mapi_clist_av1 kline_clist[] = { &kline_msgtab, &unkline_msgtab, NULL };
d8a4c5f6
WP
67
68DECLARE_MODULE_AV1(kline, NULL, NULL, kline_clist, NULL, NULL, "$Revision$");
212380e3 69
70/* Local function prototypes */
71static int find_user_host(struct Client *source_p, const char *userhost, char *user, char *host);
72static int valid_comment(struct Client *source_p, char *comment);
73static int valid_user_host(struct Client *source_p, const char *user, const char *host);
212380e3 74
75static void handle_remote_kline(struct Client *source_p, int tkline_time,
d8a4c5f6 76 const char *user, const char *host, const char *reason);
212380e3 77static void apply_kline(struct Client *source_p, struct ConfItem *aconf,
ce60772d 78 const char *reason, const char *oper_reason);
212380e3 79static void apply_tkline(struct Client *source_p, struct ConfItem *aconf,
ce60772d 80 const char *, const char *, int);
65b8e002
JT
81static void apply_prop_kline(struct Client *source_p, struct ConfItem *aconf,
82 const char *, const char *, int);
212380e3 83static int already_placed_kline(struct Client *, const char *, const char *, int);
84
d8a4c5f6 85static void handle_remote_unkline(struct Client *source_p, const char *user, const char *host);
40a1d446 86static void remove_permkline_match(struct Client *, struct ConfItem *);
4c171a9c 87static int remove_temp_kline(struct Client *, struct ConfItem *);
65b8e002 88static void remove_prop_kline(struct Client *, struct ConfItem *);
212380e3 89
90/* mo_kline()
91 *
92 * parv[1] - temp time or user@host
93 * parv[2] - user@host, "ON", or reason
94 * parv[3] - "ON", reason, or server to target
95 * parv[4] - server to target, or reason
96 * parv[5] - reason
97 */
98static int
d8a4c5f6 99mo_kline(struct Client *client_p, struct Client *source_p, int parc, const char **parv)
212380e3 100{
d8a4c5f6 101 char def[] = "No Reason";
212380e3 102 char user[USERLEN + 2];
103 char host[HOSTLEN + 2];
d8a4c5f6 104 char *reason = def;
212380e3 105 char *oper_reason;
212380e3 106 const char *target_server = NULL;
107 struct ConfItem *aconf;
108 int tkline_time = 0;
109 int loc = 1;
65b8e002 110 int propagated = 1;
212380e3 111
112 if(!IsOperK(source_p))
113 {
d8a4c5f6 114 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "kline");
212380e3 115 return 0;
116 }
117
118 if((tkline_time = valid_temp_time(parv[loc])) >= 0)
119 loc++;
120 /* we just set tkline_time to -1! */
121 else
122 tkline_time = 0;
123
124 if(find_user_host(source_p, parv[loc], user, host) == 0)
125 return 0;
126
127 loc++;
128
d8a4c5f6 129 if(parc >= loc + 2 && !irccmp(parv[loc], "ON"))
212380e3 130 {
131 if(!IsOperRemoteBan(source_p))
132 {
133 sendto_one(source_p, form_str(ERR_NOPRIVS),
d8a4c5f6 134 me.name, source_p->name, "remoteban");
212380e3 135 return 0;
136 }
137
d8a4c5f6 138 target_server = parv[loc + 1];
212380e3 139 loc += 2;
140 }
141
142 if(parc <= loc || EmptyString(parv[loc]))
143 {
144 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
145 me.name, source_p->name, "KLINE");
146 return 0;
147 }
148
149 reason = LOCAL_COPY(parv[loc]);
150
151 if(target_server != NULL)
152 {
153 propagate_generic(source_p, "KLINE", target_server, CAP_KLN,
d8a4c5f6 154 "%d %s %s :%s", tkline_time, user, host, reason);
212380e3 155
156 /* If we are sending it somewhere that doesnt include us, stop */
157 if(!match(target_server, me.name))
158 return 0;
65b8e002
JT
159
160 /* Set as local-only. */
161 propagated = 0;
212380e3 162 }
163 /* if we have cluster servers, send it to them.. */
65b8e002 164 else if(!propagated && rb_dlink_list_length(&cluster_conf_list) > 0)
d8a4c5f6 165 cluster_generic(source_p, "KLINE",
212380e3 166 (tkline_time > 0) ? SHARED_TKLINE : SHARED_PKLINE, CAP_KLN,
d8a4c5f6 167 "%lu %s %s :%s", tkline_time, user, host, reason);
212380e3 168
d8a4c5f6 169 if(!valid_user_host(source_p, user, host) ||
70fd7fc9 170 !valid_comment(source_p, reason))
212380e3 171 return 0;
172
70fd7fc9
JT
173 if(!valid_wild_card(user, host))
174 {
175 sendto_one_notice(source_p,
176 ":Please include at least %d non-wildcard "
177 "characters with the user@host",
178 ConfigFileEntry.min_nonwildcard);
179 return 0;
180 }
181
65b8e002
JT
182 if(propagated && tkline_time == 0)
183 {
184 sendto_one_notice(source_p, ":Cannot set a permanent global ban");
185 return 0;
186 }
187
212380e3 188 if(already_placed_kline(source_p, user, host, tkline_time))
189 return 0;
190
4362b282 191 rb_set_time();
212380e3 192 aconf = make_conf();
193 aconf->status = CONF_KILL;
ce60772d 194 aconf->created = rb_current_time();
62d28946
VY
195 aconf->host = rb_strdup(host);
196 aconf->user = rb_strdup(user);
212380e3 197 aconf->port = 0;
a0f4c418 198 aconf->info.oper = operhash_add(get_oper_name(source_p));
212380e3 199
200 /* Look for an oper reason */
201 if((oper_reason = strchr(reason, '|')) != NULL)
202 {
203 *oper_reason = '\0';
204 oper_reason++;
205
206 if(!EmptyString(oper_reason))
62d28946 207 aconf->spasswd = rb_strdup(oper_reason);
212380e3 208 }
90072e8b 209 aconf->passwd = rb_strdup(reason);
212380e3 210
65b8e002
JT
211 if(propagated)
212 apply_prop_kline(source_p, aconf, reason, oper_reason, tkline_time);
213 else if(tkline_time > 0)
ce60772d 214 apply_tkline(source_p, aconf, reason, oper_reason, tkline_time);
212380e3 215 else
ce60772d 216 apply_kline(source_p, aconf, reason, oper_reason);
212380e3 217
218 if(ConfigFileEntry.kline_delay)
219 {
220 if(kline_queued == 0)
221 {
bfccb2c0 222 rb_event_addonce("check_klines", check_klines_event, NULL,
d8a4c5f6 223 ConfigFileEntry.kline_delay);
212380e3 224 kline_queued = 1;
225 }
226 }
227 else
228 check_klines();
229
230 return 0;
231}
232
233/* ms_kline()
234 *
235 * parv[1] - server targeted at
236 * parv[2] - tkline time (0 if perm)
237 * parv[3] - user
238 * parv[4] - host
239 * parv[5] - reason
240 */
241static int
242ms_kline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
243{
244 int tkline_time = atoi(parv[2]);
245
246 /* 1.5-3 and earlier contains a bug that allows remote klines to be
247 * sent with an empty reason field. This is a protocol violation,
248 * but its not worth dropping the link over.. --anfl
249 */
250 if(parc < 6 || EmptyString(parv[5]))
251 return 0;
252
253 propagate_generic(source_p, "KLINE", parv[1], CAP_KLN,
d8a4c5f6 254 "%d %s %s :%s", tkline_time, parv[3], parv[4], parv[5]);
212380e3 255
256 if(!match(parv[1], me.name))
257 return 0;
258
259 if(!IsPerson(source_p))
260 return 0;
261
262 handle_remote_kline(source_p, tkline_time, parv[3], parv[4], parv[5]);
263 return 0;
264}
265
266static int
267me_kline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
268{
269 /* <tkline_time> <user> <host> :<reason> */
270 if(!IsPerson(source_p))
271 return 0;
272
273 handle_remote_kline(source_p, atoi(parv[1]), parv[2], parv[3], parv[4]);
274 return 0;
275}
276
277static void
278handle_remote_kline(struct Client *source_p, int tkline_time,
d8a4c5f6 279 const char *user, const char *host, const char *kreason)
212380e3 280{
212380e3 281 char *reason = LOCAL_COPY(kreason);
282 struct ConfItem *aconf = NULL;
283 char *oper_reason;
284
285 if(!find_shared_conf(source_p->username, source_p->host,
d8a4c5f6
WP
286 source_p->servptr->name,
287 (tkline_time > 0) ? SHARED_TKLINE : SHARED_PKLINE))
212380e3 288 return;
289
290 if(!valid_user_host(source_p, user, host) ||
70fd7fc9 291 !valid_comment(source_p, reason))
212380e3 292 return;
293
70fd7fc9
JT
294 if(!valid_wild_card(user, host))
295 {
296 sendto_one_notice(source_p,
297 ":Please include at least %d non-wildcard "
298 "characters with the user@host",
299 ConfigFileEntry.min_nonwildcard);
300 return 0;
301 }
302
212380e3 303 if(already_placed_kline(source_p, user, host, tkline_time))
304 return;
305
306 aconf = make_conf();
307
308 aconf->status = CONF_KILL;
ce60772d 309 aconf->created = rb_current_time();
62d28946
VY
310 aconf->user = rb_strdup(user);
311 aconf->host = rb_strdup(host);
a0f4c418 312 aconf->info.oper = operhash_add(get_oper_name(source_p));
212380e3 313
314 /* Look for an oper reason */
315 if((oper_reason = strchr(reason, '|')) != NULL)
316 {
317 *oper_reason = '\0';
318 oper_reason++;
319
320 if(!EmptyString(oper_reason))
62d28946 321 aconf->spasswd = rb_strdup(oper_reason);
212380e3 322 }
90072e8b 323 aconf->passwd = rb_strdup(reason);
212380e3 324
212380e3 325 if(tkline_time > 0)
ce60772d 326 apply_tkline(source_p, aconf, reason, oper_reason, tkline_time);
212380e3 327 else
ce60772d 328 apply_kline(source_p, aconf, reason, oper_reason);
212380e3 329
330 if(ConfigFileEntry.kline_delay)
331 {
332 if(kline_queued == 0)
333 {
bfccb2c0 334 rb_event_addonce("check_klines", check_klines_event, NULL,
d8a4c5f6 335 ConfigFileEntry.kline_delay);
212380e3 336 kline_queued = 1;
337 }
338 }
339 else
340 check_klines();
341
342 return;
343}
344
345/* mo_unkline()
346 *
347 * parv[1] - kline to remove
348 * parv[2] - optional "ON"
349 * parv[3] - optional target server
350 */
351static int
352mo_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
353{
354 const char *user;
355 char *host;
356 char splat[] = "*";
357 char *h = LOCAL_COPY(parv[1]);
40a1d446 358 struct ConfItem *aconf;
65b8e002 359 int propagated = 1;
212380e3 360
361 if(!IsOperUnkline(source_p))
362 {
d8a4c5f6 363 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "unkline");
212380e3 364 return 0;
365 }
366
367 if((host = strchr(h, '@')) || *h == '*' || strchr(h, '.') || strchr(h, ':'))
368 {
369 /* Explicit user@host mask given */
370
371 if(host) /* Found user@host */
372 {
373 *host++ = '\0';
374
375 /* check for @host */
376 if(*h)
377 user = h;
378 else
379 user = splat;
380
381 /* check for user@ */
382 if(!*host)
383 host = splat;
384 }
385 else
386 {
387 user = splat; /* no @ found, assume its *@somehost */
388 host = h;
389 }
390 }
391 else
392 {
5366977b 393 sendto_one_notice(source_p, ":Invalid parameters");
212380e3 394 return 0;
395 }
396
397 /* possible remote kline.. */
398 if((parc > 3) && (irccmp(parv[2], "ON") == 0))
399 {
400 if(!IsOperRemoteBan(source_p))
401 {
402 sendto_one(source_p, form_str(ERR_NOPRIVS),
d8a4c5f6 403 me.name, source_p->name, "remoteban");
212380e3 404 return 0;
405 }
406
d8a4c5f6 407 propagate_generic(source_p, "UNKLINE", parv[3], CAP_UNKLN, "%s %s", user, host);
212380e3 408
409 if(match(parv[3], me.name) == 0)
410 return 0;
65b8e002
JT
411
412 propagated = 0;
212380e3 413 }
65b8e002
JT
414
415 aconf = find_exact_conf_by_address(host, CONF_KILL, user);
416
417 /* No clustering for removing a propagated kline */
418 if(propagated && (aconf == NULL || !aconf->lifetime) &&
419 rb_dlink_list_length(&cluster_conf_list) > 0)
212380e3 420 cluster_generic(source_p, "UNKLINE", SHARED_UNKLINE, CAP_UNKLN,
421 "%s %s", user, host);
422
40a1d446
JT
423 if(aconf == NULL)
424 {
425 sendto_one_notice(source_p, ":No K-Line for %s@%s", user, host);
426 return 0;
427 }
65b8e002
JT
428
429 if(aconf->lifetime)
430 {
431 if(propagated)
432 remove_prop_kline(source_p, aconf);
433 else
434 sendto_one_notice(source_p, ":Cannot remove global K-Line %s@%s on specific servers", user, host);
435 return 0;
436 }
40a1d446 437
4c171a9c 438 if(remove_temp_kline(source_p, aconf))
212380e3 439 return 0;
212380e3 440
40a1d446 441 remove_permkline_match(source_p, aconf);
212380e3 442
443 return 0;
444}
445
446/* ms_unkline()
447 *
448 * parv[1] - target server
449 * parv[2] - user to unkline
450 * parv[3] - host to unkline
451 */
452static int
453ms_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
454{
d8a4c5f6
WP
455 /* parv[0] parv[1] parv[2] parv[3]
456 * oper target server user host */
457 propagate_generic(source_p, "UNKLINE", parv[1], CAP_UNKLN, "%s %s", parv[2], parv[3]);
212380e3 458
459 if(!match(parv[1], me.name))
460 return 0;
461
462 if(!IsPerson(source_p))
463 return 0;
464
465 handle_remote_unkline(source_p, parv[2], parv[3]);
466 return 0;
467}
468
469static int
470me_unkline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
471{
472 /* user host */
473 if(!IsPerson(source_p))
474 return 0;
475
476 handle_remote_unkline(source_p, parv[1], parv[2]);
477 return 0;
478}
479
480static void
481handle_remote_unkline(struct Client *source_p, const char *user, const char *host)
482{
40a1d446
JT
483 struct ConfItem *aconf;
484
212380e3 485 if(!find_shared_conf(source_p->username, source_p->host,
d8a4c5f6 486 source_p->servptr->name, SHARED_UNKLINE))
212380e3 487 return;
488
40a1d446
JT
489 aconf = find_exact_conf_by_address(host, CONF_KILL, user);
490 if(aconf == NULL)
491 {
492 sendto_one_notice(source_p, ":No K-Line for %s@%s", user, host);
493 return;
494 }
65b8e002
JT
495 if(aconf->lifetime)
496 {
497 sendto_one_notice(source_p, ":Cannot remove global K-Line %s@%s on specific servers", user, host);
498 return;
499 }
40a1d446 500
4c171a9c 501 if(remove_temp_kline(source_p, aconf))
212380e3 502 return;
212380e3 503
40a1d446 504 remove_permkline_match(source_p, aconf);
212380e3 505}
506
507/* apply_kline()
508 *
509 * inputs -
510 * output - NONE
511 * side effects - kline as given, is added to the hashtable
512 * and conf file
513 */
514static void
515apply_kline(struct Client *source_p, struct ConfItem *aconf,
ce60772d 516 const char *reason, const char *oper_reason)
212380e3 517{
969a1ae6 518 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf);
d8a4c5f6
WP
519 bandb_add(BANDB_KLINE, source_p, aconf->user, aconf->host,
520 reason, EmptyString(oper_reason) ? NULL : oper_reason, 0);
d63447bf
WP
521
522 /* no oper reason.. */
523 if(EmptyString(oper_reason))
524 {
525 sendto_realops_snomask(SNO_GENERAL, L_ALL,
526 "%s added K-Line for [%s@%s] [%s]",
527 get_oper_name(source_p), aconf->user, aconf->host, reason);
528 ilog(L_KLINE, "K %s 0 %s %s %s",
529 get_oper_name(source_p), aconf->user, aconf->host, reason);
530 }
531 else
532 {
533 sendto_realops_snomask(SNO_GENERAL, L_ALL,
534 "%s added K-Line for [%s@%s] [%s|%s]",
535 get_oper_name(source_p), aconf->user, aconf->host,
536 reason, oper_reason);
537 ilog(L_KLINE, "K %s 0 %s %s %s|%s",
538 get_oper_name(source_p), aconf->user, aconf->host, reason, oper_reason);
539 }
540
541 sendto_one_notice(source_p, ":Added K-Line [%s@%s]",
542 aconf->user, aconf->host);
212380e3 543}
544
545/* apply_tkline()
546 *
547 * inputs -
548 * output - NONE
549 * side effects - tkline as given is placed
550 */
551static void
552apply_tkline(struct Client *source_p, struct ConfItem *aconf,
ce60772d 553 const char *reason, const char *oper_reason, int tkline_time)
212380e3 554{
9f6bbe3c 555 aconf->hold = rb_current_time() + tkline_time;
212380e3 556 add_temp_kline(aconf);
557
558 /* no oper reason.. */
559 if(EmptyString(oper_reason))
560 {
561 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
562 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
563 get_oper_name(source_p), tkline_time / 60,
564 aconf->user, aconf->host, reason);
212380e3 565 ilog(L_KLINE, "K %s %d %s %s %s",
d8a4c5f6 566 get_oper_name(source_p), tkline_time / 60, aconf->user, aconf->host, reason);
212380e3 567 }
568 else
569 {
570 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
571 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
572 get_oper_name(source_p), tkline_time / 60,
573 aconf->user, aconf->host, reason, oper_reason);
212380e3 574 ilog(L_KLINE, "K %s %d %s %s %s|%s",
d8a4c5f6
WP
575 get_oper_name(source_p), tkline_time / 60,
576 aconf->user, aconf->host, reason, oper_reason);
212380e3 577 }
578
579 sendto_one_notice(source_p, ":Added temporary %d min. K-Line [%s@%s]",
580 tkline_time / 60, aconf->user, aconf->host);
581}
582
65b8e002
JT
583static void
584apply_prop_kline(struct Client *source_p, struct ConfItem *aconf,
585 const char *reason, const char *oper_reason, int tkline_time)
586{
587 rb_dlink_node *ptr;
588 struct ConfItem *oldconf;
589
590 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
591 aconf->hold = rb_current_time() + tkline_time;
592 aconf->lifetime = aconf->hold;
593
594 ptr = find_prop_ban(aconf->status, aconf->user, aconf->host);
595 if(ptr != NULL)
596 {
597 oldconf = ptr->data;
598 /* Remember at least as long as the old one. */
599 if(oldconf->lifetime > aconf->lifetime)
600 aconf->lifetime = oldconf->lifetime;
601 /* Force creation time to increase. */
602 if(oldconf->created >= aconf->created)
603 aconf->created = oldconf->created + 1;
e49d8185
JT
604 /* Leave at least one second of validity. */
605 if(aconf->hold <= aconf->created)
606 aconf->hold = aconf->created + 1;
607 if(aconf->lifetime < aconf->hold)
608 aconf->lifetime = aconf->hold;
65b8e002
JT
609 /* Tell deactivate_conf() to destroy it. */
610 oldconf->lifetime = rb_current_time();
611 deactivate_conf(oldconf, ptr);
612 }
613
614 rb_dlinkAddAlloc(aconf, &prop_bans);
615 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf);
616
617 /* no oper reason.. */
618 if(EmptyString(oper_reason))
619 {
620 sendto_realops_snomask(SNO_GENERAL, L_ALL,
621 "%s added global %d min. K-Line for [%s@%s] [%s]",
622 get_oper_name(source_p), tkline_time / 60,
623 aconf->user, aconf->host, reason);
624 ilog(L_KLINE, "K %s %d %s %s %s",
625 get_oper_name(source_p), tkline_time / 60, aconf->user, aconf->host, reason);
626 }
627 else
628 {
629 sendto_realops_snomask(SNO_GENERAL, L_ALL,
630 "%s added global %d min. K-Line for [%s@%s] [%s|%s]",
631 get_oper_name(source_p), tkline_time / 60,
632 aconf->user, aconf->host, reason, oper_reason);
633 ilog(L_KLINE, "K %s %d %s %s %s|%s",
634 get_oper_name(source_p), tkline_time / 60,
635 aconf->user, aconf->host, reason, oper_reason);
636 }
637
638 sendto_one_notice(source_p, ":Added global %d min. K-Line [%s@%s]",
639 tkline_time / 60, aconf->user, aconf->host);
640
641 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
e49d8185 642 ":%s BAN K %s %s %lu %d %d * :%s%s%s",
65b8e002
JT
643 source_p->id, aconf->user, aconf->host,
644 (unsigned long)aconf->created,
645 (int)(aconf->hold - aconf->created),
646 (int)(aconf->lifetime - aconf->created),
647 reason,
648 oper_reason ? "|" : "",
649 oper_reason ? oper_reason : "");
650}
651
212380e3 652/* find_user_host()
653 *
654 * inputs - client placing kline, user@host, user buffer, host buffer
655 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
656 * side effects -
657 */
658static int
659find_user_host(struct Client *source_p, const char *userhost, char *luser, char *lhost)
660{
661 char *hostp;
662
663 hostp = strchr(userhost, '@');
d8a4c5f6 664
212380e3 665 if(hostp != NULL) /* I'm a little user@host */
666 {
667 *(hostp++) = '\0'; /* short and squat */
668 if(*userhost)
907468c4 669 rb_strlcpy(luser, userhost, USERLEN + 1); /* here is my user */
212380e3 670 else
671 strcpy(luser, "*");
672 if(*hostp)
907468c4 673 rb_strlcpy(lhost, hostp, HOSTLEN + 1); /* here is my host */
212380e3 674 else
675 strcpy(lhost, "*");
d8a4c5f6 676 }
212380e3 677 else
678 {
679 /* no '@', no '.', so its not a user@host or host, therefore
680 * its a nick, which support was removed for.
681 */
682 if(strchr(userhost, '.') == NULL && strchr(userhost, ':') == NULL)
1dc9ac1d
JT
683 {
684 sendto_one_notice(source_p, ":K-Line must be a user@host or host");
212380e3 685 return 0;
1dc9ac1d 686 }
212380e3 687
688 luser[0] = '*'; /* no @ found, assume its *@somehost */
689 luser[1] = '\0';
907468c4 690 rb_strlcpy(lhost, userhost, HOSTLEN + 1);
212380e3 691 }
692
693 return 1;
694}
695
696/* valid_user_host()
697 *
698 * inputs - user buffer, host buffer
699 * output - 0 if invalid, 1 if valid
700 * side effects -
701 */
702static int
703valid_user_host(struct Client *source_p, const char *luser, const char *lhost)
704{
d8a4c5f6
WP
705 /* # is invalid, as are '!' (n!u@h kline) and '@' (u@@h kline) */
706 if(strchr(lhost, '#') || strchr(luser, '#') || strchr(luser, '!') || strchr(lhost, '@'))
212380e3 707 {
708 sendto_one_notice(source_p, ":Invalid K-Line");
709 return 0;
710 }
711
712 return 1;
713}
714
212380e3 715/*
716 * valid_comment
717 * inputs - pointer to client
718 * - pointer to comment
719 * output - 0 if no valid comment, 1 if valid
720 * side effects - NONE
721 */
722static int
723valid_comment(struct Client *source_p, char *comment)
724{
725 if(strchr(comment, '"'))
726 {
727 sendto_one_notice(source_p, ":Invalid character '\"' in comment");
728 return 0;
729 }
730
61569b65 731 if(strlen(comment) > BANREASONLEN)
732 comment[BANREASONLEN] = '\0';
212380e3 733
734 return 1;
735}
736
737/* already_placed_kline()
738 *
739 * inputs - source to notify, user@host to check, tkline time
740 * outputs - 1 if a perm kline or a tkline when a tkline is being
741 * set exists, else 0
742 * side effects - notifies source_p kline exists
743 */
744/* Note: This currently works if the new K-line is a special case of an
745 * existing K-line, but not the other way round. To do that we would
746 * have to walk the hash and check every existing K-line. -A1kmm.
747 */
748static int
749already_placed_kline(struct Client *source_p, const char *luser, const char *lhost, int tkline)
750{
df7a3250 751 const char *reason, *p;
3ea5fee7 752 struct rb_sockaddr_storage iphost, *piphost;
212380e3 753 struct ConfItem *aconf;
d8a4c5f6 754 int t, bits;
df7a3250
JT
755
756 aconf = find_exact_conf_by_address(lhost, CONF_KILL, luser);
d8a4c5f6 757 if(aconf == NULL && ConfigFileEntry.non_redundant_klines)
212380e3 758 {
df7a3250 759 bits = 0;
d8a4c5f6 760 if((t = parse_netmask(lhost, (struct sockaddr *) &iphost, &bits)) != HM_HOST)
212380e3 761 {
2c2e0aa9 762#ifdef RB_IPV6
212380e3 763 if(t == HM_IPV6)
764 t = AF_INET6;
765 else
766#endif
767 t = AF_INET;
d8a4c5f6 768
212380e3 769 piphost = &iphost;
770 }
771 else
772 piphost = NULL;
773
d8a4c5f6
WP
774 aconf = find_conf_by_address(lhost, NULL, NULL, (struct sockaddr *) piphost,
775 CONF_KILL, t, luser, NULL);
776 if(aconf != NULL)
212380e3 777 {
df7a3250
JT
778 /* The above was really a lookup of a single IP,
779 * so check if the new kline is wider than the
780 * existing one.
781 * -- jilles
782 */
783 p = strchr(aconf->host, '/');
d8a4c5f6 784 if(bits > 0 && (p == NULL || bits < atoi(p + 1)))
df7a3250
JT
785 aconf = NULL;
786 }
787 }
d8a4c5f6 788 if(aconf != NULL)
df7a3250
JT
789 {
790 /* setting a tkline, or existing one is perm */
791 if(tkline || ((aconf->flags & CONF_FLAGS_TEMPORARY) == 0))
792 {
793 reason = aconf->passwd ? aconf->passwd : "<No Reason>";
794
795 sendto_one_notice(source_p,
796 ":[%s@%s] already K-Lined by [%s@%s] - %s",
d8a4c5f6 797 luser, lhost, aconf->user, aconf->host, reason);
df7a3250 798 return 1;
212380e3 799 }
800 }
801
802 return 0;
803}
804
805/* remove_permkline_match()
806 *
807 * hunts for a permanent kline, and removes it.
808 */
809static void
40a1d446 810remove_permkline_match(struct Client *source_p, struct ConfItem *aconf)
212380e3 811{
d8a4c5f6 812 sendto_one_notice(source_p, ":K-Line for [%s@%s] is removed", aconf->user, aconf->host);
212380e3 813
814 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
815 "%s has removed the K-Line for: [%s@%s]",
816 get_oper_name(source_p), aconf->user, aconf->host);
212380e3 817
d8a4c5f6 818 ilog(L_KLINE, "UK %s %s %s", get_oper_name(source_p), aconf->user, aconf->host);
40a1d446 819
21c9d815 820 remove_reject_mask(aconf->user, aconf->host);
d8a4c5f6 821 bandb_del(BANDB_KLINE, aconf->user, aconf->host);
40a1d446
JT
822 delete_one_address_conf(aconf->host, aconf);
823
212380e3 824 return;
825}
826
212380e3 827/* remove_temp_kline()
828 *
829 * inputs - username, hostname to unkline
830 * outputs -
831 * side effects - tries to unkline anything that matches
832 */
833static int
4c171a9c 834remove_temp_kline(struct Client *source_p, struct ConfItem *aconf)
212380e3 835{
08d11e34 836 rb_dlink_node *ptr;
212380e3 837 int i;
838
d8a4c5f6 839 for(i = 0; i < LAST_TEMP_TYPE; i++)
212380e3 840 {
08d11e34 841 RB_DLINK_FOREACH(ptr, temp_klines[i].head)
212380e3 842 {
d8a4c5f6 843 if(aconf == ptr->data)
212380e3 844 {
4c171a9c 845 sendto_one_notice(source_p,
d8a4c5f6
WP
846 ":Un-klined [%s@%s] from temporary k-lines",
847 aconf->user, aconf->host);
4c171a9c 848 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
849 "%s has removed the temporary K-Line for: [%s@%s]",
850 get_oper_name(source_p), aconf->user,
851 aconf->host);
4c171a9c
JT
852
853 ilog(L_KLINE, "UK %s %s %s",
d8a4c5f6 854 get_oper_name(source_p), aconf->user, aconf->host);
9f6c3353 855 rb_dlinkDestroy(ptr, &temp_klines[i]);
21c9d815 856 remove_reject_mask(aconf->user, aconf->host);
40a1d446
JT
857 delete_one_address_conf(aconf->host, aconf);
858 return YES;
212380e3 859 }
212380e3 860 }
861 }
862
863 return NO;
864}
65b8e002
JT
865
866static void
867remove_prop_kline(struct Client *source_p, struct ConfItem *aconf)
868{
869 rb_dlink_node *ptr;
870
871 ptr = rb_dlinkFind(aconf, &prop_bans);
872 if (!ptr)
873 return;
874 sendto_one_notice(source_p,
875 ":Un-klined [%s@%s] from global k-lines",
876 aconf->user, aconf->host);
877 sendto_realops_snomask(SNO_GENERAL, L_ALL,
878 "%s has removed the global K-Line for: [%s@%s]",
879 get_oper_name(source_p), aconf->user,
880 aconf->host);
881
882 ilog(L_KLINE, "UK %s %s %s",
883 get_oper_name(source_p), aconf->user, aconf->host);
884 if(aconf->created < rb_current_time())
885 aconf->created = rb_current_time();
886 else
887 aconf->created++;
e49d8185 888 aconf->hold = aconf->created;
65b8e002
JT
889 operhash_delete(aconf->info.oper);
890 aconf->info.oper = operhash_add(get_oper_name(source_p));
891 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
892 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
e49d8185 893 ":%s BAN K %s %s %lu %d %d * :*",
65b8e002
JT
894 source_p->id, aconf->user, aconf->host,
895 (unsigned long)aconf->created,
896 0,
897 (int)(aconf->lifetime - aconf->created));
898 remove_reject_mask(aconf->user, aconf->host);
899 deactivate_conf(aconf, ptr);
900}