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];
101 char buffer
[IRCD_BUFSIZE
];
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
);
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
);
190 rb_snprintf(buffer
, sizeof(buffer
),
191 "Temporary K-line %d min. - %s",
192 (int) (tkline_time
/ 60), reason
);
193 aconf
->passwd
= rb_strdup(buffer
);
194 apply_tkline(source_p
, aconf
, reason
, oper_reason
, tkline_time
);
198 aconf
->passwd
= rb_strdup(reason
);
199 apply_kline(source_p
, aconf
, reason
, oper_reason
);
202 if(ConfigFileEntry
.kline_delay
)
204 if(kline_queued
== 0)
206 rb_event_addonce("check_klines", check_klines_event
, NULL
,
207 ConfigFileEntry
.kline_delay
);
219 * parv[1] - server targeted at
220 * parv[2] - tkline time (0 if perm)
226 ms_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
228 int tkline_time
= atoi(parv
[2]);
230 /* 1.5-3 and earlier contains a bug that allows remote klines to be
231 * sent with an empty reason field. This is a protocol violation,
232 * but its not worth dropping the link over.. --anfl
234 if(parc
< 6 || EmptyString(parv
[5]))
237 propagate_generic(source_p
, "KLINE", parv
[1], CAP_KLN
,
238 "%d %s %s :%s", tkline_time
, parv
[3], parv
[4], parv
[5]);
240 if(!match(parv
[1], me
.name
))
243 if(!IsPerson(source_p
))
246 handle_remote_kline(source_p
, tkline_time
, parv
[3], parv
[4], parv
[5]);
251 me_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
253 /* <tkline_time> <user> <host> :<reason> */
254 if(!IsPerson(source_p
))
257 handle_remote_kline(source_p
, atoi(parv
[1]), parv
[2], parv
[3], parv
[4]);
262 handle_remote_kline(struct Client
*source_p
, int tkline_time
,
263 const char *user
, const char *host
, const char *kreason
)
265 char buffer
[BUFSIZE
];
266 char *reason
= LOCAL_COPY(kreason
);
267 struct ConfItem
*aconf
= NULL
;
270 if(!find_shared_conf(source_p
->username
, source_p
->host
,
271 source_p
->servptr
->name
,
272 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
))
275 if(!valid_user_host(source_p
, user
, host
) ||
276 !valid_wild_card(source_p
, user
, host
) || !valid_comment(source_p
, reason
))
279 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
284 aconf
->status
= CONF_KILL
;
285 aconf
->created
= rb_current_time();
286 aconf
->user
= rb_strdup(user
);
287 aconf
->host
= rb_strdup(host
);
289 /* Look for an oper reason */
290 if((oper_reason
= strchr(reason
, '|')) != NULL
)
295 if(!EmptyString(oper_reason
))
296 aconf
->spasswd
= rb_strdup(oper_reason
);
301 rb_snprintf(buffer
, sizeof(buffer
),
302 "Temporary K-line %d min. - %s",
303 (int) (tkline_time
/ 60), reason
);
304 aconf
->passwd
= rb_strdup(buffer
);
305 apply_tkline(source_p
, aconf
, reason
, oper_reason
, tkline_time
);
309 aconf
->passwd
= rb_strdup(reason
);
310 apply_kline(source_p
, aconf
, reason
, oper_reason
);
313 if(ConfigFileEntry
.kline_delay
)
315 if(kline_queued
== 0)
317 rb_event_addonce("check_klines", check_klines_event
, NULL
,
318 ConfigFileEntry
.kline_delay
);
330 * parv[1] - kline to remove
331 * parv[2] - optional "ON"
332 * parv[3] - optional target server
335 mo_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
340 char *h
= LOCAL_COPY(parv
[1]);
341 struct ConfItem
*aconf
;
343 if(!IsOperUnkline(source_p
))
345 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "unkline");
349 if((host
= strchr(h
, '@')) || *h
== '*' || strchr(h
, '.') || strchr(h
, ':'))
351 /* Explicit user@host mask given */
353 if(host
) /* Found user@host */
357 /* check for @host */
363 /* check for user@ */
369 user
= splat
; /* no @ found, assume its *@somehost */
375 sendto_one_notice(source_p
, ":Invalid parameters");
379 /* possible remote kline.. */
380 if((parc
> 3) && (irccmp(parv
[2], "ON") == 0))
382 if(!IsOperRemoteBan(source_p
))
384 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
385 me
.name
, source_p
->name
, "remoteban");
389 propagate_generic(source_p
, "UNKLINE", parv
[3], CAP_UNKLN
, "%s %s", user
, host
);
391 if(match(parv
[3], me
.name
) == 0)
394 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
395 cluster_generic(source_p
, "UNKLINE", SHARED_UNKLINE
, CAP_UNKLN
,
396 "%s %s", user
, host
);
398 aconf
= find_exact_conf_by_address(host
, CONF_KILL
, user
);
401 sendto_one_notice(source_p
, ":No K-Line for %s@%s", user
, host
);
405 if(remove_temp_kline(source_p
, aconf
))
408 remove_permkline_match(source_p
, aconf
);
415 * parv[1] - target server
416 * parv[2] - user to unkline
417 * parv[3] - host to unkline
420 ms_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
422 /* parv[0] parv[1] parv[2] parv[3]
423 * oper target server user host */
424 propagate_generic(source_p
, "UNKLINE", parv
[1], CAP_UNKLN
, "%s %s", parv
[2], parv
[3]);
426 if(!match(parv
[1], me
.name
))
429 if(!IsPerson(source_p
))
432 handle_remote_unkline(source_p
, parv
[2], parv
[3]);
437 me_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
440 if(!IsPerson(source_p
))
443 handle_remote_unkline(source_p
, parv
[1], parv
[2]);
448 handle_remote_unkline(struct Client
*source_p
, const char *user
, const char *host
)
450 struct ConfItem
*aconf
;
452 if(!find_shared_conf(source_p
->username
, source_p
->host
,
453 source_p
->servptr
->name
, SHARED_UNKLINE
))
456 aconf
= find_exact_conf_by_address(host
, CONF_KILL
, user
);
459 sendto_one_notice(source_p
, ":No K-Line for %s@%s", user
, host
);
463 if(remove_temp_kline(source_p
, aconf
))
466 remove_permkline_match(source_p
, aconf
);
473 * side effects - kline as given, is added to the hashtable
477 apply_kline(struct Client
*source_p
, struct ConfItem
*aconf
,
478 const char *reason
, const char *oper_reason
)
480 add_conf_by_address(aconf
->host
, CONF_KILL
, aconf
->user
, NULL
, aconf
);
481 bandb_add(BANDB_KLINE
, source_p
, aconf
->user
, aconf
->host
,
482 reason
, EmptyString(oper_reason
) ? NULL
: oper_reason
, 0);
484 /* no oper reason.. */
485 if(EmptyString(oper_reason
))
487 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
488 "%s added K-Line for [%s@%s] [%s]",
489 get_oper_name(source_p
), aconf
->user
, aconf
->host
, reason
);
490 ilog(L_KLINE
, "K %s 0 %s %s %s",
491 get_oper_name(source_p
), aconf
->user
, aconf
->host
, reason
);
495 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
496 "%s added K-Line for [%s@%s] [%s|%s]",
497 get_oper_name(source_p
), aconf
->user
, aconf
->host
,
498 reason
, oper_reason
);
499 ilog(L_KLINE
, "K %s 0 %s %s %s|%s",
500 get_oper_name(source_p
), aconf
->user
, aconf
->host
, reason
, oper_reason
);
503 sendto_one_notice(source_p
, ":Added K-Line [%s@%s]",
504 aconf
->user
, aconf
->host
);
511 * side effects - tkline as given is placed
514 apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
515 const char *reason
, const char *oper_reason
, int tkline_time
)
517 aconf
->hold
= rb_current_time() + tkline_time
;
518 add_temp_kline(aconf
);
520 /* no oper reason.. */
521 if(EmptyString(oper_reason
))
523 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
524 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
525 get_oper_name(source_p
), tkline_time
/ 60,
526 aconf
->user
, aconf
->host
, reason
);
527 ilog(L_KLINE
, "K %s %d %s %s %s",
528 get_oper_name(source_p
), tkline_time
/ 60, aconf
->user
, aconf
->host
, reason
);
532 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
533 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
534 get_oper_name(source_p
), tkline_time
/ 60,
535 aconf
->user
, aconf
->host
, reason
, oper_reason
);
536 ilog(L_KLINE
, "K %s %d %s %s %s|%s",
537 get_oper_name(source_p
), tkline_time
/ 60,
538 aconf
->user
, aconf
->host
, reason
, oper_reason
);
541 sendto_one_notice(source_p
, ":Added temporary %d min. K-Line [%s@%s]",
542 tkline_time
/ 60, aconf
->user
, aconf
->host
);
547 * inputs - client placing kline, user@host, user buffer, host buffer
548 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
552 find_user_host(struct Client
*source_p
, const char *userhost
, char *luser
, char *lhost
)
556 hostp
= strchr(userhost
, '@');
558 if(hostp
!= NULL
) /* I'm a little user@host */
560 *(hostp
++) = '\0'; /* short and squat */
562 rb_strlcpy(luser
, userhost
, USERLEN
+ 1); /* here is my user */
566 rb_strlcpy(lhost
, hostp
, HOSTLEN
+ 1); /* here is my host */
572 /* no '@', no '.', so its not a user@host or host, therefore
573 * its a nick, which support was removed for.
575 if(strchr(userhost
, '.') == NULL
&& strchr(userhost
, ':') == NULL
)
577 sendto_one_notice(source_p
, ":K-Line must be a user@host or host");
581 luser
[0] = '*'; /* no @ found, assume its *@somehost */
583 rb_strlcpy(lhost
, userhost
, HOSTLEN
+ 1);
591 * inputs - user buffer, host buffer
592 * output - 0 if invalid, 1 if valid
596 valid_user_host(struct Client
*source_p
, const char *luser
, const char *lhost
)
598 /* # is invalid, as are '!' (n!u@h kline) and '@' (u@@h kline) */
599 if(strchr(lhost
, '#') || strchr(luser
, '#') || strchr(luser
, '!') || strchr(lhost
, '@'))
601 sendto_one_notice(source_p
, ":Invalid K-Line");
610 * input - user buffer, host buffer
611 * output - 0 if invalid, 1 if valid
615 valid_wild_card(struct Client
*source_p
, const char *luser
, const char *lhost
)
622 /* user has no wildcards, always accept -- jilles */
623 if(!strchr(luser
, '?') && !strchr(luser
, '*'))
626 /* check there are enough non wildcard chars */
628 while((tmpch
= *p
++))
630 if(!IsKWildChar(tmpch
))
632 /* found enough chars, return */
633 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
638 /* try host, as user didnt contain enough */
639 /* special case for cidr masks -- jilles */
640 if((p
= strrchr(lhost
, '/')) != NULL
&& IsDigit(p
[1]))
642 bitlen
= atoi(p
+ 1);
643 /* much like non-cidr for ipv6, rather arbitrary for ipv4 */
646 (strchr(lhost
, ':') ? 4 * (ConfigFileEntry
.min_nonwildcard
- nonwild
) : 6 -
653 while((tmpch
= *p
++))
655 if(!IsKWildChar(tmpch
))
656 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
661 sendto_one_notice(source_p
,
662 ":Please include at least %d non-wildcard "
663 "characters with the user@host", ConfigFileEntry
.min_nonwildcard
);
669 * inputs - pointer to client
670 * - pointer to comment
671 * output - 0 if no valid comment, 1 if valid
672 * side effects - NONE
675 valid_comment(struct Client
*source_p
, char *comment
)
677 if(strchr(comment
, '"'))
679 sendto_one_notice(source_p
, ":Invalid character '\"' in comment");
683 if(strlen(comment
) > BANREASONLEN
)
684 comment
[BANREASONLEN
] = '\0';
689 /* already_placed_kline()
691 * inputs - source to notify, user@host to check, tkline time
692 * outputs - 1 if a perm kline or a tkline when a tkline is being
694 * side effects - notifies source_p kline exists
696 /* Note: This currently works if the new K-line is a special case of an
697 * existing K-line, but not the other way round. To do that we would
698 * have to walk the hash and check every existing K-line. -A1kmm.
701 already_placed_kline(struct Client
*source_p
, const char *luser
, const char *lhost
, int tkline
)
703 const char *reason
, *p
;
704 struct rb_sockaddr_storage iphost
, *piphost
;
705 struct ConfItem
*aconf
;
708 aconf
= find_exact_conf_by_address(lhost
, CONF_KILL
, luser
);
709 if(aconf
== NULL
&& ConfigFileEntry
.non_redundant_klines
)
712 if((t
= parse_netmask(lhost
, (struct sockaddr
*) &iphost
, &bits
)) != HM_HOST
)
726 aconf
= find_conf_by_address(lhost
, NULL
, NULL
, (struct sockaddr
*) piphost
,
727 CONF_KILL
, t
, luser
, NULL
);
730 /* The above was really a lookup of a single IP,
731 * so check if the new kline is wider than the
735 p
= strchr(aconf
->host
, '/');
736 if(bits
> 0 && (p
== NULL
|| bits
< atoi(p
+ 1)))
742 /* setting a tkline, or existing one is perm */
743 if(tkline
|| ((aconf
->flags
& CONF_FLAGS_TEMPORARY
) == 0))
745 reason
= aconf
->passwd
? aconf
->passwd
: "<No Reason>";
747 sendto_one_notice(source_p
,
748 ":[%s@%s] already K-Lined by [%s@%s] - %s",
749 luser
, lhost
, aconf
->user
, aconf
->host
, reason
);
757 /* remove_permkline_match()
759 * hunts for a permanent kline, and removes it.
762 remove_permkline_match(struct Client
*source_p
, struct ConfItem
*aconf
)
764 sendto_one_notice(source_p
, ":K-Line for [%s@%s] is removed", aconf
->user
, aconf
->host
);
766 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
767 "%s has removed the K-Line for: [%s@%s]",
768 get_oper_name(source_p
), aconf
->user
, aconf
->host
);
770 ilog(L_KLINE
, "UK %s %s %s", get_oper_name(source_p
), aconf
->user
, aconf
->host
);
772 remove_reject_mask(aconf
->user
, aconf
->host
);
773 bandb_del(BANDB_KLINE
, aconf
->user
, aconf
->host
);
774 delete_one_address_conf(aconf
->host
, aconf
);
779 /* remove_temp_kline()
781 * inputs - username, hostname to unkline
783 * side effects - tries to unkline anything that matches
786 remove_temp_kline(struct Client
*source_p
, struct ConfItem
*aconf
)
791 for(i
= 0; i
< LAST_TEMP_TYPE
; i
++)
793 RB_DLINK_FOREACH(ptr
, temp_klines
[i
].head
)
795 if(aconf
== ptr
->data
)
797 sendto_one_notice(source_p
,
798 ":Un-klined [%s@%s] from temporary k-lines",
799 aconf
->user
, aconf
->host
);
800 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
801 "%s has removed the temporary K-Line for: [%s@%s]",
802 get_oper_name(source_p
), aconf
->user
,
805 ilog(L_KLINE
, "UK %s %s %s",
806 get_oper_name(source_p
), aconf
->user
, aconf
->host
);
807 rb_dlinkDestroy(ptr
, &temp_klines
[i
]);
808 remove_reject_mask(aconf
->user
, aconf
->host
);
809 delete_one_address_conf(aconf
->host
, aconf
);