smbtorture: expand test test_lease_unlink() with set-delete-on-close
[samba4-gss.git] / python / examples / winreg.py
blob46c1e023989042c01f54efdbeb27cf033a8630e5
1 #!/usr/bin/env python3
3 # tool to manipulate a remote registry
4 # Copyright Andrew Tridgell 2005
5 # Copyright Jelmer Vernooij 2007
6 # Released under the GNU GPL v3 or later
9 import sys
11 # Find right directory when running from source tree
12 sys.path.insert(0, "bin/python")
14 from samba.dcerpc import winreg,misc
15 import optparse
16 import samba.getopt as options
18 parser = optparse.OptionParser("%s <BINDING> [path]" % sys.argv[0])
19 sambaopts = options.SambaOptions(parser)
20 parser.add_option_group(sambaopts)
21 parser.add_option("--createkey", type="string", metavar="KEYNAME",
22 help="create a key")
24 opts, args = parser.parse_args()
26 if len(args) < 1:
27 parser.print_usage()
28 sys.exit(-1)
30 binding = args[0]
32 print("Connecting to " + binding)
33 conn = winreg.winreg(binding, sambaopts.get_loadparm())
35 def list_values(key):
36 (num_values, max_valnamelen, max_valbufsize) = conn.QueryInfoKey(key, winreg.String())[4:7]
37 for i in range(num_values):
38 name = winreg.ValNameBuf()
39 name.size = max_valnamelen
40 (name, type, data, _, data_len) = conn.EnumValue(key, i, name, 0, [], max_valbufsize, 0)
41 print("\ttype=%-30s size=%4d '%s'" % (type, name.size, name))
42 if type in (misc.REG_SZ, misc.REG_EXPAND_SZ):
43 print("\t\t'%s'" % data)
46 def list_path(key, path):
47 count = 0
48 (num_subkeys, max_subkeylen, max_classlen) = conn.QueryInfoKey(key, winreg.String())[1:4]
49 for i in range(num_subkeys):
50 name = winreg.StringBuf()
51 name.size = max_subkeylen+2 # utf16 0-terminator
52 keyclass = winreg.StringBuf()
53 keyclass.size = max_classlen+2 # utf16 0-terminator
54 (name, _, _) = conn.EnumKey(key, i, name, keyclass=keyclass, last_changed_time=None)
55 name2 = winreg.String()
56 name2.name = name.name
57 subkey = conn.OpenKey(key, name2, 0, winreg.KEY_QUERY_VALUE | winreg.KEY_ENUMERATE_SUB_KEYS)
58 count += list_path(subkey, "%s\\%s" % (path, name))
59 list_values(subkey)
60 return count
63 if len(args) > 1:
64 root = args[1]
65 else:
66 root = "HKLM"
68 if opts.createkey:
69 name = winreg.String()
70 name.name = "SOFTWARE"
72 # Just sample code, "HKLM\SOFTWARE" should already exist
74 root = conn.OpenHKLM(
75 None, winreg.KEY_QUERY_VALUE | winreg.KEY_ENUMERATE_SUB_KEYS)
76 conn.CreateKey(
77 root,
78 name,
79 keyclass=winreg.String(),
80 options=0,
81 access_mask=0,
82 secdesc=None,
83 action_taken=0)
84 else:
85 print("Listing registry tree '%s'" % root)
86 try:
87 root_key = getattr(conn, "Open%s" % root)(None, winreg.KEY_QUERY_VALUE | winreg.KEY_ENUMERATE_SUB_KEYS)
88 except AttributeError:
89 print("Unknown root key name %s" % root)
90 sys.exit(1)
91 count = list_path(root_key, root)
92 if count == 0:
93 print("No entries found")
94 sys.exit(1)