Cookies/Microsoft.pm untested

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