Debian Codegraph @

lualib/lua_ffi/spf.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 --[[[
18 -- @module lua_ffi/spf
19 -- This module contains ffi interfaces to SPF
20 --]]
21
22 local ffi = require 'ffi'
23
24 ffi.cdef [[
25 enum spf_mech_e {
26 SPF_FAIL,
27 SPF_SOFT_FAIL,
28 SPF_PASS,
29 SPF_NEUTRAL
30 };
31 static const unsigned RSPAMD_SPF_FLAG_IPV6 = (1 << 0);
32 static const unsigned RSPAMD_SPF_FLAG_IPV4 = (1 << 1);
33 static const unsigned RSPAMD_SPF_FLAG_ANY = (1 << 3);
34 struct spf_addr {
35 unsigned char addr6[16];
36 unsigned char addr4[4];
37 union {
38 struct {
39 uint16_t mask_v4;
40 uint16_t mask_v6;
41 } dual;
42 uint32_t idx;
43 } m;
44 unsigned flags;
45 enum spf_mech_e mech;
46 char *spf_string;
47 struct spf_addr *prev, *next;
48 };
49
50 struct spf_resolved {
51 char *domain;
52 unsigned ttl;
53 int temp_failed;
54 int na;
55 int perm_failed;
56 uint64_t digest;
57 struct GArray *elts;
58 struct ref_entry_s ref;
59 };
60
61 typedef void (*spf_cb_t)(struct spf_resolved *record,
62 struct rspamd_task *task, void *data);
63 struct rspamd_task;
64 int rspamd_spf_resolve(struct rspamd_task *task, spf_cb_t callback,
65 void *cbdata);
66 const char * rspamd_spf_get_domain (struct rspamd_task *task);
67 struct spf_resolved * spf_record_ref (struct spf_resolved *rec);
68 void spf_record_unref (struct spf_resolved *rec);
69 char * spf_addr_mask_to_string (struct spf_addr *addr);
70 struct spf_addr * spf_addr_match_task (struct rspamd_task *task, struct spf_resolved *rec);
71 ]]
72
73 local function convert_mech(mech)
74 if mech == ffi.C.SPF_FAIL then
75 return 'fail'
76 elseif mech == ffi.C.SPF_SOFT_FAIL then
77 return 'softfail'
78 elseif mech == ffi.C.SPF_PASS then
79 return 'pass'
80 elseif mech == ffi.C.SPF_NEUTRAL then
81 return 'neutral'
82 end
83 end
84
85 local NULL = ffi.new 'void*'
86
87 local function spf_addr_tolua(ffi_spf_addr)
88 local ipstr = ffi.C.spf_addr_mask_to_string(ffi_spf_addr)
89 local ret = {
90 res = convert_mech(ffi_spf_addr.mech),
91 ipnet = ffi.string(ipstr),
92 }
93
94 if ffi_spf_addr.spf_string ~= NULL then
95 ret.spf_str = ffi.string(ffi_spf_addr.spf_string)
96 end
97
98 ffi.C.g_free(ipstr)
99 return ret
100 end
101
102 local function spf_resolve(task, cb)
103 local function spf_cb(rec, _, _)
104 if not rec then
105 cb(false, 'record is empty')
106 else
107 local nelts = rec.elts.len
108 local elts = ffi.cast("struct spf_addr *", rec.elts.data)
109 local res = {
110 addrs = {}
111 }
112 local digstr = ffi.new("char[64]")
113 ffi.C.rspamd_snprintf(digstr, 64, "0x%xuL", rec.digest)
114 res.digest = ffi.string(digstr)
115 for i = 1, nelts do
116 res.addrs[i] = spf_addr_tolua(elts[i - 1])
117 end
118
119 local matched = ffi.C.spf_addr_match_task(task:topointer(), rec)
120
121 if matched ~= NULL then
122 cb(true, res, spf_addr_tolua(matched))
123 else
124 cb(true, res, nil)
125 end
126 end
127 end
128
129 local ret = ffi.C.rspamd_spf_resolve(task:topointer(), spf_cb, nil)
130
131 if not ret then
132 cb(false, 'cannot perform resolving')
133 end
134 end
135
136 local function spf_unref(rec)
137 ffi.C.spf_record_unref(rec)
138 end
139
140 return {
141 spf_resolve = spf_resolve,
142 spf_unref = spf_unref
143 }