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

Last change on this file since 260 was 259, checked in by tim, 13 years ago

attempting a fix

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