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
31 #include "irc_string.h"
32 #include "sprintf_irc.h"
38 #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
};
66 DECLARE_MODULE_AV1(kline
, NULL
, NULL
, kline_clist
, NULL
, NULL
, "$Revision: 125 $");
68 /* Local function prototypes */
69 static int find_user_host(struct Client
*source_p
, const char *userhost
, char *user
, char *host
);
70 static int valid_comment(struct Client
*source_p
, char *comment
);
71 static int valid_user_host(struct Client
*source_p
, const char *user
, const char *host
);
72 static int valid_wild_card(struct Client
*source_p
, const char *user
, const char *host
);
74 static void handle_remote_kline(struct Client
*source_p
, int tkline_time
,
75 const char *user
, const char *host
, const char *reason
);
76 static void apply_kline(struct Client
*source_p
, struct ConfItem
*aconf
,
77 const char *reason
, const char *oper_reason
, const char *current_date
);
78 static void apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
79 const char *, const char *, const char *, int);
80 static int already_placed_kline(struct Client
*, const char *, const char *, int);
82 static void handle_remote_unkline(struct Client
*source_p
,
83 const char *user
, const char *host
);
84 static void remove_permkline_match(struct Client
*, const char *, const char *);
85 static int flush_write(struct Client
*, FILE *, const char *, const char *);
86 static int remove_temp_kline(const char *, const char *);
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
,
98 int parc
, const char **parv
)
100 char def
[] = "No Reason";
101 char user
[USERLEN
+ 2];
102 char host
[HOSTLEN
+ 2];
103 char buffer
[IRCD_BUFSIZE
];
106 const char *current_date
;
107 const char *target_server
= NULL
;
108 struct ConfItem
*aconf
;
112 if(!IsOperK(source_p
))
114 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
115 me
.name
, source_p
->name
, "kline");
119 if((tkline_time
= valid_temp_time(parv
[loc
])) >= 0)
121 /* we just set tkline_time to -1! */
125 if(find_user_host(source_p
, parv
[loc
], user
, host
) == 0)
130 if(parc
>= loc
+2 && !irccmp(parv
[loc
], "ON"))
132 target_server
= parv
[loc
+1];
136 if(parc
<= loc
|| EmptyString(parv
[loc
]))
138 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
139 me
.name
, source_p
->name
, "KLINE");
143 reason
= LOCAL_COPY(parv
[loc
]);
145 if(target_server
!= NULL
)
147 if(!ConfigFileEntry
.allow_kline_on
)
149 sendto_one(source_p
, ":%s NOTICE %s :*** KLINE ON server.name is disabled.",
150 me
.name
, source_p
->name
);
153 else if(!IsOperRemoteBan(source_p
))
155 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
,
161 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "%s added temporary %d minute K-Line for [%s@%s] on %s [%s]",
162 get_oper_name(source_p
), tkline_time
/ 60, user
, host
, target_server
, reason
);
164 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "%s added K-Line for [%s@%s] on %s [%s]",
165 get_oper_name(source_p
), user
, host
, target_server
, reason
);
167 propagate_generic(source_p
, "KLINE", target_server
, CAP_KLN
,
169 tkline_time
, user
, host
, reason
);
171 /* If we are sending it somewhere that doesnt include us, stop */
172 if(!match(target_server
, me
.name
))
175 /* if we have cluster servers, send it to them.. */
176 else if(dlink_list_length(&cluster_conf_list
) > 0)
177 cluster_generic(source_p
, "KLINE",
178 (tkline_time
> 0) ? SHARED_TKLINE
: SHARED_PKLINE
, CAP_KLN
,
180 tkline_time
, user
, host
, reason
);
182 if(!valid_user_host(source_p
, user
, host
) ||
183 !valid_wild_card(source_p
, user
, host
) ||
184 !valid_comment(source_p
, reason
))
187 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
191 current_date
= smalldate();
193 aconf
->status
= CONF_KILL
;
194 DupString(aconf
->host
, host
);
195 DupString(aconf
->user
, user
);
198 /* Look for an oper reason */
199 if((oper_reason
= strchr(reason
, '|')) != NULL
)
204 if(!EmptyString(oper_reason
))
205 DupString(aconf
->spasswd
, oper_reason
);
210 ircsnprintf(buffer
, sizeof(buffer
),
211 "Temporary K-line %d min. - %s (%s)",
212 (int) (tkline_time
/ 60), reason
, current_date
);
213 DupString(aconf
->passwd
, buffer
);
214 apply_tkline(source_p
, aconf
, reason
, oper_reason
, current_date
, tkline_time
);
218 ircsnprintf(buffer
, sizeof(buffer
), "%s (%s)", reason
, current_date
);
219 DupString(aconf
->passwd
, buffer
);
220 apply_kline(source_p
, aconf
, reason
, oper_reason
, current_date
);
223 if(ConfigFileEntry
.kline_delay
)
225 if(kline_queued
== 0)
227 eventAddOnce("check_klines", check_klines_event
, NULL
,
228 ConfigFileEntry
.kline_delay
);
240 * parv[1] - server targeted at
241 * parv[2] - tkline time (0 if perm)
247 ms_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
249 int tkline_time
= atoi(parv
[2]);
251 /* 1.5-3 and earlier contains a bug that allows remote klines to be
252 * sent with an empty reason field. This is a protocol violation,
253 * but its not worth dropping the link over.. --anfl
255 if(parc
< 6 || EmptyString(parv
[5]))
258 propagate_generic(source_p
, "KLINE", parv
[1], CAP_KLN
,
260 tkline_time
, parv
[3], parv
[4], parv
[5]);
262 if(!match(parv
[1], me
.name
))
265 if(!IsPerson(source_p
))
268 handle_remote_kline(source_p
, tkline_time
, parv
[3], parv
[4], parv
[5]);
273 me_kline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
275 /* <tkline_time> <user> <host> :<reason> */
276 if(!IsPerson(source_p
))
279 handle_remote_kline(source_p
, atoi(parv
[1]), parv
[2], parv
[3], parv
[4]);
284 handle_remote_kline(struct Client
*source_p
, int tkline_time
,
285 const char *user
, const char *host
, const char *kreason
)
287 char buffer
[BUFSIZE
];
288 const char *current_date
;
289 char *reason
= LOCAL_COPY(kreason
);
290 struct ConfItem
*aconf
= NULL
;
293 if (!find_client_shared_conf(source_p
, tkline_time
> 0 ? SHARED_TKLINE
: SHARED_PKLINE
))
296 if(!valid_user_host(source_p
, user
, host
) ||
297 !valid_wild_card(source_p
, user
, host
) ||
298 !valid_comment(source_p
, reason
))
301 if(already_placed_kline(source_p
, user
, host
, tkline_time
))
306 aconf
->status
= CONF_KILL
;
307 DupString(aconf
->user
, user
);
308 DupString(aconf
->host
, host
);
310 /* Look for an oper reason */
311 if((oper_reason
= strchr(reason
, '|')) != NULL
)
316 if(!EmptyString(oper_reason
))
317 DupString(aconf
->spasswd
, oper_reason
);
320 current_date
= smalldate();
324 ircsnprintf(buffer
, sizeof(buffer
),
325 "Temporary K-line %d min. - %s (%s)",
326 (int) (tkline_time
/ 60), reason
, current_date
);
327 DupString(aconf
->passwd
, buffer
);
328 apply_tkline(source_p
, aconf
, reason
, oper_reason
, current_date
, tkline_time
);
332 ircsnprintf(buffer
, sizeof(buffer
), "%s (%s)", reason
, current_date
);
333 DupString(aconf
->passwd
, buffer
);
334 apply_kline(source_p
, aconf
, reason
, oper_reason
, current_date
);
337 if(ConfigFileEntry
.kline_delay
)
339 if(kline_queued
== 0)
341 eventAddOnce("check_klines", check_klines_event
, NULL
,
342 ConfigFileEntry
.kline_delay
);
354 * parv[1] - kline to remove
355 * parv[2] - optional "ON"
356 * parv[3] - optional target server
359 mo_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
364 char *h
= LOCAL_COPY(parv
[1]);
366 if(!IsOperUnkline(source_p
))
368 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
369 me
.name
, source_p
->name
, "unkline");
373 if((host
= strchr(h
, '@')) || *h
== '*')
375 /* Explicit user@host mask given */
377 if(host
) /* Found user@host */
381 /* check for @host */
387 /* check for user@ */
393 user
= splat
; /* no @ found, assume its *@somehost */
399 sendto_one(source_p
, ":%s NOTICE %s :Invalid parameters", me
.name
, source_p
->name
);
403 /* possible remote kline.. */
404 if((parc
> 3) && (irccmp(parv
[2], "ON") == 0))
406 if (!IsOperRemoteBan(source_p
))
408 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
,
414 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
415 "%s is removing K-Line for [%s@%s] on %s",
416 get_oper_name(source_p
), user
, host
, parv
[3]);
418 propagate_generic(source_p
, "UNKLINE", parv
[3], CAP_UNKLN
,
419 "%s %s", user
, host
);
421 if(match(parv
[3], me
.name
) == 0)
424 else if(dlink_list_length(&cluster_conf_list
) > 0)
425 cluster_generic(source_p
, "UNKLINE", SHARED_UNKLINE
, CAP_UNKLN
,
426 "%s %s", user
, host
);
428 if(remove_temp_kline(user
, host
))
431 ":%s NOTICE %s :Un-klined [%s@%s] from temporary k-lines",
432 me
.name
, parv
[0], user
, host
);
433 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
434 "%s has removed the temporary K-Line for: [%s@%s]",
435 get_oper_name(source_p
), user
, host
);
436 ilog(L_KLINE
, "UK %s %s %s",
437 get_oper_name(source_p
), user
, host
);
441 remove_permkline_match(source_p
, host
, user
);
448 * parv[1] - target server
449 * parv[2] - user to unkline
450 * parv[3] - host to unkline
453 ms_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
455 /* parv[0] parv[1] parv[2] parv[3]
456 * oper target server user host */
457 propagate_generic(source_p
, "UNKLINE", parv
[1], CAP_UNKLN
,
458 "%s %s", parv
[2], parv
[3]);
460 if(!match(parv
[1], me
.name
))
463 if(!IsPerson(source_p
))
466 handle_remote_unkline(source_p
, parv
[2], parv
[3]);
471 me_unkline(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
474 if(!IsPerson(source_p
))
477 handle_remote_unkline(source_p
, parv
[1], parv
[2]);
482 handle_remote_unkline(struct Client
*source_p
, const char *user
, const char *host
)
484 if (!find_client_shared_conf(source_p
, SHARED_UNKLINE
))
487 if(remove_temp_kline(user
, host
))
489 sendto_one_notice(source_p
,
490 ":Un-klined [%s@%s] from temporary k-lines",
493 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
494 "%s has removed the temporary K-Line for: [%s@%s]",
495 get_oper_name(source_p
), user
, host
);
497 ilog(L_KLINE
, "UK %s %s %s",
498 get_oper_name(source_p
), user
, host
);
502 remove_permkline_match(source_p
, host
, user
);
509 * side effects - kline as given, is added to the hashtable
513 apply_kline(struct Client
*source_p
, struct ConfItem
*aconf
,
514 const char *reason
, const char *oper_reason
, const char *current_date
)
516 add_conf_by_address(aconf
->host
, CONF_KILL
, aconf
->user
, aconf
);
517 write_confitem(KLINE_TYPE
, source_p
, aconf
->user
, aconf
->host
,
518 reason
, oper_reason
, current_date
, 0);
525 * side effects - tkline as given is placed
528 apply_tkline(struct Client
*source_p
, struct ConfItem
*aconf
,
529 const char *reason
, const char *oper_reason
, const char *current_date
, int tkline_time
)
531 aconf
->hold
= CurrentTime
+ tkline_time
;
532 add_temp_kline(aconf
);
534 /* no oper reason.. */
535 if(EmptyString(oper_reason
))
537 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
538 "%s added temporary %d min. K-Line for [%s@%s] [%s]",
539 get_oper_name(source_p
), tkline_time
/ 60,
540 aconf
->user
, aconf
->host
, reason
);
541 ilog(L_KLINE
, "K %s %d %s %s %s",
542 get_oper_name(source_p
), tkline_time
/ 60,
543 aconf
->user
, aconf
->host
, reason
);
547 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
548 "%s added temporary %d min. K-Line for [%s@%s] [%s|%s]",
549 get_oper_name(source_p
), tkline_time
/ 60,
550 aconf
->user
, aconf
->host
, reason
, oper_reason
);
551 ilog(L_KLINE
, "K %s %d %s %s %s|%s",
552 get_oper_name(source_p
), tkline_time
/ 60,
553 aconf
->user
, aconf
->host
, reason
, oper_reason
);
556 sendto_one_notice(source_p
, ":Added temporary %d min. K-Line [%s@%s]",
557 tkline_time
/ 60, aconf
->user
, aconf
->host
);
562 * inputs - client placing kline, user@host, user buffer, host buffer
563 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
567 find_user_host(struct Client
*source_p
, const char *userhost
, char *luser
, char *lhost
)
571 hostp
= strchr(userhost
, '@');
573 if(hostp
!= NULL
) /* I'm a little user@host */
575 *(hostp
++) = '\0'; /* short and squat */
577 strlcpy(luser
, userhost
, USERLEN
+ 1); /* here is my user */
581 strlcpy(lhost
, hostp
, HOSTLEN
+ 1); /* here is my host */
587 /* no '@', no '.', so its not a user@host or host, therefore
588 * its a nick, which support was removed for.
590 if(strchr(userhost
, '.') == NULL
)
593 luser
[0] = '*'; /* no @ found, assume its *@somehost */
595 strlcpy(lhost
, userhost
, HOSTLEN
+ 1);
603 * inputs - user buffer, host buffer
604 * output - 0 if invalid, 1 if valid
608 valid_user_host(struct Client
*source_p
, const char *luser
, const char *lhost
)
610 /* # is invalid, as is '!' (n!u@h kline) */
611 if(strchr(lhost
, '#') || strchr(luser
, '#') || strchr(luser
, '!'))
613 sendto_one_notice(source_p
, ":Invalid K-Line");
622 * input - user buffer, host buffer
623 * output - 0 if invalid, 1 if valid
627 valid_wild_card(struct Client
*source_p
, const char *luser
, const char *lhost
)
633 /* check there are enough non wildcard chars */
635 while ((tmpch
= *p
++))
637 if(!IsKWildChar(tmpch
))
639 /* found enough chars, return */
640 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
645 /* try host, as user didnt contain enough */
647 while ((tmpch
= *p
++))
649 if(!IsKWildChar(tmpch
))
650 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard
)
654 sendto_one_notice(source_p
,
655 ":Please include at least %d non-wildcard "
656 "characters with the user@host",
657 ConfigFileEntry
.min_nonwildcard
);
663 * inputs - pointer to client
664 * - pointer to comment
665 * output - 0 if no valid comment, 1 if valid
666 * side effects - NONE
669 valid_comment(struct Client
*source_p
, char *comment
)
671 if(strchr(comment
, '"'))
673 sendto_one_notice(source_p
, ":Invalid character '\"' in comment");
677 if(strlen(comment
) > REASONLEN
)
678 comment
[REASONLEN
] = '\0';
683 /* already_placed_kline()
685 * inputs - source to notify, user@host to check, tkline time
686 * outputs - 1 if a perm kline or a tkline when a tkline is being
688 * side effects - notifies source_p kline exists
690 /* Note: This currently works if the new K-line is a special case of an
691 * existing K-line, but not the other way round. To do that we would
692 * have to walk the hash and check every existing K-line. -A1kmm.
695 already_placed_kline(struct Client
*source_p
, const char *luser
, const char *lhost
, int tkline
)
698 struct irc_sockaddr_storage iphost
, *piphost
;
699 struct ConfItem
*aconf
;
701 if(ConfigFileEntry
.non_redundant_klines
)
703 if((t
= parse_netmask(lhost
, (struct sockaddr
*)&iphost
, NULL
)) != HM_HOST
)
717 if((aconf
= find_conf_by_address(lhost
, NULL
, NULL
, (struct sockaddr
*)piphost
, CONF_KILL
, t
, luser
)))
719 /* setting a tkline, or existing one is perm */
720 if(tkline
|| ((aconf
->flags
& CONF_FLAGS_TEMPORARY
) == 0))
722 reason
= aconf
->passwd
? aconf
->passwd
: "<No Reason>";
724 sendto_one_notice(source_p
,
725 ":[%s@%s] already K-Lined by [%s@%s] - %s",
726 luser
, lhost
, aconf
->user
,
727 aconf
->host
, reason
);
736 /* remove_permkline_match()
738 * hunts for a permanent kline, and removes it.
741 remove_permkline_match(struct Client
*source_p
, const char *host
, const char *user
)
745 int error_on_write
= NO
;
747 char matchbuf
[BUFSIZE
];
748 char temppath
[BUFSIZE
];
749 const char *filename
;
753 ircsnprintf(temppath
, sizeof(temppath
),
754 "%s.tmp", ConfigFileEntry
.klinefile
);
756 filename
= get_conf_name(KLINE_TYPE
);
758 if((in
= fopen(filename
, "r")) == 0)
760 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
765 if((out
= fopen(temppath
, "w")) == 0)
767 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
775 snprintf(matchbuf
, sizeof(matchbuf
), "\"%s\",\"%s\"", user
, host
);
776 matchlen
= strlen(matchbuf
);
778 while (fgets(buf
, sizeof(buf
), in
))
783 if(!strncasecmp(buf
, matchbuf
, matchlen
))
789 error_on_write
= flush_write(source_p
, out
, buf
, temppath
);
792 /* we dropped out of the loop early because we found a match,
793 * to drop into this somewhat faster loop as we presume we'll never
794 * have two matching klines --anfl
796 if(pairme
&& !error_on_write
)
798 while(fgets(buf
, sizeof(buf
), in
))
803 error_on_write
= flush_write(source_p
, out
, buf
, temppath
);
810 /* The result of the rename should be checked too... oh well */
811 /* If there was an error on a write above, then its been reported
812 * and I am not going to trash the original kline /conf file
816 sendto_one_notice(source_p
, ":Couldn't write temp kline file, aborted");
821 sendto_one_notice(source_p
, ":No K-Line for %s@%s",
825 (void) unlink(temppath
);
830 (void) rename(temppath
, filename
);
833 sendto_one_notice(source_p
, ":K-Line for [%s@%s] is removed",
836 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
837 "%s has removed the K-Line for: [%s@%s]",
838 get_oper_name(source_p
), user
, host
);
840 ilog(L_KLINE
, "UK %s %s %s",
841 get_oper_name(source_p
), user
, host
);
848 * inputs - pointer to client structure of oper requesting unkline
849 * - out is the file descriptor
850 * - buf is the buffer to write
851 * - ntowrite is the expected number of character to be written
852 * - temppath is the temporary file name to be written
853 * output - YES for error on write
855 * side effects - if successful, the buf is written to output file
856 * if a write failure happesn, and the file pointed to
857 * by temppath, if its non NULL, is removed.
859 * The idea here is, to be as robust as possible when writing to the
866 flush_write(struct Client
*source_p
, FILE * out
, const char *buf
, const char *temppath
)
868 int error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
872 sendto_one_notice(source_p
, ":Unable to write to %s",
875 (void) unlink(temppath
);
877 return (error_on_write
);
880 /* remove_temp_kline()
882 * inputs - username, hostname to unkline
884 * side effects - tries to unkline anything that matches
887 remove_temp_kline(const char *user
, const char *host
)
889 struct ConfItem
*aconf
;
891 struct irc_sockaddr_storage addr
, caddr
;
896 mtype
= parse_netmask(host
, (struct sockaddr
*)&addr
, &bits
);
898 for (i
= 0; i
< LAST_TEMP_TYPE
; i
++)
900 DLINK_FOREACH(ptr
, temp_klines
[i
].head
)
904 ktype
= parse_netmask(aconf
->host
, (struct sockaddr
*)&caddr
, &cbits
);
906 if(ktype
!= mtype
|| (user
&& irccmp(user
, aconf
->user
)))
911 if(irccmp(aconf
->host
, host
))
914 else if(bits
!= cbits
||
915 !comp_with_mask_sock((struct sockaddr
*)&addr
,
916 (struct sockaddr
*)&caddr
, bits
))
919 dlinkDestroy(ptr
, &temp_klines
[i
]);
920 delete_one_address_conf(aconf
->host
, aconf
);