2018-12-12 03:43:21 +03:00
#!/usr/bin/env python3
2007-12-24 04:19:41 +03:00
#
# tool to manipulate a remote registry
# Copyright Andrew Tridgell 2005
# Copyright Jelmer Vernooij 2007
# Released under the GNU GPL v3 or later
#
import sys
2008-05-11 07:45:49 +04:00
# Find right directory when running from source tree
sys . path . insert ( 0 , " bin/python " )
2021-06-26 15:21:49 +03:00
from samba . dcerpc import winreg , misc
2008-01-14 21:51:08 +03:00
import optparse
import samba . getopt as options
2007-12-24 04:19:41 +03:00
2008-01-14 21:51:08 +03:00
parser = optparse . OptionParser ( " %s <BINDING> [path] " % sys . argv [ 0 ] )
2008-01-24 01:33:36 +03:00
sambaopts = options . SambaOptions ( parser )
parser . add_option_group ( sambaopts )
2018-07-30 09:22:52 +03:00
parser . add_option ( " --createkey " , type = " string " , metavar = " KEYNAME " ,
2018-07-30 09:16:12 +03:00
help = " create a key " )
2007-12-24 04:19:41 +03:00
2008-01-14 21:51:08 +03:00
opts , args = parser . parse_args ( )
2007-12-24 04:19:41 +03:00
2008-01-14 21:51:08 +03:00
if len ( args ) < 1 :
parser . print_usage ( )
sys . exit ( - 1 )
binding = args [ 0 ]
2007-12-24 04:19:41 +03:00
2021-06-26 15:21:49 +03:00
print ( " Connecting to " + binding )
2008-01-24 01:33:36 +03:00
conn = winreg . winreg ( binding , sambaopts . get_loadparm ( ) )
2007-12-24 04:19:41 +03:00
2008-01-14 21:51:08 +03:00
def list_values ( key ) :
2021-06-26 15:21:49 +03:00
( num_values , max_valnamelen , max_valbufsize ) = conn . QueryInfoKey ( key , winreg . String ( ) ) [ 4 : 7 ]
2008-01-14 21:51:08 +03:00
for i in range ( num_values ) :
2021-06-26 15:21:49 +03:00
name = winreg . ValNameBuf ( )
2008-01-14 21:51:08 +03:00
name . size = max_valnamelen
2021-06-26 15:21:49 +03:00
( name , type , data , _ , data_len ) = conn . EnumValue ( key , i , name , 0 , [ ] , max_valbufsize , 0 )
print ( " \t type= %-30s size= %4d ' %s ' " % ( type , name . size , name ) )
if type in ( misc . REG_SZ , misc . REG_EXPAND_SZ ) :
print ( " \t \t ' %s ' " % data )
2007-12-24 04:19:41 +03:00
2018-07-30 09:20:39 +03:00
2008-01-14 21:51:08 +03:00
def list_path ( key , path ) :
count = 0
2021-06-26 15:21:49 +03:00
( num_subkeys , max_subkeylen , max_classlen ) = conn . QueryInfoKey ( key , winreg . String ( ) ) [ 1 : 4 ]
2008-01-14 21:51:08 +03:00
for i in range ( num_subkeys ) :
name = winreg . StringBuf ( )
2021-06-26 15:21:49 +03:00
name . size = max_subkeylen + 2 # utf16 0-terminator
2008-01-14 21:51:08 +03:00
keyclass = winreg . StringBuf ( )
2021-06-26 15:21:49 +03:00
keyclass . size = max_classlen + 2 # utf16 0-terminator
( name , _ , _ ) = conn . EnumKey ( key , i , name , keyclass = keyclass , last_changed_time = None )
name2 = winreg . String ( )
name2 . name = name . name
subkey = conn . OpenKey ( key , name2 , 0 , winreg . KEY_QUERY_VALUE | winreg . KEY_ENUMERATE_SUB_KEYS )
2008-01-14 21:51:08 +03:00
count + = list_path ( subkey , " %s \\ %s " % ( path , name ) )
list_values ( subkey )
return count
2007-12-24 04:19:41 +03:00
2018-07-30 09:21:29 +03:00
2008-01-14 21:51:08 +03:00
if len ( args ) > 1 :
root = args [ 1 ]
2007-12-24 04:19:41 +03:00
else :
2008-01-14 21:51:08 +03:00
root = " HKLM "
2007-12-24 04:19:41 +03:00
2008-01-14 21:51:08 +03:00
if opts . createkey :
2021-06-26 15:21:49 +03:00
name = winreg . String ( )
name . name = " SOFTWARE "
# Just sample code, "HKLM\SOFTWARE" should already exist
root = conn . OpenHKLM (
None , winreg . KEY_QUERY_VALUE | winreg . KEY_ENUMERATE_SUB_KEYS )
conn . CreateKey (
root ,
name ,
keyclass = winreg . String ( ) ,
options = 0 ,
access_mask = 0 ,
secdesc = None ,
action_taken = 0 )
2007-12-24 04:19:41 +03:00
else :
2021-06-26 15:21:49 +03:00
print ( " Listing registry tree ' %s ' " % root )
2008-01-14 21:51:08 +03:00
try :
root_key = getattr ( conn , " Open %s " % root ) ( None , winreg . KEY_QUERY_VALUE | winreg . KEY_ENUMERATE_SUB_KEYS )
except AttributeError :
2021-06-26 15:21:49 +03:00
print ( " Unknown root key name %s " % root )
2008-01-14 21:51:08 +03:00
sys . exit ( 1 )
count = list_path ( root_key , root )
2007-12-24 04:19:41 +03:00
if count == 0 :
2021-06-26 15:21:49 +03:00
print ( " No entries found " )
2008-01-14 21:51:08 +03:00
sys . exit ( 1 )