2 * ircd-ratbox: A slightly useful ircd.
3 * filter.c: Drop messages we don't like
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
35 #include "s_newconf.h"
40 #include "inline/stringops.h"
43 #include <hs_common.h>
44 #include <hs_runtime.h>
46 static void filter_msg_user(void *data
);
47 static void filter_msg_channel(void *data
);
48 static void on_client_exit(void *data
);
50 static void mo_setfilter(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
51 static void me_setfilter(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
53 static char *filter_data
= NULL
;
54 static size_t filter_data_len
= 0;
55 static hs_database_t
*filter_db
;
56 static hs_scratch_t
*filter_scratch
;
58 static int filter_enable
= 1;
60 static const char *cmdname
[MESSAGE_TYPE_COUNT
] = {
61 [MESSAGE_TYPE_PRIVMSG
] = "PRIVMSG",
62 [MESSAGE_TYPE_NOTICE
] = "NOTICE",
71 #define ACT_DROP (1 << 0)
72 #define ACT_KILL (1 << 1)
73 #define ACT_ALARM (1 << 2)
75 static enum filter_state state
= FILTER_EMPTY
;
76 static char check_str
[21] = "";
78 mapi_hfn_list_av1 filter_hfnlist
[] = {
79 { "privmsg_user", (hookfn
) filter_msg_user
},
80 { "privmsg_channel", (hookfn
) filter_msg_channel
},
81 { "client_exit", (hookfn
) on_client_exit
},
86 struct Message setfilter_msgtab
= {
87 "SETFILTER", 0, 0, 0, 0,
88 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, {me_setfilter
, 2}, {mo_setfilter
, 2}}
95 hs_free_scratch(filter_scratch
);
97 hs_free_database(filter_db
);
103 mapi_clist_av1 filter_clist
[] = { &setfilter_msgtab
, NULL
};
105 DECLARE_MODULE_AV1(filter
, NULL
, moddeinit
, filter_clist
, NULL
, filter_hfnlist
, "$Revision$");
108 setfilter(const char *check
, const char *data
, const char **error
)
110 if (error
) *error
= "unknown";
112 if (!strcasecmp(data
, "disable")) {
114 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
115 "Filtering disabled.");
118 if (!strcasecmp(data
, "enable")) {
120 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
121 "Filtering enabled.");
125 if (strlen(check
) > sizeof check_str
- 1) {
126 if (error
) *error
= "check string too long";
130 if (!strcasecmp(data
, "new")) {
131 if (state
== FILTER_FILLING
) {
132 rb_free(filter_data
);
136 state
= FILTER_FILLING
;
137 strcpy(check_str
, check
);
141 if (strcmp(check
, check_str
) != 0) {
142 if (error
) *error
= "check strings don't match";
146 if (!strcasecmp(data
, "apply")) {
147 if (state
!= FILTER_FILLING
) {
148 if (error
) *error
= "not loading anything";
152 hs_error_t r
= hs_deserialize_database(filter_data
, filter_data_len
, &db
);
153 if (r
!= HS_SUCCESS
) {
154 if (error
) *error
= "couldn't deserialize db";
157 r
= hs_alloc_scratch(db
, &filter_scratch
);
158 if (r
!= HS_SUCCESS
) {
159 if (error
) *error
= "couldn't allocate scratch";
163 hs_free_database(filter_db
);
165 state
= FILTER_LOADED
;
167 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
168 "New filters loaded.");
169 rb_free(filter_data
);
175 if (state
== FILTER_FILLING
) {
177 unsigned char *d
= rb_base64_decode(data
, strlen(data
), &dl
);
179 if (error
) *error
= "invalid data";
182 if (filter_data_len
+ dl
> 10000000ul) {
183 if (error
) *error
= "data over size limit";
187 filter_data
= rb_realloc(filter_data
, filter_data_len
+ dl
);
188 memcpy(filter_data
+ filter_data_len
, d
, dl
);
190 filter_data_len
+= dl
;
192 if (error
) *error
= "send \"new\" first";
198 /* /SETFILTER [server-mask] <check> { NEW | APPLY | <data> }
199 * <check> must be the same for the entirety of a new...data...apply run,
200 * and exists just to ensure runs don't mix
201 * NEW prepares a buffer to receive a hyperscan database
202 * <data> is base64 encoded chunks of hyperscan database, which are decoded
203 * and appended to the buffer
204 * APPLY deserialises the buffer and sets the resulting hyperscan database
205 * as the one to use for filtering */
207 mo_setfilter(struct MsgBuf
*msgbuf
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
212 if (!IsOperAdmin(source_p
)) {
213 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "admin");
219 if(match(parv
[1], me
.name
)) {
222 sendto_match_servs(source_p
, parv
[1],
224 "ENCAP %s SETFILTER %s :%s", parv
[1], check
, data
);
225 } else if (parc
== 3) {
230 sendto_one_notice(source_p
, ":SETFILTER needs 2 or 3 params, have %d", parc
- 1);
235 int r
= setfilter(check
, data
, &error
);
237 sendto_one_notice(source_p
, ":SETFILTER failed: %s", error
);
239 sendto_one_notice(source_p
, ":SETFILTER ok");
245 me_setfilter(struct MsgBuf
*msgbuf
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
247 if(!IsPerson(source_p
))
251 int r
= setfilter(parv
[1], parv
[2], &error
);
253 sendto_one_notice(source_p
, ":SETFILTER failed: %s", error
);
259 /* will be called for every match
260 * hyperscan provides us one piece of information about the expression
261 * matched, an integer ID. we're co-opting the lowest 3 bits of this
262 * as a flag set. conveniently, this means all we really need to do
263 * here is or the IDs together. */
264 int match_callback(unsigned id
,
265 unsigned long long from
,
266 unsigned long long to
,
270 unsigned *context
= context_
;
276 unsigned match_message(const char *msg
)
283 hs_error_t r
= hs_scan(filter_db
, msg
, strlen(msg
), 0, filter_scratch
, match_callback
, &state
);
284 if (r
!= HS_SUCCESS
&& r
!= HS_SCAN_TERMINATED
)
289 static char check_buffer
[2000];
292 filter_msg_user(void *data_
)
294 hook_data_privmsg_user
*data
= data_
;
295 struct Client
*s
= data
->source_p
;
296 /* we only need to filter once */
300 /* opers are immune to checking, for obvious reasons
301 * anything sent to an oper is also immune, because that should make it
302 * less impossible to deal with reports. */
303 if (IsOper(s
) || IsOper(data
->target_p
)) {
306 char *text
= rb_strdup(data
->text
);
308 strip_unprintable(text
);
309 snprintf(check_buffer
, sizeof check_buffer
, ":%s!%s@%s %s %s :%s",
310 s
->name
, s
->username
, s
->host
,
311 cmdname
[data
->msgtype
],
312 data
->target_p
->name
,
314 unsigned r
= match_message(check_buffer
);
316 if (r
& (ACT_DROP
| ACT_KILL
)) {
320 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
321 "FILTER: %s!%s@%s [%s]",
322 s
->name
, s
->username
, s
->host
, s
->sockhost
);
325 exit_client(NULL
, s
, s
, "Excess flood");
330 filter_msg_channel(void *data_
)
332 hook_data_privmsg_channel
*data
= data_
;
333 struct Client
*s
= data
->source_p
;
334 /* we only need to filter once */
338 /* just normal oper immunity for channels. i'd like to have a mode that
339 * disables the filter per-channel, but that's for the future */
343 char *text
= rb_strdup(data
->text
);
345 strip_unprintable(text
);
346 snprintf(check_buffer
, sizeof check_buffer
, ":%s!%s@%s %s %s :%s",
347 s
->name
, s
->username
, s
->host
,
348 cmdname
[data
->msgtype
],
351 unsigned r
= match_message(check_buffer
);
353 if (r
& (ACT_DROP
| ACT_KILL
)) {
357 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
358 "FILTER: %s!%s@%s [%s]",
359 s
->name
, s
->username
, s
->host
, s
->sockhost
);
362 exit_client(NULL
, s
, s
, "Excess flood");
367 on_client_exit(void *data_
)
369 /* If we see a netsplit, abort the current FILTER_FILLING attempt */
370 hook_data_client_exit
*data
= data_
;
372 if (!IsServer(data
->target
)) return;
374 if (state
== FILTER_FILLING
) {
375 state
= filter_db
? FILTER_LOADED
: FILTER_EMPTY
;