2 * charybdis: A slightly useful ircd.
3 * blacklist.c: Manages DNS blacklist entries and lookups
5 * Copyright (C) 2006-2008 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.
33 * $Id: blacklist.c 2743 2006-11-10 15:15:00Z jilles $
45 #include "blacklist.h"
47 dlink_list blacklist_list
= { NULL
, NULL
, 0 };
49 /* private interfaces */
50 static struct Blacklist
*find_blacklist(char *name
)
54 DLINK_FOREACH(nptr
, blacklist_list
.head
)
56 struct Blacklist
*blptr
= (struct Blacklist
*) nptr
->data
;
58 if (!irccmp(blptr
->host
, name
))
65 static void blacklist_dns_callback(void *vptr
, struct DNSReply
*reply
)
67 struct BlacklistClient
*blcptr
= (struct BlacklistClient
*) vptr
;
70 if (blcptr
== NULL
|| blcptr
->client_p
== NULL
)
73 if (blcptr
->client_p
->preClient
== NULL
)
75 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
76 "blacklist_dns_callback(): blcptr->client_p->preClient (%s) is NULL", get_client_name(blcptr
->client_p
, HIDE_IP
));
83 /* only accept 127.0.0.x as a listing */
84 if (reply
->addr
.ss_family
== AF_INET
&&
85 !memcmp(&((struct sockaddr_in
*)&reply
->addr
)->sin_addr
, "\177\0\0", 3))
87 else if (blcptr
->blacklist
->lastwarning
+ 3600 < CurrentTime
)
89 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
90 "Garbage reply from blacklist %s",
91 blcptr
->blacklist
->host
);
92 blcptr
->blacklist
->lastwarning
= CurrentTime
;
96 /* they have a blacklist entry for this client */
97 if (listed
&& blcptr
->client_p
->preClient
->dnsbl_listed
== NULL
)
99 blcptr
->client_p
->preClient
->dnsbl_listed
= blcptr
->blacklist
;
100 /* reference to blacklist moves from blcptr to client_p->preClient... */
103 unref_blacklist(blcptr
->blacklist
);
105 dlinkDelete(&blcptr
->node
, &blcptr
->client_p
->preClient
->dnsbl_queries
);
107 /* yes, it can probably happen... */
108 if (dlink_list_length(&blcptr
->client_p
->preClient
->dnsbl_queries
) == 0 && blcptr
->client_p
->flags
& FLAGS_SENTUSER
&& !EmptyString(blcptr
->client_p
->name
))
110 char buf
[USERLEN
+ 1];
111 strlcpy(buf
, blcptr
->client_p
->username
, sizeof buf
);
112 register_local_user(blcptr
->client_p
, blcptr
->client_p
, buf
);
118 /* XXX: no IPv6 implementation, not to concerned right now though. */
119 static void initiate_blacklist_dnsquery(struct Blacklist
*blptr
, struct Client
*client_p
)
121 struct BlacklistClient
*blcptr
= MyMalloc(sizeof(struct BlacklistClient
));
122 char buf
[IRCD_BUFSIZE
];
125 blcptr
->blacklist
= blptr
;
126 blcptr
->client_p
= client_p
;
128 blcptr
->dns_query
.ptr
= blcptr
;
129 blcptr
->dns_query
.callback
= blacklist_dns_callback
;
131 /* XXX: yes I know this is bad, I don't really care right now */
132 sscanf(client_p
->sockhost
, "%d.%d.%d.%d", &ip
[3], &ip
[2], &ip
[1], &ip
[0]);
134 /* becomes 2.0.0.127.torbl.ahbl.org or whatever */
135 ircsnprintf(buf
, IRCD_BUFSIZE
, "%d.%d.%d.%d.%s", ip
[0], ip
[1], ip
[2], ip
[3], blptr
->host
);
137 gethost_byname_type(buf
, &blcptr
->dns_query
, T_A
);
139 dlinkAdd(blcptr
, &blcptr
->node
, &client_p
->preClient
->dnsbl_queries
);
143 /* public interfaces */
144 struct Blacklist
*new_blacklist(char *name
, char *reject_reason
)
146 struct Blacklist
*blptr
;
148 if (name
== NULL
|| reject_reason
== NULL
)
151 blptr
= find_blacklist(name
);
154 blptr
= MyMalloc(sizeof(struct Blacklist
));
155 dlinkAddAlloc(blptr
, &blacklist_list
);
158 blptr
->status
&= ~CONF_ILLEGAL
;
159 strlcpy(blptr
->host
, name
, HOSTLEN
);
160 strlcpy(blptr
->reject_reason
, reject_reason
, IRCD_BUFSIZE
);
161 blptr
->lastwarning
= 0;
166 void unref_blacklist(struct Blacklist
*blptr
)
169 if (blptr
->status
& CONF_ILLEGAL
&& blptr
->refcount
<= 0)
171 dlinkFindDestroy(blptr
, &blacklist_list
);
176 void lookup_blacklists(struct Client
*client_p
)
180 /* We don't do IPv6 right now, sorry! */
181 if (client_p
->localClient
->ip
.ss_family
== AF_INET6
)
184 DLINK_FOREACH(nptr
, blacklist_list
.head
)
186 struct Blacklist
*blptr
= (struct Blacklist
*) nptr
->data
;
188 if (!(blptr
->status
& CONF_ILLEGAL
))
189 initiate_blacklist_dnsquery(blptr
, client_p
);
193 void abort_blacklist_queries(struct Client
*client_p
)
195 dlink_node
*ptr
, *next_ptr
;
196 struct BlacklistClient
*blcptr
;
198 if (client_p
->preClient
== NULL
)
200 DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->preClient
->dnsbl_queries
.head
)
203 dlinkDelete(&blcptr
->node
, &client_p
->preClient
->dnsbl_queries
);
204 unref_blacklist(blcptr
->blacklist
);
205 delete_resolver_queries(&blcptr
->dns_query
);
210 void destroy_blacklists(void)
212 dlink_node
*ptr
, *next_ptr
;
213 struct Blacklist
*blptr
;
215 DLINK_FOREACH_SAFE(ptr
, next_ptr
, blacklist_list
.head
)
218 blptr
->hits
= 0; /* keep it simple and consistent */
219 if (blptr
->refcount
> 0)
220 blptr
->status
|= CONF_ILLEGAL
;
224 dlinkDestroy(ptr
, &blacklist_list
);