=================================================================== RCS file: /cvs/palm/Palm-Keyring/lib/Palm/Keyring.pm,v retrieving revision 1.16 retrieving revision 1.22 diff -u -r1.16 -r1.22 --- palm/Palm-Keyring/lib/Palm/Keyring.pm 2007/01/30 04:59:55 1.16 +++ palm/Palm-Keyring/lib/Palm/Keyring.pm 2007/02/01 01:56:11 1.22 @@ -1,6 +1,6 @@ package Palm::Keyring; -# $RedRiver: Keyring.pm,v 1.15 2007/01/29 02:49:41 andrew Exp $ +# $RedRiver: Keyring.pm,v 1.21 2007/01/31 05:32:20 andrew Exp $ # # Perl class for dealing with Keyring for Palm OS databases. # @@ -25,7 +25,7 @@ Readonly my $NULL => chr 0; # One liner, to allow MakeMaker to work. -our $VERSION = 0.91; +our $VERSION = 0.92; sub new { my $classname = shift; @@ -71,14 +71,13 @@ # skip the 0 record that holds the password return $rec if ! exists $self->{'records'}; - - # skip records with no data (There shouldn't be any) return $rec if ! exists $rec->{'data'}; my ( $name, $encrypted ) = split /$NULL/xm, $rec->{'data'}, 2; return $rec if ! $encrypted; - $rec->{'data'} = $name; + delete $rec->{'data'}; + $rec->{'name'} = $name; $rec->{'encrypted'} = $encrypted; return $rec; @@ -91,7 +90,11 @@ my $rec0_id = $self->{'records'}->[0]->{'id'}; if ($rec->{'encrypted'} && ! $rec->{'id'} == $rec0_id) { - $rec->{'data'} = join $NULL, $rec->{'data'}, $rec->{'encrypted'}; + if (! defined $rec->{'name'}) { + $rec->{'name'} = $EMPTY; + } + $rec->{'data'} = join $NULL, $rec->{'name'}, $rec->{'encrypted'}; + delete $rec->{'name'}; delete $rec->{'encrypted'}; } @@ -120,21 +123,79 @@ croak("Incorrect Password!\n"); } - $self->{'digest'} ||= _calc_keys( $pass ); + $self->{'digest'} ||= _calc_keys( $pass ); $data->{'account'} ||= $EMPTY; $data->{'password'} ||= $EMPTY; $data->{'notes'} ||= $EMPTY; + my $changed = 0; + my $need_newdate = 0; + my $acct = {}; + if ($rec->{'encrypted'}) { + $acct = $self->Decrypt($rec, $pass); + foreach my $key (keys %{ $data }) { + next if $key eq 'lastchange'; + if ($data->{$key} ne $acct->{$key}) { + $changed = 1; + last; + } + } + if ( exists $data->{'lastchange'} && exists $acct->{'lastchange'} && ( + $data->{'lastchange'}->{day} != $acct->{'lastchange'}->{day} || + $data->{'lastchange'}->{month} != $acct->{'lastchange'}->{month} || + $data->{'lastchange'}->{year} != $acct->{'lastchange'}->{year} + )) { + $changed = 1; + $need_newdate = 0; + } else { + $need_newdate = 1; + } + + } else { + $changed = 1; + } + + # no need to re-encrypt if it has not changed. + return 1 if ! $changed; + + my ($day, $month, $year); + + if ($data->{'lastchange'} && ! $need_newdate ) { + $day = $data->{'lastchange'}->{'day'} || 1; + $month = $data->{'lastchange'}->{'month'} || 0; + $year = $data->{'lastchange'}->{'year'} || 0; + + # XXX Need to actually validate the above information somehow + if ($year >= 1900) { + $year -= 1900; + } + } else { + $need_newdate = 1; + } + + if ($need_newdate) { + ($day, $month, $year) = (localtime)[3,4,5]; + } + $year -= 4; + $month++; + + + my $p = $day | ($month << 5) | ($year << 9); + my $packeddate = pack 'n', $p; + my $plaintext = join $NULL, - $data->{'account'}, $data->{'password'}, $data->{'notes'}; + $data->{'account'}, $data->{'password'}, $data->{'notes'}, $packeddate; my $encrypted = _crypt3des( $plaintext, $self->{'digest'}, $ENCRYPT ); return if ! $encrypted; - $rec->{'data'} ||= $data->{'name'}; + $rec->{'attributes'}{'Dirty'} = 1; + $rec->{'attributes'}{'dirty'} = 1; + $rec->{'name'} ||= $data->{'name'}; $rec->{'encrypted'} = $encrypted; + return 1; } @@ -148,8 +209,7 @@ } if ( ! $rec) { - carp("Needed parameter 'record' not passed!\n"); - return; + croak("Needed parameter 'record' not passed!\n"); } if ( ! $self->Password($pass)) { @@ -164,13 +224,29 @@ my $decrypted = _crypt3des( $rec->{'encrypted'}, $self->{'digest'}, $DECRYPT ); - my ( $account, $password, $notes, $extra ) = split /$NULL/xm, + my ( $account, $password, $notes, $packeddate ) = split /$NULL/xm, $decrypted, 4; + my %Modified; + if ($packeddate) { + my $u = unpack 'n', $packeddate; + my $year = (($u & 0xFE00) >> 9) + 4; # since 1900 + my $month = (($u & 0x01E0) >> 5) - 1; # 0-11 + my $day = (($u & 0x001F) >> 0); # 1-31 + + %Modified = ( + year => $year, + month => $month || 0, + day => $day || 1, + ); + } + return { - account => $account, - password => $password, - notes => $notes, + name => $rec->{'name'}, + account => $account, + password => $password, + notes => $notes, + lastchange => \%Modified, }; } @@ -195,7 +271,7 @@ } my $acct = $self->Decrypt($self->{'records'}->[$i], $pass); if ( ! $acct ) { - croak("Couldn't decrypt $self->{'records'}->[$i]->{'data'}"); + croak("Couldn't decrypt $self->{'records'}->[$i]->{'name'}"); } push @accts, $acct; } @@ -207,6 +283,7 @@ foreach my $i (0..$#accts) { next if $i == 0; + delete $self->{'records'}->[$i]->{'encrypted'}; $self->Encrypt($self->{'records'}->[$i], $accts[$i], $pass); } } @@ -391,13 +468,18 @@ use Palm::PDB; use Palm::Keyring; - my $pdb = new Palm::PDB; + + my $pass = 'password'; + my $file = 'Keys-Gtkr.pdb'; + my $pdb = new Palm::PDB; $pdb->Load($file); - foreach my $rec (@{ $pdb->{'records'} }) { - print "$rec->{'plaintext'}->{'name'}\n"; + + foreach (0..$#{ $pdb->{'records'} }) { + next if $_ = 0; # skip the password record + my $rec = $pdb->{'records'}->[$_]; + my $acct = $pdb->Decrypt($rec, $pass); + print $rec->{'name'}, ' - ', $acct->{'account'}, "\n"; } - $pdb->Decrypt($password); - # do something with the decrypted parts =head1 SUBROUTINES/METHODS @@ -422,17 +504,31 @@ $acct is a hashref in the format below. my $acct = { - account => $account, - password => $password, - notes => $notes, + name => $rec->{'name'}, + account => $account, + password => $password, + notes => $notes, + lastchange => { + year => 107, # years since 1900 + month => 0, # 0-11, 0 = January, 11 = December + day => 30, # 1-31, same as localtime + }, }; +If you have changed anything other than the lastchange, or don't pass in a +lastchange record, Encrypt() will generate a new lastchange for you. + +If you pass in a lastchange field that is different than the one in the +record, it will honor what you passed in. + +It also only uses the $acct->{'name'} if there is not already a $rec->{'name'}. + =head2 Decrypt my $acct = $pdb->Decrypt($rec[, $password]); Decrypts the record and returns a hashref for the account as described -under Encrypt(); +under Encrypt(). foreach (0..$#{ $pdb->{'records'}) { next if $_ == 0;