source: trunk/python/pyregfi/__init__.py @ 294

Last change on this file since 294 was 272, checked in by tim, 13 years ago

fixed name/flags bug
improved error reporting on bad key/value names

File size: 37.3 KB
Line 
1#!/usr/bin/env python
2
3# Copyright (C) 2010-2011 Timothy D. Morgan
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; version 3 of the License.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17#
18# $Id: $
19
20## @package pyregfi
21# Python interface to the regfi library.
22#
23
24## @mainpage API Documentation
25#
26# The pyregfi module provides a Python interface to the @ref regfi Windows
27# registry library. 
28#
29# The library operates on registry hives, each of which is contained within a
30# single file.  The quickest way to get started, is to use the @ref openHive()
31# function to obtain a Hive object.  For example:
32# @code
33# >>> import pyregfi
34# >>> myHive = pyregfi.openHive('/mnt/win/c/WINDOWS/system32/config/system')
35# @endcode
36#
37# Using this Hive object, one can begin investigating what top-level keys
38# exist by starting with the root Key attribute:
39# @code
40# >>> for key in myHive.root.subkeys:
41# ...   print(key.name)
42# ControlSet001
43# ControlSet003
44# LastKnownGoodRecovery
45# MountedDevices
46# Select
47# Setup
48# WPA
49# @endcode
50#
51# From there, accessing subkeys and values by name is a simple matter of:
52# @code
53# >>> myKey = myHive.root.subkeys['Select']
54# >>> myValue = myKey.values['Current']
55# @endcode
56#
57# The data associated with a Value can be obtained through the fetch_data()
58# method:
59# @code
60# >>> print(myValue.fetch_data())
61# 1
62# @endcode
63#
64# While useful for simple exercises, using the subkeys object for deeply nested
65# paths is not efficient and doesn't make for particularly attractive code. 
66# Instead, a special-purpose HiveIterator class is provided for simplicity of
67# use and fast access to specific known paths:
68# @code
69# >>> myIter = pyregfi.HiveIterator(myHive)
70# >>> myIter.descend(['ControlSet001','Control','NetworkProvider','HwOrder'])
71# >>> myKey = myIter.current_key()
72# >>> print(myKey.values['ProviderOrder'].fetch_data())
73# RDPNP,LanmanWorkstation,WebClient
74# @endcode
75#
76# The first two lines above can be simplified in some "syntactic sugar" provided
77# by the Hive.subtree() method.  Also, as one might expect, the HiveIterator
78# also acts as an iterator, producing keys in a depth-first order.
79# For instance, to traverse all keys under the ControlSet003\\Services key,
80# printing their names as we go, we could do:
81# @code
82# >>> for key in Hive.subtree(['ControlSet003','Services']):
83# >>>   print(key.name)
84# Services
85# Abiosdsk
86# abp480n5
87# Parameters
88# PnpInterface
89# ACPI
90# [...]
91# @endcode
92#
93# Note that "Services" was printed first, since the subtree is traversed as a
94# "preordering depth-first" search starting with the HiveIterator's current_key(). 
95# As one might expect, traversals of subtrees stops when all elements in a
96# specific subtree (and none outside of it) have been traversed.
97#
98# For more information, peruse the various attributes and methods available on
99# the Hive, HiveIterator, Key, Value, and Security classes.
100#
101# @note @ref regfi is a read-only library by design and there
102# are no plans to implement write support.
103#
104# @note At present, pyregfi has been tested with Python versions 2.6 and 3.1
105#
106# @note Developers strive to make pyregfi thread-safe.
107#
108import sys
109import time
110import ctypes
111import ctypes.util
112import threading
113from pyregfi.structures import *
114
115
116## An enumeration of registry Value data types
117#
118# @note This is a static class, there is no need to instantiate it.
119#       Just access its attributes directly as DATA_TYPES.SZ, etc
120class DATA_TYPES(object):
121    # XXX: add dictionary lookup attributes to convert both directions between
122    #      the integers and typenames
123
124    ## None / Unknown
125    NONE                       =  0
126    ## String
127    SZ                         =  1
128    ## String with %...% expansions
129    EXPAND_SZ                  =  2
130    ## Binary buffer
131    BINARY                     =  3
132    ## 32 bit integer (little endian)
133    DWORD                      =  4 # DWORD, little endian
134    ## 32 bit integer (little endian)
135    DWORD_LE                   =  4
136    ## 32 bit integer (big endian)
137    DWORD_BE                   =  5 # DWORD, big endian
138    ## Symbolic link
139    LINK                       =  6
140    ## List of strings
141    MULTI_SZ                   =  7
142    ## Unknown structure
143    RESOURCE_LIST              =  8
144    ## Unknown structure
145    FULL_RESOURCE_DESCRIPTOR   =  9
146    ## Unknown structure
147    RESOURCE_REQUIREMENTS_LIST = 10
148    ## 64 bit integer
149    QWORD                      = 11 # 64-bit little endian
150
151
152## An enumeration of log message types
153#
154# @note This is a static class, there is no need to instantiate it.
155#       Just access its attributes directly as LOG_TYPES.INFO, etc
156class LOG_TYPES(object):
157    ## Informational messages, useful in debugging
158    INFO  =  0x01
159    ## Non-critical problems in structure parsing or intepretation
160    WARN  =  0x04
161    ## Major failures
162    ERROR =  0x10
163
164
165def _buffer2bytearray(char_pointer, length):
166    if length == 0 or char_pointer == None:
167        return None
168   
169    ret_val = bytearray(length)
170    for i in range(0,length):
171        ret_val[i] = char_pointer[i][0]
172
173    return ret_val
174
175
176def _strlist2charss(str_list):
177    ret_val = []
178    for s in str_list:
179        ret_val.append(s.encode('utf-8', 'replace'))
180
181    ret_val = (ctypes.c_char_p*(len(str_list)+1))(*ret_val)
182    # Terminate the char** with a NULL pointer
183    ret_val[-1] = 0
184
185    return ret_val
186
187
188def _charss2strlist(chars_pointer):
189    ret_val = []
190    i = 0
191    s = chars_pointer[i]
192    while s:
193        ret_val.append(s.decode('utf-8', 'replace'))
194        i += 1
195        s = chars_pointer[i]
196
197    return ret_val
198
199
200
201## Returns the (py)regfi library version
202#
203# @return A string indicating the version
204def getVersion():
205    return regfi.regfi_version()
206
207
208## Retrieves messages produced by regfi during parsing and interpretation
209#
210# The regfi C library may generate log messages stored in a special thread-safe
211# global data structure.  These messages should be retrieved periodically or
212# after each major operation by callers to determine if any errors or warnings
213# should be reported to the user.  Failure to retrieve these could result in
214# excessive memory consumption.
215def getLogMessages():
216    msgs = regfi.regfi_log_get_str()
217    if not msgs:
218        return ''
219    return msgs.decode('utf-8')
220
221
222## Sets the types of log messages to record
223#
224# @param log_types A sequence of message types that regfi should generate.
225#                  Message types can be found in the LOG_TYPES enumeration.
226#
227# @return True on success, False on failure.  Failures are rare, but could
228#         indicate that global logging is not operating as expected.
229#
230# Example:
231# @code
232# setLogMask((LOG_TYPES.ERROR, LOG_TYPES.WARN, LOG_TYPES.INFO))
233# @endcode
234#
235# The message mask is a global (all hives, iterators), thread-specific value.
236# For more information, see @ref regfi_log_set_mask.
237#
238def setLogMask(log_types):
239    mask = 0
240    for m in log_types:
241        mask |= m
242    return regfi.regfi_log_set_mask(mask)
243
244
245## Opens a file as a registry hive
246#
247# @param path The file path of a hive, as one would provide to the
248#             open() built-in
249#
250# @return A new Hive instance
251def openHive(path):
252    fh = open(path, 'rb')
253    return Hive(fh)
254
255
256## Abstract class for most objects returned by the library
257class _StructureWrapper(object):
258    _hive = None
259    _base = None
260
261    def __init__(self, hive, base):
262        if not hive:
263            raise Exception("Could not create _StructureWrapper,"
264                            + " hive is NULL.  Current log:\n"
265                            + getLogMessages())
266        if not base:
267            raise Exception("Could not create _StructureWrapper,"
268                            + " base is NULL.  Current log:\n"
269                            + getLogMessages())
270        self._hive = hive
271        self._base = base
272
273
274    # Memory management for most regfi structures is taken care of here
275    def __del__(self):
276        if self._base:
277            regfi.regfi_free_record(self._hive.file, self._base)
278
279
280    # Any attribute requests not explicitly defined in subclasses gets passed
281    # to the equivalent REGFI_* structure defined in structures.py
282    def __getattr__(self, name):
283        return getattr(self._base.contents, name)
284
285   
286    ## Test for equality
287    #
288    # Records returned by pyregfi may be compared with one another.  For example:
289    # @code
290    #  >>> key2 = key1.subkeys['child']
291    #  >>> key1 == key2
292    #  False
293    #  >>> key1 != key2
294    #  True
295    #  >>> key1 == key2.get_parent()
296    #  True
297    # @endcode
298    def __eq__(self, other):
299        return (type(self) == type(other)) and (self.offset == other.offset)
300
301
302    def __ne__(self, other):
303        return (not self.__eq__(other))
304
305
306class Key():
307    pass
308
309
310class Value():
311    pass
312
313
314
315## Represents a registry SK record which contains a security descriptor
316#
317class Security(_StructureWrapper):
318    ## Number of registry Keys referencing this SK record
319    ref_count = 1
320
321    ## The absolute file offset of the SK record's cell in the Hive file
322    offset = 0xCAFEBABE
323
324    ## The @ref winsec.SecurityDescriptor for this SK record
325    descriptor = object()
326
327    def __init__(self, hive, base):
328        super(Security, self).__init__(hive, base)
329        # XXX: add checks for NULL pointers
330        self.descriptor = winsec.SecurityDescriptor(base.contents.sec_desc.contents)
331
332    ## Loads the "next" Security record in the hive
333    #
334    # @note
335    # SK records are included in a circular, doubly-linked list.
336    # To iterate over all SK records, be sure to check for the repetition of
337    # the SK record you started with to determine when all have been traversed.
338    def next_security(self):
339        return Security(self._hive,
340                        regfi.regfi_next_sk(self._hive.file, self._base))
341
342    ## Loads the "previous" Security record in the hive
343    #
344    # @note
345    # SK records are included in a circular, doubly-linked list.
346    # To iterate over all SK records, be sure to check for the repetition of
347    # the SK record you started with to determine when all have been traversed.
348    def prev_security(self):
349        return Security(self._hive,
350                        regfi.regfi_prev_sk(self._hive.file, self._base))
351
352
353## Abstract class for ValueList and SubkeyList
354class _GenericList(object):
355    # XXX: consider implementing keys(), values(), items() and other dictionary methods
356    _hive = None
357    _key_base = None
358    _length = None
359    _current = None
360
361    # implementation-specific functions for SubkeyList and ValueList
362    _fetch_num = None
363    _find_element = None
364    _get_element = None
365    _constructor = None
366
367    def __init__(self, key):
368        if not key:
369            raise Exception("Could not create _GenericList; key is NULL."
370                            + "Current log:\n" + getLogMessages())
371
372        base = regfi.regfi_reference_record(key._hive.file, key._base)
373        if not base:
374            raise Exception("Could not create _GenericList; memory error."
375                            + "Current log:\n" + getLogMessages())
376        self._key_base = cast(base, type(key._base))
377        self._length = self._fetch_num(self._key_base)
378        self._hive = key._hive
379
380   
381    def __del__(self):
382        regfi.regfi_free_record(self._hive.file, self._key_base)
383
384
385    ## Length of list
386    def __len__(self):
387        return self._length
388
389
390    ## Retrieves a list element by name
391    #
392    # @param name The name of the subkey or value desired. 
393    #             This is case-insensitive.
394    #
395    # @note The registry format does not inherently prevent multiple
396    #       subkeys or values from having the same name, having a key
397    #       and a value with the same name, or having the same name in
398    #       different cases that could both match.
399    #       This interface simply returns the first match in the list.
400    #       Lookups using this method could also fail due to incorrectly
401    #       encoded strings stored as names.
402    #       To identify any duplicates or elements with malformed names,
403    #       use the iterator interface to check every list element.
404    #
405    # @return the first element whose name matches, or None if the element
406    #         could not be found
407    def __getitem__(self, name):
408        # XXX: Consider interpreting integer names as offsets in the underlying list
409        index = ctypes.c_uint32()
410        if isinstance(name, str):
411            name = name.encode('utf-8')
412
413        if name != None:
414            name = create_string_buffer(bytes(name))
415
416        if self._find_element(self._hive.file, self._key_base, 
417                              name, byref(index)):
418            return self._constructor(self._hive,
419                                     self._get_element(self._hive.file,
420                                                       self._key_base,
421                                                       index))
422        raise KeyError('')
423
424
425    ## Fetches the requested element by name, or the default value if the lookup
426    #  fails.
427    #
428    def get(self, name, default):
429        try:
430            return self[name]
431        except KeyError:
432            return default
433   
434    def __iter__(self):
435        self._current = 0
436        return self
437   
438    def __next__(self):
439        if self._current >= self._length:
440            raise StopIteration('')
441
442        elem = self._get_element(self._hive.file, self._key_base,
443                                 ctypes.c_uint32(self._current))
444        self._current += 1
445        return self._constructor(self._hive, elem)
446   
447    # For Python 2.x
448    next = __next__
449
450
451## The list of subkeys associated with a Key
452#
453# This attribute is both iterable:
454# @code
455#   for k in myKey.subkeys:
456#     ...
457# @endcode
458# and accessible as a dictionary:
459# @code
460#   mySubkey = myKey.subkeys["keyName"]
461# @endcode
462#
463# You may also request the len() of a subkeys list.
464# However keys(), values(), items() and similar methods are not currently
465# implemented.
466class SubkeyList(_GenericList):
467    _fetch_num = regfi.regfi_fetch_num_subkeys
468    _find_element = regfi.regfi_find_subkey
469    _get_element = regfi.regfi_get_subkey
470
471
472## The list of values associated with a Key
473#
474# This attribute is both iterable:
475# @code
476#   for v in myKey.values:
477#     ...
478# @endcode
479# and accessible as a dictionary:
480# @code
481#   myValue = myKey.values["valueName"]
482# @endcode
483#
484# You may also request the len() of a values list.
485# However keys(), values(), items() and similar methods are not currently
486# implemented.
487class ValueList(_GenericList):
488    _fetch_num = regfi.regfi_fetch_num_values
489    _find_element = regfi.regfi_find_value
490    _get_element = regfi.regfi_get_value
491
492
493## Registry key
494# These represent registry keys (@ref REGFI_NK records) and provide
495# access to their subkeys, values, and other metadata.
496#
497# @note Key instances may provide access to more attributes than are
498#       documented here.  However, undocumented attributes may change over time
499#       and are not officially supported.  If you need access to an attribute
500#       not shown here, see @ref pyregfi.structures.
501class Key(_StructureWrapper):
502    ## A @ref ValueList object representing the list of Values
503    #  stored on this Key
504    values = None
505
506    ## A @ref SubkeyList object representing the list of subkeys
507    #  stored on this Key
508    subkeys = None
509
510    ## The raw Key name as an uninterpreted bytearray
511    name_raw = (b"...")
512   
513    ## The name of the Key as a (unicode) string
514    name = "..."
515   
516    ## The string encoding used to store the Key's name ("ascii" or "utf-16-le")
517    name_encoding = "ascii"
518
519    ## The absolute file offset of the Key record's cell in the Hive file
520    offset = 0xCAFEBABE
521
522    ## This Key's last modified time represented as the number of seconds
523    #  since the UNIX epoch in UTC; similar to what time.time() returns
524    modified = 1300000000.123456
525
526    ## The NK record's flags field
527    flags = 0x10110001
528
529    def __init__(self, hive, base):
530        super(Key, self).__init__(hive, base)
531        self.values = ValueList(self)
532        self.subkeys = SubkeyList(self)
533
534    def __getattr__(self, name):
535        if name == "name":
536            ret_val = super(Key, self).__getattr__(name)
537
538            if not ret_val:
539                ret_val = self.name_raw
540                if ret_val != None:
541                    ret_val = ret_val.decode(self.name_encoding, 'replace')
542            else:
543                ret_val = ret_val.decode('utf-8', 'replace')
544               
545        elif name == "name_encoding":
546            flags = super(Key, self).__getattr__("flags")
547            if (flags & structures.REGFI_NK_FLAG_ASCIINAME) > 0:
548                ret_val = "ascii"
549            ret_val = "utf-16-le"
550
551        elif name == "name_raw":
552            ret_val = super(Key, self).__getattr__(name)
553            length = super(Key, self).__getattr__('name_length')
554            ret_val = _buffer2bytearray(ret_val, length)
555
556        elif name == "modified":
557            ret_val = regfi.regfi_nt2unix_time(self._base.contents.mtime)
558
559        else:
560            ret_val = super(Key, self).__getattr__(name)
561
562        return ret_val
563
564
565    ## Retrieves the Security properties for this key
566    def fetch_security(self):
567        return Security(self._hive,
568                        regfi.regfi_fetch_sk(self._hive.file, self._base))
569
570
571    ## Retrieves the class name for this key
572    #
573    # Class names are typically stored as UTF-16LE strings, so these are decoded
574    # into proper python (unicode) strings.  However, if this fails, a bytearray
575    # is instead returned containing the raw buffer stored for the class name.
576    #
577    # @return The class name as a string or bytearray.  None if a class name
578    #         doesn't exist or an unrecoverable error occurred during retrieval.
579    def fetch_classname(self):
580        ret_val = None
581        cn_p = regfi.regfi_fetch_classname(self._hive.file, self._base)
582        if cn_p:
583            cn_struct = cn_p.contents
584            if cn_struct.interpreted:
585                ret_val = cn_struct.interpreted.decode('utf-8', 'replace')
586            else:
587                ret_val = _buffer2bytearray(cn_struct.raw,
588                                            cn_struct.size)
589            regfi.regfi_free_record(self._hive.file, cn_p)
590
591        return ret_val
592
593
594    ## Retrieves this key's parent key
595    #
596    # @return The parent's Key instance or None if current key is root
597    #         (or an error occured)
598    def get_parent(self):
599        if self.is_root():
600            return None
601        parent_base = regfi.regfi_get_parentkey(self._hive.file, self._base)
602        if parent_base:
603            return Key(self._hive, parent_base)
604        return None
605
606
607    ## Checks to see if this Key is the root of its Hive
608    #
609    #  @return True if it is, False otherwise
610    def is_root(self):
611        return (self._hive.root == self)
612
613
614## Registry value (metadata)
615#
616# These represent registry values (@ref REGFI_VK records) and provide
617# access to their associated data.
618#
619# @note Value instances may provide access to more attributes than are
620#       documented here.  However, undocumented attributes may change over time
621#       and are not officially supported.  If you need access to an attribute
622#       not shown here, see @ref pyregfi.structures.
623class Value(_StructureWrapper):
624    ## The raw Value name as an uninterpreted bytearray
625    name_raw = (b"...")
626   
627    ## The name of the Value as a (unicode) string
628    name = "..."
629   
630    ## The string encoding used to store the Value's name ("ascii" or "utf-16-le")
631    name_encoding = "ascii"
632
633    ## The absolute file offset of the Value record's cell in the Hive file
634    offset = 0xCAFEBABE
635
636    ## The length of data advertised in the VK record
637    data_size = 0xCAFEBABE
638
639    ## An integer which represents the data type for this Value's data
640    # Typically this value is one of 12 types defined in @ref DATA_TYPES,
641    # but in some cases (the SAM hive) it may be used for other purposes
642    type = DATA_TYPES.NONE
643
644    ## The VK record's flags field
645    flags = 0x10110001
646
647    ## Retrieves the Value's data according to advertised type
648    #
649    # Data is loaded from its cell(s) and then interpreted based on the data
650    # type recorded in the Value.  It is not uncommon for data to be stored with
651    # the wrong type or even with invalid types.  If you have difficulty
652    # obtaining desired data here, use @ref fetch_raw_data().
653    #
654    # @return The interpreted representation of the data as one of several
655    #         possible Python types, as listed below.  None if any failure
656    #         occurred during extraction or conversion.
657    #
658    # @retval string for SZ, EXPAND_SZ, and LINK
659    # @retval int for DWORD, DWORD_BE, and QWORD
660    # @retval list(string) for MULTI_SZ
661    # @retval bytearray for NONE, BINARY, RESOURCE_LIST,
662    #         FULL_RESOURCE_DESCRIPTOR, and RESOURCE_REQUIREMENTS_LIST
663    #
664    def fetch_data(self):
665        ret_val = None
666        data_p = regfi.regfi_fetch_data(self._hive.file, self._base)
667        if not data_p:
668            return None
669        data_struct = data_p.contents
670
671        if data_struct.interpreted_size == 0:
672            ret_val = None
673        elif data_struct.type in (DATA_TYPES.SZ, DATA_TYPES.EXPAND_SZ, DATA_TYPES.LINK):
674            # Unicode strings
675            ret_val = data_struct.interpreted.string.decode('utf-8', 'replace')
676        elif data_struct.type in (DATA_TYPES.DWORD, DATA_TYPES.DWORD_BE):
677            # 32 bit integers
678            ret_val = data_struct.interpreted.dword
679        elif data_struct.type == DATA_TYPES.QWORD:
680            # 64 bit integers
681            ret_val = data_struct.interpreted.qword
682        elif data_struct.type == DATA_TYPES.MULTI_SZ:
683            ret_val = _charss2strlist(data_struct.interpreted.multiple_string)
684        elif data_struct.type in (DATA_TYPES.NONE, DATA_TYPES.RESOURCE_LIST,
685                                  DATA_TYPES.FULL_RESOURCE_DESCRIPTOR,
686                                  DATA_TYPES.RESOURCE_REQUIREMENTS_LIST,
687                                  DATA_TYPES.BINARY):
688            ret_val = _buffer2bytearray(data_struct.interpreted.none,
689                                        data_struct.interpreted_size)
690
691        regfi.regfi_free_record(self._hive.file, data_p)
692        return ret_val
693   
694
695    ## Retrieves raw representation of Value's data
696    #
697    # @return A bytearray containing the data
698    #
699    def fetch_raw_data(self):
700        ret_val = None
701        # XXX: should we load the data without interpretation instead?
702        data_p = regfi.regfi_fetch_data(self._hive.file, self._base)
703        if not data_p:
704            return None
705
706        data_struct = data_p.contents
707        ret_val = _buffer2bytearray(data_struct.raw,
708                                    data_struct.size)
709        regfi.regfi_free_record(self._hive.file, data_p)
710        return ret_val
711
712
713    def __getattr__(self, name):
714        ret_val = None
715        if name == "name":
716            ret_val = super(Value, self).__getattr__(name)
717            if not ret_val:
718                ret_val = self.name_raw
719                if ret_val != None:
720                    ret_val = ret_val.decode(self.name_encoding, 'replace')
721            else:
722                ret_val = ret_val.decode('utf-8', 'replace')
723
724        elif name == "name_encoding":
725            flags = super(Value, self).__getattr__("flags")
726            if (flags & structures.REGFI_VK_FLAG_ASCIINAME) > 0:
727                ret_val = "ascii"
728            else:
729                ret_val = "utf-16-le"
730
731        elif name == "name_raw":
732            ret_val = super(Value, self).__getattr__(name)
733            length = super(Value, self).__getattr__('name_length')
734            ret_val = _buffer2bytearray(ret_val, length)
735
736        else:
737            ret_val = super(Value, self).__getattr__(name)
738
739        return ret_val
740
741
742# Avoids chicken/egg class definitions.
743# Also makes for convenient code reuse in these lists' parent classes.
744SubkeyList._constructor = Key
745ValueList._constructor = Value
746
747
748
749## Represents a single registry hive (file)
750class Hive():
751    file = None
752    raw_file = None
753    _fh = None
754    #_root = None
755
756
757    ## The root Key of this Hive
758    root = None
759
760    ## This Hives's last modified time represented as the number of seconds
761    #  since the UNIX epoch in UTC; similar to what time.time() returns
762    modified = 1300000000.123456
763
764    ## First sequence number
765    sequence1 = 12345678
766
767    ## Second sequence number
768    sequence2 = 12345678
769
770    ## Major version
771    major_version = 1
772
773    ## Minor version
774    minor_version = 5
775
776    ## Constructor
777    #
778    # Initialize a new Hive based on a Python file object.  To open a file by
779    # path, see @ref openHive.
780    #
781    # @param fh A Python file object.  The constructor first looks for a valid
782    #           fileno attribute on this object and uses it if possible. 
783    #           Otherwise, the seek and read methods are used for file
784    #           access.
785    #
786    # @note Supplied file must be seekable.  Do not perform any operation on
787    #       the provided file object while a Hive is using it.  Do not
788    #       construct multiple Hive instances from the same file object.
789    #       If a file must be accessed by separate code and pyregfi
790    #       simultaneously, use a separate file descriptor.  Hives are
791    #       thread-safe, so multiple threads may use a single Hive object.
792    def __init__(self, fh):
793        # The fileno method may not exist, or it may throw an exception
794        # when called if the file isn't backed with a descriptor.
795        self._fh = fh
796        fn = None
797        try:
798            # XXX: Native calls to Windows filenos don't seem to work. 
799            #      Need to investigate why.
800            if not is_win32 and hasattr(fh, 'fileno'):
801                fn = fh.fileno()
802        except:
803            pass
804
805        if fn != None:
806            self.file = regfi.regfi_alloc(fn, REGFI_ENCODING_UTF8)
807            if not self.file:
808                # XXX: switch to non-generic exception
809                raise Exception("Could not open registry file.  Current log:\n"
810                                + getLogMessages())
811        else:
812            fh.seek(0)
813            self.raw_file = structures.REGFI_RAW_FILE()
814            self.raw_file.fh = fh
815            self.raw_file.seek = seek_cb_type(self.raw_file.cb_seek)
816            self.raw_file.read = read_cb_type(self.raw_file.cb_read)
817            self.file = regfi.regfi_alloc_cb(pointer(self.raw_file), REGFI_ENCODING_UTF8)
818            if not self.file:
819                # XXX: switch to non-generic exception
820                raise Exception("Could not open registry file.  Current log:\n"
821                                + getLogMessages())
822
823
824    def __getattr__(self, name):
825        if name == "root":
826            # XXX: This creates reference loops.  Need to cache better inside regfi
827            #if self._root == None:
828            #    self._root = Key(self, regfi.regfi_get_rootkey(self.file))
829            #return self._root
830            return Key(self, regfi.regfi_get_rootkey(self.file))
831
832        elif name == "modified":
833            return regfi.regfi_nt2unix_time(self._base.contents.mtime)
834
835        return getattr(self.file.contents, name)
836
837   
838    def __del__(self):
839        if self.file:
840            regfi.regfi_free(self.file)
841
842    def __iter__(self):
843        return HiveIterator(self)
844
845
846    ## Creates a @ref HiveIterator initialized at the specified path in
847    #  the hive.
848    #
849    # @param path A list of Key names which represent an absolute path within
850    #             the Hive
851    #
852    # @return A @ref HiveIterator which is positioned at the specified path.
853    #
854    # @exception Exception If the path could not be found/traversed
855    def subtree(self, path):
856        hi = HiveIterator(self)
857        hi.descend(path)
858        return hi
859
860
861## A special purpose iterator for registry hives
862#
863# Iterating over an object of this type causes all keys in a specific
864# hive subtree to be returned in a depth-first manner. These iterators
865# are typically created using the @ref Hive.subtree() function on a @ref Hive
866# object.
867#
868# HiveIterators can also be used to manually traverse up and down a
869# registry hive as they retain information about the current position in
870# the hive, along with which iteration state for subkeys and values for
871# every parent key.  See the @ref up and @ref down methods for more
872# information.
873class HiveIterator():
874    _hive = None
875    _iter = None
876    _iteration_root = None
877    _lock = None
878
879    def __init__(self, hive):
880        self._iter = regfi.regfi_iterator_new(hive.file)
881        if not self._iter:
882            raise Exception("Could not create iterator.  Current log:\n"
883                            + getLogMessages())
884        self._hive = hive
885        self._lock = threading.RLock()
886   
887    def __getattr__(self, name):
888        self._lock.acquire()
889        ret_val = getattr(self._iter.contents, name)
890        self._lock.release()
891        return ret_val
892
893    def __del__(self):
894        self._lock.acquire()
895        regfi.regfi_iterator_free(self._iter)
896        self._lock.release()
897
898    def __iter__(self):
899        self._lock.acquire()
900        self._iteration_root = None
901        self._lock.release()
902        return self
903
904    def __next__(self):
905        self._lock.acquire()
906        if self._iteration_root == None:
907            self._iteration_root = self.current_key().offset
908        elif not regfi.regfi_iterator_down(self._iter):
909            up_ret = regfi.regfi_iterator_up(self._iter)
910            while (up_ret and
911                   not regfi.regfi_iterator_next_subkey(self._iter)):
912                if self._iteration_root == self.current_key().offset:
913                    self._iteration_root = None
914                    self._lock.release()
915                    raise StopIteration('')
916                up_ret = regfi.regfi_iterator_up(self._iter)
917
918            if not up_ret:
919                self._iteration_root = None
920                self._lock.release()
921                raise StopIteration('')
922           
923            # XXX: Use non-generic exception
924            if not regfi.regfi_iterator_down(self._iter):
925                self._lock.release()
926                raise Exception('Error traversing iterator downward.'+
927                                ' Current log:\n'+ getLogMessages())
928
929        regfi.regfi_iterator_first_subkey(self._iter)
930        ret_val = self.current_key()
931        self._lock.release()
932
933        return ret_val
934
935
936    # For Python 2.x
937    next = __next__
938
939    # XXX: Should add sanity checks on some of these traversal functions
940    #      to throw exceptions if a traversal/retrieval *should* have worked
941    #      but failed for some reason.
942
943    ## Descends the iterator to a subkey
944    #
945    # Descends the iterator one level to the current subkey, or a subkey
946    # specified by name.
947    #
948    # @param subkey_name If specified, locates specified subkey by name
949    #                    (via find_subkey()) and descends to it.
950    #
951    # @return True if successful, False otherwise
952    def down(self, subkey_name=None):
953        ret_val = None
954        if subkey_name == None:
955            self._lock.acquire()
956            ret_val = regfi.regfi_iterator_down(self._iter)
957        else:
958            if name != None:
959                name = name.encode('utf-8')
960            self._lock.acquire()
961            ret_val = (regfi.regfi_iterator_find_subkey(self._iter, name) 
962                       and regfi.regfi_iterator_down(self._iter))
963       
964        self._lock.release()
965        return ret_val
966
967
968    ## Causes the iterator to ascend to the current Key's parent
969    #
970    # @return True if successful, False otherwise
971    #
972    # @note The state of current subkeys and values at this level in the tree
973    #       is lost as a side effect.  That is, if you go up() and then back
974    #       down() again, current_subkey() and current_value() will return
975    #       default selections.
976    def up(self):
977        self._lock.acquire()
978        ret_val = regfi.regfi_iterator_up(self._iter)
979        self._lock.release()
980        return ret_val
981
982
983    ## Selects first subkey of current key
984    #
985    # @return A Key instance for the first subkey. 
986    #         None on error or if the current key has no subkeys.
987    def first_subkey(self):
988        ret_val = None
989        self._lock.acquire()
990        if regfi.regfi_iterator_first_subkey(self._iter):
991            ret_val = self.current_subkey()
992        self._lock.release()
993        return ret_val
994
995
996    ## Selects first value of current Key
997    #
998    # @return A Value instance for the first value. 
999    #         None on error or if the current key has no values.
1000    def first_value(self):
1001        ret_val = None
1002        self._lock.acquire()
1003        if regfi.regfi_iterator_first_value(self._iter):
1004            ret_val = self.current_value()
1005        self._lock.release()
1006        return ret_val
1007
1008
1009    ## Selects the next subkey in the current Key's list
1010    #
1011    # @return A Key instance for the next subkey.
1012    #         None if there are no remaining subkeys or an error occurred.
1013    def next_subkey(self):
1014        ret_val = None
1015        self._lock.acquire()
1016        if regfi.regfi_iterator_next_subkey(self._iter):
1017            ret_val = self.current_subkey()
1018        self._lock.release()
1019        return ret_val
1020
1021
1022    ## Selects the next value in the current Key's list
1023   
1024    # @return A Value instance for the next value.
1025    #         None if there are no remaining values or an error occurred.
1026    def next_value(self):
1027        ret_val = None
1028        self._lock.acquire()
1029        if regfi.regfi_iterator_next_value(self._iter):
1030            ret_val = self.current_value()
1031        self._lock.release()
1032        return ret_val
1033
1034
1035    ## Selects the first subkey which has the specified name
1036    #
1037    # @return A Key instance for the selected key.
1038    #         None if it could not be located or an error occurred.
1039    def find_subkey(self, name):
1040        if name != None:
1041            name = name.encode('utf-8')
1042        ret_val = None
1043        self._lock.acquire()
1044        if regfi.regfi_iterator_find_subkey(self._iter, name):
1045            ret_val = self.current_subkey()
1046        self._lock.release()
1047        return ret_val
1048
1049
1050    ## Selects the first value which has the specified name
1051    #
1052    # @return A Value instance for the selected value.
1053    #         None if it could not be located or an error occurred.
1054    def find_value(self, name):
1055        if name != None:
1056            name = name.encode('utf-8')
1057        ret_val = None
1058        self._lock.acquire()
1059        if regfi.regfi_iterator_find_value(self._iter, name):
1060            ret_val = self.current_value()
1061        self._lock.release()
1062        return ret_val
1063
1064    ## Retrieves the currently selected subkey
1065    #
1066    # @return A Key instance of the current subkey
1067    def current_subkey(self):
1068        self._lock.acquire()
1069        ret_val = Key(self._hive, regfi.regfi_iterator_cur_subkey(self._iter))
1070        self._lock.release()
1071        return ret_val
1072
1073    ## Retrieves the currently selected value
1074    #
1075    # @return A Value instance of the current value
1076    def current_value(self):
1077        self._lock.acquire()
1078        ret_val = Value(self._hive, regfi.regfi_iterator_cur_value(self._iter))
1079        self._lock.release()
1080        return ret_val
1081
1082    ## Retrieves the current key
1083    #
1084    # @return A Key instance of the current position of the iterator
1085    def current_key(self):
1086        self._lock.acquire()
1087        ret_val = Key(self._hive, regfi.regfi_iterator_cur_key(self._iter))
1088        self._lock.release()
1089        return ret_val
1090
1091    ## Traverse downward multiple levels
1092    #
1093    # This is more efficient than calling down() multiple times
1094    #
1095    # @param path A list of Key names which represent the path to descend
1096    #
1097    # @exception Exception If path could not be located
1098    def descend(self, path):
1099        cpath = _strlist2charss(path)
1100
1101        self._lock.acquire()
1102        result = regfi.regfi_iterator_descend(self._iter, cpath)
1103        self._lock.release()
1104        if not result:
1105            # XXX: Use non-generic exception
1106            raise Exception('Could not locate path.\n'+getLogMessages())
1107
1108    ## Obtains a list of the current key's ancestry
1109    #
1110    # @return A list of all parent keys starting with the root Key and ending
1111    #         with the current Key
1112    def ancestry(self):
1113        self._lock.acquire()
1114        result = regfi.regfi_iterator_ancestry(self._iter)
1115        self._lock.release()
1116
1117        ret_val = []
1118        i = 0
1119        k = result[i]
1120        while k:
1121            k = cast(regfi.regfi_reference_record(self._hive.file, k), POINTER(REGFI_NK))
1122            ret_val.append(Key(self._hive, k))
1123            i += 1
1124            k = result[i]
1125
1126        regfi.regfi_free_record(self._hive.file, result)
1127        return ret_val
1128
1129    ## Obtains the current path of the iterator
1130    #
1131    # @return A list of key names starting with the root up to and
1132    #         including the current key
1133    #
1134    def current_path(self):
1135        ancestry = self.ancestry()
1136        return [a.name for a in ancestry]
1137
1138
1139# Freeing symbols defined for the sake of documentation
1140del Value.name,Value.name_encoding,Value.name_raw,Value.offset,Value.data_size,Value.type,Value.flags
1141del Key.name,Key.name_encoding,Key.name_raw,Key.offset,Key.modified,Key.flags
1142del Hive.root,Hive.modified,Hive.sequence1,Hive.sequence2,Hive.major_version,Hive.minor_version
1143del Security.ref_count,Security.offset,Security.descriptor
Note: See TracBrowser for help on using the repository browser.