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