#!/usr/bin/perl #=============================================================================== # # FILE: migrate_to_ng_sqlite.pl # # USAGE: ./migrate_to_ng_sqlite.pl -d # # DESCRIPTION: export state from a kasp.db file to xml. # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: --- # NOTES: --- # AUTHOR: SiƓn Lloyd (SL), sion@nominet.org.uk # COMPANY: Nominet # VERSION: 1.0 # CREATED: 26/01/12 11:12:07 # REVISION: --- #=============================================================================== use strict; use warnings; use DBI; use DBD::SQLite; use Getopt::Std qw(getopts); my %sm; my %policy; use vars ( q!$opt_d!, # Database file fo convert ); getopts('d:') or die "Please supply a database file to work on with the -d flag"; if (!$opt_d) { print STDERR "Please supply a database file to work on with the -d flag\n"; exit 1; } open my $OUT, '>', "enforcerstate.xml" or die "$0 : failed to open output file 'enforcerstate.xml' : $!\n"; my $date = localtime; print $OUT "\n"; print $OUT "\n\n"; print $OUT "\n"; ### # Make sure that we can connect to this database my $dbh = DBI->connect("dbi:SQLite:dbname=$opt_d","","") or die "Couldn't connect: $!"; ### # Prepare a keys statement that we will need later my $keys_sth = $dbh->prepare("select dk.id, keypair_id, keytype, state, publish, ready, active, retire, dead, fixedDate from dnsseckeys dk, keypairs kp where dk.keypair_id = kp.id and zone_id = ?") or die "Couldn't prepare keys_sth $!"; my $KEYPAIR_ID=1; my $KEYTYPE=2; my $STATE=3; my $PUBLISH=4; my $READY=5; my $ACTIVE=6; my $RETIRE=7; my $DEAD=8; my $FIXED_DATE = 9; ### # Create hashmap of securitymodules table my $sm_sth = $dbh->prepare("select id, name from securitymodules") or die "Couldn't prepare sm_sth $!"; $sm_sth->execute(); while (my @row = $sm_sth->fetchrow_array) { $sm{ $row[0] } = $row[1]; } ### # Create a hashmap of the policy info we need my $ID=0; my $NAME=1; my $SALT=2; my $SALT_STAMP=3; my $NSEC=4; my $policy_sth = $dbh->prepare("select p.id, name, salt, salt_stamp, pp.value from policies p, parameters_policies pp where p.id = pp.policy_id and pp.parameter_id = 9") or die "Couldn't prepare policy_sth $!"; $policy_sth->execute(); while (my @row = $policy_sth->fetchrow_array) { $row[$SALT_STAMP] =~ s/ /T/ if $row[$SALT_STAMP]; @{ $policy { $row[0] }} = @row; } ### # Let's go to work. Loop over zones my $zone_sth = $dbh->prepare("select id, name, policy_id from zones") or die "Couldn't prepare zone_sth $!"; $zone_sth->execute(); print $OUT " \n"; while (my @row = $zone_sth->fetchrow_array) { print $OUT " \n"; # Get and write keys $keys_sth->execute( $row[0] ); print $OUT " \n"; while (my @key = $keys_sth->fetchrow_array) { print $OUT " \n"; print $OUT " $key[$KEYPAIR_ID]\n"; print $OUT " ZSK\n" if $key[$KEYTYPE] == 256; print $OUT " KSK\n" if $key[$KEYTYPE] == 257; print $OUT " \n" if $key[$STATE] > 6; if ($key[$PUBLISH]) { $key[$PUBLISH] =~ s/ /T/; print $OUT " $key[$PUBLISH]\n"; } if ($key[$READY] && $key[$STATE] > 2) { $key[$READY] =~ s/ /T/; print $OUT " $key[$READY]\n"; } if ($key[$ACTIVE] && $key[$STATE] > 3) { $key[$ACTIVE] =~ s/ /T/; print $OUT " $key[$ACTIVE]\n"; } if ($key[$RETIRE] && ($key[$STATE] > 4 || $key[$FIXED_DATE] == 1)) { $key[$RETIRE] =~ s/ /T/; print $OUT " $key[$RETIRE]\n"; } if ($key[$DEAD] && $key[$STATE] > 5) { $key[$DEAD] =~ s/ /T/; print $OUT " $key[$DEAD]\n"; } print $OUT " \n"; } print $OUT " \n"; if (${ $policy{$row[2]} }[$NSEC] == 3) { print $OUT "\n \n"; print $OUT " ${ $policy{$row[2]} }[$SALT]\n"; print $OUT " ${ $policy{$row[2]} }[$SALT_STAMP]\n"; print $OUT " \n"; } print $OUT " \n"; } print $OUT " \n\n"; ### # Now add the keypairs my $keypair_sth = $dbh->prepare("select id, algorithm, size, securitymodule_id, HSMkey_id, policy_id, generate, backup, compromisedflag from keypairs") or die "Couldn't prepare keypair_sth $!"; $keypair_sth->execute(); my $ALGORITHM=1; my $SIZE=2; my $SM_ID=3; my $HSMKEY_ID=4; my $POLICY_ID=5; my $GENERATE=6; my $BACKUP=7; my $COMPROMISED=8; print $OUT " \n"; while (my @row = $keypair_sth->fetchrow_array) { print $OUT " \n"; print $OUT " $row[$ALGORITHM]\n"; print $OUT " $row[$SIZE]\n"; print $OUT " $sm{ $row[$SM_ID] }\n"; print $OUT " $row[$HSMKEY_ID]\n"; print $OUT " ${ $policy{ $row[$POLICY_ID] }}[$NAME]\n"; $row[$GENERATE] =~ s/ /T/; print $OUT " $row[$GENERATE]\n"; if ($row[$BACKUP]) { $row[$BACKUP] =~ s/ /T/; print $OUT " $row[$BACKUP]\n"; } print $OUT " \n" if $row[$COMPROMISED]; print $OUT " \n"; } print $OUT " \n"; print $OUT "\n"; close $OUT; $dbh->disconnect;