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
24 * $Id: m_kline.c 3466 2007-05-19 23:36:51Z jilles $
33 #include "irc_string.h"
34 #include "sprintf_irc.h"
40 #include "s_newconf.h"
50 static int mo_kline(struct Client
*, struct Client
*, int, const char **);
51 static int ms_kline(struct Client
*, struct Client
*, int, const char **);
52 static int me_kline(struct Client
*, struct Client
*, int, const char **);
53 static int mo_unkline(struct Client
*, struct Client
*, int, const char **);
54 static int ms_unkline(struct Client
*, struct Client
*, int, const char **);
55 static int me_unkline(struct Client
*, struct Client
*, int, const char **);
57 struct Message kline_msgtab
= {
58 "KLINE", 0, 0, 0, MFLG_SLOW
,
59 {mg_unreg
, mg_not_oper
, {ms_kline
, 5}, {ms_kline
, 5}, {me_kline
, 5}, {mo_kline
, 3}}
62 struct Message unkline_msgtab
= {
63 "UNKLINE", 0, 0, 0, MFLG_SLOW
,
64 {mg_unreg
, mg_not_oper
, {ms_unkline
, 4}, {ms_unkline
, 4}, {me_unkline
, 3}, {mo_unkline
, 2}}
67 mapi_clist_av1 kline_clist
[] = { &kline_msgtab
, &unkline_msgtab
, NULL
};
68 DECLARE_MODULE_AV1(kline
, NULL
, NULL
, kline_clist
, NULL
, NULL
, "$Revision: 3466 $");
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
, const char *current_date
);
80 static void apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
81 const char *, 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
,
85 const char *user
, const char *host
);
86 static void remove_permkline_match(struct Client
*, const char *, const char *);
87 static int flush_write(struct Client
*, FILE *, const char *, const char *);
88 static int remove_temp_kline(const char *, const char *);
92 * parv[1] - temp time or user@host
93 * parv[2] - user@host, "ON", or reason
94 * parv[3] - "ON", reason, or server to target
95 * parv[4] - server to target, or reason
99 mo_kline(struct Client
*client_p
, struct Client
*source_p
,
100 int parc
, const char **parv
)
102 char def
[] = "No Reason";
103 char user
[USERLEN
+ 2];
104 char host
[HOSTLEN
+ 2];
105 char buffer
[IRCD_BUFSIZE
];
108 const char *current_date
;
109 const char *target_server
= NULL
;
110 struct ConfItem
*aconf
;
114 if(!IsOperK(source_p
))
116 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
117 me
.name
, source_p
->name
, "kline");
121 if((tkline_time
= valid_temp_time(parv
[loc
])) >= 0)
123 /* we just set tkline_time to -1! */
127 if(find_user_host(source_p
, parv
[loc
], user
, host
) == 0)
132 if(parc
>= loc
+2 && !irccmp(parv
[loc
], "ON"))
134 if(!IsOperRemoteBan(source_p
))
136 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
137 me
.name
, source_p
->name
, "remoteban");
141 target_server
= parv
[loc
+1];
145 if(parc
<= loc
|| EmptyString(parv
[loc
]))
147 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
148 me
.name
, source_p
->name
, "KLINE");
152 reason
= LOCAL_COPY(parv
[loc
]);
154 if(target_server
!= NULL
)
156 propagate_generic(source_p
, "KLINE", target_server
, CAP_KLN
,
158 tkline_time
, user
, host
, reason
);
160 /* If we are sending it somewhere that doesnt include us, stop */
161 if(!match(target_server
, me
.name
))
164 /* if we have cluster servers, send it to them.. */
165 else if(dlink_list_length(&cluster_conf_list
) > 0)
166 cluster_generic(source_p
, "KLINE",
167 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
, CAP_KLN
,
169 tkline_time
, user
, host
, reason
);
171 if(!valid_user_host(source_p
, user
, host
) ||
172 !valid_wild_card(source_p
, user
, host
) ||
173 !valid_comment(source_p
, reason
))
176 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
180 current_date
= smalldate();
182 aconf
->status
= CONF_KILL
;
183 DupString(aconf
->host
, host
);
184 DupString(aconf
->user
, user
);
187 /* Look for an oper reason */
188 if((oper_reason
= strchr(reason
, '|')) != NULL
)
193 if(!EmptyString(oper_reason
))
194 DupString(aconf
->spasswd
, oper_reason
);
199 ircsnprintf(buffer
, sizeof(buffer
),
200 "Temporary K-line %d min. - %s (%s)",
201 (int) (tkline_time
/ 60), reason
, current_date
);
202 DupString(aconf
->passwd
, buffer
);
203 apply_tkline(source_p
, aconf
, reason
, oper_reason
, current_date
, tkline_time
);
207 ircsnprintf(buffer
, sizeof(buffer
), "%s (%s)", reason
, current_date
);
208 DupString(aconf
->passwd
, buffer
);
209 apply_kline(source_p
, aconf
, reason
, oper_reason
, current_date
);
212 if(ConfigFileEntry
.kline_delay
)
214 if(kline_queued
== 0)
216 eventAddOnce("check_klines", check_klines_event
, NULL
,
217 ConfigFileEntry
.kline_delay
);
229 * parv[1] - server targeted at
230 * parv[2] - tkline time (0 if perm)
236 ms_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
238 int tkline_time
= atoi(parv
[2]);
240 /* 1.5-3 and earlier contains a bug that allows remote klines to be
241 * sent with an empty reason field. This is a protocol violation,
242 * but its not worth dropping the link over.. --anfl
244 if(parc
< 6 || EmptyString(parv
[5]))
247 propagate_generic(source_p
, "KLINE", parv
[1], CAP_KLN
,
249 tkline_time
, parv
[3], parv
[4], parv
[5]);
251 if(!match(parv
[1], me
.name
))
254 if(!IsPerson(source_p
))
257 handle_remote_kline(source_p
, tkline_time
, parv
[3], parv
[4], parv
[5]);
262 me_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
264 /* <tkline_time> <user> <host> :<reason> */
265 if(!IsPerson(source_p
))
268 handle_remote_kline(source_p
, atoi(parv
[1]), parv
[2], parv
[3], parv
[4]);
273 handle_remote_kline(struct Client
*source_p
, int tkline_time
,
274 const char *user
, const char *host
, const char *kreason
)
276 char buffer
[BUFSIZE
];
277 const char *current_date
;
278 char *reason
= LOCAL_COPY(kreason
);
279 struct ConfItem
*aconf
= NULL
;
282 if(!find_shared_conf(source_p
->username
, source_p
->host
,
283 source_p
->user
->server
,
284 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
))
287 if(!valid_user_host(source_p
, user
, host
) ||
288 !valid_wild_card(source_p
, user
, host
) ||
289 !valid_comment(source_p
, reason
))
292 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
297 aconf
->status
= CONF_KILL
;
298 DupString(aconf
->user
, user
);
299 DupString(aconf
->host
, host
);
301 /* Look for an oper reason */
302 if((oper_reason
= strchr(reason
, '|')) != NULL
)
307 if(!EmptyString(oper_reason
))
308 DupString(aconf
->spasswd
, oper_reason
);
311 current_date
= smalldate();
315 ircsnprintf(buffer
, sizeof(buffer
),
316 "Temporary K-line %d min. - %s (%s)",
317 (int) (tkline_time
/ 60), reason
, current_date
);
318 DupString(aconf
->passwd
, buffer
);
319 apply_tkline(source_p
, aconf
, reason
, oper_reason
, current_date
, tkline_time
);
323 ircsnprintf(buffer
, sizeof(buffer
), "%s (%s)", reason
, current_date
);
324 DupString(aconf
->passwd
, buffer
);
325 apply_kline(source_p
, aconf
, reason
, oper_reason
, current_date
);
328 if(ConfigFileEntry
.kline_delay
)
330 if(kline_queued
== 0)
332 eventAddOnce("check_klines", check_klines_event
, NULL
,
333 ConfigFileEntry
.kline_delay
);
345 * parv[1] - kline to remove
346 * parv[2] - optional "ON"
347 * parv[3] - optional target server
350 mo_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
355 char *h
= LOCAL_COPY(parv
[1]);
357 if(!IsOperUnkline(source_p
))
359 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
360 me
.name
, source_p
->name
, "unkline");
364 if((host
= strchr(h
, '@')) || *h
== '*' || strchr(h
, '.') || strchr(h
, ':'))
366 /* Explicit user@host mask given */
368 if(host
) /* Found user@host */
372 /* check for @host */
378 /* check for user@ */
384 user
= splat
; /* no @ found, assume its *@somehost */
390 sendto_one_notice(source_p
, ":Invalid parameters");
394 /* possible remote kline.. */
395 if((parc
> 3) && (irccmp(parv
[2], "ON") == 0))
397 if(!IsOperRemoteBan(source_p
))
399 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
400 me
.name
, source_p
->name
, "remoteban");
404 propagate_generic(source_p
, "UNKLINE", parv
[3], CAP_UNKLN
,
405 "%s %s", user
, host
);
407 if(match(parv
[3], me
.name
) == 0)
410 else if(dlink_list_length(&cluster_conf_list
) > 0)
411 cluster_generic(source_p
, "UNKLINE", SHARED_UNKLINE
, CAP_UNKLN
,
412 "%s %s", user
, host
);
414 if(remove_temp_kline(user
, host
))
416 sendto_one_notice(source_p
, ":Un-klined [%s@%s] from temporary k-lines", user
, host
);
417 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
418 "%s has removed the temporary K-Line for: [%s@%s]",
419 get_oper_name(source_p
), user
, host
);
420 ilog(L_KLINE
, "UK %s %s %s",
421 get_oper_name(source_p
), user
, host
);
425 remove_permkline_match(source_p
, host
, user
);
432 * parv[1] - target server
433 * parv[2] - user to unkline
434 * parv[3] - host to unkline
437 ms_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
439 /* parv[0] parv[1] parv[2] parv[3]
440 * oper target server user host */
441 propagate_generic(source_p
, "UNKLINE", parv
[1], CAP_UNKLN
,
442 "%s %s", parv
[2], parv
[3]);
444 if(!match(parv
[1], me
.name
))
447 if(!IsPerson(source_p
))
450 handle_remote_unkline(source_p
, parv
[2], parv
[3]);
455 me_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
458 if(!IsPerson(source_p
))
461 handle_remote_unkline(source_p
, parv
[1], parv
[2]);
466 handle_remote_unkline(struct Client
*source_p
, const char *user
, const char *host
)
468 if(!find_shared_conf(source_p
->username
, source_p
->host
,
469 source_p
->user
->server
, SHARED_UNKLINE
))
472 if(remove_temp_kline(user
, host
))
474 sendto_one_notice(source_p
,
475 ":Un-klined [%s@%s] from temporary k-lines",
478 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
479 "%s has removed the temporary K-Line for: [%s@%s]",
480 get_oper_name(source_p
), user
, host
);
482 ilog(L_KLINE
, "UK %s %s %s",
483 get_oper_name(source_p
), user
, host
);
487 remove_permkline_match(source_p
, host
, user
);
494 * side effects - kline as given, is added to the hashtable
498 apply_kline(struct Client
*source_p
, struct ConfItem
*aconf
,
499 const char *reason
, const char *oper_reason
, const char *current_date
)
501 add_conf_by_address(aconf
->host
, CONF_KILL
, aconf
->user
, aconf
);
502 write_confitem(KLINE_TYPE
, source_p
, aconf
->user
, aconf
->host
,
503 reason
, oper_reason
, current_date
, 0);
510 * side effects - tkline as given is placed
513 apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
514 const char *reason
, const char *oper_reason
, const char *current_date
, int tkline_time
)
516 aconf
->hold
= CurrentTime
+ tkline_time
;
517 add_temp_kline(aconf
);
519 /* no oper reason.. */
520 if(EmptyString(oper_reason
))
522 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
523 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
524 get_oper_name(source_p
), tkline_time
/ 60,
525 aconf
->user
, aconf
->host
, reason
);
526 ilog(L_KLINE
, "K %s %d %s %s %s",
527 get_oper_name(source_p
), tkline_time
/ 60,
528 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 strlcpy(luser
, userhost
, USERLEN
+ 1); /* here is my user */
566 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
)
578 luser
[0] = '*'; /* no @ found, assume its *@somehost */
580 strlcpy(lhost
, userhost
, HOSTLEN
+ 1);
588 * inputs - user buffer, host buffer
589 * output - 0 if invalid, 1 if valid
593 valid_user_host(struct Client
*source_p
, const char *luser
, const char *lhost
)
595 /* # is invalid, as is '!' (n!u@h kline) */
596 if(strchr(lhost
, '#') || strchr(luser
, '#') || strchr(luser
, '!'))
598 sendto_one_notice(source_p
, ":Invalid K-Line");
607 * input - user buffer, host buffer
608 * output - 0 if invalid, 1 if valid
612 valid_wild_card(struct Client
*source_p
, const char *luser
, const char *lhost
)
619 /* user has no wildcards, always accept -- jilles */
620 if (!strchr(luser
, '?') && !strchr(luser
, '*'))
623 /* check there are enough non wildcard chars */
625 while ((tmpch
= *p
++))
627 if(!IsKWildChar(tmpch
))
629 /* found enough chars, return */
630 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
635 /* try host, as user didnt contain enough */
636 /* special case for cidr masks -- jilles */
637 if ((p
= strrchr(lhost
, '/')) != NULL
&& IsDigit(p
[1]))
639 bitlen
= atoi(p
+ 1);
640 /* much like non-cidr for ipv6, rather arbitrary for ipv4 */
641 if (bitlen
> 0 && bitlen
>= (strchr(lhost
, ':') ? 4 * (ConfigFileEntry
.min_nonwildcard
- nonwild
) : 6 - 2 * nonwild
))
647 while ((tmpch
= *p
++))
649 if(!IsKWildChar(tmpch
))
650 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
655 sendto_one_notice(source_p
,
656 ":Please include at least %d non-wildcard "
657 "characters with the user@host",
658 ConfigFileEntry
.min_nonwildcard
);
664 * inputs - pointer to client
665 * - pointer to comment
666 * output - 0 if no valid comment, 1 if valid
667 * side effects - NONE
670 valid_comment(struct Client
*source_p
, char *comment
)
672 if(strchr(comment
, '"'))
674 sendto_one_notice(source_p
, ":Invalid character '\"' in comment");
678 if(strlen(comment
) > BANREASONLEN
)
679 comment
[BANREASONLEN
] = '\0';
684 /* already_placed_kline()
686 * inputs - source to notify, user@host to check, tkline time
687 * outputs - 1 if a perm kline or a tkline when a tkline is being
689 * side effects - notifies source_p kline exists
691 /* Note: This currently works if the new K-line is a special case of an
692 * existing K-line, but not the other way round. To do that we would
693 * have to walk the hash and check every existing K-line. -A1kmm.
696 already_placed_kline(struct Client
*source_p
, const char *luser
, const char *lhost
, int tkline
)
699 struct irc_sockaddr_storage iphost
, *piphost
;
700 struct ConfItem
*aconf
;
702 if(ConfigFileEntry
.non_redundant_klines
)
704 if((t
= parse_netmask(lhost
, (struct sockaddr
*)&iphost
, NULL
)) != HM_HOST
)
718 if((aconf
= find_conf_by_address(lhost
, NULL
, NULL
, (struct sockaddr
*)piphost
, CONF_KILL
, t
, luser
)))
720 /* setting a tkline, or existing one is perm */
721 if(tkline
|| ((aconf
->flags
& CONF_FLAGS_TEMPORARY
) == 0))
723 reason
= aconf
->passwd
? aconf
->passwd
: "<No Reason>";
725 sendto_one_notice(source_p
,
726 ":[%s@%s] already K-Lined by [%s@%s] - %s",
727 luser
, lhost
, aconf
->user
,
728 aconf
->host
, reason
);
737 /* remove_permkline_match()
739 * hunts for a permanent kline, and removes it.
742 remove_permkline_match(struct Client
*source_p
, const char *host
, const char *user
)
746 int error_on_write
= NO
;
748 char matchbuf
[BUFSIZE
];
749 char temppath
[BUFSIZE
];
750 const char *filename
;
754 ircsnprintf(temppath
, sizeof(temppath
),
755 "%s.tmp", ConfigFileEntry
.klinefile
);
757 filename
= get_conf_name(KLINE_TYPE
);
759 if((in
= fopen(filename
, "r")) == 0)
761 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
766 if((out
= fopen(temppath
, "w")) == 0)
768 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
776 snprintf(matchbuf
, sizeof(matchbuf
), "\"%s\",\"%s\"", user
, host
);
777 matchlen
= strlen(matchbuf
);
779 while (fgets(buf
, sizeof(buf
), in
))
784 if(!strncasecmp(buf
, matchbuf
, matchlen
))
790 error_on_write
= flush_write(source_p
, out
, buf
, temppath
);
793 /* we dropped out of the loop early because we found a match,
794 * to drop into this somewhat faster loop as we presume we'll never
795 * have two matching klines --anfl
797 if(pairme
&& !error_on_write
)
799 while(fgets(buf
, sizeof(buf
), in
))
804 error_on_write
= flush_write(source_p
, out
, buf
, temppath
);
810 error_on_write
= YES
;
812 /* The result of the rename should be checked too... oh well */
813 /* If there was an error on a write above, then its been reported
814 * and I am not going to trash the original kline /conf file
818 sendto_one_notice(source_p
, ":Couldn't write temp kline file, aborted");
823 sendto_one_notice(source_p
, ":No K-Line for %s@%s",
827 (void) unlink(temppath
);
832 if (rename(temppath
, filename
))
834 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
839 sendto_one_notice(source_p
, ":K-Line for [%s@%s] is removed",
842 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
843 "%s has removed the K-Line for: [%s@%s]",
844 get_oper_name(source_p
), user
, host
);
846 ilog(L_KLINE
, "UK %s %s %s",
847 get_oper_name(source_p
), user
, host
);
854 * inputs - pointer to client structure of oper requesting unkline
855 * - out is the file descriptor
856 * - buf is the buffer to write
857 * - ntowrite is the expected number of character to be written
858 * - temppath is the temporary file name to be written
859 * output - YES for error on write
861 * side effects - if successful, the buf is written to output file
862 * if a write failure happesn, and the file pointed to
863 * by temppath, if its non NULL, is removed.
865 * The idea here is, to be as robust as possible when writing to the
872 flush_write(struct Client
*source_p
, FILE * out
, const char *buf
, const char *temppath
)
874 int error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
878 sendto_one_notice(source_p
, ":Unable to write to %s",
881 (void) unlink(temppath
);
883 return (error_on_write
);
886 /* remove_temp_kline()
888 * inputs - username, hostname to unkline
890 * side effects - tries to unkline anything that matches
893 remove_temp_kline(const char *user
, const char *host
)
895 struct ConfItem
*aconf
;
897 struct irc_sockaddr_storage addr
, caddr
;
902 mtype
= parse_netmask(host
, (struct sockaddr
*)&addr
, &bits
);
904 for (i
= 0; i
< LAST_TEMP_TYPE
; i
++)
906 DLINK_FOREACH(ptr
, temp_klines
[i
].head
)
910 ktype
= parse_netmask(aconf
->host
, (struct sockaddr
*)&caddr
, &cbits
);
912 if(ktype
!= mtype
|| (user
&& irccmp(user
, aconf
->user
)))
917 if(irccmp(aconf
->host
, host
))
920 else if(bits
!= cbits
||
921 !comp_with_mask_sock((struct sockaddr
*)&addr
,
922 (struct sockaddr
*)&caddr
, bits
))
925 dlinkDestroy(ptr
, &temp_klines
[i
]);
926 delete_one_address_conf(aconf
->host
, aconf
);