Cookies/Microsoft.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 238 CC 18 SCAR 47.5
CC
18
Cov
0%
load
210.0
load_cookies_from_file
12.0
epoch_time_offset_from_win32_filetime
6.0
CRAP 115.8
1 package HTTP::Cookies::Microsoft;
2  
3 use strict;
4  
5 our $VERSION = '6.12';
6  
7 require HTTP::Cookies;
8 our @ISA = qw(HTTP::Cookies);
9  
10 sub load_cookies_from_file {
11 my ($file) = @_;
12 my @cookies;
13  
14 open( my $fh, '<', $file ) || return;
15  
16 while ( my $key = <$fh> ) {
17 chomp $key;
18 my ( $value, $domain_path, $flags, $lo_expire, $hi_expire );
19 my ( $lo_create, $hi_create, $sep );
20 chomp( $value = <$fh> );
21 chomp( $domain_path = <$fh> );
22 chomp( $flags = <$fh> ); # 0x0001 bit is for secure
23 chomp( $lo_expire = <$fh> );
24 chomp( $hi_expire = <$fh> );
25 chomp( $lo_create = <$fh> );
26 chomp( $hi_create = <$fh> );
27 chomp( $sep = <$fh> );
28  
29 if ( !defined($key)
30 || !defined($value)
31 || !defined($domain_path)
32 || !defined($flags)
33 || !defined($hi_expire)
34 || !defined($lo_expire)
35 || !defined($hi_create)
36 || !defined($lo_create)
37 || !defined($sep)
38 || ( $sep ne '*' ) ) {
39 last;
40 }
41  
42 if ( $domain_path =~ /^([^\/]+)(\/.*)$/ ) {
43 my $domain = $1;
44 my $path = $2;
45  
46 push @cookies, {
47 KEY => $key, VALUE => $value, DOMAIN => $domain,
48 PATH => $path, FLAGS => $flags, HIXP => $hi_expire,
49 LOXP => $lo_expire, HICREATE => $hi_create,
50 LOCREATE => $lo_create
51 };
52 }
53 }
54  
55 return \@cookies;
56 }
57  
58 sub get_user_name {
59 use Win32;
60 use locale;
61 my $user = lc( Win32::LoginName() );
62  
63 return $user;
64 }
65  
66 # MSIE stores create and expire times as Win32 FILETIME,
67 # which is 64 bits of 100 nanosecond intervals since Jan 01 1601
68 #
69 # But Cookies code expects time in 32-bit value expressed
70 # in seconds since Jan 01 1970
71 #
72 sub epoch_time_offset_from_win32_filetime {
73 my ( $high, $low ) = @_;
74  
75 #--------------------------------------------------------
76 # USEFUL CONSTANT
77 #--------------------------------------------------------
78 # 0x019db1de 0xd53e8000 is 1970 Jan 01 00:00:00 in Win32 FILETIME
79 #
80 # 100 nanosecond intervals == 0.1 microsecond intervals
81  
82 my $filetime_low32_1970 = 0xd53e8000;
83 my $filetime_high32_1970 = 0x019db1de;
84  
85 #------------------------------------
86 # ALGORITHM
87 #------------------------------------
88 # To go from 100 nanosecond intervals to seconds since 00:00 Jan 01 1970:
89 #
90 # 1. Adjust 100 nanosecond intervals to Jan 01 1970 base
91 # 2. Divide by 10 to get to microseconds (1/millionth second)
92 # 3. Divide by 1000000 (10 ^ 6) to get to seconds
93 #
94 # We can combine Step 2 & 3 into one divide.
95 #
96 # After much trial and error, I came up with the following code which
97 # avoids using Math::BigInt or floating pt, but still gives correct answers
98  
99 # If the filetime is before the epoch, return 0
100 if (
101 ( $high < $filetime_high32_1970 )
102 || ( ( $high == $filetime_high32_1970 )
103 && ( $low < $filetime_low32_1970 ) )
104 ) {
105 return 0;
106 }
107  
108 # Can't multiply by 0x100000000, (1 << 32),
109 # without Perl issuing an integer overflow warning
110 #
111 # So use two multiplies by 0x10000 instead of one multiply by 0x100000000
112 #
113 # The result is the same.
114 #
115 my $date1970 = ( ( $filetime_high32_1970 * 0x10000 ) * 0x10000 )
116 + $filetime_low32_1970;
117 my $time = ( ( $high * 0x10000 ) * 0x10000 ) + $low;
118  
119 $time -= $date1970;
120 $time /= 10000000;
121  
122 return $time;
123 }
124  
125 sub load_cookie {
126 my ( $self, $file ) = @_;
127 my $now = time() - $HTTP::Cookies::EPOCH_OFFSET;
128 my $cookie_data;
129  
130 if ( -f $file ) {
131  
132 # open the cookie file and get the data
133 $cookie_data = load_cookies_from_file($file);
134  
135 foreach my $cookie ( @{$cookie_data} ) {
136 my $secure = ( $cookie->{FLAGS} & 1 ) != 0;
137 my $expires = epoch_time_offset_from_win32_filetime(
138 $cookie->{HIXP},
139 $cookie->{LOXP}
140 );
141  
142 $self->set_cookie(
143 undef, $cookie->{KEY}, $cookie->{VALUE},
144 $cookie->{PATH}, $cookie->{DOMAIN}, undef,
145 0, $secure, $expires - $now, 0
146 );
147 }
148 }
149 }
150  
151 sub load {
152 my ( $self, $cookie_index ) = @_;
153 my $now = time() - $HTTP::Cookies::EPOCH_OFFSET;
154 my $cookie_dir = '';
155 my $delay_load
156 = ( defined( $self->{'delayload'} ) && $self->{'delayload'} );
157 my $user_name = get_user_name();
158 my $data;
159  
160 $cookie_index ||= $self->{'file'} || return;
161 if ( $cookie_index =~ /[\\\/][^\\\/]+$/ ) {
162 $cookie_dir = $` . "\\";
163 }
164  
165 open( my $fh, '<:raw', $cookie_index ) || return;
166 if ( 256 != read( $fh, $data, 256 ) ) {
167 warn "$cookie_index file is not large enough";
168 return;
169 }
170  
171 # Cookies' index.dat file starts with 32 bytes of signature
172 # followed by an offset to the first record, stored as a little-endian DWORD
173 my ( $sig, $size ) = unpack( 'a32 V', $data );
174  
175 # check that sig is valid (only tested in IE6.0)
176 if ( ( $sig !~ /^Client UrlCache MMF Ver 5\.2/ ) || ( 0x4000 != $size ) )
177 {
178 warn "$cookie_index ['$sig' $size] does not seem to contain cookies";
179 return;
180 }
181  
182 # move the file ptr to start of the first record
183 if ( 0 == seek( $fh, $size, 0 ) ) {
184 return;
185 }
186  
187 # Cookies are usually stored in 'URL ' records in two contiguous 0x80 byte sectors (256 bytes)
188 # so read in two 0x80 byte sectors and adjust if not a Cookie.
189 while ( 256 == read( $fh, $data, 256 ) ) {
190  
191 # each record starts with a 4-byte signature
192 # and a count (little-endian DWORD) of 0x80 byte sectors for the record
193 ( $sig, $size ) = unpack( 'a4 V', $data );
194  
195 # Cookies are found in 'URL ' records
196 if ( 'URL ' ne $sig ) {
197  
198 # skip over uninteresting record: I've seen 'HASH' and 'LEAK' records
199 if ( ( $sig eq 'HASH' ) || ( $sig eq 'LEAK' ) ) {
200  
201 # '-2' takes into account the two 0x80 byte sectors we've just read in
202 if ( ( $size > 0 ) && ( $size != 2 ) ) {
203 if ( 0 == seek( $fh, ( $size - 2 ) * 0x80, 1 ) ) {
204  
205 # Seek failed. Something's wrong. Gonna stop.
206 last;
207 }
208 }
209 }
210 next;
211 }
212  
213 #$REMOVE Need to check if URL records in Cookies' index.dat will
214 # ever use more than two 0x80 byte sectors
215 if ( $size > 2 ) {
216 my $more_data = ( $size - 2 ) * 0x80;
217  
218 if ( $more_data != read( $fh, $data, $more_data, 256 ) ) {
219 last;
220 }
221 }
222  
223 ( my $user_name2 = $user_name ) =~ s/ /_/g;
224 if ( $data
225 =~ /Cookie:\Q$user_name\E@([\x21-\xFF]+).*?((?:\Q$user_name\E|\Q$user_name2\E)@[\x21-\xFF]+\.txt)/
226 ) {
227 my $cookie_file = $cookie_dir . $2; # form full pathname
228  
229 if ( !$delay_load ) {
230 $self->load_cookie($cookie_file);
231 }
232 else {
233 my $domain = $1;
234  
235 # grab only the domain name, drop everything from the first dir sep on
236 if ( $domain =~ m{[\\/]} ) {
237 $domain = $`;
238 }
239  
240 # set the delayload cookie for this domain with
241 # the cookie_file as cookie for later-loading info
242 $self->set_cookie(
243 undef, 'cookie', $cookie_file,
244 '//+delayload', $domain, undef, 0, 0, $now + 86_400, 0
245 );
246 }
247 }
248 }
249  
250 1;
251 }
252  
253 1;
254  
255 =pod
256  
257 =encoding UTF-8
258  
259 =head1 NAME
260  
261 HTTP::Cookies::Microsoft - Access to Microsoft cookies files
262  
263 =head1 VERSION
264  
265 version 6.12
266  
267 =head1 SYNOPSIS
268  
269 use LWP;
270 use HTTP::Cookies::Microsoft;
271 use Win32::TieRegistry(Delimiter => "/");
272 my $cookies_dir = $Registry->
273 {"CUser/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/Cookies"};
274  
275 $cookie_jar = HTTP::Cookies::Microsoft->new(
276 file => "$cookies_dir\\index.dat",
277 'delayload' => 1,
278 );
279 my $browser = LWP::UserAgent->new;
280 $browser->cookie_jar( $cookie_jar );
281  
282 =head1 DESCRIPTION
283  
284 This is a subclass of C<HTTP::Cookies> which
285 loads Microsoft Internet Explorer 5.x and 6.x for Windows (MSIE)
286 cookie files.
287  
288 See the documentation for L<HTTP::Cookies>.
289  
290 =head1 METHODS
291  
292 The following methods are provided:
293  
294 =over 4
295  
296 =item $cookie_jar = HTTP::Cookies::Microsoft->new;
297  
298 The constructor takes hash style parameters. In addition
299 to the regular HTTP::Cookies parameters, HTTP::Cookies::Microsoft
300 recognizes the following:
301  
302 delayload: delay loading of cookie data until a request
303 is actually made. This results in faster
304 runtime unless you use most of the cookies
305 since only the domain's cookie data
306 is loaded on demand.
307  
308 =back
309  
310 =head1 CAVEATS
311  
312 Please note that the code DOESN'T support saving to the MSIE
313 cookie file format.
314  
315 =head1 AUTHOR
316  
317 Johnny Lee <typo_pl@hotmail.com>
318  
319 =head1 COPYRIGHT
320  
321 Copyright 2002 Johnny Lee
322  
323 This library is free software; you can redistribute it and/or
324 modify it under the same terms as Perl itself.
325  
326 =head1 AUTHOR
327  
328 Gisle Aas <gisle@activestate.com>
329  
330 =head1 COPYRIGHT AND LICENSE
331  
332 This software is copyright (c) 2002 by Gisle Aas.
333  
334 This is free software; you can redistribute it and/or modify it under
335 the same terms as the Perl 5 programming language system itself.
336  
337 =cut
338  
339 __END__