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
, const char *current_date
);
79 static void apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
80 const char *, 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 *current_date
;
105 const char *target_server
= NULL
;
106 struct ConfItem
*aconf
;
110 if(!IsOperK(source_p
))
112 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "kline");
116 if((tkline_time
= valid_temp_time(parv
[loc
])) >= 0)
118 /* we just set tkline_time to -1! */
122 if(find_user_host(source_p
, parv
[loc
], user
, host
) == 0)
127 if(parc
>= loc
+ 2 && !irccmp(parv
[loc
], "ON"))
129 if(!IsOperRemoteBan(source_p
))
131 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
132 me
.name
, source_p
->name
, "remoteban");
136 target_server
= parv
[loc
+ 1];
140 if(parc
<= loc
|| EmptyString(parv
[loc
]))
142 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
143 me
.name
, source_p
->name
, "KLINE");
147 reason
= LOCAL_COPY(parv
[loc
]);
149 if(target_server
!= NULL
)
151 propagate_generic(source_p
, "KLINE", target_server
, CAP_KLN
,
152 "%d %s %s :%s", tkline_time
, user
, host
, reason
);
154 /* If we are sending it somewhere that doesnt include us, stop */
155 if(!match(target_server
, me
.name
))
158 /* if we have cluster servers, send it to them.. */
159 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
160 cluster_generic(source_p
, "KLINE",
161 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
, CAP_KLN
,
162 "%lu %s %s :%s", tkline_time
, user
, host
, reason
);
164 if(!valid_user_host(source_p
, user
, host
) ||
165 !valid_wild_card(source_p
, user
, host
) || !valid_comment(source_p
, reason
))
168 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
172 current_date
= smalldate();
174 aconf
->status
= CONF_KILL
;
175 aconf
->host
= rb_strdup(host
);
176 aconf
->user
= rb_strdup(user
);
179 /* Look for an oper reason */
180 if((oper_reason
= strchr(reason
, '|')) != NULL
)
185 if(!EmptyString(oper_reason
))
186 aconf
->spasswd
= rb_strdup(oper_reason
);
191 rb_snprintf(buffer
, sizeof(buffer
),
192 "Temporary K-line %d min. - %s (%s)",
193 (int) (tkline_time
/ 60), reason
, current_date
);
194 aconf
->passwd
= rb_strdup(buffer
);
195 apply_tkline(source_p
, aconf
, reason
, oper_reason
, current_date
, tkline_time
);
199 rb_snprintf(buffer
, sizeof(buffer
), "%s (%s)", reason
, current_date
);
200 aconf
->passwd
= rb_strdup(buffer
);
201 apply_kline(source_p
, aconf
, reason
, oper_reason
, current_date
);
204 if(ConfigFileEntry
.kline_delay
)
206 if(kline_queued
== 0)
208 rb_event_addonce("check_klines", check_klines_event
, NULL
,
209 ConfigFileEntry
.kline_delay
);
221 * parv[1] - server targeted at
222 * parv[2] - tkline time (0 if perm)
228 ms_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
230 int tkline_time
= atoi(parv
[2]);
232 /* 1.5-3 and earlier contains a bug that allows remote klines to be
233 * sent with an empty reason field. This is a protocol violation,
234 * but its not worth dropping the link over.. --anfl
236 if(parc
< 6 || EmptyString(parv
[5]))
239 propagate_generic(source_p
, "KLINE", parv
[1], CAP_KLN
,
240 "%d %s %s :%s", tkline_time
, parv
[3], parv
[4], parv
[5]);
242 if(!match(parv
[1], me
.name
))
245 if(!IsPerson(source_p
))
248 handle_remote_kline(source_p
, tkline_time
, parv
[3], parv
[4], parv
[5]);
253 me_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
255 /* <tkline_time> <user> <host> :<reason> */
256 if(!IsPerson(source_p
))
259 handle_remote_kline(source_p
, atoi(parv
[1]), parv
[2], parv
[3], parv
[4]);
264 handle_remote_kline(struct Client
*source_p
, int tkline_time
,
265 const char *user
, const char *host
, const char *kreason
)
267 char buffer
[BUFSIZE
];
268 const char *current_date
;
269 char *reason
= LOCAL_COPY(kreason
);
270 struct ConfItem
*aconf
= NULL
;
273 if(!find_shared_conf(source_p
->username
, source_p
->host
,
274 source_p
->servptr
->name
,
275 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
))
278 if(!valid_user_host(source_p
, user
, host
) ||
279 !valid_wild_card(source_p
, user
, host
) || !valid_comment(source_p
, reason
))
282 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
287 aconf
->status
= CONF_KILL
;
288 aconf
->user
= rb_strdup(user
);
289 aconf
->host
= rb_strdup(host
);
291 /* Look for an oper reason */
292 if((oper_reason
= strchr(reason
, '|')) != NULL
)
297 if(!EmptyString(oper_reason
))
298 aconf
->spasswd
= rb_strdup(oper_reason
);
301 current_date
= smalldate();
305 rb_snprintf(buffer
, sizeof(buffer
),
306 "Temporary K-line %d min. - %s (%s)",
307 (int) (tkline_time
/ 60), reason
, current_date
);
308 aconf
->passwd
= rb_strdup(buffer
);
309 apply_tkline(source_p
, aconf
, reason
, oper_reason
, current_date
, tkline_time
);
313 rb_snprintf(buffer
, sizeof(buffer
), "%s (%s)", reason
, current_date
);
314 aconf
->passwd
= rb_strdup(buffer
);
315 apply_kline(source_p
, aconf
, reason
, oper_reason
, current_date
);
318 if(ConfigFileEntry
.kline_delay
)
320 if(kline_queued
== 0)
322 rb_event_addonce("check_klines", check_klines_event
, NULL
,
323 ConfigFileEntry
.kline_delay
);
335 * parv[1] - kline to remove
336 * parv[2] - optional "ON"
337 * parv[3] - optional target server
340 mo_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
345 char *h
= LOCAL_COPY(parv
[1]);
346 struct ConfItem
*aconf
;
348 if(!IsOperUnkline(source_p
))
350 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "unkline");
354 if((host
= strchr(h
, '@')) || *h
== '*' || strchr(h
, '.') || strchr(h
, ':'))
356 /* Explicit user@host mask given */
358 if(host
) /* Found user@host */
362 /* check for @host */
368 /* check for user@ */
374 user
= splat
; /* no @ found, assume its *@somehost */
380 sendto_one_notice(source_p
, ":Invalid parameters");
384 /* possible remote kline.. */
385 if((parc
> 3) && (irccmp(parv
[2], "ON") == 0))
387 if(!IsOperRemoteBan(source_p
))
389 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
390 me
.name
, source_p
->name
, "remoteban");
394 propagate_generic(source_p
, "UNKLINE", parv
[3], CAP_UNKLN
, "%s %s", user
, host
);
396 if(match(parv
[3], me
.name
) == 0)
399 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
400 cluster_generic(source_p
, "UNKLINE", SHARED_UNKLINE
, CAP_UNKLN
,
401 "%s %s", user
, host
);
403 aconf
= find_exact_conf_by_address(host
, CONF_KILL
, user
);
406 sendto_one_notice(source_p
, ":No K-Line for %s@%s", user
, host
);
410 if(remove_temp_kline(source_p
, aconf
))
413 remove_permkline_match(source_p
, aconf
);
420 * parv[1] - target server
421 * parv[2] - user to unkline
422 * parv[3] - host to unkline
425 ms_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
427 /* parv[0] parv[1] parv[2] parv[3]
428 * oper target server user host */
429 propagate_generic(source_p
, "UNKLINE", parv
[1], CAP_UNKLN
, "%s %s", parv
[2], parv
[3]);
431 if(!match(parv
[1], me
.name
))
434 if(!IsPerson(source_p
))
437 handle_remote_unkline(source_p
, parv
[2], parv
[3]);
442 me_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
445 if(!IsPerson(source_p
))
448 handle_remote_unkline(source_p
, parv
[1], parv
[2]);
453 handle_remote_unkline(struct Client
*source_p
, const char *user
, const char *host
)
455 struct ConfItem
*aconf
;
457 if(!find_shared_conf(source_p
->username
, source_p
->host
,
458 source_p
->servptr
->name
, SHARED_UNKLINE
))
461 aconf
= find_exact_conf_by_address(host
, CONF_KILL
, user
);
464 sendto_one_notice(source_p
, ":No K-Line for %s@%s", user
, host
);
468 if(remove_temp_kline(source_p
, aconf
))
471 remove_permkline_match(source_p
, aconf
);
478 * side effects - kline as given, is added to the hashtable
482 apply_kline(struct Client
*source_p
, struct ConfItem
*aconf
,
483 const char *reason
, const char *oper_reason
, const char *current_date
)
485 add_conf_by_address(aconf
->host
, CONF_KILL
, aconf
->user
, NULL
, aconf
);
486 bandb_add(BANDB_KLINE
, source_p
, aconf
->user
, aconf
->host
,
487 reason
, EmptyString(oper_reason
) ? NULL
: oper_reason
, 0);
494 * side effects - tkline as given is placed
497 apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
498 const char *reason
, const char *oper_reason
, const char *current_date
, int tkline_time
)
500 aconf
->hold
= rb_current_time() + tkline_time
;
501 add_temp_kline(aconf
);
503 /* no oper reason.. */
504 if(EmptyString(oper_reason
))
506 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
507 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
508 get_oper_name(source_p
), tkline_time
/ 60,
509 aconf
->user
, aconf
->host
, reason
);
510 ilog(L_KLINE
, "K %s %d %s %s %s",
511 get_oper_name(source_p
), tkline_time
/ 60, aconf
->user
, aconf
->host
, reason
);
515 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
516 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
517 get_oper_name(source_p
), tkline_time
/ 60,
518 aconf
->user
, aconf
->host
, reason
, oper_reason
);
519 ilog(L_KLINE
, "K %s %d %s %s %s|%s",
520 get_oper_name(source_p
), tkline_time
/ 60,
521 aconf
->user
, aconf
->host
, reason
, oper_reason
);
524 sendto_one_notice(source_p
, ":Added temporary %d min. K-Line [%s@%s]",
525 tkline_time
/ 60, aconf
->user
, aconf
->host
);
530 * inputs - client placing kline, user@host, user buffer, host buffer
531 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
535 find_user_host(struct Client
*source_p
, const char *userhost
, char *luser
, char *lhost
)
539 hostp
= strchr(userhost
, '@');
541 if(hostp
!= NULL
) /* I'm a little user@host */
543 *(hostp
++) = '\0'; /* short and squat */
545 rb_strlcpy(luser
, userhost
, USERLEN
+ 1); /* here is my user */
549 rb_strlcpy(lhost
, hostp
, HOSTLEN
+ 1); /* here is my host */
555 /* no '@', no '.', so its not a user@host or host, therefore
556 * its a nick, which support was removed for.
558 if(strchr(userhost
, '.') == NULL
&& strchr(userhost
, ':') == NULL
)
560 sendto_one_notice(source_p
, ":K-Line must be a user@host or host");
564 luser
[0] = '*'; /* no @ found, assume its *@somehost */
566 rb_strlcpy(lhost
, userhost
, HOSTLEN
+ 1);
574 * inputs - user buffer, host buffer
575 * output - 0 if invalid, 1 if valid
579 valid_user_host(struct Client
*source_p
, const char *luser
, const char *lhost
)
581 /* # is invalid, as are '!' (n!u@h kline) and '@' (u@@h kline) */
582 if(strchr(lhost
, '#') || strchr(luser
, '#') || strchr(luser
, '!') || strchr(lhost
, '@'))
584 sendto_one_notice(source_p
, ":Invalid K-Line");
593 * input - user buffer, host buffer
594 * output - 0 if invalid, 1 if valid
598 valid_wild_card(struct Client
*source_p
, const char *luser
, const char *lhost
)
605 /* user has no wildcards, always accept -- jilles */
606 if(!strchr(luser
, '?') && !strchr(luser
, '*'))
609 /* check there are enough non wildcard chars */
611 while((tmpch
= *p
++))
613 if(!IsKWildChar(tmpch
))
615 /* found enough chars, return */
616 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
621 /* try host, as user didnt contain enough */
622 /* special case for cidr masks -- jilles */
623 if((p
= strrchr(lhost
, '/')) != NULL
&& IsDigit(p
[1]))
625 bitlen
= atoi(p
+ 1);
626 /* much like non-cidr for ipv6, rather arbitrary for ipv4 */
629 (strchr(lhost
, ':') ? 4 * (ConfigFileEntry
.min_nonwildcard
- nonwild
) : 6 -
636 while((tmpch
= *p
++))
638 if(!IsKWildChar(tmpch
))
639 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
644 sendto_one_notice(source_p
,
645 ":Please include at least %d non-wildcard "
646 "characters with the user@host", ConfigFileEntry
.min_nonwildcard
);
652 * inputs - pointer to client
653 * - pointer to comment
654 * output - 0 if no valid comment, 1 if valid
655 * side effects - NONE
658 valid_comment(struct Client
*source_p
, char *comment
)
660 if(strchr(comment
, '"'))
662 sendto_one_notice(source_p
, ":Invalid character '\"' in comment");
666 if(strlen(comment
) > BANREASONLEN
)
667 comment
[BANREASONLEN
] = '\0';
672 /* already_placed_kline()
674 * inputs - source to notify, user@host to check, tkline time
675 * outputs - 1 if a perm kline or a tkline when a tkline is being
677 * side effects - notifies source_p kline exists
679 /* Note: This currently works if the new K-line is a special case of an
680 * existing K-line, but not the other way round. To do that we would
681 * have to walk the hash and check every existing K-line. -A1kmm.
684 already_placed_kline(struct Client
*source_p
, const char *luser
, const char *lhost
, int tkline
)
686 const char *reason
, *p
;
687 struct rb_sockaddr_storage iphost
, *piphost
;
688 struct ConfItem
*aconf
;
691 aconf
= find_exact_conf_by_address(lhost
, CONF_KILL
, luser
);
692 if(aconf
== NULL
&& ConfigFileEntry
.non_redundant_klines
)
695 if((t
= parse_netmask(lhost
, (struct sockaddr
*) &iphost
, &bits
)) != HM_HOST
)
709 aconf
= find_conf_by_address(lhost
, NULL
, NULL
, (struct sockaddr
*) piphost
,
710 CONF_KILL
, t
, luser
, NULL
);
713 /* The above was really a lookup of a single IP,
714 * so check if the new kline is wider than the
718 p
= strchr(aconf
->host
, '/');
719 if(bits
> 0 && (p
== NULL
|| bits
< atoi(p
+ 1)))
725 /* setting a tkline, or existing one is perm */
726 if(tkline
|| ((aconf
->flags
& CONF_FLAGS_TEMPORARY
) == 0))
728 reason
= aconf
->passwd
? aconf
->passwd
: "<No Reason>";
730 sendto_one_notice(source_p
,
731 ":[%s@%s] already K-Lined by [%s@%s] - %s",
732 luser
, lhost
, aconf
->user
, aconf
->host
, reason
);
740 /* remove_permkline_match()
742 * hunts for a permanent kline, and removes it.
745 remove_permkline_match(struct Client
*source_p
, struct ConfItem
*aconf
)
747 sendto_one_notice(source_p
, ":K-Line for [%s@%s] is removed", aconf
->user
, aconf
->host
);
749 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
750 "%s has removed the K-Line for: [%s@%s]",
751 get_oper_name(source_p
), aconf
->user
, aconf
->host
);
753 ilog(L_KLINE
, "UK %s %s %s", get_oper_name(source_p
), aconf
->user
, aconf
->host
);
755 remove_reject_mask(aconf
->user
, aconf
->host
);
756 bandb_del(BANDB_KLINE
, aconf
->user
, aconf
->host
);
757 delete_one_address_conf(aconf
->host
, aconf
);
762 /* remove_temp_kline()
764 * inputs - username, hostname to unkline
766 * side effects - tries to unkline anything that matches
769 remove_temp_kline(struct Client
*source_p
, struct ConfItem
*aconf
)
774 for(i
= 0; i
< LAST_TEMP_TYPE
; i
++)
776 RB_DLINK_FOREACH(ptr
, temp_klines
[i
].head
)
778 if(aconf
== ptr
->data
)
780 sendto_one_notice(source_p
,
781 ":Un-klined [%s@%s] from temporary k-lines",
782 aconf
->user
, aconf
->host
);
783 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
784 "%s has removed the temporary K-Line for: [%s@%s]",
785 get_oper_name(source_p
), aconf
->user
,
788 ilog(L_KLINE
, "UK %s %s %s",
789 get_oper_name(source_p
), aconf
->user
, aconf
->host
);
790 rb_dlinkDestroy(ptr
, &temp_klines
[i
]);
791 remove_reject_mask(aconf
->user
, aconf
->host
);
792 delete_one_address_conf(aconf
->host
, aconf
);