#!/usr/bin/perl use XML::Parser; sub start_element(); sub end_element(); sub characters(); $|=1; $keyring_file=$ARGV[0]; $pwsafe_db=$ARGV[1]; if ($#ARGV != 1) { print "Usage: keyring_to_pwsafe keyring_file.xml pwsafe_database.dat\n"; exit 1; } $PWSAFE_CMD="/usr/local/bin/pwsafe"; if (!( -f $pwsafe_db )) { print "$pwsafe_db file does not exist\n"; exit(1); } $parser = new XML::Parser(); $parser->setHandlers(Start => \&start_element, End => \&end_element, Char => \&characters); @account_list=(); %account=(); $parser->parsefile($keyring_file) or die "Cannot parse $keyring_file: $!\n"; print "Total account(s): ".($#account_list+1)."\n"; print "Enter password for the database: "; $db_pwd=; chomp $db_pwd; open(PWSAFE_PIPE,"|$PWSAFE_CMD -f $pwsafe_db -a") or die "Cannot run $PWSAFE_CMF: $!\n"; print PWSAFE_PIPE $db_pwd."\n"; $acct_num = $#account_list; foreach $ar (@account_list) { local %a = %$ar; out_line(\*PWSAFE_PIPE, $a{name}, ""); out_line(\*PWSAFE_PIPE, $a{category}, ""); out_line(\*PWSAFE_PIPE, $a{user}, "default"); out_line(\*PWSAFE_PIPE, $a{password}, ""); out_line(\*PWSAFE_PIPE, $a{password}, ""); out_line(\*PWSAFE_PIPE, $a{note}, ""); print PWSAFE_PIPE ($acct_num == 0 ? "n" : "y"); $acct_num--; print "".($acct_num+1)." more account(s) to transfer\n"; } close(PWSAFE_PIPE); sub out_line() { local $fh = shift; local $str = shift; local $default = shift; if (!(defined $str) || $str eq "") { $str = $default; } if ($str eq "") { print $fh "\n"; return; } local @lines = split(/\n/, $str); for(local $i = 0; $i <= $#lines; $i++) { print $fh $lines[$i].($i < $#lines ? "\\" : "")."\n"; } if ($str =~ /^.*\\$/) { print $fh "\n"; } } sub start_element() { local $expat = shift; local $element = shift; if ($element eq "account") { %account={}; } } sub end_element() { local $expat = shift; local $element = shift; if ($element eq "account") { local %a = %account; push(@account_list,\%a); } } sub characters() { local $expat = shift; local $string = shift; return if ($string =~ /^ *$/); local $owner_element = $expat->current_element(); if ($account{$owner_element} ne "") { $account{$owner_element}.="\n"; } $account{$owner_element}.=$string; }