dsdb-schema: make schema_subclasses_order_recurse() static
[Samba/gebeck_regimport.git] / source4 / lib / registry / pyregistry.c
blobad411e846de933555c4433c8645f5c682b5fe102
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
5 Copyright (C) Wilco Baan Hofman <wilco@baanhofman.nl> 2010
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <Python.h>
22 #include "includes.h"
23 #include "libcli/util/pyerrors.h"
24 #include "lib/registry/registry.h"
25 #include <pytalloc.h>
26 #include "lib/events/events.h"
27 #include "auth/credentials/pycredentials.h"
28 #include "param/pyparam.h"
30 extern PyTypeObject PyRegistryKey;
31 extern PyTypeObject PyRegistry;
32 extern PyTypeObject PyHiveKey;
34 void initregistry(void);
36 /*#define PyRegistryKey_AsRegistryKey(obj) pytalloc_get_type(obj, struct registry_key)*/
37 #define PyRegistry_AsRegistryContext(obj) ((struct registry_context *)pytalloc_get_ptr(obj))
38 #define PyHiveKey_AsHiveKey(obj) ((struct hive_key*)pytalloc_get_ptr(obj))
41 static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
43 char *name;
44 WERROR result;
45 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
46 struct registry_key *key;
48 if (!PyArg_ParseTuple(args, "s", &name))
49 return NULL;
51 result = reg_get_predefined_key_by_name(ctx, name, &key);
52 PyErr_WERROR_NOT_OK_RAISE(result);
54 return pytalloc_steal(&PyRegistryKey, key);
57 static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
59 char *path;
60 WERROR result;
61 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
63 if (!PyArg_ParseTuple(args, "s", &path))
64 return NULL;
66 result = reg_key_del_abs(ctx, path);
67 PyErr_WERROR_NOT_OK_RAISE(result);
69 Py_RETURN_NONE;
72 static PyObject *py_get_predefined_key(PyObject *self, PyObject *args)
74 uint32_t hkey;
75 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
76 WERROR result;
77 struct registry_key *key;
79 if (!PyArg_ParseTuple(args, "I", &hkey))
80 return NULL;
82 result = reg_get_predefined_key(ctx, hkey, &key);
83 PyErr_WERROR_NOT_OK_RAISE(result);
85 return pytalloc_steal(&PyRegistryKey, key);
88 static PyObject *py_diff_apply(PyObject *self, PyObject *args)
90 char *filename;
91 WERROR result;
92 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
93 if (!PyArg_ParseTuple(args, "s", &filename))
94 return NULL;
96 result = reg_diff_apply(ctx, filename);
97 PyErr_WERROR_NOT_OK_RAISE(result);
99 Py_RETURN_NONE;
102 static PyObject *py_mount_hive(PyObject *self, PyObject *args)
104 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
105 uint32_t hkey;
106 PyObject *py_hivekey, *py_elements = Py_None;
107 const char **elements;
108 WERROR result;
110 if (!PyArg_ParseTuple(args, "OI|O", &py_hivekey, &hkey, &py_elements))
111 return NULL;
113 if (!PyList_Check(py_elements) && py_elements != Py_None) {
114 PyErr_SetString(PyExc_TypeError, "Expected list of elements");
115 return NULL;
118 if (py_elements == Py_None) {
119 elements = NULL;
120 } else {
121 int i;
122 elements = talloc_array(NULL, const char *, PyList_Size(py_elements));
123 for (i = 0; i < PyList_Size(py_elements); i++)
124 elements[i] = PyString_AsString(PyList_GetItem(py_elements, i));
127 SMB_ASSERT(ctx != NULL);
129 result = reg_mount_hive(ctx, PyHiveKey_AsHiveKey(py_hivekey), hkey, elements);
130 PyErr_WERROR_NOT_OK_RAISE(result);
132 Py_RETURN_NONE;
135 static PyObject *registry_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
137 WERROR result;
138 struct registry_context *ctx;
139 result = reg_open_local(NULL, &ctx);
140 PyErr_WERROR_NOT_OK_RAISE(result);
141 return pytalloc_steal(&PyRegistry, ctx);
144 static PyMethodDef registry_methods[] = {
145 { "get_predefined_key_by_name", py_get_predefined_key_by_name, METH_VARARGS,
146 "S.get_predefined_key_by_name(name) -> key\n"
147 "Find a predefined key by name" },
148 { "key_del_abs", py_key_del_abs, METH_VARARGS, "S.key_del_abs(name) -> None\n"
149 "Delete a key by absolute path." },
150 { "get_predefined_key", py_get_predefined_key, METH_VARARGS, "S.get_predefined_key(hkey_id) -> key\n"
151 "Find a predefined key by id" },
152 { "diff_apply", py_diff_apply, METH_VARARGS, "S.diff_apply(filename) -> None\n"
153 "Apply the diff from the specified file" },
154 { "mount_hive", py_mount_hive, METH_VARARGS, "S.mount_hive(key, key_id, elements=None) -> None\n"
155 "Mount the specified key at the specified path." },
156 { NULL }
159 PyTypeObject PyRegistry = {
160 .tp_name = "Registry",
161 .tp_methods = registry_methods,
162 .tp_new = registry_new,
163 .tp_basicsize = sizeof(pytalloc_Object),
164 .tp_flags = Py_TPFLAGS_DEFAULT,
167 static PyObject *py_hive_key_del(PyObject *self, PyObject *args)
169 char *name;
170 struct hive_key *key = PyHiveKey_AsHiveKey(self);
171 WERROR result;
173 if (!PyArg_ParseTuple(args, "s", &name))
174 return NULL;
176 result = hive_key_del(NULL, key, name);
178 PyErr_WERROR_NOT_OK_RAISE(result);
180 Py_RETURN_NONE;
183 static PyObject *py_hive_key_flush(PyObject *self)
185 WERROR result;
186 struct hive_key *key = PyHiveKey_AsHiveKey(self);
188 result = hive_key_flush(key);
189 PyErr_WERROR_NOT_OK_RAISE(result);
191 Py_RETURN_NONE;
194 static PyObject *py_hive_key_del_value(PyObject *self, PyObject *args)
196 char *name;
197 WERROR result;
198 struct hive_key *key = PyHiveKey_AsHiveKey(self);
200 if (!PyArg_ParseTuple(args, "s", &name))
201 return NULL;
203 result = hive_key_del_value(NULL, key, name);
205 PyErr_WERROR_NOT_OK_RAISE(result);
207 Py_RETURN_NONE;
210 static PyObject *py_hive_key_set_value(PyObject *self, PyObject *args)
212 char *name;
213 uint32_t type;
214 DATA_BLOB value;
215 int value_length = 0;
216 WERROR result;
217 struct hive_key *key = PyHiveKey_AsHiveKey(self);
219 if (!PyArg_ParseTuple(args, "siz#", &name, &type, &value.data, &value_length)) {
220 return NULL;
222 value.length = value_length;
224 if (value.data != NULL)
225 result = hive_key_set_value(key, name, type, value);
226 else
227 result = hive_key_del_value(NULL, key, name);
229 PyErr_WERROR_NOT_OK_RAISE(result);
231 Py_RETURN_NONE;
234 static PyMethodDef hive_key_methods[] = {
235 { "del", py_hive_key_del, METH_VARARGS, "S.del(name) -> None\n"
236 "Delete a subkey" },
237 { "flush", (PyCFunction)py_hive_key_flush, METH_NOARGS, "S.flush() -> None\n"
238 "Flush this key to disk" },
239 { "del_value", py_hive_key_del_value, METH_VARARGS, "S.del_value(name) -> None\n"
240 "Delete a value" },
241 { "set_value", py_hive_key_set_value, METH_VARARGS, "S.set_value(name, type, data) -> None\n"
242 "Set a value" },
243 { NULL }
246 static PyObject *hive_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
247 Py_RETURN_NONE;
250 static PyObject *py_open_hive(PyTypeObject *type, PyObject *args, PyObject *kwargs)
252 const char *kwnames[] = { "location", "lp_ctx", "session_info", "credentials", NULL };
253 WERROR result;
254 struct loadparm_context *lp_ctx;
255 PyObject *py_lp_ctx, *py_session_info, *py_credentials;
256 struct auth_session_info *session_info;
257 struct cli_credentials *credentials;
258 char *location;
259 struct hive_key *hive_key;
260 TALLOC_CTX *mem_ctx;
262 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
263 discard_const_p(char *, kwnames),
264 &location,
265 &py_lp_ctx, &py_session_info,
266 &py_credentials))
267 return NULL;
269 mem_ctx = talloc_new(NULL);
270 if (mem_ctx == NULL) {
271 PyErr_NoMemory();
272 return NULL;
275 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
276 if (lp_ctx == NULL) {
277 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
278 talloc_free(mem_ctx);
279 return NULL;
282 credentials = cli_credentials_from_py_object(py_credentials);
283 if (credentials == NULL) {
284 PyErr_SetString(PyExc_TypeError, "Expected credentials");
285 talloc_free(mem_ctx);
286 return NULL;
288 session_info = NULL;
290 result = reg_open_hive(NULL, location, session_info, credentials,
291 tevent_context_init(NULL),
292 lp_ctx, &hive_key);
293 talloc_free(mem_ctx);
294 PyErr_WERROR_NOT_OK_RAISE(result);
296 return pytalloc_steal(&PyHiveKey, hive_key);
299 PyTypeObject PyHiveKey = {
300 .tp_name = "HiveKey",
301 .tp_methods = hive_key_methods,
302 .tp_new = hive_new,
303 .tp_basicsize = sizeof(pytalloc_Object),
304 .tp_flags = Py_TPFLAGS_DEFAULT,
307 PyTypeObject PyRegistryKey = {
308 .tp_name = "RegistryKey",
309 .tp_basicsize = sizeof(pytalloc_Object),
310 .tp_flags = Py_TPFLAGS_DEFAULT,
313 static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
315 const char *kwnames[] = { "lp_ctx", "session_info", NULL };
316 struct registry_context *reg_ctx;
317 WERROR result;
318 struct loadparm_context *lp_ctx;
319 PyObject *py_lp_ctx, *py_session_info, *py_credentials;
320 struct auth_session_info *session_info;
321 struct cli_credentials *credentials;
322 TALLOC_CTX *mem_ctx;
324 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO",
325 discard_const_p(char *, kwnames),
326 &py_lp_ctx, &py_session_info,
327 &py_credentials))
328 return NULL;
330 mem_ctx = talloc_new(NULL);
331 if (mem_ctx == NULL) {
332 PyErr_NoMemory();
333 return NULL;
336 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
337 if (lp_ctx == NULL) {
338 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
339 talloc_free(mem_ctx);
340 return NULL;
343 credentials = cli_credentials_from_py_object(py_credentials);
344 if (credentials == NULL) {
345 PyErr_SetString(PyExc_TypeError, "Expected credentials");
346 talloc_free(mem_ctx);
347 return NULL;
350 session_info = NULL; /* FIXME */
352 result = reg_open_samba(NULL, &reg_ctx, NULL,
353 lp_ctx, session_info, credentials);
354 talloc_free(mem_ctx);
355 if (!W_ERROR_IS_OK(result)) {
356 PyErr_SetWERROR(result);
357 return NULL;
360 return pytalloc_steal(&PyRegistry, reg_ctx);
363 static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
365 const char *kwnames[] = { "location", "session_info", "credentials", "lp_ctx", NULL };
366 PyObject *py_session_info = Py_None, *py_credentials = Py_None, *py_lp_ctx = Py_None;
367 WERROR result;
368 char *location;
369 struct loadparm_context *lp_ctx;
370 struct cli_credentials *credentials;
371 struct hive_key *key;
372 struct auth_session_info *session_info;
373 TALLOC_CTX *mem_ctx;
375 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
376 discard_const_p(char *, kwnames),
377 &location, &py_session_info,
378 &py_credentials, &py_lp_ctx))
379 return NULL;
381 mem_ctx = talloc_new(NULL);
382 if (mem_ctx == NULL) {
383 PyErr_NoMemory();
384 return NULL;
387 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
388 if (lp_ctx == NULL) {
389 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
390 talloc_free(mem_ctx);
391 return NULL;
394 credentials = cli_credentials_from_py_object(py_credentials);
395 if (credentials == NULL) {
396 PyErr_SetString(PyExc_TypeError, "Expected credentials");
397 talloc_free(mem_ctx);
398 return NULL;
401 session_info = NULL; /* FIXME */
403 result = reg_open_ldb_file(NULL, location, session_info, credentials,
404 s4_event_context_init(NULL), lp_ctx, &key);
405 talloc_free(mem_ctx);
406 PyErr_WERROR_NOT_OK_RAISE(result);
408 return pytalloc_steal(&PyHiveKey, key);
411 static PyObject *py_str_regtype(PyObject *self, PyObject *args)
413 int regtype;
415 if (!PyArg_ParseTuple(args, "i", &regtype))
416 return NULL;
418 return PyString_FromString(str_regtype(regtype));
421 static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
423 uint32_t hkey;
424 const char *str;
426 if (!PyArg_ParseTuple(args, "I", &hkey))
427 return NULL;
429 str = reg_get_predef_name(hkey);
430 if (str == NULL)
431 Py_RETURN_NONE;
432 return PyString_FromString(str);
435 static PyMethodDef py_registry_methods[] = {
436 { "open_samba", (PyCFunction)py_open_samba, METH_VARARGS|METH_KEYWORDS, "open_samba() -> reg" },
437 { "open_ldb", (PyCFunction)py_open_ldb_file, METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
438 { "open_hive", (PyCFunction)py_open_hive, METH_VARARGS|METH_KEYWORDS, "open_hive(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
439 { "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
440 { "get_predef_name", py_get_predef_name, METH_VARARGS, "get_predef_name(hkey) -> str" },
441 { NULL }
444 void initregistry(void)
446 PyObject *m;
447 PyTypeObject *talloc_type = pytalloc_GetObjectType();
449 if (talloc_type == NULL)
450 return;
452 PyHiveKey.tp_base = talloc_type;
453 PyRegistry.tp_base = talloc_type;
454 PyRegistryKey.tp_base = talloc_type;
456 if (PyType_Ready(&PyHiveKey) < 0)
457 return;
459 if (PyType_Ready(&PyRegistry) < 0)
460 return;
462 if (PyType_Ready(&PyRegistryKey) < 0)
463 return;
465 m = Py_InitModule3("registry", py_registry_methods, "Registry");
466 if (m == NULL)
467 return;
469 PyModule_AddObject(m, "HKEY_CLASSES_ROOT", PyInt_FromLong(HKEY_CLASSES_ROOT));
470 PyModule_AddObject(m, "HKEY_CURRENT_USER", PyInt_FromLong(HKEY_CURRENT_USER));
471 PyModule_AddObject(m, "HKEY_LOCAL_MACHINE", PyInt_FromLong(HKEY_LOCAL_MACHINE));
472 PyModule_AddObject(m, "HKEY_USERS", PyInt_FromLong(HKEY_USERS));
473 PyModule_AddObject(m, "HKEY_PERFORMANCE_DATA", PyInt_FromLong(HKEY_PERFORMANCE_DATA));
474 PyModule_AddObject(m, "HKEY_CURRENT_CONFIG", PyInt_FromLong(HKEY_CURRENT_CONFIG));
475 PyModule_AddObject(m, "HKEY_DYN_DATA", PyInt_FromLong(HKEY_DYN_DATA));
476 PyModule_AddObject(m, "HKEY_PERFORMANCE_TEXT", PyInt_FromLong(HKEY_PERFORMANCE_TEXT));
477 PyModule_AddObject(m, "HKEY_PERFORMANCE_NLSTEXT", PyInt_FromLong(HKEY_PERFORMANCE_NLSTEXT));
479 Py_INCREF(&PyRegistry);
480 PyModule_AddObject(m, "Registry", (PyObject *)&PyRegistry);
482 Py_INCREF(&PyHiveKey);
483 PyModule_AddObject(m, "HiveKey", (PyObject *)&PyHiveKey);
485 Py_INCREF(&PyRegistryKey);
486 PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);