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