#!/usr/lib/perl -w
##################################################
#  CensorRT
#
#  A script to help automate the censoring process 
#  for RT tickets.
#
#  Dustin York 
#  University of Washington->C&C->SSG 2007
#
#  A quick overview of the script:
#  censorrt  
#  1. Check input.
#  2. Query Ticket for censor string for transaction id's. 
#  3. Censor strings in DB ?????? (is automation too complicated?)
#  4. Determine Hash filename for RT's Transaction Cache = sha1( transactionid_ticketid );
#  5. Determine webhosts for RT (host on RT should retern IP's.)
#  6. Delete caches on rt's webhosts if they are present.
#
###################################################


# Requires the following CPAN modules:
# Digest::SHA1
# DBI
# DBD::Pg
# Proc::InvokeEditor
# Net::DNS


use constant {
  MASON_TRANSACTION_CACHE_DIR => '/mason_data/cache/',
  RM_CMD => 'rm -f' 
};

############## Helper Functions #####################################


# Checking User input:
sub usage {
print <<"USAGE";
  Usage: $0  
    where search string is any string accepted by grep.
    Requires su2 privs.

USAGE
exit;
}

# Determin RT's web hosts
sub getRTHosts {
  use Net::DNS;
  my $name = "rt.cac.washington.edu";
  my $res  = Net::DNS::Resolver->new;

  my $query = $res->search($name);
  my @rthosts;

  print "Determining RT's web front ends:\n";
  
  if ($query) {
      foreach my $rr ($query->answer) {
          next unless ( $rr->type eq "A" || $rr->type eq "MX" );
          my $ipQuery = $res->search( $rr->address );
          push @rthosts, $ipQuery->{'answer'}[0]->{'ptrdname'};
          my @domain = split( /\./, $ipQuery->{'answer'}[0]->{'ptrdname'}  );
          print( shift( @domain ) . "\n"); # . ' - ' . $rr->address . "\n";
      }
  } else {
      warn "query failed: ", $res->errorstring, "\n";
  }

  #MX records are rt's mail servers
#  my @mx   = mx($res, $name);
  
#  if (@mx) {
#      foreach my $rr (@mx) {
#          print 'MX:' . $rr->preference, " ", $rr->exchange, "\n";
#      }
#  } else {
#      warn "Can't find MX records for $name: ", $res->errorstring, "\n";
#  }

  return @rthosts;
}

##################### The Work ##############################

if( @ARGV != 2 )
{
        usage();

}

my @rthosts = getRTHosts();

# Database Connection:
use Digest::SHA1 qw( sha1 sha1_hex );
use DBI qw(:sql_types);

my $dbh = DBI->connect('DBI:Pg:dbname=rtdb;host=localhost;port=9999', 'fake_user') 
#my $dbh = DBI->connect('DBI:Pg:dbname=rtdevdb', 'fake_user') 
          or die 'Couldn\'t connect to databse: ' . DBI->errstr;


my $attachmentsQuery = <prepare( $attachmentsQuery ) or die "Couldn't prepare Ticket Attachments statement: " . $dbh->errstr;

$sth->bind_param( 1, $ARGV[0], { TYPE => SQL_INTEGER } );
$sth->bind_param( 2, '%' . $ARGV[1] . '%', { TYPE => SQL_VARCHAR } );

$sth->execute( );

# Update Attatchments - prepare statement 
my $updateAttachmentQuery = <prepare( $updateAttachmentQuery ) or die "Couldn't prepare Attachment Update Statement: " . $dbh->errstr;

use Proc::InvokeEditor;
my $temp_edited_text;
my $cacheHashName = "";
my @cacheFiles;

print( "Transactions with confidential information:\n" );

while( @data = $sth->fetchrow_array() ) {
        print( "Transaction " . $data[0] . ' : ' . sha1_hex( $data[0] . '_' . $ARGV[0] ) . "\n");
         $temp_edited_text = Proc::InvokeEditor->edit($data[1]);
        # Double check changes before saving?

        $updateSth->bind_param( 1, $temp_edited_text, { TYPE => SQL_VARCHAR } );
        $updateSth->bind_param( 2, $data[2], { TYPE => SQL_INTEGER } );
        $updateSth->execute();
        
        $cacheHashName = sha1_hex( $data[0] . '_' . $ARGV[0] );
        push @cacheFiles, substr( $cacheHashName, 0, 1) . "/" . substr( $cacheHashName, 1, 1) . "/" . substr( $cacheHashName, 2, 1) . "/" . $cacheHashName;

 
}

$dbh->disconnect;

#delete files on rt's web hosts;

print( '*************** COMMANDS 2 DLT CACHE **************' . "\n" );

for $cacheFile ( @cacheFiles )
{
    print( RM_CMD  . MASON_TRANSACTION_CACHE_DIR . "/$cacheFile;" );

}


print( "\n" . '****************************************************' . "\n" );
1;