Debian Codegraph @

anonymize-archives

raw
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 use File::Temp qw( tempdir tempfile );
6 use Archive::Any;
7 use File::LibMagic;
8 use Path::Class;
9
10 # This program will replace the content of any files contained in a tar,
11 # tar.gz or zip archive by random noise, while preserving the permissions
12 # and ownership (assuming all files in the archive have the same owner as
13 # the first file).
14 #
15 # It will skip unrecognized files. It requires GNU tar for extracting
16 # the archives and bsdtar for repacking them.
17 #
18 # Usage: anonymize-archives file ...
19
20 my $magic = File::LibMagic->new;
21
22 # process files given on the command line
23 while ( my $file = shift ) {
24
25 # open the archive
26 my $archive = eval {
27 local $SIG{__WARN__} = sub { }; # no warnings from Archive::Any
28 Archive::Any->new($file);
29 } or do {
30 print STDERR "Skipping $file\n";
31 next;
32 };
33 my $type = $archive->type;
34 print STDERR "Processing $file ($type)\n";
35
36 # get the list of files
37 my @files = $archive->files;
38
39 # extract the archive in a temporary directory
40 my $dir = tempdir( CLEANUP => 1 );
41
42 # re-create an archive with same files and dummy content
43 my $tmp = "$file.tmp";
44 if ( $type eq 'tar' ) {
45
46 # save differently if compressed
47 my ($mime) = split ';', $magic->checktype_filename($file);
48 my $gzip = $mime eq 'application/x-gzip' ? 'z' : '';
49
50 # record list of files for tar
51 ( undef, my $list ) = tempfile( UNLINK => 1 );
52 file($list)->spew(
53 [ map +( $archive->is_naughty ? substr( $_, 1 ) : $_ ) . "\n",
54 @files
55 ]
56 );
57
58 # extract archive
59 `tar x${gzip}f $file -C $dir --delay-directory-restore`;
60
61 # replace all file content with random data
62 my @mtime;
63 for my $path (@files) {
64 my $mode;
65 my $member = file( $dir, $path );
66 unshift @mtime, [ ( ( stat $member )[9] ) x 2, "$member" ];
67 next if -d $member;
68 if ( !-w $member ) {
69 $mode = ( stat $member )[2] if !-w $member;
70 chmod 0644, $member;
71 }
72 $member->spew( pack "C*", map rand 256, 1 .. -s $member );
73 chmod $mode, $member if $mode;
74 }
75
76 # restore date and time
77 utime @$_ for @mtime;
78
79 # pick up owner and group
80 my $iter = Archive::Tar->iter($file);
81 my $member = $iter->();
82 my @opts = (
83 '--uid' => $member->uid,
84 '--uname' => $member->uname,
85 '--gid' => $member->gid,
86 '--gname' => $member->gname,
87 ( '-s', ':^:/:', '--absolute-paths' )x!! $archive->is_naughty,
88 );
89
90 # repack the file
91 `bsdtar -c${gzip}f $tmp -C $dir -T $list --no-recursion @opts`;
92 }
93 elsif ( $type eq 'zip' ) {
94
95 # read the list of files and their properties
96 my $old = Archive::Zip->new;
97 $old->read($file) and do { warn "Error reading $file\n"; next };
98 my @members = $old->members();
99
100 # create a new zip file with dummy content
101 my $zip = Archive::Zip->new;
102 for my $member (@members) {
103 $member->contents(
104 pack "C*",
105 map rand 256,
106 1 .. $member->uncompressedSize
107 );
108 $zip->addMember($member);
109 }
110
111 # save the new zip
112 $zip->writeToFileNamed($tmp);
113 }
114 else {
115 print STDERR "Skipping $file ($type)\n";
116 next;
117 }
118
119 # replace archive
120 rename $tmp, $file;
121 }