summaryrefslogtreecommitdiff
path: root/dbxrecover-2p
blob: fb00700a078d43b62353958ea0929bc59e90fefd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/perl

# dbxrecover, a program for recovering mail from damaged Outlook dbx files.
# Copyright (C) 2005  Piotr Pawlow <pp@siedziba.pl>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

use strict;
use warnings;

{
  package DBX::Chain;

  sub new
  {
    my $class = shift;
    my $self = {};
    my $arrayref = shift;
    my @ca = @{$arrayref};
    $self->{chunks} = \@ca;
    $self->{idx} = scalar @ca;
    bless ($self, $class);
    return $self;
  }

  # we assume here the single message is small enough to fit into memory
  # this may not be a case for publishing companies or other businesses sending huge amounts of data by mail :)
  sub data
  {
    my $self = shift;
    my $data = "";
    for(@{$self->{chunks}})
    {
      $data .= $_->data;
    }
    return $data;
  }

  sub broken
  {
    my $self = shift;
    return ($self->{chunks}->[$self->{idx}-1]->next ne "\x00\x00\x00\x00");
  }
}

{
  package DBX::ChainFinder;

  sub new
  {
    my $class = shift;
    my $self = {};
    bless ($self, $class);
    $self->reset;
    return $self;
  }

  sub reset
  {
    my $self = shift;
    $self->{chunks} = {};
    $self->{first} = {};
    $self->{stats} = 
    { 'chunkstot'    => 0,
      'chunksdupl'   => 0,
      'chunkidsdupl' => 0,
      'chainstot'    => 0,
      'chainloops'   => 0,
      'chainsign'    => 0,
      'chainsbroken' => 0,
    };
  }

  sub add
  {
    my $self = shift;
    my $chunk = shift;
    my $id = $chunk->id;
    my $next = $chunk->next;
    $self->{stats}->{chunkstot}++;
    my $chunks = $self->{chunks}->{$id};
    if (defined($chunks))
    {
      my @chunks = (ref($chunks) eq "ARRAY") ? @{$chunks} : ($chunks);
      for (@chunks)
      {
        $self->{stats}->{chunksdupl}++,return if (!$chunk->differ($_)); # don't add chunk if there is a duplicate present
      }
      $self->{stats}->{chunkidsdupl}++;
      push @chunks, $chunk;
      $self->{chunks}->{$id} = \@chunks;
    }
    else
    {
      $self->{chunks}->{$id} = $chunk;
    }

    # we store if there is any other chunk pointing to this one
    if (!defined($self->{first}->{$id})) { $self->{first}->{$id} = 1 };
    $self->{first}->{$next} = 0;
  }

  sub find
  {
    my $self = shift;
    my $first = $self->{first};
    my @ids;
    while (my ($key, $value) = each %$first)
    {
      push @ids,$key if $value == 1;
    }
    return \@ids;
  }

  sub walk_int
  {
    no warnings qw(recursion);

    my $self = shift;
    my $id = shift;
    my $chain = shift;
    my $chains = shift;
    my $seen = shift;
    my $combinations = shift;
    $self->{stats}->{chainsign}++,return if ($combinations > 16); # we don't extract the message if there are too many branches
    $seen->{$id} = 1; # note chunk identifiers for infinite loop testing
    my $chunks = $self->{chunks}->{$id};
    my @chunks = (ref($chunks) eq "ARRAY") ? @{$chunks} : ($chunks);
    for(@chunks)
    {
      my $chunk = $_;
      push @{$chain}, $chunk;
      if (defined($self->{chunks}->{$chunk->next}) && !$seen->{$chunk->next})
      {
        my $nextchunks = $self->{chunks}->{$chunk->next};
        my $nextcount = (ref($nextchunks) eq "ARRAY") ? scalar @{$nextchunks} : 1;
        $self->walk_int($chunk->next, $chain, $chains, $seen, $combinations * $nextcount);
      }
      else
      {
        $self->{stats}->{chainloops}++ if $seen->{$chunk->next};
        my $chainobj = DBX::Chain->new($chain);
        $self->{stats}->{chainsbroken}++ if $chainobj->broken;
        $self->{stats}->{chainstot}++;
        push @{$chains}, $chainobj;
      }
      pop @{$chain};
    }
    $seen->{$id} = 0;
  }

  sub walk
  {
    my $self = shift;
    my $id = shift;
    my $chains = [];
    $self->walk_int($id, [], $chains, {}, 1);
    return $chains;
  }

  sub stats
  {
    my $self = shift;
    return $self->{stats};
  }
}

{
  package DBX::Chunk;
  use Digest::SHA1 qw(sha1);

  sub new
  {
    my $class = shift;
    my $self = {};
    $self->{f} = shift;
    return if ($self->{f}->read($self->{h}, 16) != 16);
    my ($id, $chunksize, $datasize, $next) = unpack("VVVV", $self->{h});

    # test for a valid header
    # chunk size is always 512 bytes in all dbx files I have seen

    if (
          ($chunksize == 512) &&
          ($datasize <= $chunksize) && 
          ($datasize > 0) &&
          ($id != 0) &&
          (($next == 0) || ($datasize == $chunksize))
         )
    {
      $self->{p} = $self->{f}->getpos;

      # if the header seems valid, we skip the whole chunk

      # the chance we miss a start of another chunk is low, because
      # the test above is pretty strict and false positives are quite rare

      # it also helps in cases when there are dbx files contained inside dbx

      $self->{f}->seek($chunksize, 1);
      bless ($self, $class);
      return $self;
    }
    else
    {

      # skip 4 bytes and try again
      # headers were always at 4 byte boundary in every dbx file I have seen

      $self->{f}->seek(-12, 1);
      return 0;
    }
  }

  sub datahash
  {
    my $self = shift;
    if (!defined($self->{hash})) { $self->{hash} = sha1($self->data) };
    return $self->{hash};
  }

  sub differ
  {
    my $self = shift;
    my $other = shift;
    return 1 if (($self->{h}) ne ($other->{h}));
    # use a hash for comparision, to avoid storing message data, or retrieving message data on each comparision
    return 1 if ($self->datahash ne $other->datahash);
    return 0;
  }

  sub data
  {
    my $self = shift;
    my $datasize = unpack("V", substr($self->{h}, 8, 4));
    my $data;
    my $pos = $self->{f}->getpos;
    $self->{f}->setpos($self->{p});
    $self->{f}->read($data, $datasize);
    $self->{f}->setpos($pos);
    return $data;
  }

  sub id
  {
    my $self = shift;
    return substr($self->{h}, 0, 4);
  }

  sub next
  {
    my $self = shift;
    return substr($self->{h}, 12, 4);
  }

}

{
  package DBX::Scan;

  sub new
  {
    my $class = shift;
    my $self = {};
    $self->{file} = shift;
    $self->{cf} = DBX::ChainFinder->new;
    bless ($self, $class);
    return $self;
  }

  sub scan
  {
    my $self = shift;
    $self->{file}->binmode();
    $self->{file}->seek(0, 0);
    $self->{cf}->reset;
    my $time = 0;

    while (defined(my $chunk = DBX::Chunk->new($self->{file})))
    {
      if ($chunk)
      {
        $self->{cf}->add($chunk);
        if (time - $time > 1)
        {
          $self->printchunkstats;
          $time = time;
        }
      }
    }
    $self->printchunkstats;
    print STDERR "\n";
  }

  sub chains
  {
    my $self = shift;
    my $chainids = $self->{cf}->find;
    my $time = 0;
    my $tot = scalar @{$chainids};
    my $idx = 0;
    for (@{$chainids})
    {
      my $chains = $self->{cf}->walk($_);
      $idx++;
      for(@{$chains})
      {
        my $message = $_->data;
        $message =~ s/^>*From />$&/mg;		# mbox "From " escaping
        # we don't have the original envelope address, so just output a fake one
        print "From unknown\@unknown.invalid Mon Jan 1 00:00:00 1970\r\n";
        print $message;
        print "\r\n\r\n";
      }
      if (time - $time > 1)
      {
        $self->printchainstats(int($idx/$tot*100));
        $time = time;
      }
    }
    $self->printchainstats(100);
  }

  sub printchunkstats
  {
    my $self = shift;
    my $stats = $self->{cf}->stats;
    print STDERR "Chunks found: ".$stats->{chunkstot}."; ";
    print STDERR "Duplicates: ".$stats->{chunksdupl}."; ";
    print STDERR "Duplicate IDs: ".$stats->{chunkidsdupl}."\r";
  }

  sub printchainstats
  {
    my $self = shift;
    my $percdone = shift;
    my $stats = $self->{cf}->stats;
    print STDERR "Chains found: ".$stats->{chainstot}."; ";
    print STDERR "Broken: ".$stats->{chainsbroken}."; ";
    print STDERR "Loops: ".$stats->{chainloops}."; ";
    print STDERR "Dropped: ".$stats->{chainsign}."; ";
    print STDERR $percdone."% done\r";
  }
}

use IO::File;

(my $dbxname=$ARGV[0]) and ((scalar @ARGV) == 1) or print(STDERR "Usage: $0 input.dbx >output.mbox\n"),exit;
print STDERR "Opening $dbxname for reading...\n";
my $dbxfile = IO::File->new($dbxname, "r") or die("Error opening file!\n");
my $dbxscanner = DBX::Scan->new($dbxfile);
print STDERR "Searching for chunks...\n";
$dbxscanner->scan;
print STDERR "Searching for chains and printing...\n";
$dbxscanner->chains;
print STDERR "\n";