fpm2pass.pl (1768B)
1 #!/usr/bin/perl 2 3 # Copyright (C) 2012 Jeffrey Ratcliffe <jeffrey.ratcliffe@gmail.com>. All Rights Reserved. 4 # This file is licensed under the GPLv2+. Please see COPYING for more information. 5 6 use warnings; 7 use strict; 8 use XML::Simple; 9 use Getopt::Long; 10 use Pod::Usage; 11 12 my ($help, $man); 13 my @args = ('help' => \$help, 14 'man' => \$man,); 15 GetOptions (@args) or pod2usage(2); 16 pod2usage(1) if ($help); 17 pod2usage(-exitstatus => 0, -verbose => 2) if $man; 18 pod2usage( 19 -msg => "Syntax error: must specify a file to read.", 20 -exitval => 2, 21 -verbose => 1 22 ) 23 if (@ARGV != 1); 24 25 # Grab the XML to a perl structure 26 my $xs = XML::Simple->new(); 27 my $doc = $xs->XMLin(shift); 28 29 for (@{$doc->{PasswordList}{PasswordItem}}) { 30 my $name; 31 if (ref($_->{category}) eq 'HASH') { 32 $name = escape($_->{title}); 33 } 34 else { 35 $name = escape($_->{category})."/".escape($_->{title}); 36 } 37 my $contents = ''; 38 $contents .= "$_->{password}\n" unless (ref($_->{password}) eq 'HASH'); 39 $contents .= "user $_->{user}\n" unless (ref($_->{user}) eq 'HASH'); 40 $contents .= "url $_->{url}\n" unless (ref($_->{url}) eq 'HASH'); 41 unless (ref($_->{notes}) eq 'HASH') { 42 $_->{notes} =~ s/\n/\n /g; 43 $contents .= "notes:\n $_->{notes}\n"; 44 } 45 my $cmd = "pass insert -f -m $name"; 46 my $pid = open(my $fh, "| $cmd") or die "Couldn't fork: $!\n"; 47 print $fh $contents; 48 close $fh; 49 } 50 51 # escape inverted commas, spaces, ampersands and brackets 52 sub escape { 53 my ($s) = @_; 54 $s =~ s/\//-/g; 55 $s =~ s/(['\(\) &])/\\$1/g; 56 return $s; 57 } 58 59 =head1 NAME 60 61 fpm2pass.pl - imports an .xml exported by fpm2 into pass 62 63 =head1 SYNOPSIS 64 65 =head1 USAGE 66 67 fpm2pass.pl [--help] [--man] <xml> 68 69 The following options are available: 70 71 =over 72 73 =item --help 74 75 =item --man 76 77 =back 78 79 =cut