firewall-whitelist 3.02 KB
Newer Older
Steve Kemp's avatar
Steve Kemp committed
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
# 
# NAME
#
#   firewall-whitelist -- Automatically whitelist IP addresses.
#
# SYNOPSIS
#
9
10
11
12
#  Options:
#
#   --prefix      The directory to operate upon.
#
Steve Kemp's avatar
Steve Kemp committed
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
#  Help Options:
#
#   --help        Show the help information for this script.
#   --verbose     Show debugging information.
#
# This script is designed to automatically whitelist IP addresses which
# have been used to successfully login via SSH.
#
# It does this by parsing the output of the "last" command, and creating
# entries in /etc/symbiosis/firewall/whitelist.d/
#
# AUTHOR
#
#   Steve Kemp <steve@bytemark.co.uk>
#

#
#  Modules we require
#

require 'getoptlong'
require 'ipaddr'

opts = GetoptLong.new(
    [ '--help',    '-h', GetoptLong::NO_ARGUMENT ],
38
39
    [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
    [ '--prefix',  '-p', GetoptLong::REQUIRED_ARGUMENT ]
Steve Kemp's avatar
Steve Kemp committed
40
41
42
)


43
44
45
46
#
#  The options set by the command line.
#
$HELP    = false
Steve Kemp's avatar
Steve Kemp committed
47
$VERBOSE = false
48
$PREFIX  = "/etc/symbiosis/firewall/whitelist.d"
Steve Kemp's avatar
Steve Kemp committed
49
50
51
52

opts.each do |opt,arg|
  case opt
    when '--help'
53
      $HELP = true
Steve Kemp's avatar
Steve Kemp committed
54
55
    when '--verbose'
      $VERBOSE = true
56
57
    when '--prefix'
      $PREFIX = arg
Steve Kemp's avatar
Steve Kemp committed
58
59
60
  end
end

61

Steve Kemp's avatar
Steve Kemp committed
62
63
64
#
# CAUTION! Here be quality kode.
#
65
if $HELP
Steve Kemp's avatar
Steve Kemp committed
66
67
68
69
70
71
72
73
74
75
76
77
  # Open the file, stripping the shebang line
  lines = File.open(__FILE__){|fh| fh.readlines}[2..-1]

  lines.each do |line|
    line.chomp!
    break if line.empty?
    puts line[2..-1].to_s
  end

  exit 0
end

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

#
#  Expire old entries first of all, then add new ones.
#
puts "Expiring old whitelist entries" if ( $VERBOSE )

expired = 0

if ( File.directory?( $PREFIX ) )
  Dir.foreach( $PREFIX ) do |entry|
    if ( ( entry =~ /\.auto$/i ) && 
         (File.mtime( "#{$PREFIX}/#{entry}" ) <  ( Time.now - 8 * 24 * 60 * 60 ) ) )
    then
      puts "Removing #{$PREFIX}/#{entry}" if ( $VERBOSE )
      File.unlink("#{$PREFIX}/#{entry}")
      expired += 1
    end
  end
end
puts "Expiring done - removed #{expired} file(s)" if ( $VERBOSE )



Steve Kemp's avatar
Steve Kemp committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
ip_addresses = {}


#
#  Run last to find the IPs, store unique ones in a hash.
#
IO.popen("/usr/bin/last -i") {|pipe| pipe.readlines}.each do |l|
  next unless l =~ /[ \t]((\d{1,3}\.){3,3}\d{1,3}|[0-9a-f:]+)[ \t]/ 
  ip_addresses[ $1 ] = 1
end


#
#  Did we update?
#
updated=false

#
#  Iterate over each IP
#
ip_addresses.each_key do |ip|
  if ( ( ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) &&
       ( ip !~ /^(127|0)\./ ) )

    puts "Found IP address: #{ip}" if ( $VERBOSE )

    if ( File.exists?( "/etc/symbiosis/firewall/whitelist.d/#{ip}" ) )
      puts "\tAlready whitelisted" if ( $VERBOSE )
    else
130
      # ensure the directory exists.
Steve Kemp's avatar
Steve Kemp committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
      if ( ! File.directory?( "/etc/symbiosis/firewall/whitelist.d" ) )
        system( "mkdir -p /etc/symbiosis/firewall/whitelist.d" )
      end

      # create the file
      system( "touch /etc/symbiosis/firewall/whitelist.d/#{ip}" )

      updated=true
      puts "\tAdding to whitelist" if ( $VERBOSE )
    end
  end
end



#
#  Finally re-run the firewall if we updated
#
if ( updated )

  puts "Updated whitelist, re-running firewall" if ( $VERBOSE )

  system( "/usr/bin/firewall" )
end