#!/usr/bin/perl

use warnings;
use strict;

use Getopt::Long;
use IPC::Open3;
use Config::General;
use DateTime;
use Data::Dumper;

my %opts;
GetOptions( \%opts,  "ip=s", "--ticket=s", "help" );
unless (  $opts{ip} && ( $opts{ip} =~ /\d+\.\d+\.\d+\.\d+(\/\d+)?/ ) ) {
  print "You must specify an ip address or cidr.\n\n";
  usage();
  exit;
}


add_to_ipset($opts{ip}) or die "Failed to add " . $opts{ip} . " to ipset.";
add_to_http_whitelist_local($opts{ip}) or die "Added to ipset, but failed to add to http_whitelist_local.conf!";
restart_if_needed();

my $date = DateTime->now();
$date->set_time_zone( 'America/Chicago' );
my $user = $ENV{'RUSER'} || 'unknown';
my $ticket = $opts{ticket} || 'not provided';
my $line = '[' . $date->mdy . ':' . $date->hms . '] ' . $user . ' ';
$line .= "added $opts{ip} to http_whitelist for ticket: $ticket\n";

open my $fh, '>>', '/var/log/firewall-modification.log' or die "Failed to open log file.";
print $fh $line;
close $fh;

sub add_to_ipset {
  my $ip = shift;
  my $ipset = 'http_whitelist';

  ensure_ipset_exists($ipset);

  my ($chld_in, $chld_out, $chld_err);
  use Symbol 'gensym'; $chld_err = gensym;
  my $pid = open3($chld_in, $chld_out, $chld_err, '/usr/sbin/ipset', '-exist', 'add', $ipset, $ip);
  my @out = <$chld_out>;
  my @err = <$chld_err>;
  close $chld_in;
  close $chld_out;
  close $chld_err;
  waitpid ( $pid, 0 );
  my $stat = $? >> 8;
  return undef unless ( $stat == 0 );
  return 1; 
}

sub ensure_ipset_exists {
  my $ipset = shift;

  my ($chld_in, $chld_out, $chld_err);
  use Symbol 'gensym'; $chld_err = gensym;
  my $pid = open3($chld_in, $chld_out, $chld_err, '/usr/sbin/ipset', 'create', $ipset, 'hash:net');
  my @out = <$chld_out>;
  my @err = <$chld_err>;
  close $chld_in;
  close $chld_out;
  close $chld_err;
  waitpid ( $pid, 0 );
  my $stat = $? >> 8;
  return 1;
}

sub add_to_http_whitelist_local {
  my $ip = shift;
  my $conf = '/etc/firewall/conf/http_whitelist_local.conf';
  my @lines;

  if ( -f $conf ) {
    open my $fh, '<', $conf or die("cannot open $conf");
    @lines = <$fh>;
    close $fh;
  }
  push @lines, "$ip\n";
  
  open my $fh, '>', "$conf.new" or die("cannot open $conf.new for writing");
  print $fh $_ foreach @lines;
  close $fh;

  rename '/etc/firewall/conf/http_whitelist_local.conf.new', '/etc/firewall/conf/http_whitelist_local.conf';
}

sub restart_if_needed {
  my $http_chain = `/sbin/iptables -nL http-input`;
  unless ( $http_chain =~ /whitelist/ ) {
    print "restarting firewall .. ";
    my ($chld_in, $chld_out, $chld_err);
    use Symbol 'gensym'; $chld_err = gensym;
    my $pid = open3($chld_in, $chld_out, $chld_err, '/etc/init.d/firewall', 'restart');
    my @out = <$chld_out>;
    my @err = <$chld_err>;
    close $chld_in;
    close $chld_out;
    close $chld_err;
    waitpid ( $pid, 0 );
    my $stat = $? >> 8;
    if ( $stat == 0 ) {
      print "done\n";
    }
    else {
      print "Failed restarting firewall. Manually check this!\n";
    }
  }
}

sub usage {
  print "whitelistip - adds an ip or cidr to http_whitelist\n";
  print "\t--ip - ip or cidr to whitelist\n";
  print "\t--ticket - ticket id\n\n";
}



