X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FFile%2FKDBX%2FDumper%2FXML.pm;h=5b376b9e5cc820e8bbc28fc9bf6a523511c53f4a;hb=c98fc7d0294e641cf8844306808333bdec4fea2f;hp=23378b60aabe240b92ef57594e2d3ad44198c98e;hpb=f63182fc62b25269b1c38588dca2b3535ed1a1a2;p=chaz%2Fp5-File-KDBX diff --git a/lib/File/KDBX/Dumper/XML.pm b/lib/File/KDBX/Dumper/XML.pm index 23378b6..5b376b9 100644 --- a/lib/File/KDBX/Dumper/XML.pm +++ b/lib/File/KDBX/Dumper/XML.pm @@ -9,42 +9,68 @@ use Crypt::Misc 0.029 qw(encode_b64); use Encode qw(encode); use File::KDBX::Constants qw(:version :time); use File::KDBX::Error; -use File::KDBX::Util qw(assert_64bit erase_scoped gzip snakify); +use File::KDBX::Util qw(:class assert_64bit erase_scoped gzip snakify); use IO::Handle; -use Scalar::Util qw(isdual looks_like_number); -use Scope::Guard; +use Scalar::Util qw(blessed isdual looks_like_number); use Time::Piece; use XML::LibXML; use boolean; use namespace::clean; -use parent 'File::KDBX::Dumper'; +extends 'File::KDBX::Dumper'; our $VERSION = '999.999'; # VERSION -sub protect { - my $self = shift; - $self->{protect} = shift if @_; - $self->{protect} //= 1; -} +=attr allow_protection -sub binaries { - my $self = shift; - $self->{binaries} = shift if @_; - $self->{binaries} //= $self->kdbx->version < KDBX_VERSION_4_0; -} + $bool = $dumper->allow_protection; -sub compress_binaries { - my $self = shift; - $self->{compress_binaries} = shift if @_; - $self->{compress_binaries}; -} +Get whether or not protected strings and binaries should be written in an encrypted stream. Default: C -sub compress_datetimes { - my $self = shift; - $self->{compress_datetimes} = shift if @_; - $self->{compress_datetimes}; -} +=attr binaries + + $bool = $dumper->binaries; + +Get whether or not binaries within the database should be written. Default: C + +=attr compress_binaries + + $tristate = $dumper->compress_binaries; + +Get whether or not to compress binaries. Possible values: + +=for :list +* C - Always compress binaries +* C - Never compress binaries +* C - Compress binaries if it results in smaller database sizes (default) + +=attr compress_datetimes + + $bool = $dumper->compress_datetimes; + +Get whether or not to write compressed datetimes. Datetimes are traditionally written in the human-readable +string format of C<1970-01-01T00:00:00Z>, but they can also be written in a compressed form to save some +bytes. The default is to write compressed datetimes if the KDBX file version is 4+, otherwise use the +human-readable format. + +=attr header_hash + + $octets = $dumper->header_hash; + +Get the value to be written as the B in the B section. This is the way KDBX3 files validate +the authenticity of header data. This is unnecessary and should not be used with KDBX4 files because that +format uses HMAC-SHA256 to detect tampering. + +L automatically calculates the header hash an provides it to this module, and plain +XML files which don't have a KDBX wrapper don't have headers and so should have a header hash. Therefore there +is probably never any reason to set this manually. + +=cut + +has allow_protection => 1; +has binaries => sub { $_[0]->kdbx->version < KDBX_VERSION_4_0 }; +has 'compress_binaries'; +has 'compress_datetimes'; sub header_hash { $_[0]->{header_hash} } @@ -152,8 +178,8 @@ sub _write_xml_binaries { my $new_ref = keys %{$self->_binaries_written}; my $written = $self->_binaries_written; - my $entries = $kdbx->all_entries(history => true); - for my $entry (@$entries) { + my $entries = $kdbx->entries(history => 1); + while (my $entry = $entries->next) { for my $key (keys %{$entry->binaries}) { my $binary = $entry->binaries->{$key}; if (defined $binary->{ref} && defined $kdbx->binaries->{$binary->{ref}}) { @@ -195,15 +221,15 @@ sub _write_xml_compressed_content { $value = \$encoded; } - my $always_compress = $self->compress_binaries; - my $try_compress = $always_compress || !defined $always_compress; + my $should_compress = $self->compress_binaries; + my $try_compress = $should_compress || !defined $should_compress; my $compressed; if ($try_compress) { $compressed = gzip($$value); push @cleanup, erase_scoped $compressed; - if ($always_compress || length($compressed) < length($$value)) { + if ($should_compress || length($compressed) < length($$value)) { $value = \$compressed; $node->setAttribute('Compressed', _encode_bool(true)); } @@ -223,10 +249,10 @@ sub _write_xml_custom_icons { my $self = shift; my $node = shift; - my $custom_icons = $self->kdbx->meta->{custom_icons} || {}; + my $custom_icons = $self->kdbx->custom_icons; - for my $uuid (sort keys %$custom_icons) { - my $icon = $custom_icons->{$uuid}; + for my $icon (@$custom_icons) { + $icon->{uuid} && $icon->{data} or next; my $icon_node = $node->addNewChild(undef, 'Icon'); $self->_write_xml_from_pairs($icon_node, $icon, @@ -266,13 +292,11 @@ sub _write_xml_root { my $node = shift; my $kdbx = $self->kdbx; - my $is_locked = $kdbx->is_locked; - my $guard = Scope::Guard->new(sub { $kdbx->lock if $is_locked }); - $kdbx->unlock; + my $guard = $kdbx->unlock_scoped; - if (my $group = $kdbx->{root}) { + if (my $group = $kdbx->root) { my $group_node = $node->addNewChild(undef, 'Group'); - $self->_write_xml_group($group_node, $group); + $self->_write_xml_group($group_node, $group->_committed); } undef $guard; # re-lock if needed, as early as possible @@ -311,14 +335,14 @@ sub _write_xml_group { ) : (), ); - for my $entry (@{$group->{entries} || []}) { + for my $entry (@{$group->entries}) { my $entry_node = $node->addNewChild(undef, 'Entry'); - $self->_write_xml_entry($entry_node, $entry); + $self->_write_xml_entry($entry_node, $entry->_committed); } - for my $group (@{$group->{groups} || []}) { + for my $group (@{$group->groups}) { my $group_node = $node->addNewChild(undef, 'Group'); - $self->_write_xml_group($group_node, $group); + $self->_write_xml_group($group_node, $group->_committed); } } @@ -395,11 +419,11 @@ sub _write_xml_entry { ); if (!$in_history) { - if (my @history = @{$entry->{history} || []}) { + if (my @history = @{$entry->history}) { my $history_node = $node->addNewChild(undef, 'History'); for my $historical (@history) { my $historical_node = $history_node->addNewChild(undef, 'Entry'); - $self->_write_xml_entry($historical_node, $historical, 1); + $self->_write_xml_entry($historical_node, $historical->_committed, 1); } } } @@ -461,7 +485,7 @@ sub _write_xml_entry_string { my $protect = $string->{protect} || $memory_protection->{$memprot_key}; if ($protect) { - if ($self->protect) { + if ($self->allow_protection) { my $encoded; if (utf8::is_utf8($value)) { $encoded = encode('UTF-8', $value); @@ -541,7 +565,6 @@ sub _encode_bool { } sub _encode_datetime { - goto &_encode_datetime_binary if defined $_[2] && KDBX_VERSION_4_0 <= $_[2]; local $_ = shift; return $_->strftime('%Y-%m-%dT%H:%M:%SZ'); }