r26523: Refactor provisioning code.
[ira/wip.git] / source / scripting / bin / winreg
1 #!/bin/sh
2 exec smbscript "$0" ${1+"$@"}
3 /*
4   tool to manipulate a remote registry
5   Copyright Andrew Tridgell 2005
6   Released under the GNU GPL v2 or later
7 */      
8
9 var options = GetOptions(ARGV,
10                          "POPT_AUTOHELP",
11                          "POPT_COMMON_SAMBA",
12                          "POPT_COMMON_CREDENTIALS",
13                          "createkey=s");
14 if (options == undefined) {
15         println("Failed to parse options");
16         return -1;
17 }
18
19 libinclude("base.js");
20 libinclude("winreg.js");
21
22 if (options.ARGV.length < 1) {
23         println("Usage: winreg.js <BINDING> [path]");
24         return -1;
25 }
26 var binding = options.ARGV[0];
27 reg = winregObj();
28
29 print("Connecting to " + binding + "\n");
30 status = reg.connect(binding);
31 if (status.is_ok != true) {
32         print("Failed to connect to " + binding + " - " + status.errstr + "\n");
33         return -1;
34 }
35
36 function list_values(path) {
37         var list = reg.enum_values(path);
38         var i;
39         if (list == undefined) {
40                 return;
41         }
42         for (i=0;i<list.length;i++) {
43                 var v = list[i];
44                 printf("\ttype=%-30s size=%4d  '%s'\n", reg.typestring(v.type), v.size, v.name);
45                 if (v.type == reg.REG_SZ || v.type == reg.REG_EXPAND_SZ) {
46                         printf("\t\t'%s'\n", v.value);
47                 }
48                 if (v.type == reg.REG_MULTI_SZ) {
49                         var j;
50                         for (j in v.value) {
51                                 printf("\t\t'%s'\n", v.value[j]);
52                         }
53                 }
54                 if (v.type == reg.REG_DWORD || v.type == reg.REG_DWORD_BIG_ENDIAN) {
55                         printf("\t\t0x%08x (%d)\n", v.value, v.value);
56                 }
57                 if (v.type == reg.REG_QWORD) {
58                         printf("\t\t0x%llx (%lld)\n", v.value, v.value);
59                 }
60         }
61 }
62
63 function list_path(path) {
64         var count = 0;
65         var list = reg.enum_path(path);
66         if (list == undefined) {
67                 println("Unable to list " + path);
68                 return 0;
69         }
70         var i;
71         list_values(path);
72         count = count + list.length;
73         for (i=0;i<list.length;i++) {
74                 var npath;
75                 if (path) {
76                         npath = path + "\\" + list[i];
77                 } else {
78                         npath = list[i];
79                 }
80                 println(npath);
81                 count = count + list_path(npath);
82         }
83         return count;
84 }
85
86 var root;
87
88 if (options.ARGV.length > 1) {
89         root = options.ARGV[1];
90 } else {
91         root = '';
92 }
93
94 if (options.createkey) {
95         var ok = reg.create_key("HKLM\\SOFTWARE", options.createkey);
96         if (!ok) {
97                 println("Failed to create key");
98         }
99 } else {
100         printf("Listing registry tree '%s'\n", root);
101         var count = list_path(root);
102         if (count == 0) {
103                 println("No entries found");
104                 return 1;
105         }
106 }
107 return 0;