-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_spf_record_match.rb
102 lines (72 loc) · 1.95 KB
/
check_spf_record_match.rb
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
#!/usr/bin/env ruby
require 'rubygems'
require 'optparse'
require 'resolv'
EXIT_CODES = {
:unknown => 3,
:critical => 2,
:warning => 1,
:ok => 0
}
options =
{
:debug => false,
:domains => []
}
opt_parser = OptionParser.new do |opt|
opt.on("--domains domain,[domain]","which domains do you wish to report on?") do |domains|
options[:domains] = domains.split(',')
end
opt.on("--debug","enable debug mode") do
options[:debug] = true
end
opt.on("-h","--help","help") do
puts opt_parser
exit
end
end
opt_parser.parse!
raise OptionParser::MissingArgument, 'Missing "--domains"' if (options[:domains].empty?)
if (options[:debug])
puts 'Options: '+options.inspect
end
begin
record_variants = {}
options[:domains].each do |domain|
txt = Resolv::DNS.open do |dns|
records = dns.getresources(domain, Resolv::DNS::Resource::IN::TXT)
if (records.empty?)
(record_variants["record missing"] ||= []) << domain
puts "no TXT records for #{domain}" if (options[:debug])
next
end
txt_strings = []
records.each do |record|
txt_strings.concat record.strings
end
spf_record_n = txt_strings.index{|s| s.downcase.include?'spf1'}
if (!spf_record_n)
puts "no spf record for #{domain}" if (options[:debug])
(record_variants["record missing"] ||= []) << domain
next
end
spf_record = txt_strings[spf_record_n]
(record_variants[spf_record] ||= []) << domain
puts "record for #{domain}: #{spf_record}" if (options[:debug])
end
if (record_variants.length > 1)
puts 'CRIT: Multiple variants of the SPF record:'
record_variants.each do |record, domains|
puts record + ': ' + domains.join(',')
end
exit EXIT_CODES[:critical]
end
end
rescue SystemExit
raise
rescue Exception => e
puts 'CRIT: Unexpected error: ' + e.message + ' <' + e.backtrace[0] + '>'
exit EXIT_CODES[:critical]
end
puts 'OK: All records in sync.'
exit EXIT_CODES[:ok]