e419320b16f3a5e1370f779842b46a45dc007520
[garming/samba-autobuild/.git] / python / examples / winreg.py
1 #!/usr/bin/env python
2 #
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
7 #
8
9 import sys
10
11 # Find right directory when running from source tree
12 sys.path.insert(0, "bin/python")
13
14 from samba.dcerpc import winreg
15 import optparse
16 import samba.getopt as options
17
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")
23
24 opts, args = parser.parse_args()
25
26 if len(args) < 1:
27     parser.print_usage()
28     sys.exit(-1)
29
30 binding = args[0]
31
32 print "Connecting to " + binding
33 conn = winreg.winreg(binding, sambaopts.get_loadparm())
34
35
36 def list_values(key):
37     (num_values, max_valnamelen, max_valbufsize) = conn.QueryInfoKey(key, winreg.String())[4:8]
38     for i in range(num_values):
39         name = winreg.StringBuf()
40         name.size = max_valnamelen
41         (name, type, data, _, data_len) = conn.EnumValue(key, i, name, 0, "", max_valbufsize, 0)
42         print "\ttype=%-30s size=%4d  '%s'" % type, len, name
43         if type in (winreg.REG_SZ, winreg.REG_EXPAND_SZ):
44             print "\t\t'%s'" % data
45 #        if (v.type == reg.REG_MULTI_SZ) {
46 #            for (j in v.value) {
47 #                printf("\t\t'%s'\n", v.value[j])
48 #            }
49 #        }
50 #        if (v.type == reg.REG_DWORD || v.type == reg.REG_DWORD_BIG_ENDIAN) {
51 #            printf("\t\t0x%08x (%d)\n", v.value, v.value)
52 #        }
53 #        if (v.type == reg.REG_QWORD) {
54 #            printf("\t\t0x%llx (%lld)\n", v.value, v.value)
55 #        }
56
57
58 def list_path(key, path):
59     count = 0
60     (num_subkeys, max_subkeylen, max_subkeysize) = conn.QueryInfoKey(key, winreg.String())[1:4]
61     for i in range(num_subkeys):
62         name = winreg.StringBuf()
63         name.size = max_subkeysize
64         keyclass = winreg.StringBuf()
65         keyclass.size = max_subkeysize
66         (name, _, _) = conn.EnumKey(key, i, name, keyclass=keyclass, last_changed_time=None)[0]
67         subkey = conn.OpenKey(key, name, 0, winreg.KEY_QUERY_VALUE | winreg.KEY_ENUMERATE_SUB_KEYS)
68         count += list_path(subkey, "%s\\%s" % (path, name))
69         list_values(subkey)
70     return count
71
72 if len(args) > 1:
73     root = args[1]
74 else:
75     root = "HKLM"
76
77 if opts.createkey:
78     winreg.create_key("HKLM\\SOFTWARE", opts.createkey)
79 else:
80     print "Listing registry tree '%s'" % root
81     try:
82         root_key = getattr(conn, "Open%s" % root)(None, winreg.KEY_QUERY_VALUE | winreg.KEY_ENUMERATE_SUB_KEYS)
83     except AttributeError:
84         print "Unknown root key name %s" % root
85         sys.exit(1)
86     count = list_path(root_key, root)
87     if count == 0:
88         print "No entries found"
89         sys.exit(1)