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>
50 static void filter_msg_user(void *data
);
51 static void filter_msg_channel(void *data
);
52 static void on_client_exit(void *data
);
54 static void mo_setfilter(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
55 static void me_setfilter(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
57 static char *filter_data
= NULL
;
58 static size_t filter_data_len
= 0;
59 static hs_database_t
*filter_db
;
60 static hs_scratch_t
*filter_scratch
;
62 static int filter_enable
= 1;
64 static const char *cmdname
[MESSAGE_TYPE_COUNT
] = {
65 [MESSAGE_TYPE_PRIVMSG
] = "PRIVMSG",
66 [MESSAGE_TYPE_NOTICE
] = "NOTICE",
75 #define ACT_DROP (1 << 0)
76 #define ACT_KILL (1 << 1)
77 #define ACT_ALARM (1 << 2)
79 static enum filter_state state
= FILTER_EMPTY
;
80 static char check_str
[21] = "";
82 mapi_hfn_list_av1 filter_hfnlist
[] = {
83 { "privmsg_user", (hookfn
) filter_msg_user
},
84 { "privmsg_channel", (hookfn
) filter_msg_channel
},
85 { "client_exit", (hookfn
) on_client_exit
},
90 struct Message setfilter_msgtab
= {
91 "SETFILTER", 0, 0, 0, 0,
92 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, {me_setfilter
, 2}, {mo_setfilter
, 2}}
99 hs_free_scratch(filter_scratch
);
101 hs_free_database(filter_db
);
103 rb_free(filter_data
);
107 mapi_clist_av1 filter_clist
[] = { &setfilter_msgtab
, NULL
};
109 DECLARE_MODULE_AV1(filter
, NULL
, moddeinit
, filter_clist
, NULL
, filter_hfnlist
, "0.3");
112 setfilter(const char *check
, const char *data
, const char **error
)
114 if (error
) *error
= "unknown";
116 if (!strcasecmp(data
, "disable")) {
118 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
119 "Filtering disabled.");
122 if (!strcasecmp(data
, "enable")) {
124 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
125 "Filtering enabled.");
129 if (strlen(check
) > sizeof check_str
- 1) {
130 if (error
) *error
= "check string too long";
134 if (!strcasecmp(data
, "new")) {
135 if (state
== FILTER_FILLING
) {
136 rb_free(filter_data
);
140 state
= FILTER_FILLING
;
141 strcpy(check_str
, check
);
145 if (strcmp(check
, check_str
) != 0) {
146 if (error
) *error
= "check strings don't match";
150 if (!strcasecmp(data
, "apply")) {
151 if (state
!= FILTER_FILLING
) {
152 if (error
) *error
= "not loading anything";
156 hs_error_t r
= hs_deserialize_database(filter_data
, filter_data_len
, &db
);
157 if (r
!= HS_SUCCESS
) {
158 if (error
) *error
= "couldn't deserialize db";
161 r
= hs_alloc_scratch(db
, &filter_scratch
);
162 if (r
!= HS_SUCCESS
) {
163 if (error
) *error
= "couldn't allocate scratch";
167 hs_free_database(filter_db
);
169 state
= FILTER_LOADED
;
171 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
172 "New filters loaded.");
173 rb_free(filter_data
);
179 if (state
== FILTER_FILLING
) {
181 unsigned char *d
= rb_base64_decode(data
, strlen(data
), &dl
);
183 if (error
) *error
= "invalid data";
186 if (filter_data_len
+ dl
> 10000000ul) {
187 if (error
) *error
= "data over size limit";
191 filter_data
= rb_realloc(filter_data
, filter_data_len
+ dl
);
192 memcpy(filter_data
+ filter_data_len
, d
, dl
);
194 filter_data_len
+= dl
;
196 if (error
) *error
= "send \"new\" first";
202 /* /SETFILTER [server-mask] <check> { NEW | APPLY | <data> }
203 * <check> must be the same for the entirety of a new...data...apply run,
204 * and exists just to ensure runs don't mix
205 * NEW prepares a buffer to receive a hyperscan database
206 * <data> is base64 encoded chunks of hyperscan database, which are decoded
207 * and appended to the buffer
208 * APPLY deserialises the buffer and sets the resulting hyperscan database
209 * as the one to use for filtering */
211 mo_setfilter(struct MsgBuf
*msgbuf
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
216 if (!IsOperAdmin(source_p
)) {
217 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "admin");
223 if(match(parv
[1], me
.name
)) {
226 sendto_match_servs(source_p
, parv
[1],
228 "ENCAP %s SETFILTER %s :%s", parv
[1], check
, data
);
229 } else if (parc
== 3) {
234 sendto_one_notice(source_p
, ":SETFILTER needs 2 or 3 params, have %d", parc
- 1);
239 int r
= setfilter(check
, data
, &error
);
241 sendto_one_notice(source_p
, ":SETFILTER failed: %s", error
);
243 sendto_one_notice(source_p
, ":SETFILTER ok");
249 me_setfilter(struct MsgBuf
*msgbuf
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
251 if(!IsPerson(source_p
))
255 int r
= setfilter(parv
[1], parv
[2], &error
);
257 sendto_one_notice(source_p
, ":SETFILTER failed: %s", error
);
263 /* will be called for every match
264 * hyperscan provides us one piece of information about the expression
265 * matched, an integer ID. we're co-opting the lowest 3 bits of this
266 * as a flag set. conveniently, this means all we really need to do
267 * here is or the IDs together. */
268 int match_callback(unsigned id
,
269 unsigned long long from
,
270 unsigned long long to
,
274 unsigned *context
= context_
;
280 unsigned match_message(const char *msg
)
287 hs_error_t r
= hs_scan(filter_db
, msg
, strlen(msg
), 0, filter_scratch
, match_callback
, &state
);
288 if (r
!= HS_SUCCESS
&& r
!= HS_SCAN_TERMINATED
)
293 static char check_buffer
[2000];
294 static char clean_buffer
[BUFSIZE
];
297 filter_msg_user(void *data_
)
299 hook_data_privmsg_user
*data
= data_
;
300 struct Client
*s
= data
->source_p
;
301 /* we only need to filter once */
305 /* opers are immune to checking, for obvious reasons
306 * anything sent to an oper is also immune, because that should make it
307 * less impossible to deal with reports. */
308 if (IsOper(s
) || IsOper(data
->target_p
)) {
311 char *text
= strcpy(clean_buffer
, data
->text
);
313 strip_unprintable(text
);
314 snprintf(check_buffer
, sizeof check_buffer
, ":%s!%s@%s#%c %s 0 :%s",
330 s
->user
&& s
->user
->suser
[0] != '\0' ? '1' : '0',
331 cmdname
[data
->msgtype
],
333 unsigned r
= match_message(check_buffer
);
335 sendto_one_numeric(s
, ERR_CANNOTSENDTOCHAN
,
336 form_str(ERR_CANNOTSENDTOCHAN
),
337 data
->target_p
->name
);
341 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
342 "FILTER: %s!%s@%s [%s]",
343 s
->name
, s
->username
, s
->host
, s
->sockhost
);
347 exit_client(NULL
, s
, s
, "Excess flood");
352 filter_msg_channel(void *data_
)
354 hook_data_privmsg_channel
*data
= data_
;
355 struct Client
*s
= data
->source_p
;
356 /* we only need to filter once */
360 /* just normal oper immunity for channels. i'd like to have a mode that
361 * disables the filter per-channel, but that's for the future */
365 char *text
= strcpy(clean_buffer
, data
->text
);
367 strip_unprintable(text
);
368 snprintf(check_buffer
, sizeof check_buffer
, ":%s!%s@%s#%c %s %s :%s",
384 s
->user
&& s
->user
->suser
[0] != '\0' ? '1' : '0',
385 cmdname
[data
->msgtype
],
388 unsigned r
= match_message(check_buffer
);
390 sendto_one_numeric(s
, ERR_CANNOTSENDTOCHAN
,
391 form_str(ERR_CANNOTSENDTOCHAN
),
392 data
->chptr
->chname
);
396 sendto_realops_snomask(SNO_GENERAL
, L_ALL
| L_NETWIDE
,
397 "FILTER: %s!%s@%s [%s]",
398 s
->name
, s
->username
, s
->host
, s
->sockhost
);
402 exit_client(NULL
, s
, s
, "Excess flood");
407 on_client_exit(void *data_
)
409 /* If we see a netsplit, abort the current FILTER_FILLING attempt */
410 hook_data_client_exit
*data
= data_
;
412 if (!IsServer(data
->target
)) return;
414 if (state
== FILTER_FILLING
) {
415 state
= filter_db
? FILTER_LOADED
: FILTER_EMPTY
;