line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#=============================================================================== |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# CryptFile/lib/Filter/Crypto/CryptFile.pm |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION |
6
|
|
|
|
|
|
|
# Module providing the means to convert files to/from an encrypted state in |
7
|
|
|
|
|
|
|
# which they can be run via Filter::Crypto::Decrypt. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# COPYRIGHT |
10
|
|
|
|
|
|
|
# Copyright (C) 2004-2009, 2012-2014 Steve Hay. All rights reserved. |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# LICENCE |
13
|
|
|
|
|
|
|
# This module is free software; you can redistribute it and/or modify it under |
14
|
|
|
|
|
|
|
# the same terms as Perl itself, i.e. under the terms of either the GNU |
15
|
|
|
|
|
|
|
# General Public License or the Artistic License, as specified in the LICENCE |
16
|
|
|
|
|
|
|
# file. |
17
|
|
|
|
|
|
|
# |
18
|
|
|
|
|
|
|
#=============================================================================== |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
package Filter::Crypto::CryptFile; |
21
|
|
|
|
|
|
|
|
22
|
7
|
|
|
7
|
|
342667
|
use 5.008001; |
|
7
|
|
|
|
|
58
|
|
23
|
|
|
|
|
|
|
|
24
|
7
|
|
|
7
|
|
33
|
use strict; |
|
7
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
143
|
|
25
|
7
|
|
|
7
|
|
59
|
use warnings; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
253
|
|
26
|
|
|
|
|
|
|
|
27
|
7
|
|
|
7
|
|
39
|
use Carp qw(carp croak); |
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
331
|
|
28
|
7
|
|
|
7
|
|
47
|
use Exporter qw(); |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
150
|
|
29
|
7
|
|
|
7
|
|
29
|
use Fcntl qw(:DEFAULT :flock); |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
2408
|
|
30
|
7
|
|
|
7
|
|
40
|
use Scalar::Util qw(reftype); |
|
7
|
|
|
|
|
8
|
|
|
7
|
|
|
|
|
288
|
|
31
|
7
|
|
|
7
|
|
33
|
use XSLoader qw(); |
|
7
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
799
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
## no critic (Subroutines::ProhibitSubroutinePrototypes) |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub crypt_file($;$$); |
36
|
|
|
|
|
|
|
sub _isa_cryptmode($); |
37
|
|
|
|
|
|
|
sub _isa_filehandle($); |
38
|
|
|
|
|
|
|
sub _isa_filename($); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
#=============================================================================== |
41
|
|
|
|
|
|
|
# MODULE INITIALIZATION |
42
|
|
|
|
|
|
|
#=============================================================================== |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
our(@ISA, @EXPORT, @EXPORT_OK, $VERSION); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
BEGIN { |
47
|
7
|
|
|
7
|
|
149
|
@ISA = qw(Exporter); |
48
|
|
|
|
|
|
|
|
49
|
7
|
|
|
|
|
28
|
@EXPORT = qw( |
50
|
|
|
|
|
|
|
CRYPT_MODE_AUTO |
51
|
|
|
|
|
|
|
CRYPT_MODE_ENCRYPT |
52
|
|
|
|
|
|
|
CRYPT_MODE_DECRYPT |
53
|
|
|
|
|
|
|
CRYPT_MODE_ENCRYPTED |
54
|
|
|
|
|
|
|
CRYPT_MODE_DECRYPTED |
55
|
|
|
|
|
|
|
crypt_file |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
7
|
|
|
|
|
17
|
@EXPORT_OK = qw( |
59
|
|
|
|
|
|
|
$ErrStr |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
7
|
|
|
|
|
25
|
$VERSION = '2.08'; |
63
|
|
|
|
|
|
|
|
64
|
7
|
|
|
|
|
16086
|
XSLoader::load(__PACKAGE__, $VERSION); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Last error message. |
68
|
|
|
|
|
|
|
our $ErrStr = ''; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
#=============================================================================== |
71
|
|
|
|
|
|
|
# PUBLIC API |
72
|
|
|
|
|
|
|
#=============================================================================== |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Autoload the CRYPT_MODE_* flags from the constant() XS function. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub AUTOLOAD { |
77
|
16
|
|
|
16
|
|
3029
|
our $AUTOLOAD; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Get the name of the constant to generate a subroutine for. |
80
|
16
|
|
|
|
|
120
|
(my $constant = $AUTOLOAD) =~ s/^.*:://o; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Avoid deep recursion on AUTOLOAD() if constant() is not defined. |
83
|
16
|
50
|
|
|
|
75
|
croak('Unexpected error in AUTOLOAD(): constant() is not defined') |
84
|
|
|
|
|
|
|
if $constant eq 'constant'; |
85
|
|
|
|
|
|
|
|
86
|
16
|
|
|
|
|
130
|
my($error, $value) = constant($constant); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Handle any error from looking up the constant. |
89
|
16
|
50
|
|
|
|
42
|
croak($error) if $error; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Generate an in-line subroutine returning the required value. |
92
|
|
|
|
|
|
|
{ |
93
|
7
|
|
|
7
|
|
52
|
no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict) |
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
8263
|
|
|
16
|
|
|
|
|
20
|
|
94
|
16
|
|
|
179
|
|
156
|
*$AUTOLOAD = sub { return $value }; |
|
179
|
|
|
|
|
12803
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Switch to the subroutine that we have just generated. |
98
|
16
|
|
|
|
|
62
|
goto &$AUTOLOAD; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub crypt_file($;$$) { |
102
|
97
|
|
|
97
|
1
|
100871
|
$ErrStr = ''; |
103
|
97
|
|
|
|
|
735
|
my $num_bytes = 0; |
104
|
|
|
|
|
|
|
|
105
|
97
|
100
|
66
|
|
|
1021
|
if ( @_ == 1 or |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
106
|
|
|
|
|
|
|
(@_ == 2 and (not defined $_[1] or $_[1] eq '' or |
107
|
|
|
|
|
|
|
_isa_cryptmode($_[1]) ))) |
108
|
|
|
|
|
|
|
{ |
109
|
81
|
|
|
|
|
217
|
my($fh, $file, $opened, $flocked); |
110
|
81
|
100
|
|
|
|
358
|
if (_isa_filehandle($_[0])) { |
|
|
50
|
|
|
|
|
|
111
|
4
|
|
|
|
|
10
|
$fh = $_[0]; |
112
|
4
|
|
|
|
|
6
|
$opened = 0; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
elsif (_isa_filename($_[0])) { |
115
|
77
|
|
|
|
|
175
|
$file = $_[0]; |
116
|
77
|
100
|
|
|
|
3873
|
unless (sysopen $fh, $file, O_RDWR | O_BINARY) { |
117
|
1
|
|
|
|
|
8
|
$ErrStr = "Can't open file '$file' for updating: $!"; |
118
|
1
|
|
|
|
|
4
|
return; |
119
|
|
|
|
|
|
|
} |
120
|
76
|
|
|
|
|
294
|
$opened = 1; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
else { |
123
|
0
|
|
|
|
|
0
|
croak("'$_[0]' is not a filehandle or a file name"); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
80
|
50
|
|
|
|
951
|
if (-f $fh) { |
127
|
80
|
50
|
|
|
|
910
|
unless (flock $fh, LOCK_EX | LOCK_NB) { |
128
|
0
|
|
|
|
|
0
|
$ErrStr = "Can't acquire exclusive lock on update " . |
129
|
|
|
|
|
|
|
"filehandle: $!"; |
130
|
0
|
|
|
|
|
0
|
local($!, $^E); |
131
|
0
|
0
|
|
|
|
0
|
close $fh if $opened; |
132
|
0
|
|
|
|
|
0
|
return; |
133
|
|
|
|
|
|
|
} |
134
|
80
|
|
|
|
|
241
|
$flocked = 1; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
80
|
100
|
66
|
|
|
589
|
my $crypt_mode = (@_ == 2 and defined $_[1] and $_[1] ne '') |
138
|
|
|
|
|
|
|
? $_[1] : CRYPT_MODE_AUTO(); |
139
|
|
|
|
|
|
|
|
140
|
80
|
50
|
|
|
|
276496
|
unless (_crypt_fh($fh, $crypt_mode, $num_bytes)) { |
141
|
0
|
|
|
|
|
0
|
local($!, $^E); |
142
|
0
|
0
|
|
|
|
0
|
$opened ? close $fh : $flocked ? flock $fh, LOCK_UN : 1; |
|
|
0
|
|
|
|
|
|
143
|
0
|
|
|
|
|
0
|
return; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
80
|
100
|
|
|
|
650
|
if ($opened) { |
|
|
50
|
|
|
|
|
|
147
|
76
|
50
|
|
|
|
3822
|
close $fh or |
148
|
|
|
|
|
|
|
carp("Can't close file '$file' after updating: $!"); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
elsif ($flocked) { |
151
|
4
|
50
|
|
|
|
134
|
flock $fh, LOCK_UN or |
152
|
|
|
|
|
|
|
carp("Can't release lock on filehandle after updating: $!"); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
else { |
156
|
16
|
|
|
|
|
92
|
my($in_fh, $in_file, $in_opened, $in_flocked); |
157
|
16
|
100
|
|
|
|
86
|
if (_isa_filehandle($_[0])) { |
|
|
50
|
|
|
|
|
|
158
|
8
|
|
|
|
|
53
|
$in_fh = $_[0]; |
159
|
8
|
|
|
|
|
69
|
$in_opened = 0; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
elsif (_isa_filename($_[0])) { |
162
|
8
|
|
|
|
|
28
|
$in_file = $_[0]; |
163
|
8
|
50
|
|
|
|
493
|
unless (sysopen $in_fh, $in_file, O_RDONLY | O_BINARY) { |
164
|
0
|
|
|
|
|
0
|
$ErrStr = "Can't open input file '$in_file' for reading: $!"; |
165
|
0
|
|
|
|
|
0
|
return; |
166
|
|
|
|
|
|
|
} |
167
|
8
|
|
|
|
|
38
|
$in_opened = 1; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
else { |
170
|
0
|
|
|
|
|
0
|
croak("'$_[0]' is not a filehandle or a file name"); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
16
|
50
|
|
|
|
250
|
if (-f $in_fh) { |
174
|
16
|
50
|
|
|
|
229
|
unless (flock $in_fh, LOCK_SH | LOCK_NB) { |
175
|
0
|
|
|
|
|
0
|
$ErrStr = "Can't acquire shared lock on input filehandle: $!"; |
176
|
0
|
|
|
|
|
0
|
local($!, $^E); |
177
|
0
|
0
|
|
|
|
0
|
close $in_fh if $in_opened; |
178
|
0
|
|
|
|
|
0
|
return; |
179
|
|
|
|
|
|
|
} |
180
|
16
|
|
|
|
|
51
|
$in_flocked = 1; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
16
|
|
|
|
|
47
|
my($out_fh, $out_file, $out_opened, $out_flocked); |
184
|
16
|
100
|
|
|
|
42
|
if (_isa_filehandle($_[1])) { |
|
|
50
|
|
|
|
|
|
185
|
8
|
|
|
|
|
21
|
$out_fh = $_[1]; |
186
|
8
|
|
|
|
|
17
|
$out_opened = 0; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
elsif (_isa_filename($_[1])) { |
189
|
8
|
|
|
|
|
21
|
$out_file = $_[1]; |
190
|
8
|
50
|
|
|
|
626
|
unless (sysopen $out_fh, $out_file, |
191
|
|
|
|
|
|
|
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY) |
192
|
|
|
|
|
|
|
{ |
193
|
0
|
|
|
|
|
0
|
$ErrStr = "Can't open output file '$out_file' for writing: $!"; |
194
|
0
|
|
|
|
|
0
|
local($!, $^E); |
195
|
0
|
0
|
|
|
|
0
|
$in_opened ? close $in_fh |
|
|
0
|
|
|
|
|
|
196
|
|
|
|
|
|
|
: $in_flocked ? flock $in_fh, LOCK_UN : 1; |
197
|
0
|
|
|
|
|
0
|
return; |
198
|
|
|
|
|
|
|
} |
199
|
8
|
|
|
|
|
33
|
$out_opened = 1; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
else { |
202
|
0
|
|
|
|
|
0
|
local($!, $^E); |
203
|
0
|
0
|
|
|
|
0
|
$in_opened ? close $in_fh : $in_flocked ? flock $in_fh, LOCK_UN : 1; |
|
|
0
|
|
|
|
|
|
204
|
0
|
|
|
|
|
0
|
croak("'$_[1]' is not a valid crypt mode or a filehandle or a " . |
205
|
|
|
|
|
|
|
"file name"); |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
16
|
50
|
|
|
|
191
|
if (-f $out_fh) { |
209
|
16
|
50
|
|
|
|
181
|
unless (flock $out_fh, LOCK_EX | LOCK_NB) { |
210
|
0
|
|
|
|
|
0
|
$ErrStr = "Can't acquire exclusive lock on output " . |
211
|
|
|
|
|
|
|
"filehandle: $!"; |
212
|
0
|
|
|
|
|
0
|
local($!, $^E); |
213
|
0
|
0
|
|
|
|
0
|
$in_opened ? close $in_fh |
|
|
0
|
|
|
|
|
|
214
|
|
|
|
|
|
|
: $in_flocked ? flock $in_fh, LOCK_UN : 1; |
215
|
0
|
0
|
|
|
|
0
|
close $out_fh if $out_opened; |
216
|
0
|
|
|
|
|
0
|
return; |
217
|
|
|
|
|
|
|
} |
218
|
16
|
|
|
|
|
64
|
$out_flocked = 1; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
16
|
|
|
|
|
28
|
my $crypt_mode; |
222
|
16
|
100
|
66
|
|
|
202
|
if (@_ == 3 and defined $_[2] and $_[2] ne '') { |
|
|
|
66
|
|
|
|
|
223
|
12
|
50
|
|
|
|
99
|
if (_isa_cryptmode($_[2])) { |
224
|
12
|
|
|
|
|
70
|
$crypt_mode = $_[2]; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
else { |
227
|
0
|
|
|
|
|
0
|
local($!, $^E); |
228
|
0
|
0
|
|
|
|
0
|
$in_opened ? close $in_fh |
|
|
0
|
|
|
|
|
|
229
|
|
|
|
|
|
|
: $in_flocked ? flock $in_fh, LOCK_UN : 1; |
230
|
0
|
0
|
|
|
|
0
|
$out_opened ? close $out_fh |
|
|
0
|
|
|
|
|
|
231
|
|
|
|
|
|
|
: $out_flocked ? flock $out_fh, LOCK_UN : 1; |
232
|
0
|
|
|
|
|
0
|
croak("'$_[2]' is not a valid crypt mode"); |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
else { |
236
|
4
|
|
|
|
|
9
|
$crypt_mode = CRYPT_MODE_AUTO(); |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
16
|
50
|
|
|
|
71788
|
unless (_crypt_fhs($in_fh, $out_fh, $crypt_mode, $num_bytes)) { |
240
|
0
|
|
|
|
|
0
|
local($!, $^E); |
241
|
0
|
0
|
|
|
|
0
|
$in_opened ? close $in_fh |
|
|
0
|
|
|
|
|
|
242
|
|
|
|
|
|
|
: $in_flocked ? flock $in_fh, LOCK_UN : 1; |
243
|
0
|
0
|
|
|
|
0
|
$out_opened ? close $out_fh |
|
|
0
|
|
|
|
|
|
244
|
|
|
|
|
|
|
: $out_flocked ? flock $out_fh, LOCK_UN : 1; |
245
|
0
|
|
|
|
|
0
|
return; |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
16
|
100
|
|
|
|
144
|
if ($in_opened) { |
|
|
50
|
|
|
|
|
|
249
|
8
|
50
|
|
|
|
106
|
close $in_fh or |
250
|
|
|
|
|
|
|
carp("Can't close input file '$in_file' after reading: $!"); |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
elsif ($in_flocked) { |
253
|
8
|
50
|
|
|
|
94
|
flock $in_fh, LOCK_UN or |
254
|
|
|
|
|
|
|
carp("Can't release lock on input filehandle after " . |
255
|
|
|
|
|
|
|
"reading: $!"); |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
16
|
100
|
|
|
|
66
|
if ($out_opened) { |
|
|
50
|
|
|
|
|
|
259
|
8
|
50
|
|
|
|
315
|
close $out_fh or |
260
|
|
|
|
|
|
|
carp("Can't close output file '$out_file' after writing: $!"); |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
elsif ($out_flocked) { |
263
|
8
|
50
|
|
|
|
343
|
flock $out_fh, LOCK_UN or |
264
|
|
|
|
|
|
|
carp("Can't release lock on output filehandle after " . |
265
|
|
|
|
|
|
|
"writing: $!"); |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
96
|
100
|
|
|
|
1136
|
return $num_bytes ? $num_bytes : '0E0'; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
#=============================================================================== |
273
|
|
|
|
|
|
|
# PRIVATE API |
274
|
|
|
|
|
|
|
#=============================================================================== |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
sub _isa_cryptmode($) { |
277
|
24
|
|
|
24
|
|
148
|
my $mode = shift; |
278
|
|
|
|
|
|
|
|
279
|
24
|
|
100
|
|
|
92
|
return(($mode eq CRYPT_MODE_AUTO() or |
280
|
|
|
|
|
|
|
$mode eq CRYPT_MODE_ENCRYPT() or |
281
|
|
|
|
|
|
|
$mode eq CRYPT_MODE_DECRYPT() or |
282
|
|
|
|
|
|
|
$mode eq CRYPT_MODE_ENCRYPTED() or |
283
|
|
|
|
|
|
|
$mode eq CRYPT_MODE_DECRYPTED() )); |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
sub _isa_filehandle($) { |
287
|
113
|
|
|
113
|
|
524
|
my $fh = shift; |
288
|
|
|
|
|
|
|
|
289
|
113
|
|
66
|
|
|
2889
|
return((( ref $fh and reftype($fh) eq 'GLOB') or |
290
|
|
|
|
|
|
|
(not ref $fh and reftype(\$fh) eq 'GLOB') ) and |
291
|
|
|
|
|
|
|
defined fileno $fh); |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
sub _isa_filename($) { |
295
|
93
|
|
|
93
|
|
275
|
my $name = shift; |
296
|
|
|
|
|
|
|
|
297
|
93
|
|
33
|
|
|
687
|
return(not ref $name and reftype(\$name) eq 'SCALAR'); |
298
|
|
|
|
|
|
|
} |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
1; |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
__END__ |