2 * charybdis: A slightly useful ircd.
3 * blacklist.c: Manages DNS blacklist entries and lookups
5 * Copyright (C) 2006-2011 charybdis development team
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 /* Originally written for charybdis circa 2006 (by nenolod?).
35 * Tweaked for authd. Some functions and structs renamed. Public/private
36 * interfaces have been shifted around. Some code has been cleaned up too.
37 * -- Elizafox 24 March 2016
52 /* Blacklist accepted IP types */
56 /* A configured DNSBL */
59 char host
[IRCD_RES_HOSTLEN
+ 1];
60 char reason
[BUFSIZE
]; /* Reason template (ircd fills in the blanks) */
61 uint8_t iptype
; /* IP types supported */
62 rb_dlink_list filters
; /* Filters for queries */
64 bool delete; /* If true delete when no clients */
65 int refcount
; /* When 0 and delete is set, remove this blacklist */
68 time_t lastwarning
; /* Last warning about garbage replies sent */
71 /* A lookup in progress for a particular DNSBL for a particular client */
72 struct blacklist_lookup
74 struct blacklist
*bl
; /* Blacklist we're checking */
75 struct auth_client
*auth
; /* Client */
76 struct dns_query
*query
; /* DNS query pointer */
81 /* A blacklist filter */
82 struct blacklist_filter
84 filter_t type
; /* Type of filter */
85 char filter
[HOSTIPLEN
]; /* The filter itself */
90 /* Blacklist user data attached to auth_client instance */
93 rb_dlink_list queries
; /* Blacklist queries in flight */
96 /* public interfaces */
97 static void blacklists_destroy(void);
99 static bool blacklists_start(struct auth_client
*);
100 static void blacklists_cancel(struct auth_client
*);
102 /* private interfaces */
103 static void unref_blacklist(struct blacklist
*);
104 static struct blacklist
*new_blacklist(const char *, const char *, uint8_t, rb_dlink_list
*);
105 static struct blacklist
*find_blacklist(const char *);
106 static bool blacklist_check_reply(struct blacklist_lookup
*, const char *);
107 static void blacklist_dns_callback(const char *, bool, query_type
, void *);
108 static void initiate_blacklist_dnsquery(struct blacklist
*, struct auth_client
*);
111 static rb_dlink_list blacklist_list
= { NULL
, NULL
, 0 };
112 static int blacklist_timeout
= 15;
114 /* private interfaces */
117 unref_blacklist(struct blacklist
*bl
)
119 rb_dlink_node
*ptr
, *nptr
;
122 if (bl
->delete && bl
->refcount
<= 0)
124 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, bl
->filters
.head
)
126 rb_dlinkDelete(ptr
, &bl
->filters
);
130 rb_dlinkFindDestroy(bl
, &blacklist_list
);
135 static struct blacklist
*
136 new_blacklist(const char *name
, const char *reason
, uint8_t iptype
, rb_dlink_list
*filters
)
138 struct blacklist
*bl
;
140 if (name
== NULL
|| reason
== NULL
|| iptype
== 0)
143 if((bl
= find_blacklist(name
)) == NULL
)
145 bl
= rb_malloc(sizeof(struct blacklist
));
146 rb_dlinkAddAlloc(bl
, &blacklist_list
);
151 rb_strlcpy(bl
->host
, name
, IRCD_RES_HOSTLEN
+ 1);
152 rb_strlcpy(bl
->reason
, reason
, BUFSIZE
);
155 rb_dlinkMoveList(filters
, &bl
->filters
);
162 static struct blacklist
*
163 find_blacklist(const char *name
)
167 RB_DLINK_FOREACH(ptr
, blacklist_list
.head
)
169 struct blacklist
*bl
= (struct blacklist
*)ptr
->data
;
171 if (!strcasecmp(bl
->host
, name
))
179 blacklist_check_reply(struct blacklist_lookup
*bllookup
, const char *ipaddr
)
181 struct blacklist
*bl
= bllookup
->bl
;
182 const char *lastoctet
;
185 /* No filters and entry found - thus positive match */
186 if (!rb_dlink_list_length(&bl
->filters
))
189 /* Below will prolly have to change if IPv6 address replies are sent back */
190 if ((lastoctet
= strrchr(ipaddr
, '.')) == NULL
|| *(++lastoctet
) == '\0')
193 RB_DLINK_FOREACH(ptr
, bl
->filters
.head
)
195 struct blacklist_filter
*filter
= ptr
->data
;
198 if (filter
->type
== FILTER_ALL
)
200 else if (filter
->type
== FILTER_LAST
)
204 warn_opers(L_CRIT
, "Blacklist: Unknown blacklist filter type (host %s): %d",
205 bl
->host
, filter
->type
);
206 exit(EX_PROVIDER_ERROR
);
209 if (strcmp(cmpstr
, filter
->filter
) == 0)
216 if (bl
->lastwarning
+ 3600 < rb_current_time())
218 warn_opers(L_WARN
, "Garbage/undecipherable reply received from blacklist %s (reply %s)",
220 bl
->lastwarning
= rb_current_time();
227 blacklist_dns_callback(const char *result
, bool status
, query_type type
, void *data
)
229 struct blacklist_lookup
*bllookup
= (struct blacklist_lookup
*)data
;
230 struct blacklist_user
*bluser
;
231 struct blacklist
*bl
;
232 struct auth_client
*auth
;
234 lrb_assert(bllookup
!= NULL
);
235 lrb_assert(bllookup
->auth
!= NULL
);
238 auth
= bllookup
->auth
;
240 if((bluser
= get_provider_data(auth
, PROVIDER_BLACKLIST
)) == NULL
)
243 if (result
!= NULL
&& status
&& blacklist_check_reply(bllookup
, result
))
245 /* Match found, so proceed no further */
247 blacklists_cancel(auth
);
248 reject_client(auth
, PROVIDER_BLACKLIST
, bl
->host
, bl
->reason
);
253 cancel_query(bllookup
->query
); /* Ignore future responses */
254 rb_dlinkDelete(&bllookup
->node
, &bluser
->queries
);
257 if(!rb_dlink_list_length(&bluser
->queries
))
260 notice_client(auth
->cid
, "*** IP not found in DNS blacklist%s",
261 rb_dlink_list_length(&blacklist_list
) > 1 ? "s" : "");
263 set_provider_data(auth
, PROVIDER_BLACKLIST
, NULL
);
264 set_provider_timeout_absolute(auth
, PROVIDER_BLACKLIST
, 0);
265 provider_done(auth
, PROVIDER_BLACKLIST
);
270 initiate_blacklist_dnsquery(struct blacklist
*bl
, struct auth_client
*auth
)
272 struct blacklist_lookup
*bllookup
= rb_malloc(sizeof(struct blacklist_lookup
));
273 struct blacklist_user
*bluser
= get_provider_data(auth
, PROVIDER_BLACKLIST
);
274 char buf
[IRCD_RES_HOSTLEN
+ 1];
278 bllookup
->auth
= auth
;
280 aftype
= GET_SS_FAMILY(&auth
->c_addr
);
281 if((aftype
== AF_INET
&& (bl
->iptype
& IPTYPE_IPV4
) == 0) ||
282 (aftype
== AF_INET6
&& (bl
->iptype
& IPTYPE_IPV6
) == 0))
283 /* Incorrect blacklist type for this IP... */
289 build_rdns(buf
, sizeof(buf
), &auth
->c_addr
, bl
->host
);
291 bllookup
->query
= lookup_ip(buf
, AF_INET
, blacklist_dns_callback
, bllookup
);
293 rb_dlinkAdd(bllookup
, &bllookup
->node
, &bluser
->queries
);
298 lookup_all_blacklists(struct auth_client
*auth
)
300 struct blacklist_user
*bluser
= get_provider_data(auth
, PROVIDER_BLACKLIST
);
303 notice_client(auth
->cid
, "*** Checking your IP against DNS blacklist%s",
304 rb_dlink_list_length(&blacklist_list
) > 1 ? "s" : "");
306 RB_DLINK_FOREACH(ptr
, blacklist_list
.head
)
308 struct blacklist
*bl
= (struct blacklist
*)ptr
->data
;
311 initiate_blacklist_dnsquery(bl
, auth
);
314 set_provider_timeout_relative(auth
, PROVIDER_BLACKLIST
, blacklist_timeout
);
318 delete_blacklist(struct blacklist
*bl
)
320 if (bl
->refcount
> 0)
324 rb_dlinkFindDestroy(bl
, &blacklist_list
);
330 delete_all_blacklists(void)
332 rb_dlink_node
*ptr
, *nptr
;
334 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, blacklist_list
.head
)
336 delete_blacklist(ptr
->data
);
340 /* public interfaces */
342 blacklists_start(struct auth_client
*auth
)
344 lrb_assert(get_provider_data(auth
, PROVIDER_BLACKLIST
) == NULL
);
346 if(!rb_dlink_list_length(&blacklist_list
))
348 /* Nothing to do... */
349 notice_client(auth
->cid
, "*** No DNS blacklists configured, not checking your IP");
353 set_provider_data(auth
, PROVIDER_BLACKLIST
, rb_malloc(sizeof(struct blacklist_user
)));
355 if(is_provider_done(auth
, PROVIDER_RDNS
) && is_provider_done(auth
, PROVIDER_IDENT
))
356 /* This probably can't happen but let's handle this case anyway */
357 lookup_all_blacklists(auth
);
359 set_provider_running(auth
, PROVIDER_BLACKLIST
);
363 /* This is called every time a provider is completed as long as we are marked not done */
365 blacklists_initiate(struct auth_client
*auth
, provider_t provider
)
367 struct blacklist_user
*bluser
= get_provider_data(auth
, PROVIDER_BLACKLIST
);
369 lrb_assert(provider
!= PROVIDER_BLACKLIST
);
370 lrb_assert(!is_provider_done(auth
, PROVIDER_BLACKLIST
));
371 lrb_assert(rb_dlink_list_length(&blacklist_list
) > 0);
373 if(bluser
== NULL
|| rb_dlink_list_length(&bluser
->queries
))
376 else if(!(is_provider_done(auth
, PROVIDER_RDNS
) && is_provider_done(auth
, PROVIDER_IDENT
)))
377 /* Don't start until we've completed these */
380 lookup_all_blacklists(auth
);
384 blacklists_cancel(struct auth_client
*auth
)
386 rb_dlink_node
*ptr
, *nptr
;
387 struct blacklist_user
*bluser
= get_provider_data(auth
, PROVIDER_BLACKLIST
);
392 if(rb_dlink_list_length(&bluser
->queries
))
394 notice_client(auth
->cid
, "*** Aborting DNS blacklist queries");
396 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, bluser
->queries
.head
)
398 struct blacklist_lookup
*bllookup
= ptr
->data
;
400 cancel_query(bllookup
->query
);
401 unref_blacklist(bllookup
->bl
);
403 rb_dlinkDelete(&bllookup
->node
, &bluser
->queries
);
409 set_provider_data(auth
, PROVIDER_BLACKLIST
, NULL
);
410 set_provider_timeout_absolute(auth
, PROVIDER_BLACKLIST
, 0);
411 provider_done(auth
, PROVIDER_BLACKLIST
);
415 blacklists_destroy(void)
417 rb_dictionary_iter iter
;
418 struct auth_client
*auth
;
420 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
422 blacklists_cancel(auth
);
425 delete_all_blacklists();
429 add_conf_blacklist(const char *key
, int parc
, const char **parv
)
431 rb_dlink_list filters
= { NULL
, NULL
, 0 };
432 char *tmp
, *elemlist
= rb_strdup(parv
[2]);
438 for(char *elem
= rb_strtok_r(elemlist
, ",", &tmp
); elem
; elem
= rb_strtok_r(NULL
, ",", &tmp
))
440 struct blacklist_filter
*filter
= rb_malloc(sizeof(struct blacklist_filter
));
442 filter_t type
= FILTER_LAST
;
444 /* Check blacklist filter type and for validity */
445 for(char *c
= elem
; *c
!= '\0'; c
++)
451 warn_opers(L_CRIT
, "Blacklist: addr_conf_blacklist got a bad filter (too many octets)");
452 exit(EX_PROVIDER_ERROR
);
457 else if(!isdigit(*c
))
459 warn_opers(L_CRIT
, "Blacklist: addr_conf_blacklist got a bad filter (invalid character in blacklist filter: %c)",
461 exit(EX_PROVIDER_ERROR
);
465 if(dot_c
> 0 && dot_c
< 3)
467 warn_opers(L_CRIT
, "Blacklist: addr_conf_blacklist got a bad filter (insufficient octets)");
468 exit(EX_PROVIDER_ERROR
);
472 rb_strlcpy(filter
->filter
, elem
, sizeof(filter
->filter
));
473 rb_dlinkAdd(filter
, &filter
->node
, &filters
);
479 iptype
= atoi(parv
[1]) & 0x3;
480 if(new_blacklist(parv
[0], parv
[3], iptype
, &filters
) == NULL
)
482 warn_opers(L_CRIT
, "Blacklist: addr_conf_blacklist got a malformed blacklist");
483 exit(EX_PROVIDER_ERROR
);
488 del_conf_blacklist(const char *key
, int parc
, const char **parv
)
490 struct blacklist
*bl
= find_blacklist(parv
[0]);
493 /* Not fatal for now... */
494 warn_opers(L_WARN
, "Blacklist: tried to remove nonexistent blacklist %s", parv
[0]);
498 delete_blacklist(bl
);
502 del_conf_blacklist_all(const char *key
, int parc
, const char **parv
)
504 delete_all_blacklists();
508 add_conf_blacklist_timeout(const char *key
, int parc
, const char **parv
)
510 int timeout
= atoi(parv
[0]);
514 warn_opers(L_CRIT
, "Blacklist: blacklist timeout < 0 (value: %d)", timeout
);
515 exit(EX_PROVIDER_ERROR
);
518 blacklist_timeout
= timeout
;
523 blacklist_stats(uint32_t rid
, char letter
)
527 RB_DLINK_FOREACH(ptr
, blacklist_list
.head
)
529 struct blacklist
*bl
= ptr
->data
;
534 stats_result(rid
, letter
, "%s %hhu %u", bl
->host
, bl
->iptype
, bl
->hits
);
537 stats_done(rid
, letter
);
541 struct auth_opts_handler blacklist_options
[] =
543 { "rbl", 4, add_conf_blacklist
},
544 { "rbl_del", 1, del_conf_blacklist
},
545 { "rbl_del_all", 0, del_conf_blacklist_all
},
546 { "rbl_timeout", 1, add_conf_blacklist_timeout
},
550 struct auth_provider blacklist_provider
=
552 .id
= PROVIDER_BLACKLIST
,
553 .destroy
= blacklists_destroy
,
554 .start
= blacklists_start
,
555 .cancel
= blacklists_cancel
,
556 .timeout
= blacklists_cancel
,
557 .completed
= blacklists_initiate
,
558 .opt_handlers
= blacklist_options
,
559 /* .stats_handler = { 'B', blacklist_stats }, */