summaryrefslogtreecommitdiff
path: root/source4/lib/registry/pyregistry.c
blob: b93071f703121cbae993131311bbf5d1e0087a68 (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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* 
   Unix SMB/CIFS implementation.
   Samba utility functions
   Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
   
   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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include <Python.h>
#include "libcli/util/pyerrors.h"
#include "lib/registry/registry.h"
#include "scripting/python/modules.h" /* for py_iconv_convenience() */
#include <pytalloc.h>
#include <tevent.h>

extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);
extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);

PyAPI_DATA(PyTypeObject) PyRegistryKey;
PyAPI_DATA(PyTypeObject) PyRegistry;
PyAPI_DATA(PyTypeObject) PyHiveKey;

static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
{
	char *name;
	WERROR result;
	struct registry_context *ctx = py_talloc_get_ptr(self);
	struct registry_key *key;

	if (!PyArg_ParseTuple(args, "s", &name))
		return NULL;

	result = reg_get_predefined_key_by_name(ctx, name, &key);
	PyErr_WERROR_IS_ERR_RAISE(result);

	return py_talloc_import(&PyRegistryKey, key);
}

static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
{
	char *path;
	WERROR result;
	struct registry_context *ctx = py_talloc_get_ptr(self);

	if (!PyArg_ParseTuple(args, "s", &path))
		return NULL;

	result = reg_key_del_abs(ctx, path);
	PyErr_WERROR_IS_ERR_RAISE(result);

	return Py_None;
}

static PyObject *py_get_predefined_key(PyObject *self, PyObject *args)
{
	uint32_t hkey;
	struct registry_context *ctx = py_talloc_get_ptr(self);
	WERROR result;
	struct registry_key *key;

	if (!PyArg_ParseTuple(args, "I", &hkey))
		return NULL;

	result = reg_get_predefined_key(ctx, hkey, &key);
	PyErr_WERROR_IS_ERR_RAISE(result);

	return py_talloc_import(&PyRegistryKey, key);
}

static PyObject *py_diff_apply(PyObject *self, PyObject *args)
{
	char *filename;
	WERROR result;
	struct registry_context *ctx = py_talloc_get_ptr(self);
	if (!PyArg_ParseTuple(args, "s", &filename))
		return NULL;

	result = reg_diff_apply(ctx, py_iconv_convenience(NULL), filename);
	PyErr_WERROR_IS_ERR_RAISE(result);

	return Py_None; 
}

static PyObject *py_mount_hive(PyObject *self, PyObject *args)
{
	struct registry_context *ctx = py_talloc_get_ptr(self);
	uint32_t hkey;
	PyObject *py_hivekey, *py_elements = Py_None;
	const char **elements;
	WERROR result;

	if (!PyArg_ParseTuple(args, "OI|O", &py_hivekey, &hkey, &py_elements))
		return NULL;

	if (!PyList_Check(py_elements) && py_elements != Py_None) {
		PyErr_SetString(PyExc_TypeError, "Expected list of elements");
		return NULL;
	}

	if (py_elements == Py_None) {
		elements = NULL;
	} else {
		int i;
		elements = talloc_array(NULL, const char *, PyList_Size(py_elements));
		for (i = 0; i < PyList_Size(py_elements); i++)
			elements[i] = PyString_AsString(PyList_GetItem(py_elements, i));
	}

	SMB_ASSERT(ctx != NULL);

	result = reg_mount_hive(ctx, py_talloc_get_ptr(py_hivekey), hkey, elements);
	PyErr_WERROR_IS_ERR_RAISE(result);

	return Py_None;
}

static PyObject *registry_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
	WERROR result;
	struct registry_context *ctx;
	result = reg_open_local(NULL, &ctx);
	PyErr_WERROR_IS_ERR_RAISE(result);
	return py_talloc_import(&PyRegistry, ctx);
}

static PyMethodDef registry_methods[] = {
	{ "get_predefined_key_by_name", py_get_predefined_key_by_name, METH_VARARGS, 
		"S.get_predefined_key_by_name(name) -> key\n"
		"Find a predefined key by name" },
	{ "key_del_abs", py_key_del_abs, METH_VARARGS, "S.key_del_abs(name) -> None\n"
                "Delete a key by absolute path." },
	{ "get_predefined_key", py_get_predefined_key, METH_VARARGS, "S.get_predefined_key(hkey_id) -> key\n"
		"Find a predefined key by id" },
	{ "diff_apply", py_diff_apply, METH_VARARGS, "S.diff_apply(filename) -> None\n"
        	"Apply the diff from the specified file" },
	{ "mount_hive", py_mount_hive, METH_VARARGS, "S.mount_hive(key, key_id, elements=None) -> None\n"
		"Mount the specified key at the specified path." },
	{ NULL }
};

PyTypeObject PyRegistry = {
	.tp_name = "Registry",
	.tp_methods = registry_methods,
	.tp_new = registry_new,
	.tp_basicsize = sizeof(py_talloc_Object),
	.tp_dealloc = py_talloc_dealloc,
	.tp_flags = Py_TPFLAGS_DEFAULT,
};

static PyObject *py_hive_key_del(PyObject *self, PyObject *args)
{
	char *name;
	struct hive_key *key = py_talloc_get_ptr(self);
	WERROR result;

	if (!PyArg_ParseTuple(args, "s", &name))
		return NULL;

	result = hive_key_del(key, name);

	PyErr_WERROR_IS_ERR_RAISE(result);

	return Py_None; 
}

static PyObject *py_hive_key_flush(PyObject *self)
{
	WERROR result;
	struct hive_key *key = py_talloc_get_ptr(self);

	result = hive_key_flush(key);
	PyErr_WERROR_IS_ERR_RAISE(result);

	return Py_None;
}

static PyObject *py_hive_key_del_value(PyObject *self, PyObject *args)
{
	char *name;
	WERROR result;
	struct hive_key *key = py_talloc_get_ptr(self);

	if (!PyArg_ParseTuple(args, "s", &name))
		return NULL;

	result = hive_key_del_value(key, name);

	PyErr_WERROR_IS_ERR_RAISE(result);

	return Py_None; 
}

static PyObject *py_hive_key_set_value(PyObject *self, PyObject *args)
{
	char *name;
	uint32_t type;
	DATA_BLOB value;
	WERROR result;
	struct hive_key *key = py_talloc_get_ptr(self);

	if (!PyArg_ParseTuple(args, "siz#", &name, &type, &value.data, &value.length))
		return NULL;

	if (value.data != NULL)
		result = hive_key_set_value(key, name, type, value);
	else
		result = hive_key_del_value(key, name);

	PyErr_WERROR_IS_ERR_RAISE(result);

	return Py_None; 
}

static PyMethodDef hive_key_methods[] = {
	{ "del", py_hive_key_del, METH_VARARGS, "S.del(name) -> None\n"
		"Delete a subkey" },
	{ "flush", (PyCFunction)py_hive_key_flush, METH_NOARGS, "S.flush() -> None\n"
                "Flush this key to disk" },
	{ "del_value", py_hive_key_del_value, METH_VARARGS, "S.del_value(name) -> None\n"
                 "Delete a value" },
	{ "set_value", py_hive_key_set_value, METH_VARARGS, "S.set_value(name, type, data) -> None\n"
                 "Set a value" },
	{ NULL }
};

static PyObject *hive_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
	/* reg_open_hive */
	return Py_None;
}

PyTypeObject PyHiveKey = {
	.tp_name = "HiveKey",
	.tp_methods = hive_key_methods,
	.tp_new = hive_open,
	.tp_basicsize = sizeof(py_talloc_Object),
	.tp_dealloc = py_talloc_dealloc,
	.tp_flags = Py_TPFLAGS_DEFAULT,
};

PyTypeObject PyRegistryKey = {
	.tp_name = "RegistryKey",
	.tp_basicsize = sizeof(py_talloc_Object),
	.tp_dealloc = py_talloc_dealloc,
	.tp_flags = Py_TPFLAGS_DEFAULT,
};

static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char *kwnames[] = { "lp_ctx", "session_info", NULL };
	struct registry_context *reg_ctx;
	WERROR result;
    struct loadparm_context *lp_ctx;
	PyObject *py_lp_ctx, *py_session_info, *py_credentials;
	struct auth_session_info *session_info;
    struct cli_credentials *credentials;
	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO", discard_const_p(char *, kwnames),
					 &py_lp_ctx, &py_session_info, &py_credentials))
		return NULL;

    lp_ctx = lp_from_py_object(py_lp_ctx);
    if (lp_ctx == NULL) {
		PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
		return NULL;
    }

	credentials = cli_credentials_from_py_object(py_credentials);
	if (credentials == NULL) {
		PyErr_SetString(PyExc_TypeError, "Expected credentials");
		return NULL;
	}

	session_info = NULL; /* FIXME */

	result = reg_open_samba(NULL, &reg_ctx, NULL, 
				lp_ctx, session_info, credentials);
	if (!W_ERROR_IS_OK(result)) {
		PyErr_SetWERROR(result);
		return NULL;
	}
	
	return py_talloc_import(&PyRegistry, reg_ctx);
}

static PyObject *py_open_directory(PyObject *self, PyObject *args)
{
	char *location;
	WERROR result;
	struct hive_key *key;

	if (!PyArg_ParseTuple(args, "s", &location))
		return NULL;

	result = reg_open_directory(NULL, location, &key);
	PyErr_WERROR_IS_ERR_RAISE(result);

	return py_talloc_import(&PyHiveKey, key);
}

static PyObject *py_create_directory(PyObject *self, PyObject *args)
{
	char *location;
	WERROR result;
	struct hive_key *key;

	if (!PyArg_ParseTuple(args, "s", &location))
		return NULL;

	result = reg_create_directory(NULL, location, &key);
	PyErr_WERROR_IS_ERR_RAISE(result);

	return py_talloc_import(&PyHiveKey, key);
}

static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char *kwnames[] = { "location", "session_info", "credentials", "lp_ctx", NULL };
	PyObject *py_session_info = Py_None, *py_credentials = Py_None, *py_lp_ctx = Py_None;
	WERROR result;
	char *location;
    struct loadparm_context *lp_ctx;
    struct cli_credentials *credentials;
	struct hive_key *key;
	struct auth_session_info *session_info;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO", 
									 discard_const_p(char *, kwnames), 
									 &location, 
									 &py_session_info, &py_credentials,
									 &py_lp_ctx))
		return NULL;

    lp_ctx = lp_from_py_object(py_lp_ctx);
    if (lp_ctx == NULL) {
		PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
		return NULL;
    }

	credentials = cli_credentials_from_py_object(py_credentials);
	if (credentials == NULL) {
		PyErr_SetString(PyExc_TypeError, "Expected credentials");
		return NULL;
	}

	session_info = NULL; /* FIXME */

	result = reg_open_ldb_file(NULL, location, session_info, credentials,
							   event_context_init(NULL), lp_ctx, &key);
	PyErr_WERROR_IS_ERR_RAISE(result);

	return py_talloc_import(&PyHiveKey, key);
}

static PyObject *py_str_regtype(PyObject *self, PyObject *args)
{
	int regtype;

	if (!PyArg_ParseTuple(args, "i", &regtype))
		return NULL;
	
	return PyString_FromString(str_regtype(regtype));
}

static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
{
	uint32_t hkey;
	const char *str;

	if (!PyArg_ParseTuple(args, "I", &hkey))
		return NULL;

	str = reg_get_predef_name(hkey);
	if (str == NULL)
		return Py_None;
	return PyString_FromString(str);
}

static PyMethodDef py_registry_methods[] = {
	{ "open_samba", (PyCFunction)py_open_samba, METH_VARARGS|METH_KEYWORDS, "open_samba() -> reg" },
	{ "open_directory", py_open_directory, METH_VARARGS, "open_dir(location) -> key" },
	{ "create_directory", py_create_directory, METH_VARARGS, "create_dir(location) -> key" },
	{ "open_ldb", (PyCFunction)py_open_ldb_file, METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
	{ "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
	{ "get_predef_name", py_get_predef_name, METH_VARARGS, "get_predef_name(hkey) -> str" },
	{ NULL }
};

void initregistry(void)
{
	PyObject *m;

	if (PyType_Ready(&PyHiveKey) < 0)
		return;

	if (PyType_Ready(&PyRegistry) < 0)
		return;

	if (PyType_Ready(&PyRegistryKey) < 0)
		return;

	m = Py_InitModule3("registry", py_registry_methods, "Registry");
	if (m == NULL)
		return;

	PyModule_AddObject(m, "HKEY_CLASSES_ROOT", PyInt_FromLong(HKEY_CLASSES_ROOT));
	PyModule_AddObject(m, "HKEY_CURRENT_USER", PyInt_FromLong(HKEY_CURRENT_USER));
	PyModule_AddObject(m, "HKEY_LOCAL_MACHINE", PyInt_FromLong(HKEY_LOCAL_MACHINE));
	PyModule_AddObject(m, "HKEY_USERS", PyInt_FromLong(HKEY_USERS));
	PyModule_AddObject(m, "HKEY_PERFORMANCE_DATA", PyInt_FromLong(HKEY_PERFORMANCE_DATA));
	PyModule_AddObject(m, "HKEY_CURRENT_CONFIG", PyInt_FromLong(HKEY_CURRENT_CONFIG));
	PyModule_AddObject(m, "HKEY_DYN_DATA", PyInt_FromLong(HKEY_DYN_DATA));
	PyModule_AddObject(m, "HKEY_PERFORMANCE_TEXT", PyInt_FromLong(HKEY_PERFORMANCE_TEXT));
	PyModule_AddObject(m, "HKEY_PERFORMANCE_NLSTEXT", PyInt_FromLong(HKEY_PERFORMANCE_NLSTEXT));

	Py_INCREF(&PyRegistry);
	PyModule_AddObject(m, "Registry", (PyObject *)&PyRegistry);

	Py_INCREF(&PyHiveKey);
	PyModule_AddObject(m, "HiveKey", (PyObject *)&PyHiveKey);

	Py_INCREF(&PyRegistryKey);
	PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);
}