| 1 | # | ||
| 2 | # MTDB.pm (Mark Thomson's DataBase) | ||
| 3 | # store multi-level hash structure in single level tied hash | ||
| 4 | # and save that to a flat text database optionally with encryption | ||
| 5 | # Copyright (c) May 2002 Mark Ralf Thomson. All rights reserved. | ||
| 6 | # This program is free software; you can redistribute it and/or modify it | ||
| 7 | # under the same terms as Perl itself. | ||
| 8 | # Contains code from MLDBM.pm written by Gurusamy Sarathy and Raphael Manfredi | ||
| 9 | |||
| 10 | #################################################################### | ||
| 11 | |||
| 12 | require 5.004; | ||
| 13 | package MTDB::Serializer; ## deferred | ||
| 14 | use Carp; | ||
| 15 | |||
| 16 | sub new { bless {}, shift }; | ||
| 17 | |||
| 18 | sub serialize { confess "deferred" }; | ||
| 19 | |||
| 20 | sub deserialize { confess "deferred" }; | ||
| 21 | |||
| 22 | |||
| 23 | # | ||
| 24 | # Attributes: | ||
| 25 | # | ||
| 26 | # dumpmeth: | ||
| 27 | # the preferred dumping method. | ||
| 28 | # | ||
| 29 | # removetaint: | ||
| 30 | # untainting flag; when true, data will be untainted after | ||
| 31 | # extraction from the database. | ||
| 32 | # | ||
| 33 | # key: | ||
| 34 | # the magic string used to recognize non-natively stored data. | ||
| 35 | # | ||
| 36 | # Attribute access methods: | ||
| 37 | # | ||
| 38 | # These defaults allow readonly access. Sub-class may override | ||
| 39 | # them to allow write access if any of these attributes | ||
| 40 | # makes sense for it. | ||
| 41 | # | ||
| 42 | |||
| 43 | sub DumpMeth { | ||
| 44 | my $s = shift; | ||
| 45 | confess "can't set dumpmeth with " . ref($s) if @_; | ||
| 46 | $s->_attrib('dumpmeth'); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub RemoveTaint { | ||
| 50 | my $s = shift; | ||
| 51 | confess "can't set untaint with " . ref($s) if @_; | ||
| 52 | $s->_attrib('removetaint'); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub Key { | ||
| 56 | my $s = shift; | ||
| 57 | confess "can't set key with " . ref($s) if @_; | ||
| 58 | $s->_attrib('key'); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _attrib { | ||
| 62 | my ($s, $a, $v) = @_; | ||
| 63 | if (ref $s and @_ > 2) { | ||
| 64 | $s->{$a} = $v; | ||
| 65 | return $s; | ||
| 66 | } | ||
| 67 | $s->{$a}; | ||
| 68 | } | ||
| 69 | |||
| 70 | #################################################################### | ||
| 71 | |||
| 72 | package MTDB; | ||
| 73 | use Crypt::CBC; | ||
| 74 | require Tie::Hash; | ||
| 75 | @MTDB::ISA = 'Tie::Hash'; | ||
| 76 | |||
| 77 | use vars qw($VERSION); | ||
| 78 | |||
| 79 | $VERSION = '0.1'; | ||
| 80 | |||
| 81 | use Carp; | ||
| 82 | |||
| 83 | $MTDB::Serializer = 'Data::Dumper' unless $MTDB::Serializer; | ||
| 84 | $MTDB::Key = 'MTDB' unless $MTDB::Key; | ||
| 85 | $MTDB::DumpMeth = "" unless $MTDB::DumpMeth; | ||
| 86 | $MTDB::RemoveTaint = 0 unless $MTDB::RemoveTaint; | ||
| 87 | |||
| 88 | my $loadpack = sub { | ||
| 89 | my $pack = shift; | ||
| 90 | $pack =~ s|::|/|g; | ||
| 91 | $pack .= ".pm"; | ||
| 92 | eval { require $pack }; | ||
| 93 | if ($@) { | ||
| 94 | carp "MTDB error: " . | ||
| 95 | "Please make sure $pack is a properly installed package.\n" . | ||
| 96 | "\tPerl says: \"$@\""; | ||
| 97 | return -9; | ||
| 98 | } | ||
| 99 | 1; | ||
| 100 | }; | ||
| 101 | |||
| 102 | sub TIEHASH { | ||
| 103 | my $class = shift; | ||
| 104 | my %params = @_; | ||
| 105 | my(@_hdat,$name,$wert); | ||
| 106 | |||
| 107 | my $self = {}; | ||
| 108 | bless $self, $class; | ||
| 109 | |||
| 110 | |||
| 111 | $self->{__param}{FILE} = $params{FILE} || 'data.db'; | ||
| 112 | $self->{__param}{CRYPT} = $params{CRYPT} || 0; | ||
| 113 | $self->{__param}{MODE} = $params{MODE} || 0664; | ||
| 114 | $self->{__param}{LOCK} = $params{LOCK} || 0; | ||
| 115 | $self->{__param}{CREATE} = $params{CREATE} || 0; | ||
| 116 | $self->{__param}{SAFER} = $params{SAFER} || 0; | ||
| 117 | |||
| 118 | if (!-e $self->{__param}{FILE}) { | ||
| 119 | if (!$self->{__param}{CREATE}) {return -1;} | ||
| 120 | |||
| 121 | open(HF,">$self->{__param}{FILE}") || return -3; | ||
| 122 | close HF; | ||
| 123 | if(!chmod 0755, $self->{__param}{FILE}) { return -2;} | ||
| 124 | } | ||
| 125 | my $szr = $MTDB::Serializer; | ||
| 126 | unless (ref $szr) | ||
| 127 | { | ||
| 128 | $szr = "MTDB::Serializer::$szr" # allow convenient short names | ||
| 129 | unless $szr =~ /^MTDB::Serializer::/; | ||
| 130 | &$loadpack($szr) or return -10; | ||
| 131 | $szr = $szr->new($MTDB::DumpMeth, | ||
| 132 | $MTDB::RemoveTaint, | ||
| 133 | $MTDB::Key); | ||
| 134 | } | ||
| 135 | $self->Serializer($szr); | ||
| 136 | |||
| 137 | if ($self->{__param}{CRYPT}) | ||
| 138 | { | ||
| 139 | $self->{__param}{DCOBJ} = Crypt::CBC->new( | ||
| 140 | { 'key' => $self->{__param}{CRYPT}, | ||
| 141 | 'cipher' => 'Blowfish', | ||
| 142 | 'iv' => '?pr?Nt:}', | ||
| 143 | 'regenerate_key' => 0, | ||
| 144 | 'padding' => 'space', | ||
| 145 | 'prepend_iv' => 0, | ||
| 146 | } ); | ||
| 147 | croak ("Error - crypt key: self->{__param}{DCOBJ} - $!") if (!$self->{__param}{DCOBJ}); | ||
| 148 | } | ||
| 149 | open(HF,"<$self->{__param}{FILE}") || return -4; | ||
| 150 | flock(HF,1) if ($self->{__param}{LOCK}); | ||
| 151 | @_hdat = <HF>; | ||
| 152 | close (HF); | ||
| 153 | |||
| 154 | foreach (@_hdat) | ||
| 155 | { | ||
| 156 | chomp $_; | ||
| 157 | $_ = $self->{__param}{DCOBJ}->decrypt_hex($_) if ($self->{__param}{DCOBJ}); | ||
| 158 | ($name, $wert) = split( /\t/ , $_ ); | ||
| 159 | $wert =~ s/\\n/\n/g; | ||
| 160 | $wert =~ s/\\t/\t/g; | ||
| 161 | |||
| 162 | $self->{$name} = $wert; | ||
| 163 | } | ||
| 164 | undef @_hdat; | ||
| 165 | |||
| 166 | return $self; | ||
| 167 | |||
| 168 | } | ||
| 169 | |||
| 170 | sub new { &TIEHASH } | ||
| 171 | |||
| 172 | sub FETCH { | ||
| 173 | my ($self, $key) = @_; | ||
| 174 | my($temp); | ||
| 175 | return undef if ($key eq '__param'); | ||
| 176 | |||
| 177 | if(exists ($self->{$key})) | ||
| 178 | { | ||
| 179 | $temp = $self->{__param}{SR}->deserialize($self->{$key}); | ||
| 180 | if ( exists($temp->{$self->{__param}{KEY}}) && exists($self->{__param}{PHRASE})) | ||
| 181 | { $self->decrypt_r($temp->{$self->{__param}{KEY}}); } | ||
| 182 | return $temp; | ||
| 183 | } | ||
| 184 | |||
| 185 | #$self->{$key} = undef; | ||
| 186 | #return $self->{__param}{SR}->deserialize($self->{$key}); | ||
| 187 | |||
| 188 | return undef; | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | sub STORE { | ||
| 193 | my ($self, $key, $value) = @_; | ||
| 194 | |||
| 195 | if ((exists $self->{$key} && $self->{$key} == $value) | ||
| 196 | || $key eq '__param') | ||
| 197 | { return undef; } | ||
| 198 | |||
| 199 | if ( exists($value->{$self->{__param}{KEY}}) && exists($self->{__param}{PHRASE})) | ||
| 200 | { $self->encrypt_r($value->{$self->{__param}{KEY}}); } #ref | ||
| 201 | |||
| 202 | $value = $self->{__param}{SR}->serialize($value); | ||
| 203 | |||
| 204 | $self->{$key} = $value; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub encrypt_r | ||
| 208 | { | ||
| 209 | my ($self) = shift; | ||
| 210 | my ($rkey) = shift; | ||
| 211 | my ($key,$value); | ||
| 212 | if (ref($rkey) eq 'HASH') | ||
| 213 | { | ||
| 214 | while (($key, $value) = each %$rkey) | ||
| 215 | { | ||
| 216 | if (ref($rkey->{$key}) eq 'HASH') | ||
| 217 | { $self->encrypt_r($value); } | ||
| 218 | else {$rkey->{$key} = $self->encrypt($value); } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | else {return $self->encrypt($rkey);} | ||
| 222 | } | ||
| 223 | |||
| 224 | sub decrypt_r | ||
| 225 | { | ||
| 226 | my ($self) = shift; | ||
| 227 | my ($rkey) = shift; | ||
| 228 | my ($key,$value); | ||
| 229 | if (ref($rkey) eq 'HASH') | ||
| 230 | { | ||
| 231 | while (($key, $value) = each %$rkey) | ||
| 232 | { | ||
| 233 | if (ref($rkey->{$key}) eq 'HASH') | ||
| 234 | {$self->decrypt_r($value); } | ||
| 235 | else {$rkey->{$key} = $self->decrypt($value);} | ||
| 236 | } | ||
| 237 | } | ||
| 238 | else {return $self->decrypt($rkey);} | ||
| 239 | } | ||
| 240 | |||
| 241 | sub encrypt | ||
| 242 | { | ||
| 243 | my ($self,$text) = @_; | ||
| 244 | $text = $self->{__param}{COBJ}->encrypt($text); | ||
| 245 | return $text; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub decrypt | ||
| 249 | { | ||
| 250 | my ($self,$text) = @_; | ||
| 251 | $text = $self->{__param}{COBJ}->decrypt($text); | ||
| 252 | return $text; | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | sub FIRSTKEY { | ||
| 258 | my $self = shift; | ||
| 259 | my $it_pair; | ||
| 260 | |||
| 261 | my @hkeys = keys %$self; | ||
| 262 | $self->{__param}{ITERATOR} = \@hkeys; | ||
| 263 | |||
| 264 | $it_pair = shift @{$self->{__param}{ITERATOR}}; | ||
| 265 | if($it_pair eq '__param') { | ||
| 266 | $it_pair = shift @{$self->{__param}{ITERATOR}}; | ||
| 267 | } | ||
| 268 | return $it_pair; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub NEXTKEY { | ||
| 272 | my $self = shift; | ||
| 273 | my $it_pair; | ||
| 274 | |||
| 275 | $it_pair = shift @{$self->{__param}{ITERATOR}}; | ||
| 276 | if($it_pair eq '__param') { | ||
| 277 | $it_pair = shift @{$self->{__param}{ITERATOR}}; | ||
| 278 | } | ||
| 279 | return $it_pair; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub EXISTS { | ||
| 283 | my ($self, $key) = @_; | ||
| 284 | |||
| 285 | return exists $self->{$key}; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub DELETE { | ||
| 289 | my ($self, $key) = @_; | ||
| 290 | |||
| 291 | delete $self->{$key} if exists $self->{$key}; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub sync { | ||
| 295 | my $self = shift; | ||
| 296 | my($key, $value); | ||
| 297 | open(HF,">$self->{__param}{FILE}") || return undef; | ||
| 298 | flock(HF,2) if ($self->{__param}{LOCK}); | ||
| 299 | while (($key, $value) = each %$self) | ||
| 300 | { | ||
| 301 | if($key eq '__param') { next; } | ||
| 302 | $value =~ s/\n/\\n/g; | ||
| 303 | $value =~ s/\t/\\t/g; | ||
| 304 | if($self->{__param}{DCOBJ}) | ||
| 305 | {print HF $self->{__param}{DCOBJ}->encrypt_hex("$key\t$value")."\n"; } | ||
| 306 | else { print HF "$key\t$value\n"; } | ||
| 307 | } | ||
| 308 | close HF; | ||
| 309 | |||
| 310 | if (defined $self->{__param}{MODE}) { | ||
| 311 | chmod $self->{__param}{MODE}, $self->{__param}{FILE} | ||
| 312 | or return undef; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | sub CLEAR { | ||
| 317 | my $self = shift; | ||
| 318 | my($key, $value); | ||
| 319 | $self->sync() if(!$self->{__param}{SAFER}); | ||
| 320 | while (($key, $value) = each %$self) | ||
| 321 | { | ||
| 322 | if($key eq '__param') { next; } | ||
| 323 | delete $self->{$key}; | ||
| 324 | } | ||
| 325 | |||
| 326 | } | ||
| 327 | |||
| 328 | sub DESTROY { | ||
| 329 | my $self = shift; | ||
| 330 | undef %$self; | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | sub setkey { | ||
| 335 | my $self = shift; | ||
| 336 | |||
| 337 | $self->{__param}{KEY} = shift; | ||
| 338 | |||
| 339 | } | ||
| 340 | |||
| 341 | sub setphrase { | ||
| 342 | my $self = shift; | ||
| 343 | |||
| 344 | $self->{__param}{PHRASE} = shift; | ||
| 345 | undef $$self->{__param}{COBJ} if (exists ($self->{__param}{COBJ})); | ||
| 346 | $self->{__param}{COBJ} = Crypt::CBC->new( | ||
| 347 | { 'key' => $self->{__param}{PHRASE}, | ||
| 348 | 'cipher' => 'Blowfish', | ||
| 349 | 'iv' => 'T*_0yx%|', | ||
| 350 | 'regenerate_key' => 0, | ||
| 351 | 'padding' => 'space', | ||
| 352 | 'prepend_iv' => 0, | ||
| 353 | } ); | ||
| 354 | croak ("Error - crypt key: $self->{__param}{COBJ} - $!") if (!$self->{__param}{COBJ}); | ||
| 355 | |||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | # delegate messages to the underlaying Serializer | ||
| 360 | sub DumpMeth { my $self = shift; $self->{__param}{SR}->DumpMeth(@_); } | ||
| 361 | sub RemoveTaint { my $self = shift; $self->{__param}{SR}->RemoveTaint(@_); } | ||
| 362 | sub Key { my $self = shift; $self->{__param}{SR}->Key(@_); } | ||
| 363 | |||
| 364 | # get/set the Serializer object | ||
| 365 | sub Serializer { my $self = shift; @_ ? ($self->{__param}{SR} = shift) : $self->{__param}{SR}; } | ||
| 366 | |||
| 367 | sub import { | ||
| 368 | my (undef,$szr) = @_; | ||
| 369 | $MTDB::Serializer = $szr if defined $szr and $szr; | ||
| 370 | |||
| 371 | } | ||
| 372 | |||
| 373 | 1; | ||
| 374 | __END__ |