6cdc3a589803273bcb9a66682c08f90ed37aa42f
[ira/wip.git] / source / scripting / bin / winreg.py
1 #!/usr/bin/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 options = GetOptions(ARGV,
12                          "POPT_AUTOHELP",
13                          "POPT_COMMON_SAMBA",
14                          "POPT_COMMON_CREDENTIALS",
15                          "createkey=s")
16 if (options == undefined) {
17         print "Failed to parse options"
18         sys.exit(-1)
19
20 if len(sys.argv < 2:
21         print "Usage: %s <BINDING> [path]" % sys.argv[0]
22         sys.exit(-1)
23
24 binding = options.ARGV[0]
25 reg = winregObj()
26
27 print "Connecting to " + binding
28 status = reg.connect(binding)
29 if (status.is_ok != true) {
30         print("Failed to connect to " + binding + " - " + status.errstr + "\n")
31         return -1
32 }
33
34 def list_values(path):
35         list = reg.enum_values(path)
36         if (list == undefined) {
37                 return
38         }
39         for (i=0;i<list.length;i++) {
40                 v = list[i]
41                 printf("\ttype=%-30s size=%4d  '%s'\n", reg.typestring(v.type), v.size, v.name)
42                 if (v.type == reg.REG_SZ || v.type == reg.REG_EXPAND_SZ) {
43                         printf("\t\t'%s'\n", v.value)
44                 }
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(path):
59         count = 0
60         list = reg.enum_path(path)
61         if (list == undefined) {
62                 println("Unable to list " + path)
63                 return 0
64         }
65         list_values(path)
66         count = count + list.length
67         for (i=0;i<list.length;i++) {
68                 if (path) {
69                         npath = path + "\\" + list[i]
70                 } else {
71                         npath = list[i]
72                 }
73                 println(npath)
74                 count = count + list_path(npath)
75         }
76         return count
77
78 if len(sys.argv) > 2:
79         root = sys.argv[2]
80 else:
81         root = ''
82
83 if options.createkey:
84     try:
85             reg.create_key("HKLM\\SOFTWARE", options.createkey)
86     except:
87                 print "Failed to create key"
88 else:
89         printf("Listing registry tree '%s'\n", root)
90         count = list_path(root)
91     if count == 0:
92                 println("No entries found")
93                 sys.exit(1)