summaryrefslogtreecommitdiff
path: root/source4/scripting/libjs/winreg.js
blob: adcefcae85b8e80ae14de3c027d95084e7fc7e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
	winreg rpc utility functions 
	Copyright Andrew Tridgell 2005
	released under the GNU GPL v2 or later
*/	

libinclude("base.js");

/*
  close a handle
*/
function __winreg_close(handle)
{
	var io = irpcObj();
	io.input.handle = handle;
	this.winreg_CloseKey(io);
}


/*
  open a hive
*/
function __winreg_open_hive(hive)
{
	var io = irpcObj();
	io.input.system_name = NULL;
	io.input.access_required = this.SEC_FLAG_MAXIMUM_ALLOWED;
	var status;
	if (hive == "HKLM") {
		status = this.winreg_OpenHKLM(io);
	} else if (hive == "HKCR") {
		status = this.winreg_OpenHKCR(io);
	} else if (hive == "HKPD") {
		status = this.winreg_OpenHKPD(io);
	} else if (hive == "HKU") {
		status = this.winreg_OpenHKU(io);
	} else {
		this._last_error = "Unknown hive " + hive;
		return undefined;
	}
	if (!status.is_ok) {
		return undefined;
	}
	return io.output.handle;
}

/*
  open a handle to a path
*/
function __winreg_open_path(path)
{
	var s = string_init();
	var i, components = s.split('\\', path);
	var list = new Object();

	list.length = 0;

	/* cope with a leading slash */
	if (components[0] == '') {
		for (i=0;i<(components.length-1);i++) {
			components[i] = components[i+1];
		}
		components.length--;
	}
	
	if (components.length == 0) {
		return undefined;
	}

	var handle = this.open_hive(components[0]);
	if (handle == undefined) {
		return undefined;
	}

	if (components.length == 1) {
		return handle;
	}

	var hpath = components[1];

	for (i=2;i<components.length;i++) {
		hpath = hpath + "\\" + components[i];
	}

	io = irpcObj();
	io.input.handle  = handle;
	io.input.keyname = hpath;
	io.input.unknown = 0;
	io.input.access_mask = this.SEC_FLAG_MAXIMUM_ALLOWED;
	var status = this.winreg_OpenKey(io);

	this.close(handle);

	if (!status.is_ok) {
		return undefined;
	}
	if (io.output.result != "WERR_OK") {
		return undefined;
	}
	
	return io.output.handle;
}

/*
	return a list of keys for a winreg server given a path
	usage:
	   list = reg.enum_path(path);
*/
function __winreg_enum_path(path)
{
	var list = new Object();
	list.length = 0;

	if (path == null || path == "\\" || path == "") {
		return new Array("HKLM", "HKU");
	}
	
	var handle = this.open_path(path);
	if (handle == undefined) {
		return undefined;
	}

	var io = irpcObj();
	io.input.handle            = handle;
	io.input.name = new Object();
	io.input.name.length = 0;
	io.input.name.size   = 32;
	io.input.name.name   = NULL;
	io.input.class = new Object();
	io.input.class.length = 0;
	io.input.class.size   = 1024;
	io.input.class.name   = NULL;
	io.input.last_changed_time = 0;

	var idx = 0;
	for (idx=0;idx >= 0;idx++) {
		io.input.enum_index = idx;
		var status = this.winreg_EnumKey(io);
		if (!status.is_ok) {
			this.close(handle);
			return list;
		}
		var out = io.output;
		if (out.result == "WERR_MORE_DATA") {
			io.input.name.size = io.input.name.size * 2;
			idx--;
			if (io.input.name.size > 32000) {
				this.close(handle);
				return list;
			}
			continue;
		}
		if (out.result != "WERR_OK") {
			this.close(handle);
			return list;
		}
		list[list.length] = out.name.name;
		list.length++;
	}

	this.close(handle);
	return list;
}


/*
	return a list of values for a winreg server given a path
	usage:
	   list = reg.enum_values(path);

	each returned list element is an object containing a name, a
	type and a value
*/
function __winreg_enum_values(path)
{
	var data = datablob_init();
	var list = new Object();
	list.length = 0;

	var handle = this.open_path(path);
	if (handle == undefined) {
		return undefined;
	}

	var io = irpcObj();
	io.input.handle      = handle;
	io.input.name        = new Object();
	io.input.name.length = 0;
	io.input.name.size   = 128;
	io.input.name.name   = "";
	io.input.type        = 0;
	io.input.value       = new Array(0);
	io.input.size        = 1024;
	io.input.length      = 0;

	var idx;
	for (idx=0;idx >= 0;idx++) {
		io.input.enum_index = idx;
		var status = this.winreg_EnumValue(io);
		if (!status.is_ok) {
			this.close(handle);
			return list;
		}
		var out = io.output;
		if (out.result == "WERR_MORE_DATA") {
			io.input.size = io.input.size * 2;
			io.input.name.size = io.input.name.size * 2;
			idx--;
			/* limit blobs to 1M */
			if (io.input.size > 1000000) {
				this.close(handle);
				return list;
			}
			continue;
		}
		if (out.result != "WERR_OK") {
			this.close(handle);
			return list;
		}
		var el   = new Object();
		el.name  = out.name.name;
		el.type  = out.type;
		el.rawvalue = out.value;
		el.value = data.regToVar(el.rawvalue, el.type);
		el.size  = out.size;
		list[list.length] = el;
		list.length++;
	}

	this.close(handle);
	return list;
}


/*
  create a new key
    ok = reg.create_key(path, key);
*/
function __winreg_create_key(path, key)
{
	var handle = this.open_path(path);
	if (handle == undefined) {
		return undefined;
	}

	var io = irpcObj();
	io.input.handle = handle;
	io.input.name = key;
	io.input.class = NULL;
	io.input.options = 0;
	io.input.access_required = this.SEC_FLAG_MAXIMUM_ALLOWED;
	io.input.secdesc = NULL;
	io.input.action_taken = 0;	

	var status = this.winreg_CreateKey(io);
	this.close(handle);
	if (!status.is_ok) {
		return false;
	}
	if (io.output.result != "WERR_OK") {
		return false;
	}
	this.close(io.output.new_handle);
	return true;
}


/*
  return a string for a winreg type
*/
function __winreg_typestring(type)
{
	return this.typenames[type];
}

/*
  initialise the winreg lib, returning an object
*/
function winregObj()
{
	var reg = winreg_init();
	security_init(reg);

	reg.typenames = new Array("REG_NONE", "REG_SZ", "REG_EXPAND_SZ", "REG_BINARY", 
				  "REG_DWORD", "REG_DWORD_BIG_ENDIAN", "REG_LINK", "REG_MULTI_SZ",
				  "REG_RESOURCE_LIST", "REG_FULL_RESOURCE_DESCRIPTOR", 
				  "REG_RESOURCE_REQUIREMENTS_LIST", "REG_QWORD");

	reg.close       = __winreg_close;
	reg.open_hive   = __winreg_open_hive;
	reg.open_path   = __winreg_open_path;
	reg.enum_path   = __winreg_enum_path;
	reg.enum_values = __winreg_enum_values;
	reg.create_key  = __winreg_create_key;
	reg.typestring  = __winreg_typestring;

	return reg;
}