summaryrefslogtreecommitdiff
path: root/lib/custodian/protocoltest/smtprelay.rb
blob: 0a7dfdc1f34616270f07d2f1f20fd016cb2a8fe7 (plain)
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
require 'net/smtp'



module Custodian

  module ProtocolTest

    class SMTPRelayTest < TCPTest

      # save away state from the configuration line.
      def initialize( line )
        @line = line
        @host  = line.split( /\s+/)[0]

        if ( line =~ /must\s+not\s+run\s+/ )
          @inverted = true
        else
          @inverted = false
        end
      end


      # run the test for open relays of SMTP protocol - return true on success.
      # false on fail.
      # this requires love, just trying to get it to run for now..
      def run_test
        @error = nil # for if we've run the test before
        message = "Subject: SMTP Relay check\nThis is a test for OPEN SMTP relays."
        
        begin

          Net::SMTP.start(@host,25) do |smtp|
            sent = smtp.send_message message, "noreply@bytemark.co.uk", "noreply@bytemark.co.uk"

            @status = sent.status.to_s
            
            if @inverted === true
              @success = true
              @failure = false
            else 
              @success = false
              @failure = true
            end
            
            if @status === "250" #and @inverted == true
              @error = "NOT OK: message sent on #{@host} with status #{@status}"
              return @success
            else 
              @error = "OK: message not sent on #{@host} with status #{@status}"
              return @failure
            end
            
          end # Net SMTP

        rescue Exception => ex # for if we fail to send a message; this is a good thing
        
          @error = "OK: Timed out or connection refused on #{@host} with status #{@status}"
          return @failure
        end

      end


      # if the test failed return a suitable error message
      def error
        @error
      end

      # register ourselves with the factory so we're invoked for lines of the form:
      #  TARGET must (not) run xxx otherwise ...
      register_test_type "smtprelay"
    end
  end
end