Add dynamic lookup support
[exim.git] / src / src / lookups / spf.c
1 /* $Cambridge: exim/src/src/lookups/spf.c,v 1.3 2009/11/11 14:43:28 nm4 Exp $ */
2
3 /*************************************************
4 *     Exim - an Internet mail transport agent    *
5 *************************************************/
6
7 /*
8  * Exim - SPF lookup module using libspf2
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  * Copyright (c) 2005 Chris Webb, Arachsys Internet Services Ltd
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  */
19
20 #include "../exim.h"
21
22 #ifndef EXPERIMENTAL_SPF
23 static void dummy(int x) { dummy(x-1); }
24 #else
25
26 #include "lf_functions.h"
27 #ifndef HAVE_NS_TYPE
28 #define HAVE_NS_TYPE
29 #endif
30 #include <spf2/spf.h>
31 #include <spf2/spf_dns_resolv.h>
32 #include <spf2/spf_dns_cache.h>
33
34 static void *spf_open(uschar *filename, uschar **errmsg) {
35   SPF_server_t *spf_server = NULL;
36   spf_server = SPF_server_new(SPF_DNS_CACHE, 0);
37   if (spf_server == NULL) {
38     *errmsg = US"SPF_server_new() failed";
39     return NULL;
40   }
41   return (void *) spf_server;
42 }
43
44 static void spf_close(void *handle) {
45   SPF_server_t *spf_server = handle;
46   if (spf_server) SPF_server_free(spf_server);
47 }
48
49 static int spf_find(void *handle, uschar *filename, uschar *keystring, int key_len,
50              uschar **result, uschar **errmsg, BOOL *do_cache) {
51   SPF_server_t *spf_server = handle;
52   SPF_request_t *spf_request = NULL;
53   SPF_response_t *spf_response = NULL;
54
55   spf_request = SPF_request_new(spf_server);
56   if (spf_request == NULL) {
57     *errmsg = US"SPF_request_new() failed";
58     return FAIL;
59   }
60
61   if (SPF_request_set_ipv4_str(spf_request, CS filename)) {
62     *errmsg = string_sprintf("invalid IP address '%s'", filename);
63     return FAIL;
64   }
65   if (SPF_request_set_env_from(spf_request, CS keystring)) {
66     *errmsg = string_sprintf("invalid envelope from address '%s'", keystring);
67     return FAIL;
68   }
69
70   SPF_request_query_mailfrom(spf_request, &spf_response);
71   *result = string_copy(US SPF_strresult(SPF_response_result(spf_response)));
72   SPF_response_free(spf_response);
73   SPF_request_free(spf_request);
74   return OK;
75 }
76
77 static lookup_info _lookup_info = {
78   US"spf",                       /* lookup name */
79   0,                             /* not absfile, not query style */
80   spf_open,                      /* open function */
81   NULL,                          /* no check function */
82   spf_find,                      /* find function */
83   spf_close,                     /* close function */
84   NULL,                          /* no tidy function */
85   NULL                           /* no quoting function */
86 };
87
88 #ifdef DYNLOOKUP
89 #define spf_lookup_module_info _lookup_module_info
90 #endif
91
92 static lookup_info *_lookup_list[] = { &_lookup_info };
93 lookup_module_info spf_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
94
95 #endif /* EXPERIMENTAL_SPF */