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 int blacklist_timeout
= 15;
117 /* private interfaces */
120 unref_blacklist(struct blacklist
*bl
)
122 rb_dlink_node
*ptr
, *nptr
;
125 if (bl
->delete && bl
->refcount
<= 0)
127 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, bl
->filters
.head
)
129 rb_dlinkDelete(ptr
, &bl
->filters
);
133 rb_dlinkFindDestroy(bl
, &blacklist_list
);
138 static struct blacklist
*
139 new_blacklist(const char *name
, const char *reason
, unsigned char iptype
, rb_dlink_list
*filters
)
141 struct blacklist
*bl
;
143 if (name
== NULL
|| reason
== NULL
|| iptype
== 0)
146 if((bl
= find_blacklist(name
)) == NULL
)
148 bl
= rb_malloc(sizeof(struct blacklist
));
149 rb_dlinkAddAlloc(bl
, &blacklist_list
);
154 rb_strlcpy(bl
->host
, name
, IRCD_RES_HOSTLEN
+ 1);
155 rb_strlcpy(bl
->reason
, reason
, BUFSIZE
);
158 rb_dlinkMoveList(filters
, &bl
->filters
);
165 static struct blacklist
*
166 find_blacklist(const char *name
)
170 RB_DLINK_FOREACH(ptr
, blacklist_list
.head
)
172 struct blacklist
*bl
= (struct blacklist
*)ptr
->data
;
174 if (!strcasecmp(bl
->host
, name
))
182 blacklist_check_reply(struct blacklist_lookup
*bllookup
, const char *ipaddr
)
184 struct blacklist
*bl
= bllookup
->bl
;
185 const char *lastoctet
;
188 /* No filters and entry found - thus positive match */
189 if (!rb_dlink_list_length(&bl
->filters
))
192 /* Below will prolly have to change if IPv6 address replies are sent back */
193 if ((lastoctet
= strrchr(ipaddr
, '.')) == NULL
|| *(++lastoctet
) == '\0')
196 RB_DLINK_FOREACH(ptr
, bl
->filters
.head
)
198 struct blacklist_filter
*filter
= ptr
->data
;
201 if (filter
->type
== FILTER_ALL
)
203 else if (filter
->type
== FILTER_LAST
)
207 warn_opers(L_CRIT
, "BUG: Unknown blacklist filter type on blacklist %s: %d",
208 bl
->host
, filter
->type
);
212 if (strcmp(cmpstr
, filter
->filter
) == 0)
219 if (bl
->lastwarning
+ 3600 < rb_current_time())
221 warn_opers(L_WARN
, "Garbage/undecipherable reply received from blacklist %s (reply %s)",
223 bl
->lastwarning
= rb_current_time();
229 blacklist_dns_callback(const char *result
, bool status
, query_type type
, void *data
)
231 struct blacklist_lookup
*bllookup
= (struct blacklist_lookup
*)data
;
232 struct blacklist_user
*bluser
;
233 struct blacklist
*bl
;
234 struct auth_client
*auth
;
236 if (bllookup
== NULL
|| bllookup
->auth
== NULL
)
240 auth
= bllookup
->auth
;
241 bluser
= auth
->data
[PROVIDER_BLACKLIST
];
245 if (result
!= NULL
&& status
&& blacklist_check_reply(bllookup
, result
))
247 /* Match found, so proceed no further */
248 blacklists_cancel(auth
);
249 reject_client(auth
, PROVIDER_BLACKLIST
, bl
->reason
);
254 cancel_query(bllookup
->query
); /* Ignore future responses */
255 rb_dlinkDelete(&bllookup
->node
, &bluser
->queries
);
258 if(!rb_dlink_list_length(&bluser
->queries
))
262 auth
->data
[PROVIDER_BLACKLIST
] = NULL
;
263 provider_done(auth
, PROVIDER_BLACKLIST
);
268 initiate_blacklist_dnsquery(struct blacklist
*bl
, struct auth_client
*auth
)
270 struct blacklist_lookup
*bllookup
= rb_malloc(sizeof(struct blacklist_lookup
));
271 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
272 char buf
[IRCD_RES_HOSTLEN
+ 1];
276 bllookup
->auth
= auth
;
278 aftype
= GET_SS_FAMILY(&auth
->c_addr
);
279 if((aftype
== AF_INET
&& (bl
->iptype
& IPTYPE_IPV4
) == 0) ||
280 (aftype
== AF_INET6
&& (bl
->iptype
& IPTYPE_IPV6
) == 0))
281 /* Incorrect blacklist type for this IP... */
284 build_rdns(buf
, sizeof(buf
), &auth
->c_addr
, bl
->host
);
286 bllookup
->query
= lookup_ip(buf
, AF_INET
, blacklist_dns_callback
, bllookup
);
288 rb_dlinkAdd(bllookup
, &bllookup
->node
, &bluser
->queries
);
292 /* Timeout outstanding queries */
294 timeout_blacklist_queries_event(void *notused
)
296 struct auth_client
*auth
;
297 rb_dictionary_iter iter
;
299 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
301 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
303 if(bluser
!= NULL
&& bluser
->timeout
< rb_current_time())
305 blacklists_cancel(auth
);
306 provider_done(auth
, PROVIDER_BLACKLIST
);
312 lookup_all_blacklists(struct auth_client
*auth
)
314 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
317 RB_DLINK_FOREACH(ptr
, blacklist_list
.head
)
319 struct blacklist
*bl
= (struct blacklist
*)ptr
->data
;
322 initiate_blacklist_dnsquery(bl
, auth
);
325 bluser
->timeout
= rb_current_time() + blacklist_timeout
;
329 delete_blacklist(struct blacklist
*bl
)
331 if (bl
->refcount
> 0)
335 rb_dlinkFindDestroy(bl
, &blacklist_list
);
341 delete_all_blacklists(void)
343 rb_dlink_node
*ptr
, *nptr
;
345 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, blacklist_list
.head
)
347 delete_blacklist(ptr
->data
);
351 /* public interfaces */
353 blacklists_start(struct auth_client
*auth
)
355 if(auth
->data
[PROVIDER_BLACKLIST
] != NULL
)
358 if(!rb_dlink_list_length(&blacklist_list
))
359 /* Nothing to do... */
362 auth
->data
[PROVIDER_BLACKLIST
] = rb_malloc(sizeof(struct blacklist_user
));
364 if(is_provider_done(auth
, PROVIDER_RDNS
) && is_provider_done(auth
, PROVIDER_IDENT
))
365 /* This probably can't happen but let's handle this case anyway */
366 lookup_all_blacklists(auth
);
368 set_provider_on(auth
, PROVIDER_BLACKLIST
);
372 /* This is called every time a provider is completed as long as we are marked not done */
374 blacklists_initiate(struct auth_client
*auth
, provider_t provider
)
376 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
378 lrb_assert(provider
!= PROVIDER_BLACKLIST
);
379 lrb_assert(!is_provider_done(auth
, PROVIDER_BLACKLIST
));
380 lrb_assert(rb_dlink_list_length(&blacklist_list
) > 0);
382 if(bluser
== NULL
|| rb_dlink_list_length(&bluser
->queries
))
385 else if(!(is_provider_done(auth
, PROVIDER_RDNS
) && is_provider_done(auth
, PROVIDER_IDENT
)))
386 /* Don't start until we've completed these */
389 lookup_all_blacklists(auth
);
393 blacklists_cancel(struct auth_client
*auth
)
395 rb_dlink_node
*ptr
, *nptr
;
396 struct blacklist_user
*bluser
= auth
->data
[PROVIDER_BLACKLIST
];
401 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, bluser
->queries
.head
)
403 struct blacklist_lookup
*bllookup
= ptr
->data
;
405 cancel_query(bllookup
->query
);
406 unref_blacklist(bllookup
->bl
);
408 rb_dlinkDelete(&bllookup
->node
, &bluser
->queries
);
413 auth
->data
[PROVIDER_BLACKLIST
] = NULL
;
417 blacklists_init(void)
419 timeout_ev
= rb_event_addish("timeout_blacklist_queries_event", timeout_blacklist_queries_event
, NULL
, 1);
420 return (timeout_ev
!= NULL
);
424 blacklists_destroy(void)
426 rb_dlink_node
*ptr
, *nptr
;
427 rb_dictionary_iter iter
;
428 struct blacklist
*bl
;
429 struct auth_client
*auth
;
431 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
433 blacklists_cancel(auth
);
436 delete_all_blacklists();
437 rb_event_delete(timeout_ev
);
441 add_conf_blacklist(const char *key
, int parc
, const char **parv
)
443 rb_dlink_list filters
= { NULL
, NULL
, 0 };
444 char *tmp
, *elemlist
= rb_strdup(parv
[2]);
445 unsigned char iptype
;
450 for(char *elem
= rb_strtok_r(elemlist
, ",", &tmp
); elem
; elem
= rb_strtok_r(NULL
, ",", &tmp
))
452 struct blacklist_filter
*filter
= rb_malloc(sizeof(struct blacklist_filter
));
454 filter_t type
= FILTER_LAST
;
457 /* Check blacklist filter type and for validity */
458 for(char *c
= elem
; *c
!= '\0'; c
++)
464 warn_opers(L_CRIT
, "addr_conf_blacklist got a bad filter (too many octets)");
471 else if(!isdigit(*c
))
473 warn_opers(L_CRIT
, "addr_conf_blacklist got a bad filter (invalid character in blacklist filter: %c)", *c
);
479 if(valid
&& dot_c
> 0 && dot_c
< 3)
481 warn_opers(L_CRIT
, "addr_conf_blacklist got a bad filter (insufficient octets)");
492 rb_strlcpy(filter
->filter
, elem
, sizeof(filter
->filter
));
493 rb_dlinkAdd(filter
, &filter
->node
, &filters
);
499 iptype
= atoi(parv
[1]) & 0x3;
500 if(new_blacklist(parv
[0], parv
[3], iptype
, &filters
) == NULL
)
502 rb_dlink_node
*ptr
, *nptr
;
504 warn_opers(L_CRIT
, "addr_conf_blacklist got a malformed blacklist");
506 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, filters
.head
)
509 rb_dlinkDelete(ptr
, &filters
);
515 del_conf_blacklist(const char *key
, int parc
, const char **parv
)
517 struct blacklist
*bl
= find_blacklist(parv
[0]);
520 warn_opers(L_CRIT
, "BUG: tried to remove nonexistent blacklist %s", parv
[0]);
524 delete_blacklist(bl
);
528 del_conf_blacklist_all(const char *key
, int parc
, const char **parv
)
530 delete_all_blacklists();
534 add_conf_blacklist_timeout(const char *key
, int parc
, const char **parv
)
536 int timeout
= atoi(parv
[0]);
540 warn_opers(L_CRIT
, "BUG: blacklist timeout < 0 (value: %d)", timeout
);
544 blacklist_timeout
= timeout
;
547 struct auth_opts_handler blacklist_options
[] =
549 { "rbl", 4, add_conf_blacklist
},
550 { "rbl_del", 1, del_conf_blacklist
},
551 { "rbl_del_all", 0, del_conf_blacklist_all
},
552 { "rbl_timeout", 1, add_conf_blacklist_timeout
},
556 struct auth_provider blacklist_provider
=
558 .id
= PROVIDER_BLACKLIST
,
559 .init
= blacklists_init
,
560 .destroy
= blacklists_destroy
,
561 .start
= blacklists_start
,
562 .cancel
= blacklists_cancel
,
563 .completed
= blacklists_initiate
,
564 .opt_handlers
= blacklist_options
,