summaryrefslogtreecommitdiff
path: root/lib/custodian/protocoltest/dnsbl.rb
diff options
context:
space:
mode:
authorSteve Kemp <steve@steve.org.uk>2013-08-05 20:38:30 +0100
committerSteve Kemp <steve@steve.org.uk>2013-08-05 20:38:30 +0100
commit73802df72765350089380bb9f51fa33ee5331339 (patch)
tree60e57c39ba50a11ee233eb8b2396901f7460e891 /lib/custodian/protocoltest/dnsbl.rb
parenta149b22ca1a974eace02e34bfe10fbd8ef7178ea (diff)
DNSBL test.
Diffstat (limited to 'lib/custodian/protocoltest/dnsbl.rb')
-rw-r--r--lib/custodian/protocoltest/dnsbl.rb113
1 files changed, 113 insertions, 0 deletions
diff --git a/lib/custodian/protocoltest/dnsbl.rb b/lib/custodian/protocoltest/dnsbl.rb
new file mode 100644
index 0000000..7cdf0b9
--- /dev/null
+++ b/lib/custodian/protocoltest/dnsbl.rb
@@ -0,0 +1,113 @@
+require 'resolv'
+
+
+
+
+#
+# The DNSBL test.
+#
+# This object is instantiated if the parser sees a line such as:
+#
+###
+### 1.2.3.4 must not run dnsbl via zen.spamhaus.org otherwise 'The IP is blacklisted in spamhaus'.
+###
+#
+#
+
+module Custodian
+
+ module ProtocolTest
+
+ class DNSBLTest < TestFactory
+
+
+ #
+ # Save away state from the configuration line.
+ #
+ def initialize( line )
+ @line = line
+ @host = line.split( /\s+/)[0]
+
+ #
+ # Ensure the host is an IP address.
+ #
+ raise ArgumentError, "The target must be an IP address" unless( @host =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ )
+
+ #
+ # Save the host we're testing against
+ #
+ if ( line =~ /via\s+([0-9]+)/ )
+ @zones = $1.dup
+ else
+ @zones = "zen.spamhaus.org"
+ end
+
+ #
+ # Is this test inverted?
+ #
+ if ( line =~ /must\s+not\s+run\s+/ )
+ @inverted = true
+ else
+ @inverted = false
+ end
+ end
+
+
+
+ #
+ # Allow this test to be serialized.
+ #
+ def to_s
+ @line
+ end
+
+
+
+ #
+ # Run the test.
+ #
+ # Return "true" on success - if the IP is listed. False if not.
+ #
+ def run_test
+
+ # The error is empty.
+ @error = nil
+
+ @zones.split( "," ).each do |zone|
+
+ #
+ # Convert the IP to be looked up.
+ #
+ # Given IP 1.2.3.4 we lookup the address of the name
+ # 4.3.2.1.$zone
+ #
+ if ( @host =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ )
+ name = "#{$4}.#{$3}.#{$2}.#{$1}.#{zone}"
+
+ result = Custodian::Util::DNS.hostname_to_ip( name )
+
+ if ( ( !result.nil? ) && ( result.length > 0 ) )
+ @error = "IP #{@host} listed in blacklist #{zone}. Lookup of #{name} lead to result: #{result}"
+ return true
+ end
+
+ end
+ #
+ end
+
+ return false
+ end
+
+
+ #
+ # If the test failed here we will return a suitable error message.
+ #
+ def error
+ @error
+ end
+
+ # register ourselves with the class-factory
+ register_test_type "dnsbl"
+ end
+ end
+end