Class: SshGuard::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_guard.rb

Defined Under Namespace

Classes: Parser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCore

Returns a new instance of Core.



19
20
21
22
23
24
25
26
27
# File 'lib/ssh_guard.rb', line 19

def initialize
  unless i_am_root?
    raise "ssh_guard should be started as root!!!"
  end
  @database = Database.new
  @parser   = Parser.new
  @firewall = FirewallAdapters::IPFWAdapter.new
  @log_file = "/var/log/secure.log"
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



7
8
9
# File 'lib/ssh_guard.rb', line 7

def database
  @database
end

#firewallObject (readonly)

Returns the value of attribute firewall.



8
9
10
# File 'lib/ssh_guard.rb', line 8

def firewall
  @firewall
end

Class Method Details

.i_am_root?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/ssh_guard.rb', line 47

def self.i_am_root?
  `whoami` =~ /^root$/
end

Instance Method Details

#<<(line) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/ssh_guard.rb', line 29

def <<(line)
  if entry = @parser.parse_line(line)
    if database.should_block? entry[:ip_address]
      firewall.block_host entry[:ip_address] unless firewall.blocked?(entry[:ip_address])
    else
      database.add_entry(entry) unless firewall.blocked?(entry[:ip_address])
    end
  end
end

#i_am_root?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ssh_guard.rb', line 50

def i_am_root?
  self.class.i_am_root?
end

#startObject



39
40
41
42
43
44
45
# File 'lib/ssh_guard.rb', line 39

def start
  IO.popen("tail -f #{@log_file}") do |f|
    while line = f.gets
      self << line if line =~ /sshd/
    end
  end
end