From c796e1fd3c4b6c659255b3a814662d4377836ed0 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Thu, 22 Nov 2012 06:49:21 +0000 Subject: Moved tests into a namespace. --- lib/custodian/protocoltest/dns.rb | 87 ++++++------ lib/custodian/protocoltest/ftp.rb | 127 +++++++++--------- lib/custodian/protocoltest/http.rb | 112 ++++++++-------- lib/custodian/protocoltest/jabber.rb | 130 +++++++++--------- lib/custodian/protocoltest/ldap.rb | 129 +++++++++--------- lib/custodian/protocoltest/ping.rb | 155 +++++++++++----------- lib/custodian/protocoltest/rsync.rb | 129 +++++++++--------- lib/custodian/protocoltest/smtp.rb | 129 +++++++++--------- lib/custodian/protocoltest/ssh.rb | 135 ++++++++++--------- lib/custodian/protocoltest/tcp.rb | 249 ++++++++++++++++++----------------- 10 files changed, 717 insertions(+), 665 deletions(-) (limited to 'lib/custodian') diff --git a/lib/custodian/protocoltest/dns.rb b/lib/custodian/protocoltest/dns.rb index ab26a30..0fccb96 100644 --- a/lib/custodian/protocoltest/dns.rb +++ b/lib/custodian/protocoltest/dns.rb @@ -9,75 +9,80 @@ ### # # -class DNSTest < TestFactory +module Custodian + module ProtocolTest + class DNSTest < TestFactory - # - # The line from which we were constructed. - # - attr_reader :line + # + # The line from which we were constructed. + # + attr_reader :line - # - # Constructor - # - def initialize( line ) - # - # Save the line - # - @line = line + # + # Constructor + # + def initialize( line ) - end + # + # Save the line + # + @line = line + end - # - # Helper for development. - # - def to_s - "dns-test - TODO." - end + # + # Helper for development. + # + def to_s + "dns-test - TODO." + end - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # - # Run the test. - # - def run_test - @error = "Not implemented" - false - end + # + # Run the test. + # + def run_test + @error = "Not implemented" + false + end - # - # If the test fails then report the error. - # - def error - @error - end + + # + # If the test fails then report the error. + # + def error + @error + end - register_test_type "dns" + register_test_type "dns" + end + end end diff --git a/lib/custodian/protocoltest/ftp.rb b/lib/custodian/protocoltest/ftp.rb index c92f295..dfa86e1 100644 --- a/lib/custodian/protocoltest/ftp.rb +++ b/lib/custodian/protocoltest/ftp.rb @@ -11,105 +11,110 @@ require 'custodian/protocoltest/tcp' # # The specification of the port is optional and defaults to 21 # -class FTPTest < TCPTest +module Custodian + module ProtocolTest + class FTPTest < TCPTest - # - # The line from which we were constructed. - # - attr_reader :line + # + # The line from which we were constructed. + # + attr_reader :line - # - # The host to test against. - # - attr_reader :host + # + # The host to test against. + # + attr_reader :host - # - # The port to connect to. - # - attr_reader :port + # + # The port to connect to. + # + attr_reader :port - # - # Constructor - # - def initialize( line ) - # - # Save the line - # - @line = line + # + # Constructor + # + def initialize( line ) - # - # Save the host - # - @host = line.split( /\s+/)[0] + # + # Save the line + # + @line = line - # - # Save the port - # - if ( line =~ /on\s+([0-9]+)/ ) - @port = $1.dup - else - @port = 21 - end - end + # + # Save the host + # + @host = line.split( /\s+/)[0] + # + # Save the port + # + if ( line =~ /on\s+([0-9]+)/ ) + @port = $1.dup + else + @port = 21 + end + end - # - # Helper for development. - # - def to_s - "ftp-test of #{@host}:#{@port}." - end + # + # Helper for development. + # + def to_s + "ftp-test of #{@host}:#{@port}." + end - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # Run the TCP-protocol test. - # - def run_test - # reset the error, in case we were previously executed. - @error = nil + # Run the TCP-protocol test. + # + def run_test - run_test_internal( @host, @port, "^220" ) - end + # reset the error, in case we were previously executed. + @error = nil + run_test_internal( @host, @port, "^220" ) + end - # - # If the test fails then report the error. - # - def error - @error - end + + # + # If the test fails then report the error. + # + def error + @error + end - register_test_type "ftp" + register_test_type "ftp" + end + end end diff --git a/lib/custodian/protocoltest/http.rb b/lib/custodian/protocoltest/http.rb index fa97223..1610698 100644 --- a/lib/custodian/protocoltest/http.rb +++ b/lib/custodian/protocoltest/http.rb @@ -9,89 +9,95 @@ ### # # -class HTTPTest < TestFactory +module Custodian + module ProtocolTest - # - # The line from which we were constructed. - # - attr_reader :line + class HTTPTest < TestFactory - # - # The URL to poll - # - attr_reader :url + # + # The line from which we were constructed. + # + attr_reader :line - # - # Constructor - # - def initialize( line ) - # - # Save the line - # - @line = line + # + # The URL to poll + # + attr_reader :url - # - # Save the URL - # - @url = line.split( /\s+/)[0] + # + # Constructor + # + def initialize( line ) + # + # Save the line + # + @line = line - if ( @url !~ /^https?:/ ) - raise ArgumentError, "The target wasn't an URL" - end + # + # Save the URL + # + @url = line.split( /\s+/)[0] - end + if ( @url !~ /^https?:/ ) + raise ArgumentError, "The target wasn't an URL" + end + end - # - # Helper for development. - # - def to_s - "http-test of #{@url}." - end + # + # Helper for development. + # + def to_s + "http-test of #{@url}." + end - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # - # Run the test. - # - def run_test - @error = "Not implemented" - false - end + # + # Run the test. + # + def run_test + @error = "Not implemented" + false + end - # - # If the test fails then report the error. - # - def error - @error - end + + + # + # If the test fails then report the error. + # + def error + @error + end - register_test_type "http" - register_test_type "https" + register_test_type "http" + register_test_type "https" + end + end end diff --git a/lib/custodian/protocoltest/jabber.rb b/lib/custodian/protocoltest/jabber.rb index 166e98e..c8235fc 100644 --- a/lib/custodian/protocoltest/jabber.rb +++ b/lib/custodian/protocoltest/jabber.rb @@ -12,103 +12,109 @@ require 'custodian/protocoltest/tcp' # # The specification of the port is optional, and defaults to 5222. # -class JABBERTest < TCPTest +module Custodian + module ProtocolTest - # - # The line from which we were constructed. - # - attr_reader :line + class JABBERTest < TCPTest - # - # The host to test against. - # - attr_reader :host + # + # The line from which we were constructed. + # + attr_reader :line - # - # The port to connect to. - # - attr_reader :port + # + # The host to test against. + # + attr_reader :host + # + # The port to connect to. + # + attr_reader :port - # - # Constructor - # - def initialize( line ) - # - # Save the line - # - @line = line - # - # Save the host - # - @host = line.split( /\s+/)[0] + # + # Constructor + # + def initialize( line ) - # - # Save the port - # - if ( line =~ /on\s+([0-9]+)/ ) - @port = $1.dup - else - @port = 5222 - end - end + # + # Save the line + # + @line = line + # + # Save the host + # + @host = line.split( /\s+/)[0] + # + # Save the port + # + if ( line =~ /on\s+([0-9]+)/ ) + @port = $1.dup + else + @port = 5222 + end + end - # - # Helper for development. - # - def to_s - "jabber-test of #{@host}:#{@port}." - end + # + # Helper for development. + # + def to_s + "jabber-test of #{@host}:#{@port}." + end - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # - # Run the TCP-protocol test. - # - def run_test - # reset the error, in case we were previously executed. - @error = nil - run_test_internal( @host, @port, " @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # - # Run the TCP-protocol test. - # - def run_test - # reset the error, in case we were previously executed. - @error = nil + # + # Run the TCP-protocol test. + # + def run_test - run_test_internal( @host, @port, nil, false ) - end + # reset the error, in case we were previously executed. + @error = nil + run_test_internal( @host, @port, nil, false ) + end - # - # If the test fails then report the error. - # - def error - @error - end + + # + # If the test fails then report the error. + # + def error + @error + end - register_test_type "ldap" + register_test_type "ldap" + end + end end diff --git a/lib/custodian/protocoltest/ping.rb b/lib/custodian/protocoltest/ping.rb index cd8490d..765540c 100644 --- a/lib/custodian/protocoltest/ping.rb +++ b/lib/custodian/protocoltest/ping.rb @@ -11,119 +11,124 @@ require 'custodian/testfactory.rb' ### # # -class PINGTest < TestFactory +module Custodian + module ProtocolTest + class PINGTest < TestFactory - # - # The line from which we were constructed. - # - attr_reader :line + # + # The line from which we were constructed. + # + attr_reader :line - # - # The host to test against. - # - attr_reader :host + # + # The host to test against. + # + attr_reader :host - # - # Constructor - # - def initialize( line ) - # - # Save the line - # - @line = line + # + # Constructor + # + def initialize( line ) - # - # Save the host - # - @host = line.split( /\s+/)[0] - end + # + # Save the line + # + @line = line + # + # Save the host + # + @host = line.split( /\s+/)[0] + end - # - # Helper for development. - # - def to_s - "ping-test - #{@host}." - end + # + # Helper for development. + # + def to_s + "ping-test - #{@host}." + end - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # - # Run the test. - # - def run_test - # - # Find the binary we're going to invoke. - # - binary = nil - binary = "/usr/bin/multi-ping" if ( File.exists?( "/usr/bin/multi-ping" ) ) + # + # Run the test. + # + def run_test - if ( binary.nil? ) - @error = "Failed to find '/usr/bin/multi-ping'" - return false - end + # + # Find the binary we're going to invoke. + # + binary = nil + binary = "/usr/bin/multi-ping" if ( File.exists?( "/usr/bin/multi-ping" ) ) + if ( binary.nil? ) + @error = "Failed to find '/usr/bin/multi-ping'" + return false + end - # - # Sanity check the hostname for ping-tests, to - # avoid this security hole: - # - # $(/tmp/exploit.sh) must run ping .. - # - if ( @host !~ /^([a-zA-Z0-9:\-\.]+)$/ ) - @error = "Invalid hostname for ping-test: #{@host}" - return false - end + # + # Sanity check the hostname for ping-tests, to + # avoid this security hole: + # + # $(/tmp/exploit.sh) must run ping .. + # + if ( @host !~ /^([a-zA-Z0-9:\-\.]+)$/ ) + @error = "Invalid hostname for ping-test: #{@host}" + return false + end - # - # Run the test. - # - if ( system( "#{binary} #{host}" ) == true ) - return true - else - @error = "Ping failed." - return false - end - end + # + # Run the test. + # + if ( system( "#{binary} #{host}" ) == true ) + return true + else + @error = "Ping failed." + return false + end + end - # - # If the test fails then report the error. - # - def error - @error - end + + # + # If the test fails then report the error. + # + def error + @error + end - register_test_type "ping" + register_test_type "ping" + end + end end diff --git a/lib/custodian/protocoltest/rsync.rb b/lib/custodian/protocoltest/rsync.rb index eb8614f..bb0a97c 100644 --- a/lib/custodian/protocoltest/rsync.rb +++ b/lib/custodian/protocoltest/rsync.rb @@ -12,104 +12,109 @@ require 'custodian/protocoltest/tcp' # # The specification of the port is optional and defaults to 873 # -class RSYNCTest < TCPTest +module Custodian + module ProtocolTest - # - # The line from which we were constructed. - # - attr_reader :line + class RSYNCTest < TCPTest + # + # The line from which we were constructed. + # + attr_reader :line - # - # The host to test against. - # - attr_reader :host + # + # The host to test against. + # + attr_reader :host - # - # The port to connect to. - # - attr_reader :port + # + # The port to connect to. + # + attr_reader :port - # - # Constructor - # - def initialize( line ) - # - # Save the line. - # - @line = line + # + # Constructor + # + def initialize( line ) - # - # Save the host - # - @host = line.split( /\s+/)[0] + # + # Save the line. + # + @line = line - # - # Save the port - # - if ( line =~ /on\s+([0-9]+)/ ) - @port = $1.dup - else - @port = 873 - end - end + # + # Save the host + # + @host = line.split( /\s+/)[0] + # + # Save the port + # + if ( line =~ /on\s+([0-9]+)/ ) + @port = $1.dup + else + @port = 873 + end + end - # - # Helper for development. - # - def to_s - "rsync-test of #{@host}:#{@port}." - end + # + # Helper for development. + # + def to_s + "rsync-test of #{@host}:#{@port}." + end - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # - # Run the protocol test. - # - def run_test - # reset the error, in case we were previously executed. - @error = nil + # + # Run the protocol test. + # + def run_test - run_test_internal( @host, @port, "@RSYNCD" ) - end + # reset the error, in case we were previously executed. + @error = nil + run_test_internal( @host, @port, "@RSYNCD" ) + end - # - # If the test fails then report the error. - # - def error - @error - end + + # + # If the test fails then report the error. + # + def error + @error + end - register_test_type "rsync" + register_test_type "rsync" + end + end end diff --git a/lib/custodian/protocoltest/smtp.rb b/lib/custodian/protocoltest/smtp.rb index 374db6c..08912f1 100644 --- a/lib/custodian/protocoltest/smtp.rb +++ b/lib/custodian/protocoltest/smtp.rb @@ -12,105 +12,110 @@ require 'custodian/protocoltest/tcp' # # The specification of the port is optional and defaults to 25. # -class SMTPTest < TCPTest +module Custodian + module ProtocolTest + class SMTPTest < TCPTest - # - # The line from which we were constructed. - # - attr_reader :line + # + # The line from which we were constructed. + # + attr_reader :line - # - # The host to test against. - # - attr_reader :host + # + # The host to test against. + # + attr_reader :host - # - # The port to connect to. - # - attr_reader :port + # + # The port to connect to. + # + attr_reader :port - # - # Constructor - # - def initialize( line ) - # - # Save the line. - # - @line = line + # + # Constructor + # + def initialize( line ) - # - # Save the host - # - @host = line.split( /\s+/)[0] + # + # Save the line. + # + @line = line - # - # Save the port - # - if ( line =~ /on\s+([0-9]+)/ ) - @port = $1.dup - else - @port = 25 - end - end + # + # Save the host + # + @host = line.split( /\s+/)[0] + # + # Save the port + # + if ( line =~ /on\s+([0-9]+)/ ) + @port = $1.dup + else + @port = 25 + end + end - # - # Helper for development. - # - def to_s - "smtp-test of #{@host}:#{@port}." - end + # + # Helper for development. + # + def to_s + "smtp-test of #{@host}:#{@port}." + end - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # - # Run the TCP-protocol test. - # - def run_test - # reset the error, in case we were previously executed. - @error = nil + # + # Run the TCP-protocol test. + # + def run_test - run_test_internal( @host, @port, "SMTP" ) - end + # reset the error, in case we were previously executed. + @error = nil + run_test_internal( @host, @port, "SMTP" ) + end - # - # If the test fails then report the error. - # - def error - @error - end + + # + # If the test fails then report the error. + # + def error + @error + end - register_test_type "smtp" + register_test_type "smtp" + end + end end diff --git a/lib/custodian/protocoltest/ssh.rb b/lib/custodian/protocoltest/ssh.rb index 792d114..b557a77 100644 --- a/lib/custodian/protocoltest/ssh.rb +++ b/lib/custodian/protocoltest/ssh.rb @@ -11,107 +11,112 @@ require 'custodian/protocoltest/tcp' # # The specification of the port is optional. # -class SSHTest < TCPTest +module Custodian + module ProtocolTest + class SSHTest < TCPTest - # - # The line from which we were constructed. - # - attr_reader :line + # + # The line from which we were constructed. + # + attr_reader :line - # - # The host to test against. - # - attr_reader :host + # + # The host to test against. + # + attr_reader :host - # - # The port to connect to. - # - attr_reader :port + # + # The port to connect to. + # + attr_reader :port - # - # Constructor - # - # Ensure we received a port to run the test against. - # - def initialize( line ) - # - # Save the line - # - @line = line + # + # Constructor + # + # Ensure we received a port to run the test against. + # + def initialize( line ) - # - # Save the host - # - @host = line.split( /\s+/)[0] + # + # Save the line + # + @line = line - # - # Save the port - # - if ( line =~ /on\s+([0-9]+)/ ) - @port = $1.dup - else - @port = 22 - end - end + # + # Save the host + # + @host = line.split( /\s+/)[0] + # + # Save the port + # + if ( line =~ /on\s+([0-9]+)/ ) + @port = $1.dup + else + @port = 22 + end + end - # - # Helper for development. - # - def to_s - "ssh-test of #{@host}:#{@port}." - end + # + # Helper for development. + # + def to_s + "ssh-test of #{@host}:#{@port}." + end - # - # Convert this class to JSON such that it may be - # serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be + # serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # - # Run the TCP-protocol test. - # - def run_test - # reset the error, in case we were previously executed. - @error = nil + # + # Run the TCP-protocol test. + # + def run_test - run_test_internal( @host, @port, "SSH" ) - end + # reset the error, in case we were previously executed. + @error = nil + run_test_internal( @host, @port, "SSH" ) + end - # - # If the test fails then report the error. - # - def error - @error - end + + # + # If the test fails then report the error. + # + def error + @error + end - register_test_type "ssh" + register_test_type "ssh" + end + end end diff --git a/lib/custodian/protocoltest/tcp.rb b/lib/custodian/protocoltest/tcp.rb index 4f80b42..c09eb6b 100644 --- a/lib/custodian/protocoltest/tcp.rb +++ b/lib/custodian/protocoltest/tcp.rb @@ -15,180 +15,185 @@ require 'timeout' # # The specification of the port is mandatory, the banner is optional. # -class TCPTest < TestFactory +module Custodian + module ProtocolTest + class TCPTest < TestFactory - # - # The input line - # - attr_reader :line + # + # The input line + # + attr_reader :line - # - # The host to test against. - # - attr_reader :host + # + # The host to test against. + # + attr_reader :host - # - # The port to connect to. - # - attr_reader :port + # + # The port to connect to. + # + attr_reader :port - # - # The banner to look for, may be nil. - # - attr_reader :banner + # + # The banner to look for, may be nil. + # + attr_reader :banner - # - # Constructor - # - # Ensure we received a port to run the TCP-test against. - # - def initialize( line ) - # - # Save the line - # - @line = line + # + # Constructor + # + # Ensure we received a port to run the TCP-test against. + # + def initialize( line ) - # - # Save the host - # - @host = line.split( /\s+/)[0] + # + # Save the line + # + @line = line - # - # Save the port - # - if ( line =~ /on\s+([0-9]+)/ ) - @port = $1.dup - else - @port = nil - end + # + # Save the host + # + @host = line.split( /\s+/)[0] - # - # Save the optional banner. - # - if ( line =~ /with\s+banner\s+'([^']+)'/ ) - @banner = $1.dup - else - @banner = nil - end + # + # Save the port + # + if ( line =~ /on\s+([0-9]+)/ ) + @port = $1.dup + else + @port = nil + end - @error = nil + # + # Save the optional banner. + # + if ( line =~ /with\s+banner\s+'([^']+)'/ ) + @banner = $1.dup + else + @banner = nil + end - if ( @port.nil? ) - raise ArgumentError, "Missing port to test against" - end - end + @error = nil + if ( @port.nil? ) + raise ArgumentError, "Missing port to test against" + end + end - # - # Helper for development. - # - def to_s - "tcp-test of #{@host}:#{@port} looking for banner '#{@banner}'." - end + # + # Helper for development. + # + def to_s + "tcp-test of #{@host}:#{@port} looking for banner '#{@banner}'." + end - # - # Convert this class to JSON such that it may be serialized. - # - def to_json - hash = { :line => @line } - hash.to_json - end + # + # Convert this class to JSON such that it may be serialized. + # + def to_json + hash = { :line => @line } + hash.to_json + end - # - # Run the TCP-protocol test. - # - def run_test - # reset the error, in case we were previously executed. - @error = nil + # + # Run the TCP-protocol test. + # + def run_test - return( run_test_internal( @host, @port, @banner ) ) - end + # reset the error, in case we were previously executed. + @error = nil + + return( run_test_internal( @host, @port, @banner ) ) + end - # - # Run the connection test - optionally matching against the banner. - # - # If the banner is nil then we're merely testing we can connect and - # send the string "quit". - # - # - def run_test_internal( host, port, banner = nil, do_read = true ) - begin - timeout(30) do + # + # Run the connection test - optionally matching against the banner. + # + # If the banner is nil then we're merely testing we can connect and + # send the string "quit". + # + # + def run_test_internal( host, port, banner = nil, do_read = true ) begin - socket = TCPSocket.new( host, port ) - socket.puts( "QUIT") - - # read a banner from the remote server - read = nil - read = socket.gets(nil) if ( do_read ) - - # trim to a sane length & strip newlines. - read = read[0,255] unless ( read.nil? ) - read.gsub!(/[\n\r]/, "") unless ( read.nil? ) - - socket.close() - - if ( banner.nil? ) - @error = nil - return true - else - # test for banner - if ( ( !read.nil? ) && ( read =~ /#{banner}/i ) ) - return true + timeout(30) do + begin + socket = TCPSocket.new( host, port ) + socket.puts( "QUIT") + + # read a banner from the remote server + read = nil + read = socket.gets(nil) if ( do_read ) + + # trim to a sane length & strip newlines. + read = read[0,255] unless ( read.nil? ) + read.gsub!(/[\n\r]/, "") unless ( read.nil? ) + + socket.close() + + if ( banner.nil? ) + @error = nil + return true + else + # test for banner + if ( ( !read.nil? ) && ( read =~ /#{banner}/i ) ) + return true + end + + @error = "We expected a banner matching '#{banner}' but we got '#{read}'" + return false + end + rescue + @error = "Exception connecting to host #{host}:#{port} - #{$!}" + return false end - - @error = "We expected a banner matching '#{banner}' but we got '#{read}'" - return false end - rescue - @error = "Exception connecting to host #{host}:#{port} - #{$!}" + rescue Timeout::Error => e + @error = "TIMEOUT: #{e}" return false end + @error = "Misc failure" + return false end - rescue Timeout::Error => e - @error = "TIMEOUT: #{e}" - return false - end - @error = "Misc failure" - return false - end - # - # If the test fails then report the error. - # - def error - @error - end + # + # If the test fails then report the error. + # + def error + @error + end - register_test_type "tcp" + register_test_type "tcp" + end + end end -- cgit v1.2.1