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"
49 static int mo_kline(struct Client
*, struct Client
*, int, const char **);
50 static int ms_kline(struct Client
*, struct Client
*, int, const char **);
51 static int me_kline(struct Client
*, struct Client
*, int, const char **);
52 static int mo_unkline(struct Client
*, struct Client
*, int, const char **);
53 static int ms_unkline(struct Client
*, struct Client
*, int, const char **);
54 static int me_unkline(struct Client
*, struct Client
*, int, const char **);
56 struct Message kline_msgtab
= {
57 "KLINE", 0, 0, 0, MFLG_SLOW
,
58 {mg_unreg
, mg_not_oper
, {ms_kline
, 5}, {ms_kline
, 5}, {me_kline
, 5}, {mo_kline
, 3}}
61 struct Message unkline_msgtab
= {
62 "UNKLINE", 0, 0, 0, MFLG_SLOW
,
63 {mg_unreg
, mg_not_oper
, {ms_unkline
, 4}, {ms_unkline
, 4}, {me_unkline
, 3}, {mo_unkline
, 2}}
66 mapi_clist_av1 kline_clist
[] = { &kline_msgtab
, &unkline_msgtab
, NULL
};
68 DECLARE_MODULE_AV1(kline
, NULL
, NULL
, kline_clist
, NULL
, NULL
, "$Revision$");
70 /* Local function prototypes */
71 static int find_user_host(struct Client
*source_p
, const char *userhost
, char *user
, char *host
);
72 static int valid_comment(struct Client
*source_p
, char *comment
);
73 static int valid_user_host(struct Client
*source_p
, const char *user
, const char *host
);
74 static int valid_wild_card(struct Client
*source_p
, const char *user
, const char *host
);
76 static void handle_remote_kline(struct Client
*source_p
, int tkline_time
,
77 const char *user
, const char *host
, const char *reason
);
78 static void apply_kline(struct Client
*source_p
, struct ConfItem
*aconf
,
79 const char *reason
, const char *oper_reason
);
80 static void apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
81 const char *, const char *, int);
82 static int already_placed_kline(struct Client
*, const char *, const char *, int);
84 static void handle_remote_unkline(struct Client
*source_p
, const char *user
, const char *host
);
85 static void remove_permkline_match(struct Client
*, struct ConfItem
*);
86 static int remove_temp_kline(struct Client
*, struct ConfItem
*);
90 * parv[1] - temp time or user@host
91 * parv[2] - user@host, "ON", or reason
92 * parv[3] - "ON", reason, or server to target
93 * parv[4] - server to target, or reason
97 mo_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
99 char def
[] = "No Reason";
100 char user
[USERLEN
+ 2];
101 char host
[HOSTLEN
+ 2];
104 const char *target_server
= NULL
;
105 struct ConfItem
*aconf
;
109 if(!IsOperK(source_p
))
111 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "kline");
115 if((tkline_time
= valid_temp_time(parv
[loc
])) >= 0)
117 /* we just set tkline_time to -1! */
121 if(find_user_host(source_p
, parv
[loc
], user
, host
) == 0)
126 if(parc
>= loc
+ 2 && !irccmp(parv
[loc
], "ON"))
128 if(!IsOperRemoteBan(source_p
))
130 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
131 me
.name
, source_p
->name
, "remoteban");
135 target_server
= parv
[loc
+ 1];
139 if(parc
<= loc
|| EmptyString(parv
[loc
]))
141 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
142 me
.name
, source_p
->name
, "KLINE");
146 reason
= LOCAL_COPY(parv
[loc
]);
148 if(target_server
!= NULL
)
150 propagate_generic(source_p
, "KLINE", target_server
, CAP_KLN
,
151 "%d %s %s :%s", tkline_time
, user
, host
, reason
);
153 /* If we are sending it somewhere that doesnt include us, stop */
154 if(!match(target_server
, me
.name
))
157 /* if we have cluster servers, send it to them.. */
158 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
159 cluster_generic(source_p
, "KLINE",
160 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
, CAP_KLN
,
161 "%lu %s %s :%s", tkline_time
, user
, host
, reason
);
163 if(!valid_user_host(source_p
, user
, host
) ||
164 !valid_wild_card(source_p
, user
, host
) || !valid_comment(source_p
, reason
))
167 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
172 aconf
->status
= CONF_KILL
;
173 aconf
->created
= rb_current_time();
174 aconf
->host
= rb_strdup(host
);
175 aconf
->user
= rb_strdup(user
);
177 aconf
->passwd
= rb_strdup(reason
);
178 aconf
->info
.oper
= operhash_add(get_oper_name(source_p
));
180 /* Look for an oper reason */
181 if((oper_reason
= strchr(reason
, '|')) != NULL
)
186 if(!EmptyString(oper_reason
))
187 aconf
->spasswd
= rb_strdup(oper_reason
);
191 apply_tkline(source_p
, aconf
, reason
, oper_reason
, tkline_time
);
193 apply_kline(source_p
, aconf
, reason
, oper_reason
);
195 if(ConfigFileEntry
.kline_delay
)
197 if(kline_queued
== 0)
199 rb_event_addonce("check_klines", check_klines_event
, NULL
,
200 ConfigFileEntry
.kline_delay
);
212 * parv[1] - server targeted at
213 * parv[2] - tkline time (0 if perm)
219 ms_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
221 int tkline_time
= atoi(parv
[2]);
223 /* 1.5-3 and earlier contains a bug that allows remote klines to be
224 * sent with an empty reason field. This is a protocol violation,
225 * but its not worth dropping the link over.. --anfl
227 if(parc
< 6 || EmptyString(parv
[5]))
230 propagate_generic(source_p
, "KLINE", parv
[1], CAP_KLN
,
231 "%d %s %s :%s", tkline_time
, parv
[3], parv
[4], parv
[5]);
233 if(!match(parv
[1], me
.name
))
236 if(!IsPerson(source_p
))
239 handle_remote_kline(source_p
, tkline_time
, parv
[3], parv
[4], parv
[5]);
244 me_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
246 /* <tkline_time> <user> <host> :<reason> */
247 if(!IsPerson(source_p
))
250 handle_remote_kline(source_p
, atoi(parv
[1]), parv
[2], parv
[3], parv
[4]);
255 handle_remote_kline(struct Client
*source_p
, int tkline_time
,
256 const char *user
, const char *host
, const char *kreason
)
258 char *reason
= LOCAL_COPY(kreason
);
259 struct ConfItem
*aconf
= NULL
;
262 if(!find_shared_conf(source_p
->username
, source_p
->host
,
263 source_p
->servptr
->name
,
264 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
))
267 if(!valid_user_host(source_p
, user
, host
) ||
268 !valid_wild_card(source_p
, user
, host
) || !valid_comment(source_p
, reason
))
271 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
276 aconf
->status
= CONF_KILL
;
277 aconf
->created
= rb_current_time();
278 aconf
->user
= rb_strdup(user
);
279 aconf
->host
= rb_strdup(host
);
280 aconf
->passwd
= rb_strdup(reason
);
281 aconf
->info
.oper
= operhash_add(get_oper_name(source_p
));
283 /* Look for an oper reason */
284 if((oper_reason
= strchr(reason
, '|')) != NULL
)
289 if(!EmptyString(oper_reason
))
290 aconf
->spasswd
= rb_strdup(oper_reason
);
294 apply_tkline(source_p
, aconf
, reason
, oper_reason
, tkline_time
);
296 apply_kline(source_p
, aconf
, reason
, oper_reason
);
298 if(ConfigFileEntry
.kline_delay
)
300 if(kline_queued
== 0)
302 rb_event_addonce("check_klines", check_klines_event
, NULL
,
303 ConfigFileEntry
.kline_delay
);
315 * parv[1] - kline to remove
316 * parv[2] - optional "ON"
317 * parv[3] - optional target server
320 mo_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
325 char *h
= LOCAL_COPY(parv
[1]);
326 struct ConfItem
*aconf
;
328 if(!IsOperUnkline(source_p
))
330 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "unkline");
334 if((host
= strchr(h
, '@')) || *h
== '*' || strchr(h
, '.') || strchr(h
, ':'))
336 /* Explicit user@host mask given */
338 if(host
) /* Found user@host */
342 /* check for @host */
348 /* check for user@ */
354 user
= splat
; /* no @ found, assume its *@somehost */
360 sendto_one_notice(source_p
, ":Invalid parameters");
364 /* possible remote kline.. */
365 if((parc
> 3) && (irccmp(parv
[2], "ON") == 0))
367 if(!IsOperRemoteBan(source_p
))
369 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
370 me
.name
, source_p
->name
, "remoteban");
374 propagate_generic(source_p
, "UNKLINE", parv
[3], CAP_UNKLN
, "%s %s", user
, host
);
376 if(match(parv
[3], me
.name
) == 0)
379 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
380 cluster_generic(source_p
, "UNKLINE", SHARED_UNKLINE
, CAP_UNKLN
,
381 "%s %s", user
, host
);
383 aconf
= find_exact_conf_by_address(host
, CONF_KILL
, user
);
386 sendto_one_notice(source_p
, ":No K-Line for %s@%s", user
, host
);
390 if(remove_temp_kline(source_p
, aconf
))
393 remove_permkline_match(source_p
, aconf
);
400 * parv[1] - target server
401 * parv[2] - user to unkline
402 * parv[3] - host to unkline
405 ms_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
407 /* parv[0] parv[1] parv[2] parv[3]
408 * oper target server user host */
409 propagate_generic(source_p
, "UNKLINE", parv
[1], CAP_UNKLN
, "%s %s", parv
[2], parv
[3]);
411 if(!match(parv
[1], me
.name
))
414 if(!IsPerson(source_p
))
417 handle_remote_unkline(source_p
, parv
[2], parv
[3]);
422 me_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
425 if(!IsPerson(source_p
))
428 handle_remote_unkline(source_p
, parv
[1], parv
[2]);
433 handle_remote_unkline(struct Client
*source_p
, const char *user
, const char *host
)
435 struct ConfItem
*aconf
;
437 if(!find_shared_conf(source_p
->username
, source_p
->host
,
438 source_p
->servptr
->name
, SHARED_UNKLINE
))
441 aconf
= find_exact_conf_by_address(host
, CONF_KILL
, user
);
444 sendto_one_notice(source_p
, ":No K-Line for %s@%s", user
, host
);
448 if(remove_temp_kline(source_p
, aconf
))
451 remove_permkline_match(source_p
, aconf
);
458 * side effects - kline as given, is added to the hashtable
462 apply_kline(struct Client
*source_p
, struct ConfItem
*aconf
,
463 const char *reason
, const char *oper_reason
)
465 add_conf_by_address(aconf
->host
, CONF_KILL
, aconf
->user
, NULL
, aconf
);
466 bandb_add(BANDB_KLINE
, source_p
, aconf
->user
, aconf
->host
,
467 reason
, EmptyString(oper_reason
) ? NULL
: oper_reason
, 0);
469 /* no oper reason.. */
470 if(EmptyString(oper_reason
))
472 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
473 "%s added K-Line for [%s@%s] [%s]",
474 get_oper_name(source_p
), aconf
->user
, aconf
->host
, reason
);
475 ilog(L_KLINE
, "K %s 0 %s %s %s",
476 get_oper_name(source_p
), aconf
->user
, aconf
->host
, reason
);
480 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
481 "%s added K-Line for [%s@%s] [%s|%s]",
482 get_oper_name(source_p
), aconf
->user
, aconf
->host
,
483 reason
, oper_reason
);
484 ilog(L_KLINE
, "K %s 0 %s %s %s|%s",
485 get_oper_name(source_p
), aconf
->user
, aconf
->host
, reason
, oper_reason
);
488 sendto_one_notice(source_p
, ":Added K-Line [%s@%s]",
489 aconf
->user
, aconf
->host
);
496 * side effects - tkline as given is placed
499 apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
500 const char *reason
, const char *oper_reason
, int tkline_time
)
502 aconf
->hold
= rb_current_time() + tkline_time
;
503 add_temp_kline(aconf
);
505 /* no oper reason.. */
506 if(EmptyString(oper_reason
))
508 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
509 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
510 get_oper_name(source_p
), tkline_time
/ 60,
511 aconf
->user
, aconf
->host
, reason
);
512 ilog(L_KLINE
, "K %s %d %s %s %s",
513 get_oper_name(source_p
), tkline_time
/ 60, aconf
->user
, aconf
->host
, reason
);
517 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
518 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
519 get_oper_name(source_p
), tkline_time
/ 60,
520 aconf
->user
, aconf
->host
, reason
, oper_reason
);
521 ilog(L_KLINE
, "K %s %d %s %s %s|%s",
522 get_oper_name(source_p
), tkline_time
/ 60,
523 aconf
->user
, aconf
->host
, reason
, oper_reason
);
526 sendto_one_notice(source_p
, ":Added temporary %d min. K-Line [%s@%s]",
527 tkline_time
/ 60, aconf
->user
, aconf
->host
);
532 * inputs - client placing kline, user@host, user buffer, host buffer
533 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
537 find_user_host(struct Client
*source_p
, const char *userhost
, char *luser
, char *lhost
)
541 hostp
= strchr(userhost
, '@');
543 if(hostp
!= NULL
) /* I'm a little user@host */
545 *(hostp
++) = '\0'; /* short and squat */
547 rb_strlcpy(luser
, userhost
, USERLEN
+ 1); /* here is my user */
551 rb_strlcpy(lhost
, hostp
, HOSTLEN
+ 1); /* here is my host */
557 /* no '@', no '.', so its not a user@host or host, therefore
558 * its a nick, which support was removed for.
560 if(strchr(userhost
, '.') == NULL
&& strchr(userhost
, ':') == NULL
)
562 sendto_one_notice(source_p
, ":K-Line must be a user@host or host");
566 luser
[0] = '*'; /* no @ found, assume its *@somehost */
568 rb_strlcpy(lhost
, userhost
, HOSTLEN
+ 1);
576 * inputs - user buffer, host buffer
577 * output - 0 if invalid, 1 if valid
581 valid_user_host(struct Client
*source_p
, const char *luser
, const char *lhost
)
583 /* # is invalid, as are '!' (n!u@h kline) and '@' (u@@h kline) */
584 if(strchr(lhost
, '#') || strchr(luser
, '#') || strchr(luser
, '!') || strchr(lhost
, '@'))
586 sendto_one_notice(source_p
, ":Invalid K-Line");
595 * input - user buffer, host buffer
596 * output - 0 if invalid, 1 if valid
600 valid_wild_card(struct Client
*source_p
, const char *luser
, const char *lhost
)
607 /* user has no wildcards, always accept -- jilles */
608 if(!strchr(luser
, '?') && !strchr(luser
, '*'))
611 /* check there are enough non wildcard chars */
613 while((tmpch
= *p
++))
615 if(!IsKWildChar(tmpch
))
617 /* found enough chars, return */
618 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
623 /* try host, as user didnt contain enough */
624 /* special case for cidr masks -- jilles */
625 if((p
= strrchr(lhost
, '/')) != NULL
&& IsDigit(p
[1]))
627 bitlen
= atoi(p
+ 1);
628 /* much like non-cidr for ipv6, rather arbitrary for ipv4 */
631 (strchr(lhost
, ':') ? 4 * (ConfigFileEntry
.min_nonwildcard
- nonwild
) : 6 -
638 while((tmpch
= *p
++))
640 if(!IsKWildChar(tmpch
))
641 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
646 sendto_one_notice(source_p
,
647 ":Please include at least %d non-wildcard "
648 "characters with the user@host", ConfigFileEntry
.min_nonwildcard
);
654 * inputs - pointer to client
655 * - pointer to comment
656 * output - 0 if no valid comment, 1 if valid
657 * side effects - NONE
660 valid_comment(struct Client
*source_p
, char *comment
)
662 if(strchr(comment
, '"'))
664 sendto_one_notice(source_p
, ":Invalid character '\"' in comment");
668 if(strlen(comment
) > BANREASONLEN
)
669 comment
[BANREASONLEN
] = '\0';
674 /* already_placed_kline()
676 * inputs - source to notify, user@host to check, tkline time
677 * outputs - 1 if a perm kline or a tkline when a tkline is being
679 * side effects - notifies source_p kline exists
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.
686 already_placed_kline(struct Client
*source_p
, const char *luser
, const char *lhost
, int tkline
)
688 const char *reason
, *p
;
689 struct rb_sockaddr_storage iphost
, *piphost
;
690 struct ConfItem
*aconf
;
693 aconf
= find_exact_conf_by_address(lhost
, CONF_KILL
, luser
);
694 if(aconf
== NULL
&& ConfigFileEntry
.non_redundant_klines
)
697 if((t
= parse_netmask(lhost
, (struct sockaddr
*) &iphost
, &bits
)) != HM_HOST
)
711 aconf
= find_conf_by_address(lhost
, NULL
, NULL
, (struct sockaddr
*) piphost
,
712 CONF_KILL
, t
, luser
, NULL
);
715 /* The above was really a lookup of a single IP,
716 * so check if the new kline is wider than the
720 p
= strchr(aconf
->host
, '/');
721 if(bits
> 0 && (p
== NULL
|| bits
< atoi(p
+ 1)))
727 /* setting a tkline, or existing one is perm */
728 if(tkline
|| ((aconf
->flags
& CONF_FLAGS_TEMPORARY
) == 0))
730 reason
= aconf
->passwd
? aconf
->passwd
: "<No Reason>";
732 sendto_one_notice(source_p
,
733 ":[%s@%s] already K-Lined by [%s@%s] - %s",
734 luser
, lhost
, aconf
->user
, aconf
->host
, reason
);
742 /* remove_permkline_match()
744 * hunts for a permanent kline, and removes it.
747 remove_permkline_match(struct Client
*source_p
, struct ConfItem
*aconf
)
749 sendto_one_notice(source_p
, ":K-Line for [%s@%s] is removed", aconf
->user
, aconf
->host
);
751 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
752 "%s has removed the K-Line for: [%s@%s]",
753 get_oper_name(source_p
), aconf
->user
, aconf
->host
);
755 ilog(L_KLINE
, "UK %s %s %s", get_oper_name(source_p
), aconf
->user
, aconf
->host
);
757 remove_reject_mask(aconf
->user
, aconf
->host
);
758 bandb_del(BANDB_KLINE
, aconf
->user
, aconf
->host
);
759 delete_one_address_conf(aconf
->host
, aconf
);
764 /* remove_temp_kline()
766 * inputs - username, hostname to unkline
768 * side effects - tries to unkline anything that matches
771 remove_temp_kline(struct Client
*source_p
, struct ConfItem
*aconf
)
776 for(i
= 0; i
< LAST_TEMP_TYPE
; i
++)
778 RB_DLINK_FOREACH(ptr
, temp_klines
[i
].head
)
780 if(aconf
== ptr
->data
)
782 sendto_one_notice(source_p
,
783 ":Un-klined [%s@%s] from temporary k-lines",
784 aconf
->user
, aconf
->host
);
785 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
786 "%s has removed the temporary K-Line for: [%s@%s]",
787 get_oper_name(source_p
), aconf
->user
,
790 ilog(L_KLINE
, "UK %s %s %s",
791 get_oper_name(source_p
), aconf
->user
, aconf
->host
);
792 rb_dlinkDestroy(ptr
, &temp_klines
[i
]);
793 remove_reject_mask(aconf
->user
, aconf
->host
);
794 delete_one_address_conf(aconf
->host
, aconf
);