1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
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 */
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 /******************************************************************************/
33 * Copyright © 2008 Florent Bruneau
43 typedef struct rbl_context_t {
45 rbl_result_callback_t call;
50 static struct ub_ctx *ctx = NULL;
51 static client_t *async_event = NULL;
52 static PA(rbl_context_t) ctx_pool = ARRAY_INIT;
54 static rbl_context_t *rbl_context_new(void)
56 return p_new(rbl_context_t, 1);
59 static void rbl_context_delete(rbl_context_t **context)
66 static void rbl_context_wipe(rbl_context_t *context)
71 static rbl_context_t *rbl_context_acquire(void)
73 if (array_len(ctx_pool) > 0) {
74 return array_pop_last(ctx_pool);
76 return rbl_context_new();
80 static void rbl_context_release(rbl_context_t *context)
82 rbl_context_wipe(context);
83 array_add(ctx_pool, context);
86 static void rbl_exit(void)
92 if (async_event != NULL) {
93 client_release(async_event);
96 array_deep_wipe(ctx_pool, rbl_context_delete);
98 module_exit(rbl_exit);
100 static void rbl_callback(void *arg, int err, struct ub_result *result)
102 rbl_context_t *context = arg;
104 debug("asynchronous request led to an error");
105 *context->result = RBL_ERROR;
106 } else if (result->nxdomain) {
107 debug("asynchronous request done, %s NOT FOUND", result->qname);
108 *context->result = RBL_NOTFOUND;
110 debug("asynchronous request done, %s FOUND", result->qname);
111 *context->result = RBL_FOUND;
113 if (context->call != NULL) {
114 context->call(context->result, context->data);
116 ub_resolve_free(result);
117 rbl_context_release(context);
120 static int rbl_handler(client_t *event, void *config)
123 debug("rbl_handler called: ub_fd triggered");
124 client_io_none(event);
125 if ((retval = ub_process(ctx)) != 0) {
126 err("error in DNS resolution: %s", ub_strerror(retval));
132 static inline bool rbl_dns_check(const char *hostname, rbl_result_t *result,
133 rbl_result_callback_t callback, void *data)
136 ctx = ub_ctx_create();
137 ub_ctx_async(ctx, true);
138 if ((async_event = client_register(ub_fd(ctx), rbl_handler, NULL)) == NULL) {
139 crit("cannot register asynchronous DNS event handler");
143 rbl_context_t *context = rbl_context_acquire();
144 context->result = result;
145 context->call = callback;
146 context->data = data;
147 debug("running dns resolution on %s", hostname);
148 if (ub_resolve_async(ctx, (char*)hostname, 1, 1, context, rbl_callback, NULL) == 0) {
153 rbl_context_release(context);
158 bool rbl_check(const char *rbl, uint32_t ip, rbl_result_t *result,
159 rbl_result_callback_t callback, void *data)
164 len = snprintf(host, 257, "%d.%d.%d.%d.%s.",
165 ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff,
167 if (len >= (int)sizeof(host))
169 if (host[len - 2] == '.')
170 host[len - 1] = '\0';
171 return rbl_dns_check(host, result, callback, data);
174 bool rhbl_check(const char *rhbl, const char *hostname, rbl_result_t *result,
175 rbl_result_callback_t callback, void *data)
180 len = snprintf(host, 257, "%s.%s.", hostname, rhbl);
181 if (len >= (int)sizeof(host))
183 if (host[len - 2] == '.')
184 host[len - 1] = '\0';
185 return rbl_dns_check(host, result, callback, data);