2 * ircd-ratbox: A slightly useful ircd.
3 * m_kline.c: Bans/unbans a user.
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
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.
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.
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
37 #include "s_newconf.h"
48 static int mo_kline(struct Client
*, struct Client
*, int, const char **);
49 static int ms_kline(struct Client
*, struct Client
*, int, const char **);
50 static int me_kline(struct Client
*, struct Client
*, int, const char **);
51 static int mo_unkline(struct Client
*, struct Client
*, int, const char **);
52 static int ms_unkline(struct Client
*, struct Client
*, int, const char **);
53 static int me_unkline(struct Client
*, struct Client
*, int, const char **);
55 struct Message kline_msgtab
= {
56 "KLINE", 0, 0, 0, MFLG_SLOW
,
57 {mg_unreg
, mg_not_oper
, {ms_kline
, 5}, {ms_kline
, 5}, {me_kline
, 5}, {mo_kline
, 3}}
60 struct Message unkline_msgtab
= {
61 "UNKLINE", 0, 0, 0, MFLG_SLOW
,
62 {mg_unreg
, mg_not_oper
, {ms_unkline
, 4}, {ms_unkline
, 4}, {me_unkline
, 3}, {mo_unkline
, 2}}
65 mapi_clist_av1 kline_clist
[] = { &kline_msgtab
, &unkline_msgtab
, NULL
};
67 DECLARE_MODULE_AV1(kline
, NULL
, NULL
, kline_clist
, NULL
, NULL
, "$Revision$");
69 /* Local function prototypes */
70 static int find_user_host(struct Client
*source_p
, const char *userhost
, char *user
, char *host
);
71 static int valid_comment(struct Client
*source_p
, char *comment
);
72 static int valid_user_host(struct Client
*source_p
, const char *user
, const char *host
);
73 static int valid_wild_card(struct Client
*source_p
, const char *user
, const char *host
);
75 static void handle_remote_kline(struct Client
*source_p
, int tkline_time
,
76 const char *user
, const char *host
, const char *reason
);
77 static void apply_kline(struct Client
*source_p
, struct ConfItem
*aconf
,
78 const char *reason
, const char *oper_reason
);
79 static void apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
80 const char *, const char *, int);
81 static int already_placed_kline(struct Client
*, const char *, const char *, int);
83 static void handle_remote_unkline(struct Client
*source_p
, const char *user
, const char *host
);
84 static void remove_permkline_match(struct Client
*, struct ConfItem
*);
85 static int remove_temp_kline(struct Client
*, struct ConfItem
*);
89 * parv[1] - temp time or user@host
90 * parv[2] - user@host, "ON", or reason
91 * parv[3] - "ON", reason, or server to target
92 * parv[4] - server to target, or reason
96 mo_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
98 char def
[] = "No Reason";
99 char user
[USERLEN
+ 2];
100 char host
[HOSTLEN
+ 2];
103 const char *target_server
= NULL
;
104 struct ConfItem
*aconf
;
108 if(!IsOperK(source_p
))
110 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "kline");
114 if((tkline_time
= valid_temp_time(parv
[loc
])) >= 0)
116 /* we just set tkline_time to -1! */
120 if(find_user_host(source_p
, parv
[loc
], user
, host
) == 0)
125 if(parc
>= loc
+ 2 && !irccmp(parv
[loc
], "ON"))
127 if(!IsOperRemoteBan(source_p
))
129 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
130 me
.name
, source_p
->name
, "remoteban");
134 target_server
= parv
[loc
+ 1];
138 if(parc
<= loc
|| EmptyString(parv
[loc
]))
140 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
141 me
.name
, source_p
->name
, "KLINE");
145 reason
= LOCAL_COPY(parv
[loc
]);
147 if(target_server
!= NULL
)
149 propagate_generic(source_p
, "KLINE", target_server
, CAP_KLN
,
150 "%d %s %s :%s", tkline_time
, user
, host
, reason
);
152 /* If we are sending it somewhere that doesnt include us, stop */
153 if(!match(target_server
, me
.name
))
156 /* if we have cluster servers, send it to them.. */
157 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
158 cluster_generic(source_p
, "KLINE",
159 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
, CAP_KLN
,
160 "%lu %s %s :%s", tkline_time
, user
, host
, reason
);
162 if(!valid_user_host(source_p
, user
, host
) ||
163 !valid_wild_card(source_p
, user
, host
) || !valid_comment(source_p
, reason
))
166 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
171 aconf
->status
= CONF_KILL
;
172 aconf
->created
= rb_current_time();
173 aconf
->host
= rb_strdup(host
);
174 aconf
->user
= rb_strdup(user
);
176 aconf
->passwd
= rb_strdup(reason
);
178 /* Look for an oper reason */
179 if((oper_reason
= strchr(reason
, '|')) != NULL
)
184 if(!EmptyString(oper_reason
))
185 aconf
->spasswd
= rb_strdup(oper_reason
);
189 apply_tkline(source_p
, aconf
, reason
, oper_reason
, tkline_time
);
191 apply_kline(source_p
, aconf
, reason
, oper_reason
);
193 if(ConfigFileEntry
.kline_delay
)
195 if(kline_queued
== 0)
197 rb_event_addonce("check_klines", check_klines_event
, NULL
,
198 ConfigFileEntry
.kline_delay
);
210 * parv[1] - server targeted at
211 * parv[2] - tkline time (0 if perm)
217 ms_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
219 int tkline_time
= atoi(parv
[2]);
221 /* 1.5-3 and earlier contains a bug that allows remote klines to be
222 * sent with an empty reason field. This is a protocol violation,
223 * but its not worth dropping the link over.. --anfl
225 if(parc
< 6 || EmptyString(parv
[5]))
228 propagate_generic(source_p
, "KLINE", parv
[1], CAP_KLN
,
229 "%d %s %s :%s", tkline_time
, parv
[3], parv
[4], parv
[5]);
231 if(!match(parv
[1], me
.name
))
234 if(!IsPerson(source_p
))
237 handle_remote_kline(source_p
, tkline_time
, parv
[3], parv
[4], parv
[5]);
242 me_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
244 /* <tkline_time> <user> <host> :<reason> */
245 if(!IsPerson(source_p
))
248 handle_remote_kline(source_p
, atoi(parv
[1]), parv
[2], parv
[3], parv
[4]);
253 handle_remote_kline(struct Client
*source_p
, int tkline_time
,
254 const char *user
, const char *host
, const char *kreason
)
256 char *reason
= LOCAL_COPY(kreason
);
257 struct ConfItem
*aconf
= NULL
;
260 if(!find_shared_conf(source_p
->username
, source_p
->host
,
261 source_p
->servptr
->name
,
262 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
))
265 if(!valid_user_host(source_p
, user
, host
) ||
266 !valid_wild_card(source_p
, user
, host
) || !valid_comment(source_p
, reason
))
269 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
274 aconf
->status
= CONF_KILL
;
275 aconf
->created
= rb_current_time();
276 aconf
->user
= rb_strdup(user
);
277 aconf
->host
= rb_strdup(host
);
278 aconf
->passwd
= rb_strdup(reason
);
280 /* Look for an oper reason */
281 if((oper_reason
= strchr(reason
, '|')) != NULL
)
286 if(!EmptyString(oper_reason
))
287 aconf
->spasswd
= rb_strdup(oper_reason
);
291 apply_tkline(source_p
, aconf
, reason
, oper_reason
, tkline_time
);
293 apply_kline(source_p
, aconf
, reason
, oper_reason
);
295 if(ConfigFileEntry
.kline_delay
)
297 if(kline_queued
== 0)
299 rb_event_addonce("check_klines", check_klines_event
, NULL
,
300 ConfigFileEntry
.kline_delay
);
312 * parv[1] - kline to remove
313 * parv[2] - optional "ON"
314 * parv[3] - optional target server
317 mo_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
322 char *h
= LOCAL_COPY(parv
[1]);
323 struct ConfItem
*aconf
;
325 if(!IsOperUnkline(source_p
))
327 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "unkline");
331 if((host
= strchr(h
, '@')) || *h
== '*' || strchr(h
, '.') || strchr(h
, ':'))
333 /* Explicit user@host mask given */
335 if(host
) /* Found user@host */
339 /* check for @host */
345 /* check for user@ */
351 user
= splat
; /* no @ found, assume its *@somehost */
357 sendto_one_notice(source_p
, ":Invalid parameters");
361 /* possible remote kline.. */
362 if((parc
> 3) && (irccmp(parv
[2], "ON") == 0))
364 if(!IsOperRemoteBan(source_p
))
366 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
367 me
.name
, source_p
->name
, "remoteban");
371 propagate_generic(source_p
, "UNKLINE", parv
[3], CAP_UNKLN
, "%s %s", user
, host
);
373 if(match(parv
[3], me
.name
) == 0)
376 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
377 cluster_generic(source_p
, "UNKLINE", SHARED_UNKLINE
, CAP_UNKLN
,
378 "%s %s", user
, host
);
380 aconf
= find_exact_conf_by_address(host
, CONF_KILL
, user
);
383 sendto_one_notice(source_p
, ":No K-Line for %s@%s", user
, host
);
387 if(remove_temp_kline(source_p
, aconf
))
390 remove_permkline_match(source_p
, aconf
);
397 * parv[1] - target server
398 * parv[2] - user to unkline
399 * parv[3] - host to unkline
402 ms_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
404 /* parv[0] parv[1] parv[2] parv[3]
405 * oper target server user host */
406 propagate_generic(source_p
, "UNKLINE", parv
[1], CAP_UNKLN
, "%s %s", parv
[2], parv
[3]);
408 if(!match(parv
[1], me
.name
))
411 if(!IsPerson(source_p
))
414 handle_remote_unkline(source_p
, parv
[2], parv
[3]);
419 me_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
422 if(!IsPerson(source_p
))
425 handle_remote_unkline(source_p
, parv
[1], parv
[2]);
430 handle_remote_unkline(struct Client
*source_p
, const char *user
, const char *host
)
432 struct ConfItem
*aconf
;
434 if(!find_shared_conf(source_p
->username
, source_p
->host
,
435 source_p
->servptr
->name
, SHARED_UNKLINE
))
438 aconf
= find_exact_conf_by_address(host
, CONF_KILL
, user
);
441 sendto_one_notice(source_p
, ":No K-Line for %s@%s", user
, host
);
445 if(remove_temp_kline(source_p
, aconf
))
448 remove_permkline_match(source_p
, aconf
);
455 * side effects - kline as given, is added to the hashtable
459 apply_kline(struct Client
*source_p
, struct ConfItem
*aconf
,
460 const char *reason
, const char *oper_reason
)
462 add_conf_by_address(aconf
->host
, CONF_KILL
, aconf
->user
, NULL
, aconf
);
463 bandb_add(BANDB_KLINE
, source_p
, aconf
->user
, aconf
->host
,
464 reason
, EmptyString(oper_reason
) ? NULL
: oper_reason
, 0);
466 /* no oper reason.. */
467 if(EmptyString(oper_reason
))
469 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
470 "%s added K-Line for [%s@%s] [%s]",
471 get_oper_name(source_p
), aconf
->user
, aconf
->host
, reason
);
472 ilog(L_KLINE
, "K %s 0 %s %s %s",
473 get_oper_name(source_p
), aconf
->user
, aconf
->host
, reason
);
477 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
478 "%s added K-Line for [%s@%s] [%s|%s]",
479 get_oper_name(source_p
), aconf
->user
, aconf
->host
,
480 reason
, oper_reason
);
481 ilog(L_KLINE
, "K %s 0 %s %s %s|%s",
482 get_oper_name(source_p
), aconf
->user
, aconf
->host
, reason
, oper_reason
);
485 sendto_one_notice(source_p
, ":Added K-Line [%s@%s]",
486 aconf
->user
, aconf
->host
);
493 * side effects - tkline as given is placed
496 apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
497 const char *reason
, const char *oper_reason
, int tkline_time
)
499 aconf
->hold
= rb_current_time() + tkline_time
;
500 add_temp_kline(aconf
);
502 /* no oper reason.. */
503 if(EmptyString(oper_reason
))
505 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
506 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
507 get_oper_name(source_p
), tkline_time
/ 60,
508 aconf
->user
, aconf
->host
, reason
);
509 ilog(L_KLINE
, "K %s %d %s %s %s",
510 get_oper_name(source_p
), tkline_time
/ 60, aconf
->user
, aconf
->host
, reason
);
514 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
515 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
516 get_oper_name(source_p
), tkline_time
/ 60,
517 aconf
->user
, aconf
->host
, reason
, oper_reason
);
518 ilog(L_KLINE
, "K %s %d %s %s %s|%s",
519 get_oper_name(source_p
), tkline_time
/ 60,
520 aconf
->user
, aconf
->host
, reason
, oper_reason
);
523 sendto_one_notice(source_p
, ":Added temporary %d min. K-Line [%s@%s]",
524 tkline_time
/ 60, aconf
->user
, aconf
->host
);
529 * inputs - client placing kline, user@host, user buffer, host buffer
530 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
534 find_user_host(struct Client
*source_p
, const char *userhost
, char *luser
, char *lhost
)
538 hostp
= strchr(userhost
, '@');
540 if(hostp
!= NULL
) /* I'm a little user@host */
542 *(hostp
++) = '\0'; /* short and squat */
544 rb_strlcpy(luser
, userhost
, USERLEN
+ 1); /* here is my user */
548 rb_strlcpy(lhost
, hostp
, HOSTLEN
+ 1); /* here is my host */
554 /* no '@', no '.', so its not a user@host or host, therefore
555 * its a nick, which support was removed for.
557 if(strchr(userhost
, '.') == NULL
&& strchr(userhost
, ':') == NULL
)
559 sendto_one_notice(source_p
, ":K-Line must be a user@host or host");
563 luser
[0] = '*'; /* no @ found, assume its *@somehost */
565 rb_strlcpy(lhost
, userhost
, HOSTLEN
+ 1);
573 * inputs - user buffer, host buffer
574 * output - 0 if invalid, 1 if valid
578 valid_user_host(struct Client
*source_p
, const char *luser
, const char *lhost
)
580 /* # is invalid, as are '!' (n!u@h kline) and '@' (u@@h kline) */
581 if(strchr(lhost
, '#') || strchr(luser
, '#') || strchr(luser
, '!') || strchr(lhost
, '@'))
583 sendto_one_notice(source_p
, ":Invalid K-Line");
592 * input - user buffer, host buffer
593 * output - 0 if invalid, 1 if valid
597 valid_wild_card(struct Client
*source_p
, const char *luser
, const char *lhost
)
604 /* user has no wildcards, always accept -- jilles */
605 if(!strchr(luser
, '?') && !strchr(luser
, '*'))
608 /* check there are enough non wildcard chars */
610 while((tmpch
= *p
++))
612 if(!IsKWildChar(tmpch
))
614 /* found enough chars, return */
615 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
620 /* try host, as user didnt contain enough */
621 /* special case for cidr masks -- jilles */
622 if((p
= strrchr(lhost
, '/')) != NULL
&& IsDigit(p
[1]))
624 bitlen
= atoi(p
+ 1);
625 /* much like non-cidr for ipv6, rather arbitrary for ipv4 */
628 (strchr(lhost
, ':') ? 4 * (ConfigFileEntry
.min_nonwildcard
- nonwild
) : 6 -
635 while((tmpch
= *p
++))
637 if(!IsKWildChar(tmpch
))
638 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
643 sendto_one_notice(source_p
,
644 ":Please include at least %d non-wildcard "
645 "characters with the user@host", ConfigFileEntry
.min_nonwildcard
);
651 * inputs - pointer to client
652 * - pointer to comment
653 * output - 0 if no valid comment, 1 if valid
654 * side effects - NONE
657 valid_comment(struct Client
*source_p
, char *comment
)
659 if(strchr(comment
, '"'))
661 sendto_one_notice(source_p
, ":Invalid character '\"' in comment");
665 if(strlen(comment
) > BANREASONLEN
)
666 comment
[BANREASONLEN
] = '\0';
671 /* already_placed_kline()
673 * inputs - source to notify, user@host to check, tkline time
674 * outputs - 1 if a perm kline or a tkline when a tkline is being
676 * side effects - notifies source_p kline exists
678 /* Note: This currently works if the new K-line is a special case of an
679 * existing K-line, but not the other way round. To do that we would
680 * have to walk the hash and check every existing K-line. -A1kmm.
683 already_placed_kline(struct Client
*source_p
, const char *luser
, const char *lhost
, int tkline
)
685 const char *reason
, *p
;
686 struct rb_sockaddr_storage iphost
, *piphost
;
687 struct ConfItem
*aconf
;
690 aconf
= find_exact_conf_by_address(lhost
, CONF_KILL
, luser
);
691 if(aconf
== NULL
&& ConfigFileEntry
.non_redundant_klines
)
694 if((t
= parse_netmask(lhost
, (struct sockaddr
*) &iphost
, &bits
)) != HM_HOST
)
708 aconf
= find_conf_by_address(lhost
, NULL
, NULL
, (struct sockaddr
*) piphost
,
709 CONF_KILL
, t
, luser
, NULL
);
712 /* The above was really a lookup of a single IP,
713 * so check if the new kline is wider than the
717 p
= strchr(aconf
->host
, '/');
718 if(bits
> 0 && (p
== NULL
|| bits
< atoi(p
+ 1)))
724 /* setting a tkline, or existing one is perm */
725 if(tkline
|| ((aconf
->flags
& CONF_FLAGS_TEMPORARY
) == 0))
727 reason
= aconf
->passwd
? aconf
->passwd
: "<No Reason>";
729 sendto_one_notice(source_p
,
730 ":[%s@%s] already K-Lined by [%s@%s] - %s",
731 luser
, lhost
, aconf
->user
, aconf
->host
, reason
);
739 /* remove_permkline_match()
741 * hunts for a permanent kline, and removes it.
744 remove_permkline_match(struct Client
*source_p
, struct ConfItem
*aconf
)
746 sendto_one_notice(source_p
, ":K-Line for [%s@%s] is removed", aconf
->user
, aconf
->host
);
748 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
749 "%s has removed the K-Line for: [%s@%s]",
750 get_oper_name(source_p
), aconf
->user
, aconf
->host
);
752 ilog(L_KLINE
, "UK %s %s %s", get_oper_name(source_p
), aconf
->user
, aconf
->host
);
754 remove_reject_mask(aconf
->user
, aconf
->host
);
755 bandb_del(BANDB_KLINE
, aconf
->user
, aconf
->host
);
756 delete_one_address_conf(aconf
->host
, aconf
);
761 /* remove_temp_kline()
763 * inputs - username, hostname to unkline
765 * side effects - tries to unkline anything that matches
768 remove_temp_kline(struct Client
*source_p
, struct ConfItem
*aconf
)
773 for(i
= 0; i
< LAST_TEMP_TYPE
; i
++)
775 RB_DLINK_FOREACH(ptr
, temp_klines
[i
].head
)
777 if(aconf
== ptr
->data
)
779 sendto_one_notice(source_p
,
780 ":Un-klined [%s@%s] from temporary k-lines",
781 aconf
->user
, aconf
->host
);
782 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
783 "%s has removed the temporary K-Line for: [%s@%s]",
784 get_oper_name(source_p
), aconf
->user
,
787 ilog(L_KLINE
, "UK %s %s %s",
788 get_oper_name(source_p
), aconf
->user
, aconf
->host
);
789 rb_dlinkDestroy(ptr
, &temp_klines
[i
]);
790 remove_reject_mask(aconf
->user
, aconf
->host
);
791 delete_one_address_conf(aconf
->host
, aconf
);