6 #include <arpa/inet.h> // in_addr required by spf2/spf.h
7 #include <arpa/nameser.h>
17 Result::Result(int value
)
20 case SPF_RESULT_INVALID
: value_
= INVALID
; break;
21 case SPF_RESULT_NEUTRAL
: value_
= NEUTRAL
; break;
22 case SPF_RESULT_PASS
: value_
= PASS
; break;
23 case SPF_RESULT_FAIL
: value_
= FAIL
; break;
24 case SPF_RESULT_SOFTFAIL
: value_
= SOFTFAIL
; break;
25 case SPF_RESULT_NONE
: value_
= NONE
; break;
26 case SPF_RESULT_TEMPERROR
: value_
= TEMPERROR
; break;
27 case SPF_RESULT_PERMERROR
: value_
= PERMERROR
; break;
29 LOG(ERROR
) << "unrecognized SPF_result_t value: " << value
;
33 char const* Result::c_str(value_t value
)
36 case INVALID
: return "invalid";
37 case NEUTRAL
: return "neutral";
38 case PASS
: return "pass";
39 case FAIL
: return "fail";
40 case SOFTFAIL
: return "softfail";
41 case NONE
: return "none";
42 case TEMPERROR
: return "temperror";
43 case PERMERROR
: return "permerror";
45 LOG(ERROR
) << "unknown Result value";
46 return "** unknown **";
50 std::ostream
& operator<<(std::ostream
& os
, Result result
)
52 return os
<< result
.c_str();
55 std::ostream
& operator<<(std::ostream
& os
, Result::value_t result
)
57 return os
<< Result::c_str(result
);
60 Server::Server(char const* fqdn
)
61 : srv_(CHECK_NOTNULL(SPF_server_new(SPF_DNS_RESOLV
, 1)))
63 CHECK_EQ(SPF_E_SUCCESS
, SPF_server_set_rec_dom(srv_
, CHECK_NOTNULL(fqdn
)));
69 SPF_server_free(srv_
);
72 Server::initializer::initializer()
74 // Hook info libspf2's error procs.
75 SPF_error_handler
= log_error_
;
76 SPF_warning_handler
= log_warning_
;
77 SPF_info_handler
= log_info_
;
78 SPF_debug_handler
= nullptr;
81 Request::Request(Server
const& srv
)
82 : req_(CHECK_NOTNULL(SPF_request_new(srv
.srv_
)))
89 SPF_request_free(req_
);
92 void Request::set_ip_str(char const* ip
)
94 if (IP4::is_address(ip
)) {
97 else if (IP6::is_address(ip
)) {
101 LOG(FATAL
) << "non IP address passed to set_ip_str: " << ip
;
104 void Request::set_ipv4_str(char const* ipv4
)
106 CHECK_EQ(SPF_E_SUCCESS
, SPF_request_set_ipv4_str(req_
, ipv4
));
108 void Request::set_ipv6_str(char const* ipv6
)
110 CHECK_EQ(SPF_E_SUCCESS
, SPF_request_set_ipv6_str(req_
, ipv6
));
112 void Request::set_helo_dom(char const* dom
)
114 CHECK_EQ(SPF_E_SUCCESS
, SPF_request_set_helo_dom(req_
, dom
));
116 void Request::set_env_from(char const* frm
)
118 CHECK_EQ(SPF_E_SUCCESS
, SPF_request_set_env_from(req_
, frm
));
121 char const* Request::get_sender_dom() const
123 auto sender_dom
= req_
->env_from_dp
;
124 if (sender_dom
== nullptr)
125 sender_dom
= req_
->helo_dom
;
129 Response::Response(Request
const& req
)
131 // We ignore the return code from this call, as everything we need
132 // to know is in the SPF_response_t struct.
133 SPF_request_query_mailfrom(req
.req_
, &res_
);
137 Response::~Response()
140 SPF_response_free(res_
);
143 Result
Response::result() const { return Result(SPF_response_result(res_
)); }
145 char const* Response::smtp_comment() const
147 return SPF_response_get_smtp_comment(res_
);
150 char const* Response::header_comment() const
152 return SPF_response_get_header_comment(res_
);
155 char const* Response::received_spf() const
157 return SPF_response_get_received_spf(res_
);
161 SPF::Server::initializer
SPF::Server::init
;