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