#!/usr/bin/perl -I/usr/local/sauron
#
# remove-hosts -- removes hosts based on list of MACs read from a file
#
# Copyright (c) Michal Kostenec <kostenec@civ.zcu.cz> 2013-2014.
# Copyright (c) Timo Kokkonen <tjko@iki.fi>  2003.
# $Id: remove-hosts,v 1.4 2005/04/27 06:25:28 tjko Exp $
#
use Getopt::Long;
use Sauron::DB;
use Sauron::BackEnd;
use Sauron::Util;
use Sauron::Sauron;

load_config();

GetOptions("help|h","mac","duid","ip","host","commit");

if ($opt_help || @ARGV < 2) {
  print "syntax: $0 [OPTIONS] <servername> <inputfile>\n\n",
        "\toptions:\n",
	"\t--mac\t\tinput file contains MAC addresses (one per line)\n",
	"\t--duid\t\tinput file contains DUIDs (one per line)\n",
	"\t--ip\t\tinput file contains IP addresses (one per line)\n",
	"\t--host\t\tinput file contains hostnames (one per line)\n\n",
	"\t--commit\tcommit changes (without this no changes are made)\n\n";
  exit(($opt_help ? 0 : 1));
}

$server=shift;
$filename=shift;
$opt_commit=($opt_commit ? 1 : 0);

fatal("cannot find modhosts script in PROG_DIR ($PROG_DIR)")
  unless (-x "$PROG_DIR/modhosts");
fatal("cannot read input file: $filename")
  if ($filename ne '-' && ! -r $filename);
fatal("cannot specify both --ip and --mac options")  if ($opt_ip && $opt_mac);
fatal("cannot specify both --ip and --duid options")  if ($opt_ip && $opt_duid);
fatal("neither --ip or --mac or --duid or --host option specified") 
  unless ($opt_ip || $opt_mac || $opt_host || $opt_duid);

db_connect();

$serverid=get_server_id($server);
fatal("cannot find server '$server'") unless ($serverid > 0);


open(FILE,$filename) || fatal("cannot open file: $filename");

while(<FILE>) {
  chomp;
  next if /^\s*$/;

  $ip=''; $mac=''; $host=''; $duid='';

  if ($opt_mac) {
    s/[\s:-]//g;
    next unless /([0-9A-Fa-f]{12})/;
    $mac=uc($1);
    print "MAC=$mac ";
    db_query("SELECT h.id,h.domain FROM hosts h, zones z " .
	     "WHERE z.server=$serverid AND h.zone=z.id " .
	     " AND h.ether = '$mac'",\@q);
    if ($q[0][0] > 0) {
      print "$q[0][1] (id=$q[0][0])\n";
      $delhash{$q[0][0]}++;
    } else {
      print "not found\n";
    }
  }
  elsif ($opt_ip) {
    $ip = $_;
    $ip =~ s/\s+//g; 
    next unless is_cidr($ip);
    
    print "IP=$ip ";
    db_query("SELECT h.id,h.domain FROM hosts h, zones z, a_entries a " .
	     "WHERE z.server=$serverid AND h.zone=z.id AND a.host=h.id " .
	     " AND a.ip='$ip'",\@q);
    if ($q[0][0] > 0) {
      print "$q[0][1] (id=$q[0][0])\n";
      $delhash{$q[0][0]}++;
    } else {
      print "not found\n";
    }
  }
  elsif ($opt_host) {
    next unless /([0-9A-Za-z\.\-]+)/;
    $host=lc($1);
    print "Host=$host ";
    db_query("SELECT h.id,h.domain FROM hosts h, zones z, a_entries a " .
             "WHERE z.server=$serverid AND h.zone=z.id AND a.host=h.id " .
             " AND h.domain='$host'",\@q);
    if ($q[0][0] > 0) {
      print "$q[0][1] (id=$q[0][0])\n";
      $delhash{$q[0][0]}++;
    } else {
      print "not found\n";
    }
  }
  elsif ($opt_duid) {
    s/[\s:]//g;
    next unless /([0-9A-Fa-f]{24,40})/;
    $duid=uc($1);
    db_query("SELECT h.id,h.domain FROM hosts h, zones z " .
         "WHERE z.server=$serverid AND h.zone=z.id " .
         " AND h.duid = '$duid'",\@q);
    foreach $qq (@q) {
        if ($$qq[0] > 0) {
            print "DUID=$duid $$qq[1] (id=$$qq[0])\n";
            $delhash{$$qq[0]}++;
        } 
        else {
            print "not found\n";
        }
    }
  }
}

close(FILE);

unless ($opt_commit) {
  print "Found ". (keys %delhash) . " hosts to delete. (no changes made)\n";
  exit;
}

print "Deleting " . (keys %delhash) . " hosts:\n";

foreach $id (sort keys %delhash) {
  print "Deleting host (id=$id)...\n";
  fatal("failed to delete host (id=$id)") if (delete_host($id) < 0);
}


# eof
