source: vendor/3.6.0/source4/lib/registry/pyregistry.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 14.9 KB
Line 
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
6
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.
11
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.
16
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/>.
19*/
20
21#include <Python.h>
22#include "includes.h"
23#include "libcli/util/pyerrors.h"
24#include "lib/registry/registry.h"
25#include "lib/talloc/pytalloc.h"
26#include "lib/events/events.h"
27#include "auth/credentials/pycredentials.h"
28#include "param/pyparam.h"
29
30extern PyTypeObject PyRegistryKey;
31extern PyTypeObject PyRegistry;
32extern PyTypeObject PyHiveKey;
33
34/*#define PyRegistryKey_AsRegistryKey(obj) py_talloc_get_type(obj, struct registry_key)*/
35#define PyRegistry_AsRegistryContext(obj) ((struct registry_context *)py_talloc_get_ptr(obj))
36#define PyHiveKey_AsHiveKey(obj) ((struct hive_key*)py_talloc_get_ptr(obj))
37
38
39static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
40{
41 char *name;
42 WERROR result;
43 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
44 struct registry_key *key;
45
46 if (!PyArg_ParseTuple(args, "s", &name))
47 return NULL;
48
49 result = reg_get_predefined_key_by_name(ctx, name, &key);
50 PyErr_WERROR_IS_ERR_RAISE(result);
51
52 return py_talloc_steal(&PyRegistryKey, key);
53}
54
55static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
56{
57 char *path;
58 WERROR result;
59 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
60
61 if (!PyArg_ParseTuple(args, "s", &path))
62 return NULL;
63
64 result = reg_key_del_abs(ctx, path);
65 PyErr_WERROR_IS_ERR_RAISE(result);
66
67 Py_RETURN_NONE;
68}
69
70static PyObject *py_get_predefined_key(PyObject *self, PyObject *args)
71{
72 uint32_t hkey;
73 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
74 WERROR result;
75 struct registry_key *key;
76
77 if (!PyArg_ParseTuple(args, "I", &hkey))
78 return NULL;
79
80 result = reg_get_predefined_key(ctx, hkey, &key);
81 PyErr_WERROR_IS_ERR_RAISE(result);
82
83 return py_talloc_steal(&PyRegistryKey, key);
84}
85
86static PyObject *py_diff_apply(PyObject *self, PyObject *args)
87{
88 char *filename;
89 WERROR result;
90 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
91 if (!PyArg_ParseTuple(args, "s", &filename))
92 return NULL;
93
94 result = reg_diff_apply(ctx, filename);
95 PyErr_WERROR_IS_ERR_RAISE(result);
96
97 Py_RETURN_NONE;
98}
99
100static PyObject *py_mount_hive(PyObject *self, PyObject *args)
101{
102 struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
103 uint32_t hkey;
104 PyObject *py_hivekey, *py_elements = Py_None;
105 const char **elements;
106 WERROR result;
107
108 if (!PyArg_ParseTuple(args, "OI|O", &py_hivekey, &hkey, &py_elements))
109 return NULL;
110
111 if (!PyList_Check(py_elements) && py_elements != Py_None) {
112 PyErr_SetString(PyExc_TypeError, "Expected list of elements");
113 return NULL;
114 }
115
116 if (py_elements == Py_None) {
117 elements = NULL;
118 } else {
119 int i;
120 elements = talloc_array(NULL, const char *, PyList_Size(py_elements));
121 for (i = 0; i < PyList_Size(py_elements); i++)
122 elements[i] = PyString_AsString(PyList_GetItem(py_elements, i));
123 }
124
125 SMB_ASSERT(ctx != NULL);
126
127 result = reg_mount_hive(ctx, PyHiveKey_AsHiveKey(py_hivekey), hkey, elements);
128 PyErr_WERROR_IS_ERR_RAISE(result);
129
130 Py_RETURN_NONE;
131}
132
133static PyObject *registry_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
134{
135 WERROR result;
136 struct registry_context *ctx;
137 result = reg_open_local(NULL, &ctx);
138 PyErr_WERROR_IS_ERR_RAISE(result);
139 return py_talloc_steal(&PyRegistry, ctx);
140}
141
142static PyMethodDef registry_methods[] = {
143 { "get_predefined_key_by_name", py_get_predefined_key_by_name, METH_VARARGS,
144 "S.get_predefined_key_by_name(name) -> key\n"
145 "Find a predefined key by name" },
146 { "key_del_abs", py_key_del_abs, METH_VARARGS, "S.key_del_abs(name) -> None\n"
147 "Delete a key by absolute path." },
148 { "get_predefined_key", py_get_predefined_key, METH_VARARGS, "S.get_predefined_key(hkey_id) -> key\n"
149 "Find a predefined key by id" },
150 { "diff_apply", py_diff_apply, METH_VARARGS, "S.diff_apply(filename) -> None\n"
151 "Apply the diff from the specified file" },
152 { "mount_hive", py_mount_hive, METH_VARARGS, "S.mount_hive(key, key_id, elements=None) -> None\n"
153 "Mount the specified key at the specified path." },
154 { NULL }
155};
156
157PyTypeObject PyRegistry = {
158 .tp_name = "Registry",
159 .tp_methods = registry_methods,
160 .tp_new = registry_new,
161 .tp_basicsize = sizeof(py_talloc_Object),
162 .tp_flags = Py_TPFLAGS_DEFAULT,
163};
164
165static PyObject *py_hive_key_del(PyObject *self, PyObject *args)
166{
167 char *name;
168 struct hive_key *key = PyHiveKey_AsHiveKey(self);
169 WERROR result;
170
171 if (!PyArg_ParseTuple(args, "s", &name))
172 return NULL;
173
174 result = hive_key_del(NULL, key, name);
175
176 PyErr_WERROR_IS_ERR_RAISE(result);
177
178 Py_RETURN_NONE;
179}
180
181static PyObject *py_hive_key_flush(PyObject *self)
182{
183 WERROR result;
184 struct hive_key *key = PyHiveKey_AsHiveKey(self);
185
186 result = hive_key_flush(key);
187 PyErr_WERROR_IS_ERR_RAISE(result);
188
189 Py_RETURN_NONE;
190}
191
192static PyObject *py_hive_key_del_value(PyObject *self, PyObject *args)
193{
194 char *name;
195 WERROR result;
196 struct hive_key *key = PyHiveKey_AsHiveKey(self);
197
198 if (!PyArg_ParseTuple(args, "s", &name))
199 return NULL;
200
201 result = hive_key_del_value(NULL, key, name);
202
203 PyErr_WERROR_IS_ERR_RAISE(result);
204
205 Py_RETURN_NONE;
206}
207
208static PyObject *py_hive_key_set_value(PyObject *self, PyObject *args)
209{
210 char *name;
211 uint32_t type;
212 DATA_BLOB value;
213 WERROR result;
214 struct hive_key *key = PyHiveKey_AsHiveKey(self);
215
216 if (!PyArg_ParseTuple(args, "siz#", &name, &type, &value.data, &value.length))
217 return NULL;
218
219 if (value.data != NULL)
220 result = hive_key_set_value(key, name, type, value);
221 else
222 result = hive_key_del_value(NULL, key, name);
223
224 PyErr_WERROR_IS_ERR_RAISE(result);
225
226 Py_RETURN_NONE;
227}
228
229static PyMethodDef hive_key_methods[] = {
230 { "del", py_hive_key_del, METH_VARARGS, "S.del(name) -> None\n"
231 "Delete a subkey" },
232 { "flush", (PyCFunction)py_hive_key_flush, METH_NOARGS, "S.flush() -> None\n"
233 "Flush this key to disk" },
234 { "del_value", py_hive_key_del_value, METH_VARARGS, "S.del_value(name) -> None\n"
235 "Delete a value" },
236 { "set_value", py_hive_key_set_value, METH_VARARGS, "S.set_value(name, type, data) -> None\n"
237 "Set a value" },
238 { NULL }
239};
240
241static PyObject *hive_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
242 Py_RETURN_NONE;
243}
244
245static PyObject *py_open_hive(PyTypeObject *type, PyObject *args, PyObject *kwargs)
246{
247 const char *kwnames[] = { "location", "lp_ctx", "session_info", "credentials", NULL };
248 WERROR result;
249 struct loadparm_context *lp_ctx;
250 PyObject *py_lp_ctx, *py_session_info, *py_credentials;
251 struct auth_session_info *session_info;
252 struct cli_credentials *credentials;
253 char *location;
254 struct hive_key *hive_key;
255 TALLOC_CTX *mem_ctx;
256
257 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
258 discard_const_p(char *, kwnames),
259 &location,
260 &py_lp_ctx, &py_session_info,
261 &py_credentials))
262 return NULL;
263
264 mem_ctx = talloc_new(NULL);
265 if (mem_ctx == NULL) {
266 PyErr_NoMemory();
267 return NULL;
268 }
269
270 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
271 if (lp_ctx == NULL) {
272 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
273 talloc_free(mem_ctx);
274 return NULL;
275 }
276
277 credentials = cli_credentials_from_py_object(py_credentials);
278 if (credentials == NULL) {
279 PyErr_SetString(PyExc_TypeError, "Expected credentials");
280 talloc_free(mem_ctx);
281 return NULL;
282 }
283 session_info = NULL;
284
285 result = reg_open_hive(NULL, location, session_info, credentials,
286 tevent_context_init(NULL),
287 lp_ctx, &hive_key);
288 talloc_free(mem_ctx);
289 PyErr_WERROR_IS_ERR_RAISE(result);
290
291 return py_talloc_steal(&PyHiveKey, hive_key);
292}
293
294PyTypeObject PyHiveKey = {
295 .tp_name = "HiveKey",
296 .tp_methods = hive_key_methods,
297 .tp_new = hive_new,
298 .tp_basicsize = sizeof(py_talloc_Object),
299 .tp_flags = Py_TPFLAGS_DEFAULT,
300};
301
302PyTypeObject PyRegistryKey = {
303 .tp_name = "RegistryKey",
304 .tp_basicsize = sizeof(py_talloc_Object),
305 .tp_flags = Py_TPFLAGS_DEFAULT,
306};
307
308static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
309{
310 const char *kwnames[] = { "lp_ctx", "session_info", NULL };
311 struct registry_context *reg_ctx;
312 WERROR result;
313 struct loadparm_context *lp_ctx;
314 PyObject *py_lp_ctx, *py_session_info, *py_credentials;
315 struct auth_session_info *session_info;
316 struct cli_credentials *credentials;
317 TALLOC_CTX *mem_ctx;
318
319 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO",
320 discard_const_p(char *, kwnames),
321 &py_lp_ctx, &py_session_info,
322 &py_credentials))
323 return NULL;
324
325 mem_ctx = talloc_new(NULL);
326 if (mem_ctx == NULL) {
327 PyErr_NoMemory();
328 return NULL;
329 }
330
331 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
332 if (lp_ctx == NULL) {
333 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
334 talloc_free(mem_ctx);
335 return NULL;
336 }
337
338 credentials = cli_credentials_from_py_object(py_credentials);
339 if (credentials == NULL) {
340 PyErr_SetString(PyExc_TypeError, "Expected credentials");
341 talloc_free(mem_ctx);
342 return NULL;
343 }
344
345 session_info = NULL; /* FIXME */
346
347 result = reg_open_samba(NULL, &reg_ctx, NULL,
348 lp_ctx, session_info, credentials);
349 talloc_free(mem_ctx);
350 if (!W_ERROR_IS_OK(result)) {
351 PyErr_SetWERROR(result);
352 return NULL;
353 }
354
355 return py_talloc_steal(&PyRegistry, reg_ctx);
356}
357
358static PyObject *py_open_directory(PyObject *self, PyObject *args)
359{
360 char *location;
361 WERROR result;
362 struct hive_key *key;
363
364 if (!PyArg_ParseTuple(args, "s", &location))
365 return NULL;
366
367 result = reg_open_directory(NULL, location, &key);
368 PyErr_WERROR_IS_ERR_RAISE(result);
369
370 return py_talloc_steal(&PyHiveKey, key);
371}
372
373static PyObject *py_create_directory(PyObject *self, PyObject *args)
374{
375 char *location;
376 WERROR result;
377 struct hive_key *key;
378
379 if (!PyArg_ParseTuple(args, "s", &location))
380 return NULL;
381
382 result = reg_create_directory(NULL, location, &key);
383 PyErr_WERROR_IS_ERR_RAISE(result);
384
385 return py_talloc_steal(&PyHiveKey, key);
386}
387
388static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
389{
390 const char *kwnames[] = { "location", "session_info", "credentials", "lp_ctx", NULL };
391 PyObject *py_session_info = Py_None, *py_credentials = Py_None, *py_lp_ctx = Py_None;
392 WERROR result;
393 char *location;
394 struct loadparm_context *lp_ctx;
395 struct cli_credentials *credentials;
396 struct hive_key *key;
397 struct auth_session_info *session_info;
398 TALLOC_CTX *mem_ctx;
399
400 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
401 discard_const_p(char *, kwnames),
402 &location, &py_session_info,
403 &py_credentials, &py_lp_ctx))
404 return NULL;
405
406 mem_ctx = talloc_new(NULL);
407 if (mem_ctx == NULL) {
408 PyErr_NoMemory();
409 return NULL;
410 }
411
412 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
413 if (lp_ctx == NULL) {
414 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
415 talloc_free(mem_ctx);
416 return NULL;
417 }
418
419 credentials = cli_credentials_from_py_object(py_credentials);
420 if (credentials == NULL) {
421 PyErr_SetString(PyExc_TypeError, "Expected credentials");
422 talloc_free(mem_ctx);
423 return NULL;
424 }
425
426 session_info = NULL; /* FIXME */
427
428 result = reg_open_ldb_file(NULL, location, session_info, credentials,
429 s4_event_context_init(NULL), lp_ctx, &key);
430 talloc_free(mem_ctx);
431 PyErr_WERROR_IS_ERR_RAISE(result);
432
433 return py_talloc_steal(&PyHiveKey, key);
434}
435
436static PyObject *py_str_regtype(PyObject *self, PyObject *args)
437{
438 int regtype;
439
440 if (!PyArg_ParseTuple(args, "i", &regtype))
441 return NULL;
442
443 return PyString_FromString(str_regtype(regtype));
444}
445
446static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
447{
448 uint32_t hkey;
449 const char *str;
450
451 if (!PyArg_ParseTuple(args, "I", &hkey))
452 return NULL;
453
454 str = reg_get_predef_name(hkey);
455 if (str == NULL)
456 Py_RETURN_NONE;
457 return PyString_FromString(str);
458}
459
460static PyMethodDef py_registry_methods[] = {
461 { "open_samba", (PyCFunction)py_open_samba, METH_VARARGS|METH_KEYWORDS, "open_samba() -> reg" },
462 { "open_directory", py_open_directory, METH_VARARGS, "open_dir(location) -> key" },
463 { "create_directory", py_create_directory, METH_VARARGS, "create_dir(location) -> key" },
464 { "open_ldb", (PyCFunction)py_open_ldb_file, METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
465 { "open_hive", (PyCFunction)py_open_hive, METH_VARARGS|METH_KEYWORDS, "open_hive(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
466 { "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
467 { "get_predef_name", py_get_predef_name, METH_VARARGS, "get_predef_name(hkey) -> str" },
468 { NULL }
469};
470
471void initregistry(void)
472{
473 PyObject *m;
474 PyTypeObject *talloc_type = PyTalloc_GetObjectType();
475
476 if (talloc_type == NULL)
477 return;
478
479 PyHiveKey.tp_base = talloc_type;
480 PyRegistry.tp_base = talloc_type;
481 PyRegistryKey.tp_base = talloc_type;
482
483 if (PyType_Ready(&PyHiveKey) < 0)
484 return;
485
486 if (PyType_Ready(&PyRegistry) < 0)
487 return;
488
489 if (PyType_Ready(&PyRegistryKey) < 0)
490 return;
491
492 m = Py_InitModule3("registry", py_registry_methods, "Registry");
493 if (m == NULL)
494 return;
495
496 PyModule_AddObject(m, "HKEY_CLASSES_ROOT", PyInt_FromLong(HKEY_CLASSES_ROOT));
497 PyModule_AddObject(m, "HKEY_CURRENT_USER", PyInt_FromLong(HKEY_CURRENT_USER));
498 PyModule_AddObject(m, "HKEY_LOCAL_MACHINE", PyInt_FromLong(HKEY_LOCAL_MACHINE));
499 PyModule_AddObject(m, "HKEY_USERS", PyInt_FromLong(HKEY_USERS));
500 PyModule_AddObject(m, "HKEY_PERFORMANCE_DATA", PyInt_FromLong(HKEY_PERFORMANCE_DATA));
501 PyModule_AddObject(m, "HKEY_CURRENT_CONFIG", PyInt_FromLong(HKEY_CURRENT_CONFIG));
502 PyModule_AddObject(m, "HKEY_DYN_DATA", PyInt_FromLong(HKEY_DYN_DATA));
503 PyModule_AddObject(m, "HKEY_PERFORMANCE_TEXT", PyInt_FromLong(HKEY_PERFORMANCE_TEXT));
504 PyModule_AddObject(m, "HKEY_PERFORMANCE_NLSTEXT", PyInt_FromLong(HKEY_PERFORMANCE_NLSTEXT));
505
506 Py_INCREF(&PyRegistry);
507 PyModule_AddObject(m, "Registry", (PyObject *)&PyRegistry);
508
509 Py_INCREF(&PyHiveKey);
510 PyModule_AddObject(m, "HiveKey", (PyObject *)&PyHiveKey);
511
512 Py_INCREF(&PyRegistryKey);
513 PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);
514}
Note: See TracBrowser for help on using the repository browser.