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