2 # A perl module provided easy Windows Registry access
4 # Author: Shu-Min Chang
6 # Copyright(c) 2002 Intel Corporation. All rights reserved
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
11 # 1. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright notice
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution
16 # 3. Neither the name of Intel Corporation nor the names of its contributors
17 # may be used to endorse or promote products derived from this software
18 # without specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE INTEL CORPORATION AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL CORPORATION OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO PROCUREMENT OF SUBSTITUE
26 # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVICED OF THE POSSIBILITY OF SUCH
34 use Win32API
::Registry
0.21 qw( :ALL );
37 ###############################################################################
39 #-----------------------------------------
40 sub GetRegKeyVal
($*) {
41 my ($FullRegPath, $value) = @_;
42 #-----------------------------------------
43 # Purpose: uses Win32API to get registry information from a given server
45 # WARNING: this procedure is VERY Win32 specific, you'll need a Win32 manual
46 # to figure out why something is done.
47 # input: $FullRegPath: a MS specific way of fully qualifying a registry path
48 # \\Server\RootKey\Path\ValueName
49 # output: *value: the value of the registry key of $FullRegPath
52 my ($RemoteMachine, $RootKey, $RegPath, $KeyName, $i);
54 #print "in sub:GetRegKeyVal:Parameters:", @_, "\n";
56 # Check the for valid fully qualified registry path
57 return -1 if (! ($FullRegPath =~ /\\.+\\.+/)) && (!($FullRegPath =~ /\\\\.+\\.+\\.+/));
60 $RemoteMachine = (index($FullRegPath, "\\\\") == $[ ?
substr($FullRegPath, $[+2, index($FullRegPath, "\\", $[+2)-2):0);
62 #print "RemoteMachine = $RemoteMachine\n";
64 $i = $RemoteMachine ?
$[+3+length($RemoteMachine) : $[+1;
65 $RootKey = substr ($FullRegPath, $i, index($FullRegPath, "\\", $i)-$i);
67 $KeyName = $FullRegPath;
68 $KeyName =~ s/.*\\(.+)/$1/;
69 #print "KeyName = $KeyName\n";
71 $i = index($FullRegPath, $RootKey, $[+length($RemoteMachine)) + $[ + length($RootKey)+1;
72 $RegPath = substr ($FullRegPath, $i, length($FullRegPath) - length($KeyName) -$i - 1);
73 #print "RegPath = $RegPath\n";
75 my ($RootKeyHandle, $handle, $key, $type);
78 $RootKeyHandle = regConstant
($RootKey);
80 if (!RegConnectRegistry
($RemoteMachine, $RootKeyHandle, $handle)) {
81 $$value = regLastError
();
84 } else { # not valid actually because I can't find the mapping table of default
85 # local handle mapping. Should always pass in the Machine name to use for now
89 if (!RegOpenKeyEx
($handle, $RegPath, 0, KEY_READ
, $key)) {
90 $$value = regLastError
();
91 #print "regLastError = $$value\n";
94 if (!RegQueryValueEx
( $key, $KeyName, [], $type, $$value, [] )) {
95 $$value = regLastError
();
96 #print "regLastError = $$value\n";
100 #print "RegType=$type\n"; # Perl doesn't fetch type, at this in this
101 # ActiveState 5.6.0 that I'm using
102 #print "RegValue=$$value\n";
104 RegCloseKey
($handle);
109 ###############################################################################
111 #-----------------------------------------
112 sub GetRegSubkeyList
($*) {
113 my ($FullKeyRegPath, $Subkeys) = @_;
114 #-----------------------------------------
115 # Purpose: uses Win32API to get registry subkey list from a given server
117 # WARNING: this procedure is VERY Win32 specific, you'll need a Win32 manual
118 # to figure out why something is done.
119 # input: $FullKeyRegPath: a MS specific way of fully qualifying a registry path
120 # \\Server\RootKey\Path\KeyName
121 # output: *Subkeys: the list of subkeys in array of the registry key of
125 my ($RemoteMachine, $RootKey, $RegPath, $KeyName, $i);
127 #print "in sub:GetRegSubkeyList:Parameters:", @_, "\n";
129 # Check the for valid registry key path
130 return -1 if (! ($FullKeyRegPath =~ /\\.+\\.+/)) && (!($FullKeyRegPath =~ /\\\\.+\\.+\\.+/));
133 $RemoteMachine = (index($FullKeyRegPath, "\\\\") == $[ ?
substr($FullKeyRegPath, $[+2, index($FullKeyRegPath, "\\", $[+2)-2):0);
135 #print "RemoteMachine = $RemoteMachine\n";
137 $i = $RemoteMachine ?
$[+3+length($RemoteMachine) : $[+1;
138 $RootKey = substr ($FullKeyRegPath, $i, index($FullKeyRegPath, "\\", $i)-$i);
140 $i = index($FullKeyRegPath, $RootKey, $[+length($RemoteMachine)) + $[ + length($RootKey)+1;
141 $RegPath = substr ($FullKeyRegPath, $i);
143 #print "RegPath = $RegPath\n";
145 my ($RootKeyHandle, $handle, $key, $type);
147 if ($RemoteMachine) {
148 $RootKeyHandle = regConstant
($RootKey);
150 if (!RegConnectRegistry
($RemoteMachine, $RootKeyHandle, $handle)) {
151 @
$Subkeys[0]= regLastError
();
154 } else { # not valid actually because I can't find the mapping table of default
155 # local handle mapping. Should always pass in the Machine name to use for now
159 if (!RegOpenKeyEx
($handle, $RegPath, 0, KEY_READ
, $key)) {
160 @
$Subkeys[0] = regLastError
();
161 #print "regLastError = @$Subkeys[0]\n";
166 # For some reason, the regLastError() stays at ERROR_NO_MORE_ITEMS
167 # in occasional call sequence, so I'm resetting the error code
168 # before entering the loop
170 for ($i=0; regLastError
()==regConstant
("ERROR_NO_MORE_ITEMS"); $i++) {
171 #print "\nERROR: error enumumerating reg\n";
172 if (RegEnumKeyEx
($key, $i, $tmp, [], [], [], [], [])) {
173 @
$Subkeys[$i] = $tmp;
177 #print "RegType=$type\n";
178 #print "RegValue=@$Subkeys\n";
180 RegCloseKey
($handle);
185 #####################################################
187 sub ExtractOptionIps
($) {
188 my ($MSDHCPOption6Value) = @_;
190 # purpose: DHCP registry specific; to return the extracted IP addresses from
193 # $MSDHCPOption6Value: Option 6 was used to develop, but it works for any
194 # other options of the same datatype.
197 # @ip: an arry of IP addresses in human readable format.
200 # First extract the size of the option
201 my ($byte, $size, $ind1, $ind2, @octet) = unpack("VVVV", $MSDHCPOption6Value);
202 # print "byte = $byte\nsize=$size\nind1=$ind1\nind2=$ind2\n";
204 # Calculate total number of bytes that IP addresses occupy
205 my $number = $size * $ind1;
206 ($byte, $size, $ind1, $ind2, @octet) = unpack("VVVVC$number", $MSDHCPOption6Value);
208 for (my $i=0; $i<$#octet; $i=$i+4) {
209 $ip[$i/4] = "$octet[$i+3]\.$octet[$i+2]\.$octet[$i+1]\.$octet[$i]";
215 #####################################################
217 sub ExtractOptionStrings
($) {
218 my ($MSDHCPOption15Value) = @_;
220 # purpose: DHCP registry specific; to return the extracted string from
223 # $MSDHCPOption15Value: Option 15 was used to develop, but it works for any
224 # other options of the same datatype.
227 # @string: an arry of strings in human readable format.
230 # First extract the size of the option
231 my ($byte, $start, $ind1, $ind2, $size, @data) = unpack("VVVVV", $MSDHCPOption15Value);
232 # print "byte = $byte\nstart=$start\nind1=$ind1\nind2=$ind2\nsize=$size\n";
234 # Calculate total number of bytes that IP addresses occupy
235 my $number = $size * $ind1;
236 ($byte, $start, $ind1, $ind2, $size, @data) = unpack("VVVVVC$number", $MSDHCPOption15Value);
238 for (my $i=0; $i<$ind1; $i++) {
239 # actually this is only programmed to do one string, until I see
240 # example of how the multiple strings are represented, I don't have a
241 # guess to how to program them properly.
242 for (my $j=0; $j<$#data & $data[$j]!=0; $j+=2) {
243 $string[$i] = $string[$i].chr($data[$j]);
250 #####################################################
252 sub ExtractOptionHex
($) {
253 my ($MSDHCPOption46Value) = @_;
255 # purpose: DHCP registry specific; to return the extracted hex from the input
258 # $MSDHCPOption46Value: Option 46 was used to develop, but it works for any
259 # other options of the same datatype.
262 # @Hex: an arry of hex strings in human readable format.
266 # First extract the size of the option
267 my ($byte, $unknown, $ind1, $ind2, @data) = unpack("VVVV", $MSDHCPOption46Value);
268 # print "byte=$byte\nunknown=$unknown\nind1=$ind1\nind2=$ind2\n";
270 # Calculate total number of bytes that IP addresses occupy
271 my $number = $byte - 15;
272 ($byte, $unknown, $ind1, $ind2, @data) = unpack("VVVVC$number", $MSDHCPOption46Value);
274 # printf "data=%4x\n", $data[0];
276 for (my $i=0; $i<$ind1; $i++) {
277 # actually this is only programmed to do one Hex, until I see
278 # example of how the multiple Hexes are represented, I don't have a
279 # guess to how to program them properly.
280 for (my $j=3; $j>=0; $j--) {
281 $Hex[$i] = $Hex[$i].sprintf ("%x", $data[$j+$i*4]);
288 #####################################################
290 sub ExtractExclusionRanges
($) {
291 my ($MSDHCPExclusionRanges) = @_;
293 # purpose: DHCP registry specific; to return the extracted exclusion ranges
294 # from the input variable
296 # $MSDHCPExclusionRanges: Exclusion range as DHCP server returns them
299 # @RangeList: an arry of paird IP addresses strings in human readable format.
302 # First extract the size of the option
303 my ($paircount, @data) = unpack("V", $MSDHCPExclusionRanges);
304 # print "paircount = $paircount\n";
306 # Calculate total number of bytes that IP addresses occupy
307 # my $number = $paircount * 4*2;
308 # ($paircount, @data) = unpack("VC$number", $MSDHCPExclusionRanges);
310 # for (my $i=0; $i<$#data; $i=$i+4) {
311 # $ip[$i/4] = "$data[$i+3]\.$data[$i+2]\.$data[$i+1]\.$data[$i]";
314 my $number = $paircount * 2;
315 ($paircount, @data) = unpack("VL$number", $MSDHCPExclusionRanges);
317 for (my $i=0; $i<=$#data; $i++) {
318 $RangeList[$i] = pack ("L", $data[$i]);
319 # print "extracted", ExtractIp ($RangeList[$i]), "\n";
324 #####################################################
328 # purpose: to return the registry saved IP address in a readable form
330 # $octet: a 4 byte data storing the IP address as the registry save it as
332 # return: anonymous variable of a string of IP address
334 my (@data) = unpack ("C4", $octet);
336 return "$data[3]\.$data[2]\.$data[1]\.$data[0]";
339 #####################################################
344 # purpose: to return the registry saved hex number in a readable form
346 # $octet: a 4 byte data storing the hex number as the registry save it as
349 # $Hex: string of hex digit
352 # First extract the size of the option
353 my (@data) = unpack("C4", $HexVal);
355 for (my $i=3; $i>=0; $i--) {
356 $Hex[0] = $Hex[0] . sprintf ("%x", $data[$i]);