1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
#include <unistd.h>
#include "alloc.h"
#include "byte.h"
#include "case.h"
#include "fmt.h"
#include "ip.h"
#include "scan.h"
#include "str.h"
#include "stralloc.h"
#include "dns.h"
#include "ipalloc.h"
#include "ipme.h"
#include "now.h"
#include "spf.h"
// shared by spf.c + spfdnsip.c
extern stralloc dnsname;
extern char ip4remote[4];
extern char ip6remote[16];
extern int flagip6;
/**
@brief match_ip
compares IPv4/IPv6 addreses up to prefix length
@param input: ip_address1,prefix length, ip_address2
@return 1 ok; 0 failure
*/
int match_ip4(unsigned char ip1[4], int prefix, char ip2[4])
{
stralloc iptest1 = {0};
stralloc iptest2 = {0};
if (flagip6) return 0;
if (ip4_bytestring(&iptest1, ip1, prefix) == prefix)
if (ip4_bytestring(&iptest2, ip2, prefix) == prefix)
if (byte_diff(iptest1.s, prefix, iptest2.s)) return 0;
return 1;
}
int match_ip6(unsigned char ip1[16], int prefix, char ip2[16])
{
stralloc iptest1 = {0};
stralloc iptest2 = {0};
if (!flagip6) return 0;
if (ip6_bytestring(&iptest1, ip1, prefix) == prefix)
if (ip6_bytestring(&iptest2, ip2, prefix) == prefix)
if (byte_diff(iptest1.s, prefix, iptest2.s)) return 0;
return 1;
}
/**
@brief get_prefix
return integer value of prefix length
@param input: pointer to prefix
@return (int) length of prefix
*/
int get_prefix(char *prefix)
{
unsigned long r;
int pos;
if (!prefix || *prefix == '0') {
if (flagip6 == 0) return 32;
if (flagip6 == 1) return 128;
}
pos = scan_ulong(prefix, &r);
if (!pos || (prefix[pos] && !(prefix[pos] == '/'))) return SPF_SYNTAX;
if (flagip6 == 0 && r > 32) return SPF_SYNTAX;
if (flagip6 == 1 && r > 128) return SPF_SYNTAX;
return r;
}
/* DNS Record: -------------------------------------- Fetch multiple SPF TXT RRs */
/**
@brief spf_records
get TXT records for domain and extract SPF information
@param input: pointer stralloc domain
output: pointer to stralloc spf records
@return SPF_OK, SPF_NONE; SPF_MULTIRR, SPF_DNSSOFT, SPF_NOMEM
*/
int spf_records(stralloc *spfrec, stralloc *domain)
{
static stralloc out = {0};
static stralloc spf = {0};
int i, k;
int begin;
int r = 0;
begin = -1;
DNS_INIT
r = dns_txt(&out, (const stralloc *)domain);
switch (r) {
case DNS_MEM: return SPF_NOMEM;
case DNS_ERR: return SPF_DNSSOFT; /* return 2main */
case DNS_NXD: return SPF_NONE;
}
r = SPF_NONE;
for (k = 0; k < out.len; ++k) {
if (case_starts(out.s + k, "v=spf1")) {
begin = k;
break;
}
}
if (begin >= 0) {
if (case_starts(out.s + k + 6, "v=spf1")) return SPF_MULTIRR; /* return 2main */
if (!stralloc_copys(&spf, "")) return SPF_NOMEM;
for (i = begin; i < out.len; ++i) {
if (out.s[i] == '\r' || out.s[i] == '\n' || out.s[i] == '\0') break;
if (!stralloc_append(&spf, out.s + i)) return SPF_NOMEM;
}
if (!stralloc_0(&spf)) return SPF_NOMEM;
if (!stralloc_copys(spfrec, spf.s)) return SPF_NOMEM;
r = SPF_OK;
}
return r;
}
/* Mechanisms: -------------------------------------- Lookup functions */
/**
@brief spf_a (a; a:fqdns; a:fqdns/56)
compares A + AAAA records for SPF info and client host
@param input: pointer to spfspecification, pointer to prefix
@return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM
*/
int spf_a(char *spfspec, char *prefix)
{
stralloc sa = {0};
stralloc ip = {0};
int ipprefix, r, j;
ipprefix = get_prefix(prefix);
if (ipprefix < 0) return SPF_SYNTAX;
if (!stralloc_copys(&sa, spfspec)) return SPF_NOMEM;
if (!stralloc_readyplus(&ip, 0)) return SPF_NOMEM;
if (!spf_info("MA/AAAA=", spfspec)) return SPF_NOMEM;
DNS_INIT
switch (dns_ip4(&ip, &sa)) {
case DNS_MEM: return SPF_NOMEM;
case DNS_ERR: r = SPF_DNSSOFT; break;
case DNS_NXD: r = SPF_NONE; break;
default:
r = SPF_NONE;
for (j = 0; j + 4 <= ip.len; j += 4)
if (match_ip4(ip.s + j, ipprefix, ip4remote)) return SPF_OK;
}
switch (dns_ip6(&ip, &sa)) {
case DNS_MEM: return SPF_NOMEM;
case DNS_ERR: r = SPF_DNSSOFT; break;
case DNS_NXD: r = SPF_NONE; break;
default:
r = SPF_NONE;
for (j = 0; j + 16 <= ip.len; j += 16)
if (match_ip6(ip.s + j, ipprefix, ip6remote)) return SPF_OK;
}
return r;
}
/**
@brief spf_mx (mx; mx:domain; mx:domain/24)
compares MX records for SPF info and client host
@param input: pointer to spfspecification, pointer to prefix
@return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM
*/
int spf_mx(char *spfspec, char *prefix)
{
stralloc sa = {0};
ipalloc ia = {0};
unsigned long random;
int ipprefix;
int j, r;
ipprefix = get_prefix(prefix);
if (ipprefix < 0) return SPF_SYNTAX;
random = now() + (getpid() << 16);
if (!stralloc_copys(&sa, spfspec)) return SPF_NOMEM;
if (!spf_info("MMX=", spfspec)) return SPF_NOMEM;
switch (dns_mxip(&ia, &sa, random)) {
case DNS_MEM: return SPF_NOMEM;
case DNS_ERR: return SPF_DNSSOFT;
default:
r = SPF_NONE;
for (j = 0; j < ia.len; ++j) {
if (byte_diff(ip6remote, 16, V6localnet) && !ip6_isv4mapped(ip6remote)) {
if (match_ip6(ia.ix[j].addr.ip6.d, ipprefix, ip6remote)) return SPF_OK;
}
if (byte_diff(ip4remote, 4, V4localnet)) {
if (match_ip4(ia.ix[j].addr.ip4.d, ipprefix, ip4remote)) return SPF_OK;
}
}
}
return r;
}
/**
@brief spf_ptr (ptr; ptr:fqdn)
compares PTR records from SPF info and client host
@param input: pointer to spfspecification; prefix not used
@return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM
*/
int spf_ptr(char *spfspec, char *prefix)
{
stralloc fqdn = {0};
stralloc out = {0};
stralloc ip = {0};
int slen = str_len(spfspec);
int rc, r;
int k = 0;
int pos;
int l = 0;
/* we didn't find host with the matching IP before */
if (dnsname.len == 7 && str_equal(dnsname.s, "unknown")) return SPF_NONE;
if (!spf_info("MPTR=", spfspec)) return SPF_NOMEM;
/* the hostname found will probably be the same as before */
while (dnsname.len) {
pos = dnsname.len - slen;
if (pos < 0) break;
if (pos > 0 && dnsname.s[pos - 1] != '.') break;
if (case_diffb(dnsname.s + pos, slen, spfspec)) break;
return SPF_OK;
}
/* ok, either it's the first test or it's a very weired setup
Assumptions:
ip -> inverse DNS name (only one!)
inverse DNS name -> (same) ip (only one!)
*/
if (!stralloc_readyplus(&fqdn, 255)) return SPF_NOMEM;
if (!stralloc_readyplus(&out, 255)) return SPF_NOMEM;
if (!stralloc_readyplus(&ip, 32)) return SPF_NOMEM;
if (flagip6) {
rc = dns_name6(&out, ip6remote); // usually: 2. . .ip6.addr => only one
switch (rc) {
case DNS_MEM: return SPF_NOMEM;
case DNS_COM: r = SPF_DNSSOFT; break;
case DNS_ERR: r = SPF_NONE; break;
case DNS_NXD: r = SPF_NONE; break;
default:
r = SPF_NONE;
l++;
if (l > LOOKUP_LIMIT) {
r = SPF_ERROR;
break;
}
switch (dns_ip6(&ip, &out)) { // theoretical more IPs cound be retrieved
case DNS_MEM: return SPF_NOMEM;
case DNS_ERR: r = SPF_DNSSOFT; break;
case DNS_NXD: r = SPF_NONE; break;
default:
r = SPF_NONE;
for (k = 0; k + 16 <= ip.len; k += 16) {
if (k > 32 * LOOKUP_LIMIT) {
r = SPF_ERROR;
break;
}
if (match_ip6(ip.s + k, 128, ip6remote)) {
if (!dnsname.len)
if (!stralloc_copy(&dnsname, &out)) return SPF_NOMEM;
pos = out.len - slen;
if (pos < 0) continue;
if (pos > 0 && out.s[pos - 1] != '.') continue;
if (case_diffb(out.s + pos, slen, spfspec)) continue;
if (!stralloc_copy(&dnsname, &out)) return SPF_NOMEM;
r = SPF_OK;
}
}
}
}
} else { // IP4 branch
rc = dns_name4(&out, ip4remote); // usual answer: d.c.b.e.in-arpa.addr for IP4 a.b.c.d => only one
switch (rc) {
case DNS_MEM: return SPF_NOMEM;
case DNS_ERR: r = SPF_DNSSOFT; break;
case DNS_NXD: r = SPF_NONE; break;
default:
r = SPF_NONE;
l++;
if (l > LOOKUP_LIMIT) {
r = SPF_ERROR;
break;
}
switch (dns_ip4(&ip, &out)) {
case DNS_MEM: return SPF_NOMEM;
case DNS_ERR: r = SPF_DNSSOFT; break;
case DNS_NXD: r = SPF_NONE; break;
default:
r = SPF_NONE;
for (k = 0; k + 4 <= ip.len; k += 4) {
if (k > 32 * LOOKUP_LIMIT) {
r = SPF_ERROR;
break;
}
if (match_ip4(ip.s + k, 32, ip4remote)) {
if (!dnsname.len)
if (!stralloc_copy(&dnsname, &out)) return SPF_NOMEM;
pos = out.len - slen;
if (pos < 0) continue;
if (pos > 0 && out.s[pos - 1] != '.') continue;
if (case_diffb(out.s + pos, slen, spfspec)) continue;
if (!stralloc_copy(&dnsname, &out)) return SPF_NOMEM;
r = SPF_OK;
}
}
}
}
}
if (!dnsname.len)
if (!stralloc_copys(&dnsname, "unknown")) return SPF_NOMEM;
return r;
}
/**
@brief spf_ip4 (ip4; ip4:fqdn; ip4:fqdn/24)
compares A records for SPF info and client host
@param input: pointer to spfspecification, pointer to prefix
@return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM
*/
int spf_ip4(char *spfspec, char *prefix)
{
char spfip[4];
if (flagip6) return SPF_NONE;
int ipprefix = get_prefix(prefix);
if (ipprefix < 0) return SPF_SYNTAX;
if (!ip4_scan(spfspec, spfip)) return SPF_SYNTAX;
if (!spf_info("MIPv4=", spfspec)) return SPF_NOMEM;
if (!match_ip4(spfip, ipprefix, ip4remote)) return SPF_NONE;
return SPF_OK;
}
/**
@brief spf_ip6 (ip6; ip6:fqdn; ip6:fqdn/56)
compares AAAA records for SPF info and client host
@param input: pointer to spfspecification, pointer to prefix
@return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM
*/
int spf_ip6(char *spfspec, char *prefix)
{
char spfip[16];
if (!flagip6) return SPF_NONE;
int ipprefix = get_prefix(prefix);
if (ipprefix < 0) return SPF_SYNTAX;
if (!ip6_scan(spfspec, spfip)) return SPF_SYNTAX;
if (!spf_info("MIPv6=", spfspec)) return SPF_NOMEM;
if (!match_ip6(spfip, ipprefix, ip6remote)) return SPF_NONE;
return SPF_OK;
}
/**
@brief spf_exists (exists; exists:fqdn)
simply looks for a A records only for SPF info and client host
@param input: pointer to spfspecification, prefix not used
@return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM
*/
int spf_exists(char *spfspec, char *prefix)
{
stralloc sa = {0};
stralloc ip = {0};
if (!stralloc_copys(&sa, spfspec)) return SPF_NOMEM;
if (!spf_info("MExists=", spfspec)) return SPF_NOMEM;
switch (dns_ip4(&ip, &sa)) {
case DNS_MEM: return SPF_NOMEM;
case DNS_ERR: return SPF_DNSSOFT;
case DNS_NXD: return SPF_NONE;
default: return SPF_OK;
}
}
|