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 unsigned char 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 */
67 time_t lastwarning
; /* Last warning about garbage replies sent */
70 /* A lookup in progress for a particular DNSBL for a particular client */
71 struct blacklist_lookup
73 struct blacklist
*bl
; /* Blacklist we're checking */
74 struct auth_client
*auth
; /* Client */
75 struct dns_query
*query
; /* DNS query pointer */
80 /* A blacklist filter */
81 struct blacklist_filter
83 filter_t type
; /* Type of filter */
84 char filter
[HOSTIPLEN
]; /* The filter itself */
89 /* Blacklist user data attached to auth_client instance */
92 rb_dlink_list queries
; /* Blacklist queries in flight */
93 time_t timeout
; /* When this times out */
96 /* public interfaces */
97 static bool blacklists_init(void);
98 static void blacklists_destroy(void);
100 static bool blacklists_start(struct auth_client
*);
101 static void blacklists_cancel(struct auth_client
*);
103 /* private interfaces */
104 static void unref_blacklist(struct blacklist
*);
105 static struct blacklist
*new_blacklist(const char *, const char *, unsigned char, rb_dlink_list
*);
106 static struct blacklist
*find_blacklist(const char *);
107 static bool blacklist_check_reply(struct blacklist_lookup
*, const char *);
108 static void blacklist_dns_callback(const char *, bool, query_type
, void *);
109 static void initiate_blacklist_dnsquery(struct blacklist
*, struct auth_client
*);
110 static void timeout_blacklist_queries_event(void *);
113 static rb_dlink_list blacklist_list
= { NULL
, NULL
, 0 };
114 static struct ev_entry
*timeout_ev
;
115 static EVH timeout_blacklists
;
116 static int blacklist_timeout
= 15;
118 /* private interfaces */
121 unref_blacklist(struct blacklist
*bl
)
123 rb_dlink_node
*ptr
, *nptr
;
126 if (bl
->delete && bl
->refcount
<= 0)
128 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, bl
->filters
.head
)
130 rb_dlinkDelete(ptr
, &bl
->filters
);
134 rb_dlinkFindDestroy(bl
, &blacklist_list
);
139 static struct blacklist
*
140 new_blacklist(const char *name
, const char *reason
, unsigned char iptype
, rb_dlink_list
*filters
)
142 struct blacklist
*bl
;
144 if (name
== NULL
|| reason
== NULL
|| iptype
== 0)
147 if((bl
= find_blacklist(name
)) == NULL
)
149 bl
= rb_malloc(sizeof(struct blacklist
));
150 rb_dlinkAddAlloc(bl
, &blacklist_list
);
155 rb_strlcpy(bl
->host
, name
, IRCD_RES_HOSTLEN
+ 1);
156 rb_strlcpy(bl
->reason
, reason
, BUFSIZE
);
159 rb_dlinkMoveList(filters
, &bl
->filters
);
166 static struct blacklist
*
167 find_blacklist(const char *name
)
171 RB_DLINK_FOREACH(ptr
, blacklist_list
.head
)
173 struct blacklist
*bl
= (struct blacklist
*)ptr
->data
;
175 if (!strcasecmp(bl
->host
, name
))
183 blacklist_check_reply(struct blacklist_lookup
*bllookup
, const char *ipaddr
)
185 struct blacklist
*bl
= bllookup
->bl
;
186 const char *lastoctet
;
189 /* No filters and entry found - thus positive match */
190 if (!rb_dlink_list_length(&bl
->filters
))
193 /* Below will prolly have to change if IPv6 address replies are sent back */
194 if ((lastoctet
= strrchr(ipaddr
, '.')) == NULL
|| *(++lastoctet
) == '\0')
197 RB_DLINK_FOREACH(ptr
, bl
->filters
.head
)
199 struct blacklist_filter
*filter
= ptr
->data
;
202 if (filter
->type
== FILTER_ALL
)
204 else if (filter
->type
== FILTER_LAST
)
208 warn_opers(L_CRIT
, "BUG: Unknown blacklist filter type on blacklist %s: %d",
209 bl
->host
, filter
->type
);
213 if (strcmp(cmpstr
, filter
->filter
) == 0)
220 if (bl
->lastwarning
+ 3600 < rb_current_time())
222 warn_opers(L_WARN
, "Garbage/undecipherable reply received from blacklist %s (reply %s)",
224 bl
->lastwarning
= rb_current_time();
230 blacklist_dns_callback(const char *result
, bool status
, query_type type
, void *data
)
232 struct blacklist_lookup
*bllookup
= (struct blacklist_lookup
*)data
;
233 struct blacklist_user
*bluser
;
234 struct auth_client
*auth
;
236 if (bllookup
== NULL
|| bllookup
->auth
== NULL
)
239 auth
= bllookup
->auth
;
240 bluser
= auth
->data
[PROVIDER_BLACKLIST
];
244 if (result
!= NULL
&& status
&& blacklist_check_reply(bllookup
, result
))
246 /* Match found, so proceed no further */
247 blacklists_cancel(auth
);
248 reject_client(auth
, PROVIDER_BLACKLIST
, bllookup
->bl
->reason
);
252 unref_blacklist(bllookup
->bl
);
253 rb_dlinkDelete(&bllookup
->node
, &bluser
->queries
);
256 if(!rb_dlink_list_length(&bluser
->queries
))
259 provider_done(auth
, PROVIDER_BLACKLIST
);
261 auth
->data
[PROVIDER_BLACKLIST
] = NULL
;
266 initiate_blacklist_dnsquery(struct blacklist
*bl
, struct auth_client
*auth
)
268 struct blacklist_lookup
*bllookup
= rb_malloc(sizeof(struct blacklist_lookup
));
269 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
270 char buf
[IRCD_RES_HOSTLEN
+ 1];
274 bllookup
->auth
= auth
;
276 aftype
= GET_SS_FAMILY(&auth
->c_addr
);
277 if((aftype
== AF_INET
&& (bl
->iptype
& IPTYPE_IPV4
) == 0) ||
278 (aftype
== AF_INET6
&& (bl
->iptype
& IPTYPE_IPV6
) == 0))
279 /* Incorrect blacklist type for this IP... */
282 build_rdns(buf
, sizeof(buf
), &auth
->c_addr
, bl
->host
);
284 bllookup
->query
= lookup_ip(buf
, AF_INET
, blacklist_dns_callback
, bllookup
);
286 rb_dlinkAdd(bllookup
, &bllookup
->node
, &bluser
->queries
);
290 /* Timeout outstanding queries */
292 timeout_blacklist_queries_event(void *notused
)
294 struct auth_client
*auth
;
295 rb_dictionary_iter iter
;
297 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
299 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
301 if(bluser
!= NULL
&& bluser
->timeout
< rb_current_time())
303 blacklists_cancel(auth
);
304 provider_done(auth
, PROVIDER_BLACKLIST
);
310 lookup_all_blacklists(struct auth_client
*auth
)
312 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
315 RB_DLINK_FOREACH(ptr
, blacklist_list
.head
)
317 struct blacklist
*bl
= (struct blacklist
*)ptr
->data
;
320 initiate_blacklist_dnsquery(bl
, auth
);
323 bluser
->timeout
= rb_current_time() + blacklist_timeout
;
326 /* public interfaces */
328 blacklists_start(struct auth_client
*auth
)
330 if(auth
->data
[PROVIDER_BLACKLIST
] != NULL
)
333 if(!rb_dlink_list_length(&blacklist_list
))
334 /* Nothing to do... */
337 auth
->data
[PROVIDER_BLACKLIST
] = rb_malloc(sizeof(struct blacklist_user
));
339 if(is_provider_done(auth
, PROVIDER_RDNS
) && is_provider_done(auth
, PROVIDER_IDENT
))
340 /* This probably can't happen but let's handle this case anyway */
341 lookup_all_blacklists(auth
);
343 set_provider_on(auth
, PROVIDER_BLACKLIST
);
347 /* This is called every time a provider is completed as long as we are marked not done */
349 blacklists_initiate(struct auth_client
*auth
, provider_t provider
)
351 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
353 lrb_assert(provider
!= PROVIDER_BLACKLIST
);
354 lrb_assert(!is_provider_done(auth
, PROVIDER_BLACKLIST
));
355 lrb_assert(rb_dlink_list_length(&blacklist_list
) > 0);
357 if(bluser
== NULL
|| rb_dlink_list_length(&bluser
->queries
))
360 else if(!(is_provider_done(auth
, PROVIDER_RDNS
) && is_provider_done(auth
, PROVIDER_IDENT
)))
361 /* Don't start until we've completed these */
364 lookup_all_blacklists(auth
);
368 blacklists_cancel(struct auth_client
*auth
)
370 rb_dlink_node
*ptr
, *nptr
;
371 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
376 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, bluser
->queries
.head
)
378 struct blacklist_lookup
*bllookup
= ptr
->data
;
379 rb_dlinkDelete(&bllookup
->node
, &bluser
->queries
);
380 unref_blacklist(bllookup
->bl
);
381 cancel_query(bllookup
->query
);
386 auth
->data
[PROVIDER_BLACKLIST
] = NULL
;
390 blacklists_init(void)
392 timeout_ev
= rb_event_addish("timeout_blacklist_queries_event", timeout_blacklist_queries_event
, NULL
, 1);
393 return (timeout_ev
!= NULL
);
397 blacklists_destroy(void)
399 rb_dlink_node
*ptr
, *nptr
;
400 rb_dictionary_iter iter
;
401 struct blacklist
*bl
;
402 struct auth_client
*auth
;
404 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
406 blacklists_cancel(auth
);
409 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, blacklist_list
.head
)
412 if (bl
->refcount
> 0)
417 rb_dlinkDestroy(ptr
, &blacklist_list
);
423 add_conf_blacklist(const char *key
, int parc
, const char **parv
)
425 rb_dlink_list filters
;
426 char *tmp
, *elemlist
= rb_strdup(parv
[2]);
427 unsigned char iptype
;
429 for(char *elem
= rb_strtok_r(elemlist
, ",", &tmp
); elem
; elem
= rb_strtok_r(NULL
, ",", &tmp
))
431 struct blacklist_filter
*filter
= rb_malloc(sizeof(struct blacklist_filter
));
433 filter_t type
= FILTER_LAST
;
436 /* Check blacklist filter type and for validity */
437 for(char *c
= elem
; *c
!= '\0'; c
++)
443 warn_opers(L_CRIT
, "addr_conf_blacklist got a bad filter (too many octets)");
450 else if(!isdigit(*c
))
452 warn_opers(L_CRIT
, "addr_conf_blacklist got a bad filter (invalid character in blacklist filter: %c)", *c
);
458 if(valid
&& dot_c
> 0 && dot_c
< 3)
460 warn_opers(L_CRIT
, "addr_conf_blacklist got a bad filter (insufficient octets)");
471 rb_strlcpy(filter
->filter
, elem
, sizeof(filter
->filter
));
472 rb_dlinkAdd(filter
, &filter
->node
, &filters
);
477 iptype
= atoi(parv
[1]) & 0x3;
478 if(new_blacklist(parv
[0], parv
[3], iptype
, &filters
) == NULL
)
480 rb_dlink_node
*ptr
, *nptr
;
482 warn_opers(L_CRIT
, "addr_conf_blacklist got a malformed blacklist");
484 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, filters
.head
)
487 rb_dlinkDelete(ptr
, &filters
);
493 add_conf_blacklist_timeout(const char *key
, int parc
, const char **parv
)
495 int timeout
= atoi(parv
[0]);
499 warn_opers(L_CRIT
, "BUG: blacklist timeout < 0 (value: %d)", timeout
);
503 blacklist_timeout
= timeout
;
506 struct auth_opts_handler blacklist_options
[] =
508 { "rbl", 4, add_conf_blacklist
},
509 { "rbl_timeout", 1, add_conf_blacklist_timeout
},
513 struct auth_provider blacklist_provider
=
515 .id
= PROVIDER_BLACKLIST
,
516 .init
= blacklists_init
,
517 .destroy
= blacklists_destroy
,
518 .start
= blacklists_start
,
519 .cancel
= blacklists_cancel
,
520 .completed
= blacklists_initiate
,
521 .opt_handlers
= blacklist_options
,