Merge commit 'pan/master' into not-linux
[apps/pfixtools.git] / common / rbl.c
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
10 /*  1. Redistributions of source code must retain the above copyright         */
11 /*     notice, this list of conditions and the following disclaimer.          */
12 /*  2. Redistributions in binary form must reproduce the above copyright      */
13 /*     notice, this list of conditions and the following disclaimer in the    */
14 /*     documentation and/or other materials provided with the distribution.   */
15 /*  3. The names of its contributors may not be used to endorse or promote    */
16 /*     products derived from this software without specific prior written     */
17 /*     permission.                                                            */
18 /*                                                                            */
19 /*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
20 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
21 /*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
22 /*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
23 /*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
24 /*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
25 /*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
26 /*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
27 /*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
28 /*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
29 /*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
30 /******************************************************************************/
31
32 /*
33  * Copyright © 2008 Florent Bruneau
34  */
35
36 #include <unbound.h>
37 #include <netdb.h>
38 #include "array.h"
39 #include "server.h"
40 #include "rbl.h"
41
42
43 typedef struct rbl_context_t {
44     rbl_result_t *result;
45     rbl_result_callback_t call;
46     void *data;
47 } rbl_context_t;
48 ARRAY(rbl_context_t);
49
50 static struct ub_ctx *ctx = NULL;
51 static server_t *async_event = NULL;
52 static PA(rbl_context_t) ctx_pool = ARRAY_INIT;
53
54 static rbl_context_t *rbl_context_new(void)
55 {
56     return p_new(rbl_context_t, 1);
57 }
58
59 static void rbl_context_delete(rbl_context_t **context)
60 {
61     if (*context) {
62         p_delete(context);
63     }
64 }
65
66 static void rbl_context_wipe(rbl_context_t *context)
67 {
68     p_clear(context, 1);
69 }
70
71 static rbl_context_t *rbl_context_acquire(void)
72 {
73     if (array_len(ctx_pool) > 0) {
74         return array_pop_last(ctx_pool);
75     } else {
76         return rbl_context_new();
77     }
78 }
79
80 static void rbl_context_release(rbl_context_t *context)
81 {
82     rbl_context_wipe(context);
83     array_add(ctx_pool, context);
84 }
85
86 static void rbl_exit(void)
87 {
88     if (ctx != NULL) {
89         ub_ctx_delete(ctx);
90         ctx = NULL;
91     }
92     if (async_event != NULL) {
93         async_event->fd = -1;
94         server_release(async_event);
95         async_event = NULL;
96     }
97     array_deep_wipe(ctx_pool, rbl_context_delete);
98 }
99 module_exit(rbl_exit);
100
101 static void rbl_callback(void *arg, int err, struct ub_result *result)
102 {
103     rbl_context_t *context = arg;
104     if (err != 0) {
105         debug("asynchronous request led to an error");
106         *context->result = RBL_ERROR;
107     } else if (result->nxdomain) {
108         debug("asynchronous request done, %s NOT FOUND", result->qname);
109         *context->result = RBL_NOTFOUND;
110     } else {
111         debug("asynchronous request done, %s FOUND", result->qname);
112         *context->result = RBL_FOUND;
113     }
114     if (context->call != NULL) {
115         debug("calling callback");
116         context->call(context->result, context->data);
117     } else {
118         debug("no callback defined");
119     }
120     ub_resolve_free(result);
121     rbl_context_release(context);
122 }
123
124 static int rbl_handler(server_t *event, void *config)
125 {
126     int retval = 0;
127     debug("rbl_handler called: ub_fd triggered");
128     server_none(event);
129     if ((retval = ub_process(ctx)) != 0) {
130         err("error in DNS resolution: %s", ub_strerror(retval));
131     }
132     server_ro(event);
133     return 0;
134 }
135
136 static inline bool rbl_dns_check(const char *hostname, rbl_result_t *result,
137                                  rbl_result_callback_t callback, void *data)
138 {
139     if (ctx == NULL) {
140         ctx = ub_ctx_create();
141         ub_ctx_async(ctx, true);
142         if ((async_event = server_register(ub_fd(ctx), rbl_handler, NULL)) == NULL) {
143             crit("cannot register asynchronous DNS event handler");
144             abort();
145         }
146     }
147     rbl_context_t *context = rbl_context_acquire();
148     context->result = result;
149     context->call   = callback;
150     context->data   = data;
151     debug("running dns resolution on %s", hostname);
152     if (ub_resolve_async(ctx, (char*)hostname, 1, 1, context, rbl_callback, NULL) == 0) {
153         *result = RBL_ASYNC;
154         return true;
155     } else {
156         *result = RBL_ERROR;
157         rbl_context_release(context);
158         return false;
159     }
160 }
161
162 bool rbl_check(const char *rbl, uint32_t ip, rbl_result_t *result,
163                rbl_result_callback_t callback, void *data)
164 {
165     char host[257];
166     int len;
167
168     len = snprintf(host, 257, "%d.%d.%d.%d.%s.",
169                    ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff,
170                    rbl);
171     if (len >= (int)sizeof(host))
172         return RBL_ERROR;
173     if (host[len - 2] == '.')
174         host[len - 1] = '\0';
175     return rbl_dns_check(host, result, callback, data);
176 }
177
178 bool rhbl_check(const char *rhbl, const char *hostname, rbl_result_t *result,
179                 rbl_result_callback_t callback, void *data)
180 {
181     char host[257];
182     int len;
183
184     len = snprintf(host, 257, "%s.%s.", hostname, rhbl);
185     if (len >= (int)sizeof(host))
186         return RBL_ERROR;
187     if (host[len - 2] == '.')
188         host[len - 1] = '\0';
189     return rbl_dns_check(host, result, callback, data);
190 }