]>
Dogcows Code - chaz/p5-File-KDBX/blob - lib/File/KDBX/Dumper.pm
d300a02b0a930b41fcbe9956ee14b32adb5779ce
1 package File
::KDBX
::Dumper
;
2 # ABSTRACT: Write KDBX files
7 use Crypt
::Digest
qw(digest_data);
8 use File
::KDBX
::Constants
qw(:magic :header :version :random_stream);
10 use File
::KDBX
::Util
qw(:class);
14 use Ref
::Util
qw(is_ref is_scalarref);
15 use Scalar
::Util
qw(looks_like_number openhandle);
18 our $VERSION = '0.903'; # VERSION
23 my $self = bless {}, $class;
32 @$self{keys %args} = values %args;
39 my $format = shift // $self->format;
41 my $version = $self->kdbx->version;
45 if (defined $format) {
48 elsif (!defined $version) {
51 elsif ($self->kdbx->sig2 == KDBX_SIG2_1
) {
54 elsif (looks_like_number
($version)) {
55 my $major = $version & KDBX_VERSION_MAJOR_MASK
;
57 KDBX_VERSION_2_0
() => 'V3',
58 KDBX_VERSION_3_0
() => 'V3',
59 KDBX_VERSION_4_0
() => 'V4',
61 if ($major == KDBX_VERSION_2_0
) {
62 alert
sprintf("Upgrading KDBX version %x to version %x\n", $version, KDBX_VERSION_3_1
);
63 $self->kdbx->version(KDBX_VERSION_3_1
);
65 $subclass = $subclasses{$major}
66 or throw
sprintf('Unsupported KDBX file version: %x', $version), version
=> $version;
69 throw
sprintf('Unknown file version: %s', $version), version
=> $version;
72 load
"File::KDBX::Dumper::$subclass";
73 bless $self, "File::KDBX::Dumper::$subclass";
87 return $self->dump_handle($dst, @_) if openhandle
($dst);
88 return $self->dump_string($dst, @_) if is_scalarref
($dst);
89 return $self->dump_file($dst, @_) if defined $dst && !is_ref
($dst);
90 throw
'Programmer error: Must pass a stringref, filepath or IO handle to dump';
96 my $ref = is_scalarref
($_[0]) ? shift : undef;
97 my %args = @_ % 2 == 0 ? @_ : (key
=> shift, @_);
99 my $key = delete $args{key
};
100 $args{kdbx
} //= $self->kdbx;
107 open(my $fh, '>', $ref) or throw
"Failed to open string buffer: $!";
109 $self = $self->new if !ref $self;
110 $self->init(%args, fh
=> $fh)->_dump($fh, $key);
118 my $filepath = shift;
119 my %args = @_ % 2 == 0 ? @_ : (key
=> shift, @_);
121 my $key = delete $args{key
};
122 my $mode = delete $args{mode
};
123 my $uid = delete $args{uid
};
124 my $gid = delete $args{gid
};
125 my $atomic = delete $args{atomic
} // 1;
127 $args{kdbx
} //= $self->kdbx;
129 my ($fh, $filepath_temp);
132 ($fh, $filepath_temp) = eval { File
::Temp
::tempfile
("${filepath}-XXXXXX", UNLINK
=> 1) };
133 if (!$fh or my $err = $@) {
134 $err //= 'Unknown error';
135 throw
sprintf('Open file failed (%s): %s', $filepath_temp, $err),
137 filepath
=> $filepath_temp;
141 open($fh, '>:raw', $filepath) or throw
"Open file failed ($filepath): $!", filepath
=> $filepath;
145 $self = $self->new if !ref $self;
146 $self->init(%args, fh
=> $fh, filepath
=> $filepath);
147 $self->_dump($fh, $key);
150 my ($file_mode, $file_uid, $file_gid) = (stat($filepath))[2, 4, 5];
152 if ($filepath_temp) {
153 $mode //= $file_mode // do { my $m = umask; defined $m ? oct(666) &~ $m : undef };
154 $uid //= $file_uid // -1;
155 $gid //= $file_gid // -1;
156 chmod($mode, $filepath_temp) if defined $mode;
157 chown($uid, $gid, $filepath_temp);
158 rename($filepath_temp, $filepath) or throw
"Failed to write file ($filepath): $!",
159 filepath
=> $filepath;
169 my %args = @_ % 2 == 0 ? @_ : (key
=> shift, @_);
171 $fh = *STDOUT
if $fh eq '-';
173 my $key = delete $args{key
};
174 $args{kdbx
} //= $self->kdbx;
176 $self = $self->new if !ref $self;
177 $self->init(%args, fh
=> $fh)->_dump($fh, $key);
183 return File
::KDBX-
>new if !ref $self;
184 $self->{kdbx
} = shift if @_;
185 $self->{kdbx
} //= File
::KDBX-
>new;
189 has 'format', is => 'ro';
190 has 'inner_format', is => 'ro', default => 'XML';
191 has 'allow_upgrade', is => 'ro', default => 1;
192 has 'randomize_seeds', is => 'ro', default => 1;
194 sub _fh
{ $_[0]->{fh
} or throw
'IO handle not set' }
201 my $kdbx = $self->kdbx;
203 my $min_version = $kdbx->minimum_version;
204 if ($kdbx->version < $min_version && $self->allow_upgrade) {
205 alert
sprintf("Implicitly upgrading database from %x to %x\n", $kdbx->version, $min_version),
206 version
=> $kdbx->version, min_version
=> $min_version;
207 $kdbx->version($min_version);
211 if (ref($self) =~ /::(?:KDB|V[34])$/) {
212 $key //= $kdbx->key ? $kdbx->key->reload : undef;
213 defined $key or throw
'Must provide a master key', type
=> 'key.missing';
218 my $magic = $self->_write_magic_numbers($fh);
219 my $headers = $self->_write_headers($fh);
223 $self->_write_body($fh, $key, "$magic$headers");
230 my $kdbx = $self->kdbx;
232 if ($kdbx->version < KDBX_VERSION_4_0
) {
233 # force Salsa20 inner random stream
234 $kdbx->inner_random_stream_id(STREAM_ID_SALSA20
);
235 my $key = $kdbx->inner_random_stream_key;
236 substr($key, 32) = '';
237 $kdbx->inner_random_stream_key($key);
240 $kdbx->randomize_seeds if $self->randomize_seeds;
243 sub _write_magic_numbers
{
247 my $kdbx = $self->kdbx;
249 $kdbx->sig1 == KDBX_SIG1
or throw
'Invalid file signature', sig1
=> $kdbx->sig1;
250 $kdbx->version < KDBX_VERSION_OLDEST
|| KDBX_VERSION_LATEST
< $kdbx->version
251 and throw
'Unsupported file version', version
=> $kdbx->version;
253 my @magic = ($kdbx->sig1, $kdbx->sig2, $kdbx->version);
255 my $buf = pack('L<3', @magic);
256 $fh->print($buf) or throw
'Failed to write file signature';
261 sub _write_headers
{ die "Not implemented" }
263 sub _write_body
{ die "Not implemented" }
265 sub _write_inner_body
{
268 my $current_pkg = ref $self;
269 require Scope
::Guard
;
270 my $guard = Scope
::Guard-
>new(sub { bless $self, $current_pkg });
272 $self->_rebless($self->inner_format);
273 $self->_write_inner_body(@_);
286 File::KDBX::Dumper - Write KDBX files
296 $kdbx = $dumper->kdbx;
297 $dumper->kdbx($kdbx);
299 Get or set the L<File::KDBX> instance with the data to be dumped.
303 Get the file format used for writing the database. Normally the format is auto-detected from the database,
304 which is the safest choice. Possible formats:
322 C<XML> (only used if explicitly set)
326 C<Raw> (only used if explicitly set)
330 B<WARNING:> There is a potential for data loss if you explicitly use a format that doesn't support the
331 features used by the KDBX database being written.
333 The most common reason to explicitly specify the file format is to save a database as an unencrypted XML file:
335 $kdbx->dump_file('database.xml', format => 'XML');
339 Get the format of the data inside the KDBX envelope. This only applies to C<V3> and C<V4> formats. Possible
346 C<XML> - Write the database groups and entries as XML (default)
350 C<Raw> - Write L<File::KDBX/raw> instead of the actual database contents
356 $bool = $dumper->allow_upgrade;
358 Whether or not to allow implicitly upgrading a database to a newer version. When enabled, in order to avoid
359 potential data loss, the database can be upgraded as-needed in cases where the database file format version is
360 too low to support new features being used.
362 The default is to allow upgrading.
364 =head2 randomize_seeds
366 $bool = $dumper->randomize_seeds;
368 Whether or not to randomize seeds in a database before writing. The default is to randomize seeds, and there's
369 not often a good reason not to do so. If disabled, the seeds associated with the KDBX database will be used as
376 $dumper = File::KDBX::Dumper->new(%attributes);
378 Construct a new L<File::KDBX::Dumper>.
382 $dumper = $dumper->init(%attributes);
384 Initialize a L<File::KDBX::Dumper> with a new set of attributes.
386 This is called by L</new>.
390 $dumper = $dumper->reset;
392 Set a L<File::KDBX::Dumper> to a blank state, ready to dump another KDBX file.
396 $dumper->dump(\$string, %options);
397 $dumper->dump(\$string, $key, %options);
398 $dumper->dump(*IO, %options);
399 $dumper->dump(*IO, $key, %options);
400 $dumper->dump($filepath, %options);
401 $dumper->dump($filepath, $key, %options);
405 The C<$key> is either a L<File::KDBX::Key> or a primitive castable to a Key object. Available options:
411 C<kdbx> - Database to dump (default: value of L</kdbx>)
415 C<key> - Alternative way to specify C<$key> (default: value of L</File::KDBX/key>)
419 Other options are supported depending on the first argument. See L</dump_string>, L</dump_file> and
424 $dumper->dump_string(\$string, %options);
425 $dumper->dump_string(\$string, $key, %options);
426 \$string = $dumper->dump_string(%options);
427 \$string = $dumper->dump_string($key, %options);
429 Dump a KDBX file to a string / memory buffer. Available options:
435 C<kdbx> - Database to dump (default: value of L</kdbx>)
439 C<key> - Alternative way to specify C<$key> (default: value of L</File::KDBX/key>)
445 $dumper->dump_file($filepath, %options);
446 $dumper->dump_file($filepath, $key, %options);
448 Dump a KDBX file to a filesystem. Available options:
454 C<kdbx> - Database to dump (default: value of L</kdbx>)
458 C<key> - Alternative way to specify C<$key> (default: value of L</File::KDBX/key>)
462 C<mode> - File mode / permissions (see L<perlfunc/"chmod LIST">
466 C<uid> - User ID (see L<perlfunc/"chown LIST">)
470 C<gid> - Group ID (see L<perlfunc/"chown LIST">)
474 C<atomic> - Write to the filepath atomically (default: true)
480 $dumper->dump_handle($fh, %options);
481 $dumper->dump_handle(*IO, $key, %options);
482 $dumper->dump_handle($fh, %options);
483 $dumper->dump_handle(*IO, $key, %options);
485 Dump a KDBX file to an output stream / file handle. Available options:
491 C<kdbx> - Database to dump (default: value of L</kdbx>)
495 C<key> - Alternative way to specify C<$key> (default: value of L</File::KDBX/key>)
501 Please report any bugs or feature requests on the bugtracker website
502 L<https://github.com/chazmcgarvey/File-KDBX/issues>
504 When submitting a bug or request, please include a test-file or a
505 patch to an existing test-file that illustrates the bug or desired
510 Charles McGarvey <ccm@cpan.org>
512 =head1 COPYRIGHT AND LICENSE
514 This software is copyright (c) 2022 by Charles McGarvey.
516 This is free software; you can redistribute it and/or modify it under
517 the same terms as the Perl 5 programming language system itself.
This page took 0.059369 seconds and 3 git commands to generate.