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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/*
Samba Unix/Linux SMB implementation
RPC backend for the registry library
Copyright (C) 2003-2004 Jelmer Vernooij, jelmer@samba.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "includes.h"
/**
* This is the RPC backend for the registry library.
*/
static void init_winreg_String(struct winreg_String *name, const char *s)
{
name->name = s;
if (s) {
name->name_len = 2 * (strlen_m(s) + 1);
name->name_size = name->name_len;
} else {
name->name_len = 0;
name->name_size = 0;
}
}
#define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
{ \
struct winreg_Open ## u r; \
struct winreg_OpenUnknown unknown; \
NTSTATUS status; \
\
unknown.unknown0 = 0x84e0; \
unknown.unknown1 = 0x0000; \
r.in.unknown = &unknown; \
r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED; \
r.out.handle = hnd;\
\
status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \
if (NT_STATUS_IS_ERR(status)) {\
DEBUG(0,("Error executing open\n"));\
return ntstatus_to_werror(status);\
}\
\
return r.out.result;\
}
openhive(HKLM)
openhive(HKCU)
openhive(HKPD)
openhive(HKU)
openhive(HKCR)
struct rpc_key_data {
struct policy_handle pol;
int num_subkeys;
int num_values;
int max_valnamelen;
int max_valdatalen;
};
struct {
const char *name;
WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *, struct policy_handle *h);
} known_hives[] = {
{ "HKEY_LOCAL_MACHINE", open_HKLM },
{ "HKEY_CURRENT_USER", open_HKCU },
{ "HKEY_CLASSES_ROOT", open_HKCR },
{ "HKEY_PERFORMANCE_DATA", open_HKPD },
{ "HKEY_USERS", open_HKU },
{ NULL, NULL }
};
static WERROR rpc_query_key(struct registry_key *k);
static WERROR rpc_list_hives (TALLOC_CTX *mem_ctx, const char *location, const char *credentials, char ***hives)
{
int i = 0;
*hives = talloc_p(mem_ctx, char *);
for(i = 0; known_hives[i].name; i++) {
*hives = talloc_realloc_p(mem_ctx, *hives, char *, i+2);
(*hives)[i] = talloc_strdup(mem_ctx, known_hives[i].name);
}
(*hives)[i] = NULL;
return WERR_OK;
}
static WERROR rpc_close_hive (struct registry_hive *h)
{
dcerpc_pipe_close(h->backend_data);
return WERR_OK;
}
static WERROR rpc_open_hive(TALLOC_CTX *mem_ctx, struct registry_hive *h, struct registry_key **k)
{
NTSTATUS status;
char *user;
char *pass;
struct rpc_key_data *mykeydata;
struct dcerpc_pipe *p;
int n;
if (!h->credentials) return WERR_INVALID_PARAM;
/* Default to local smbd if no connection is specified */
if (!h->location) {
h->location = talloc_strdup(mem_ctx, "ncalrpc:");
}
user = talloc_strdup(mem_ctx, h->credentials);
pass = strchr(user, '%');
if (pass) {
*pass = '\0';
pass = strdup(pass+1);
} else {
pass = strdup("");
}
status = dcerpc_pipe_connect(&p, h->location,
DCERPC_WINREG_UUID,
DCERPC_WINREG_VERSION,
lp_workgroup(),
user, pass);
free(pass);
h->backend_data = p;
if(NT_STATUS_IS_ERR(status)) {
DEBUG(1, ("Unable to open '%s': %s\n", h->location, nt_errstr(status)));
return ntstatus_to_werror(status);
}
for(n = 0; known_hives[n].name; n++)
{
if(!strcmp(known_hives[n].name, h->backend_hivename)) break;
}
if(!known_hives[n].name) {
DEBUG(1, ("No such hive %s\n", known_hives[n].name));
return WERR_NO_MORE_ITEMS;
}
*k = talloc_p(mem_ctx, struct registry_key);
(*k)->backend_data = mykeydata = talloc_p(mem_ctx, struct rpc_key_data);
mykeydata->num_values = -1;
mykeydata->num_subkeys = -1;
return known_hives[n].open((struct dcerpc_pipe *)h->backend_data, *k, &(mykeydata->pol));
}
#if 0
static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
{
struct winreg_OpenKey r;
struct rpc_key_data *mykeydata;
k->backend_data = mykeydata = talloc_p(mem_ctx, struct rpc_key_data);
mykeydata->num_values = -1;
mykeydata->num_subkeys = -1;
/* Then, open the handle using the hive */
memset(&r, 0, sizeof(struct winreg_OpenKey));
r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
init_winreg_String(&r.in.keyname, k->path);
r.in.unknown = 0x00000000;
r.in.access_mask = 0x02000000;
r.out.handle = &mykeydata->pol;
dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
return r.out.result;
}
#endif
static WERROR rpc_open_rel_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
{
struct rpc_key_data *mykeydata;
struct winreg_OpenKey r;
*key = talloc_p(mem_ctx, struct registry_key);
(*key)->name = talloc_strdup(mem_ctx, name);
(*key)->backend_data = mykeydata = talloc_p(mem_ctx, struct rpc_key_data);
mykeydata->num_values = -1;
mykeydata->num_subkeys = -1;
/* Then, open the handle using the hive */
memset(&r, 0, sizeof(struct winreg_OpenKey));
r.in.handle = &(((struct rpc_key_data *)h->backend_data)->pol);
init_winreg_String(&r.in.keyname, name);
r.in.unknown = 0x00000000;
r.in.access_mask = 0x02000000;
r.out.handle = &mykeydata->pol;
dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
return r.out.result;
}
static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **key)
{
return rpc_open_rel_key(mem_ctx, h->root, name, key);
}
static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_value **value)
{
struct rpc_key_data *mykeydata = parent->backend_data;
WERROR error;
struct winreg_EnumValue r;
uint32 type, len1, len2 = 0;
struct EnumValueIn buf_name, buf_val;
NTSTATUS status;
if(mykeydata->num_values == -1) {
error = rpc_query_key(parent);
if(!W_ERROR_IS_OK(error)) return error;
}
len1 = mykeydata->max_valdatalen;
r.in.handle = &mykeydata->pol;
r.in.enum_index = n;
r.in.name_in.len = 0;
r.in.name_in.max_len = mykeydata->max_valnamelen * 2;
buf_name.max_len = mykeydata->max_valnamelen;
buf_name.offset = 0;
buf_name.len = 0;
r.in.name_in.buffer = &buf_name;
r.in.type = &type;
buf_val.max_len = mykeydata->max_valdatalen;
buf_val.offset = 0;
buf_val.len = 0;
r.in.value_in = &buf_val;
r.in.value_len1 = &len1;
r.in.value_len2 = &len2;
r.out.type = &type;
status = dcerpc_winreg_EnumValue((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
if(NT_STATUS_IS_ERR(status)) {
DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
return WERR_GENERAL_FAILURE;
}
if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
*value = talloc_p(mem_ctx, struct registry_value);
(*value)->parent = parent;
(*value)->name = talloc_strdup(mem_ctx, r.out.name_out.name);
(*value)->data_type = type;
(*value)->data_len = r.out.value_out->buffer.length;
(*value)->data_blk = talloc_memdup(mem_ctx, r.out.value_out->buffer.data, r.out.value_out->buffer.length);
return WERR_OK;
}
return r.out.result;
}
static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_key **subkey)
{
struct winreg_EnumKey r;
struct winreg_EnumKeyNameRequest keyname;
struct winreg_String classname;
struct winreg_Time tm;
struct rpc_key_data *mykeydata = parent->backend_data;
NTSTATUS status;
r.in.handle = &mykeydata->pol;
keyname.unknown = 0x0000020a;
init_winreg_String(&keyname.key_name, NULL);
init_winreg_String(&classname, NULL);
r.in.in_name = &keyname;
r.in.class = &classname;
tm.low = tm.high = 0x7fffffff;
r.in.last_changed_time = &tm;
r.in.enum_index = n;
r.in.unknown = r.out.unknown = 0x0414;
r.in.key_name_len = r.out.key_name_len = 0;
status = dcerpc_winreg_EnumKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
return rpc_open_rel_key(mem_ctx, parent, talloc_strdup(mem_ctx, r.out.out_name->name), subkey);
}
return r.out.result;
}
static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, SEC_DESC *sec, struct registry_key **key)
{
return WERR_NOT_SUPPORTED;
}
static WERROR rpc_query_key(struct registry_key *k)
{
NTSTATUS status;
struct winreg_QueryInfoKey r;
struct rpc_key_data *mykeydata = k->backend_data;
TALLOC_CTX *mem_ctx = talloc_init("query_key");
init_winreg_String(&r.in.class, NULL);
r.in.handle = &mykeydata->pol;
status = dcerpc_winreg_QueryInfoKey((struct dcerpc_pipe *)(k->hive->backend_data), mem_ctx, &r);
talloc_destroy(mem_ctx);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
return ntstatus_to_werror(status);
}
if (W_ERROR_IS_OK(r.out.result)) {
mykeydata->num_subkeys = r.out.num_subkeys;
mykeydata->num_values = r.out.num_values;
mykeydata->max_valnamelen = r.out.max_valnamelen;
mykeydata->max_valdatalen = r.out.max_valbufsize;
}
return r.out.result;
}
static WERROR rpc_del_key(struct registry_key *k)
{
NTSTATUS status;
struct rpc_key_data *mykeydata = k->backend_data;
struct winreg_DeleteKey r;
struct registry_key *parent;
WERROR error;
TALLOC_CTX *mem_ctx = talloc_init("del_key");
error = reg_key_get_parent(mem_ctx, k, &parent);
if(!W_ERROR_IS_OK(error)) {
talloc_destroy(mem_ctx);
return error;
}
mykeydata = parent->backend_data;
r.in.handle = &mykeydata->pol;
init_winreg_String(&r.in.key, k->name);
status = dcerpc_winreg_DeleteKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
talloc_destroy(mem_ctx);
return r.out.result;
}
static WERROR rpc_num_values(struct registry_key *key, int *count) {
struct rpc_key_data *mykeydata = key->backend_data;
WERROR error;
if(mykeydata->num_values == -1) {
error = rpc_query_key(key);
if(!W_ERROR_IS_OK(error)) return error;
}
*count = mykeydata->num_values;
return WERR_OK;
}
static WERROR rpc_num_subkeys(struct registry_key *key, int *count) {
struct rpc_key_data *mykeydata = key->backend_data;
WERROR error;
if(mykeydata->num_subkeys == -1) {
error = rpc_query_key(key);
if(!W_ERROR_IS_OK(error)) return error;
}
*count = mykeydata->num_subkeys;
return WERR_OK;
}
static struct registry_operations reg_backend_rpc = {
.name = "rpc",
.open_hive = rpc_open_hive,
.close_hive = rpc_close_hive,
.open_key = rpc_open_key,
.get_subkey_by_index = rpc_get_subkey_by_index,
.get_value_by_index = rpc_get_value_by_index,
.add_key = rpc_add_key,
.del_key = rpc_del_key,
.num_subkeys = rpc_num_subkeys,
.num_values = rpc_num_values,
.list_available_hives = rpc_list_hives,
};
NTSTATUS registry_rpc_init(void)
{
return register_backend("registry", ®_backend_rpc);
}
|