Line data Source code
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 "lib/replace/system/python.h"
22 : #include "python/py3compat.h"
23 : #include "includes.h"
24 : #include "python/modules.h"
25 : #include "libcli/util/pyerrors.h"
26 : #include "lib/registry/registry.h"
27 : #include <pytalloc.h>
28 : #include "lib/events/events.h"
29 : #include "auth/credentials/pycredentials.h"
30 : #include "param/pyparam.h"
31 :
32 : extern PyTypeObject PyRegistryKey;
33 : extern PyTypeObject PyRegistry;
34 : extern PyTypeObject PyHiveKey;
35 :
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))
39 :
40 :
41 0 : static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
42 : {
43 0 : char *name;
44 0 : WERROR result;
45 0 : struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
46 0 : struct registry_key *key;
47 :
48 0 : if (!PyArg_ParseTuple(args, "s", &name))
49 0 : return NULL;
50 :
51 0 : result = reg_get_predefined_key_by_name(ctx, name, &key);
52 0 : PyErr_WERROR_NOT_OK_RAISE(result);
53 :
54 0 : return pytalloc_steal(&PyRegistryKey, key);
55 : }
56 :
57 0 : static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
58 : {
59 0 : char *path;
60 0 : WERROR result;
61 0 : struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
62 :
63 0 : if (!PyArg_ParseTuple(args, "s", &path))
64 0 : return NULL;
65 :
66 0 : result = reg_key_del_abs(ctx, path);
67 0 : PyErr_WERROR_NOT_OK_RAISE(result);
68 :
69 0 : Py_RETURN_NONE;
70 : }
71 :
72 0 : static PyObject *py_get_predefined_key(PyObject *self, PyObject *args)
73 : {
74 0 : uint32_t hkey;
75 0 : struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
76 0 : WERROR result;
77 0 : struct registry_key *key;
78 :
79 0 : if (!PyArg_ParseTuple(args, "I", &hkey))
80 0 : return NULL;
81 :
82 0 : result = reg_get_predefined_key(ctx, hkey, &key);
83 0 : PyErr_WERROR_NOT_OK_RAISE(result);
84 :
85 0 : return pytalloc_steal(&PyRegistryKey, key);
86 : }
87 :
88 205 : static PyObject *py_diff_apply(PyObject *self, PyObject *args)
89 : {
90 23 : char *filename;
91 23 : WERROR result;
92 205 : struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
93 205 : if (!PyArg_ParseTuple(args, "s", &filename))
94 0 : return NULL;
95 :
96 205 : result = reg_diff_apply(ctx, filename);
97 205 : PyErr_WERROR_NOT_OK_RAISE(result);
98 :
99 205 : Py_RETURN_NONE;
100 : }
101 :
102 205 : static PyObject *py_mount_hive(PyObject *self, PyObject *args)
103 : {
104 205 : struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
105 23 : uint32_t hkey;
106 205 : PyObject *py_hivekey, *py_elements = Py_None;
107 23 : const char **elements;
108 23 : WERROR result;
109 :
110 205 : if (!PyArg_ParseTuple(args, "OI|O", &py_hivekey, &hkey, &py_elements))
111 0 : return NULL;
112 :
113 205 : if (!PyList_Check(py_elements) && py_elements != Py_None) {
114 0 : PyErr_SetString(PyExc_TypeError, "Expected list of elements");
115 0 : return NULL;
116 : }
117 :
118 205 : if (py_elements == Py_None) {
119 182 : elements = NULL;
120 : } else {
121 0 : int i;
122 0 : elements = talloc_array(NULL, const char *, PyList_Size(py_elements));
123 0 : for (i = 0; i < PyList_Size(py_elements); i++)
124 0 : elements[i] = PyUnicode_AsUTF8(PyList_GetItem(py_elements, i));
125 : }
126 :
127 205 : SMB_ASSERT(ctx != NULL);
128 :
129 205 : result = reg_mount_hive(ctx, PyHiveKey_AsHiveKey(py_hivekey), hkey, elements);
130 205 : PyErr_WERROR_NOT_OK_RAISE(result);
131 :
132 205 : Py_RETURN_NONE;
133 : }
134 :
135 206 : static PyObject *registry_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
136 : {
137 24 : WERROR result;
138 24 : struct registry_context *ctx;
139 206 : result = reg_open_local(NULL, &ctx);
140 206 : PyErr_WERROR_NOT_OK_RAISE(result);
141 206 : return pytalloc_steal(&PyRegistry, ctx);
142 : }
143 :
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 : {0}
157 : };
158 :
159 : PyTypeObject PyRegistry = {
160 : .tp_name = "Registry",
161 : .tp_methods = registry_methods,
162 : .tp_new = registry_new,
163 : .tp_flags = Py_TPFLAGS_DEFAULT,
164 : };
165 :
166 0 : static PyObject *py_hive_key_del(PyObject *self, PyObject *args)
167 : {
168 0 : char *name;
169 0 : struct hive_key *key = PyHiveKey_AsHiveKey(self);
170 0 : WERROR result;
171 :
172 0 : if (!PyArg_ParseTuple(args, "s", &name))
173 0 : return NULL;
174 :
175 0 : result = hive_key_del(NULL, key, name);
176 :
177 0 : PyErr_WERROR_NOT_OK_RAISE(result);
178 :
179 0 : Py_RETURN_NONE;
180 : }
181 :
182 1 : static PyObject *py_hive_key_flush(PyObject *self,
183 : PyObject *Py_UNUSED(ignored))
184 : {
185 1 : WERROR result;
186 1 : struct hive_key *key = PyHiveKey_AsHiveKey(self);
187 :
188 1 : result = hive_key_flush(key);
189 1 : PyErr_WERROR_NOT_OK_RAISE(result);
190 :
191 1 : Py_RETURN_NONE;
192 : }
193 :
194 2 : static PyObject *py_hive_key_del_value(PyObject *self, PyObject *args)
195 : {
196 2 : char *name;
197 2 : WERROR result;
198 2 : struct hive_key *key = PyHiveKey_AsHiveKey(self);
199 :
200 2 : if (!PyArg_ParseTuple(args, "s", &name))
201 0 : return NULL;
202 :
203 2 : result = hive_key_del_value(NULL, key, name);
204 :
205 2 : PyErr_WERROR_NOT_OK_RAISE(result);
206 :
207 1 : Py_RETURN_NONE;
208 : }
209 :
210 3 : static PyObject *py_hive_key_set_value(PyObject *self, PyObject *args)
211 : {
212 3 : char *name;
213 3 : uint32_t type;
214 3 : DATA_BLOB value;
215 3 : Py_ssize_t value_length = 0;
216 3 : WERROR result;
217 3 : struct hive_key *key = PyHiveKey_AsHiveKey(self);
218 :
219 3 : if (!PyArg_ParseTuple(args, "sIz#", &name, &type, &value.data, &value_length)) {
220 0 : return NULL;
221 : }
222 3 : value.length = value_length;
223 :
224 3 : if (value.data != NULL)
225 3 : result = hive_key_set_value(key, name, type, value);
226 : else
227 0 : result = hive_key_del_value(NULL, key, name);
228 :
229 3 : PyErr_WERROR_NOT_OK_RAISE(result);
230 :
231 3 : Py_RETURN_NONE;
232 : }
233 :
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 : {0}
244 : };
245 :
246 0 : static PyObject *hive_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
247 0 : Py_RETURN_NONE;
248 : }
249 :
250 0 : static PyObject *py_open_hive(PyTypeObject *type, PyObject *args, PyObject *kwargs)
251 : {
252 0 : const char *kwnames[] = { "location", "lp_ctx", "session_info", "credentials", NULL };
253 0 : WERROR result;
254 0 : struct loadparm_context *lp_ctx;
255 0 : PyObject *py_lp_ctx = Py_None;
256 0 : PyObject *py_session_info = Py_None;
257 0 : PyObject *py_credentials = Py_None;
258 0 : struct auth_session_info *session_info;
259 0 : struct cli_credentials *credentials;
260 0 : char *location;
261 0 : struct hive_key *hive_key;
262 0 : TALLOC_CTX *mem_ctx;
263 :
264 0 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
265 : discard_const_p(char *, kwnames),
266 : &location,
267 : &py_lp_ctx, &py_session_info,
268 : &py_credentials))
269 0 : return NULL;
270 :
271 0 : mem_ctx = talloc_new(NULL);
272 0 : if (mem_ctx == NULL) {
273 0 : PyErr_NoMemory();
274 0 : return NULL;
275 : }
276 :
277 0 : lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
278 0 : if (lp_ctx == NULL) {
279 0 : PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
280 0 : talloc_free(mem_ctx);
281 0 : return NULL;
282 : }
283 :
284 0 : credentials = cli_credentials_from_py_object(py_credentials);
285 0 : if (credentials == NULL) {
286 0 : PyErr_SetString(PyExc_TypeError, "Expected credentials");
287 0 : talloc_free(mem_ctx);
288 0 : return NULL;
289 : }
290 0 : session_info = NULL;
291 :
292 0 : result = reg_open_hive(NULL, location, session_info, credentials,
293 : samba_tevent_context_init(NULL),
294 : lp_ctx, &hive_key);
295 0 : talloc_free(mem_ctx);
296 0 : PyErr_WERROR_NOT_OK_RAISE(result);
297 :
298 0 : return pytalloc_steal(&PyHiveKey, hive_key);
299 : }
300 :
301 : PyTypeObject PyHiveKey = {
302 : .tp_name = "HiveKey",
303 : .tp_methods = hive_key_methods,
304 : .tp_new = hive_new,
305 : .tp_flags = Py_TPFLAGS_DEFAULT,
306 : };
307 :
308 : PyTypeObject PyRegistryKey = {
309 : .tp_name = "RegistryKey",
310 : .tp_flags = Py_TPFLAGS_DEFAULT,
311 : };
312 :
313 0 : static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
314 : {
315 0 : const char *kwnames[] = { "lp_ctx", "session_info", NULL };
316 0 : struct registry_context *reg_ctx;
317 0 : WERROR result;
318 0 : struct loadparm_context *lp_ctx;
319 0 : PyObject *py_lp_ctx = Py_None;
320 0 : PyObject *py_session_info = Py_None;
321 0 : PyObject *py_credentials = Py_None;
322 0 : struct auth_session_info *session_info;
323 0 : struct cli_credentials *credentials;
324 0 : TALLOC_CTX *mem_ctx;
325 :
326 0 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO",
327 : discard_const_p(char *, kwnames),
328 : &py_lp_ctx, &py_session_info,
329 : &py_credentials))
330 0 : return NULL;
331 :
332 0 : mem_ctx = talloc_new(NULL);
333 0 : if (mem_ctx == NULL) {
334 0 : PyErr_NoMemory();
335 0 : return NULL;
336 : }
337 :
338 0 : lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
339 0 : if (lp_ctx == NULL) {
340 0 : PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
341 0 : talloc_free(mem_ctx);
342 0 : return NULL;
343 : }
344 :
345 0 : credentials = cli_credentials_from_py_object(py_credentials);
346 0 : if (credentials == NULL) {
347 0 : PyErr_SetString(PyExc_TypeError, "Expected credentials");
348 0 : talloc_free(mem_ctx);
349 0 : return NULL;
350 : }
351 :
352 0 : session_info = NULL; /* FIXME */
353 :
354 0 : result = reg_open_samba(NULL, ®_ctx, NULL,
355 : lp_ctx, session_info, credentials);
356 0 : talloc_free(mem_ctx);
357 0 : if (!W_ERROR_IS_OK(result)) {
358 0 : PyErr_SetWERROR(result);
359 0 : return NULL;
360 : }
361 :
362 0 : return pytalloc_steal(&PyRegistry, reg_ctx);
363 : }
364 :
365 210 : static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
366 : {
367 210 : const char *kwnames[] = { "location", "session_info", "credentials", "lp_ctx", NULL };
368 210 : PyObject *py_session_info = Py_None, *py_credentials = Py_None, *py_lp_ctx = Py_None;
369 28 : WERROR result;
370 28 : char *location;
371 28 : struct loadparm_context *lp_ctx;
372 28 : struct cli_credentials *credentials;
373 28 : struct hive_key *key;
374 28 : struct auth_session_info *session_info;
375 28 : TALLOC_CTX *mem_ctx;
376 :
377 210 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
378 : discard_const_p(char *, kwnames),
379 : &location, &py_session_info,
380 : &py_credentials, &py_lp_ctx))
381 0 : return NULL;
382 :
383 210 : mem_ctx = talloc_new(NULL);
384 210 : if (mem_ctx == NULL) {
385 0 : PyErr_NoMemory();
386 0 : return NULL;
387 : }
388 :
389 210 : lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
390 210 : if (lp_ctx == NULL) {
391 0 : PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
392 0 : talloc_free(mem_ctx);
393 0 : return NULL;
394 : }
395 :
396 210 : credentials = cli_credentials_from_py_object(py_credentials);
397 210 : if (credentials == NULL) {
398 0 : PyErr_SetString(PyExc_TypeError, "Expected credentials");
399 0 : talloc_free(mem_ctx);
400 0 : return NULL;
401 : }
402 :
403 210 : session_info = NULL; /* FIXME */
404 :
405 210 : result = reg_open_ldb_file(NULL, location, session_info, credentials,
406 : s4_event_context_init(NULL), lp_ctx, &key);
407 210 : talloc_free(mem_ctx);
408 210 : PyErr_WERROR_NOT_OK_RAISE(result);
409 :
410 210 : return pytalloc_steal(&PyHiveKey, key);
411 : }
412 :
413 145 : static PyObject *py_str_regtype(PyObject *self, PyObject *args)
414 : {
415 1 : int regtype;
416 :
417 145 : if (!PyArg_ParseTuple(args, "i", ®type))
418 0 : return NULL;
419 :
420 145 : return PyUnicode_FromString(str_regtype(regtype));
421 : }
422 :
423 1 : static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
424 : {
425 1 : uint32_t hkey;
426 1 : const char *str;
427 :
428 1 : if (!PyArg_ParseTuple(args, "I", &hkey))
429 0 : return NULL;
430 :
431 1 : str = reg_get_predef_name(hkey);
432 1 : if (str == NULL)
433 0 : Py_RETURN_NONE;
434 1 : return PyUnicode_FromString(str);
435 : }
436 :
437 : static PyMethodDef py_registry_methods[] = {
438 : { "open_samba", PY_DISCARD_FUNC_SIG(PyCFunction, py_open_samba),
439 : METH_VARARGS|METH_KEYWORDS, "open_samba() -> reg" },
440 : { "open_ldb", PY_DISCARD_FUNC_SIG(PyCFunction, py_open_ldb_file),
441 : METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
442 : { "open_hive", PY_DISCARD_FUNC_SIG(PyCFunction, py_open_hive),
443 : METH_VARARGS|METH_KEYWORDS, "open_hive(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
444 : { "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
445 : { "get_predef_name", py_get_predef_name, METH_VARARGS, "get_predef_name(hkey) -> str" },
446 : {0}
447 : };
448 :
449 : static struct PyModuleDef moduledef = {
450 : PyModuleDef_HEAD_INIT,
451 : .m_name = "registry",
452 : .m_doc = "Registry",
453 : .m_size = -1,
454 : .m_methods = py_registry_methods,
455 : };
456 :
457 1728 : MODULE_INIT_FUNC(registry)
458 : {
459 44 : PyObject *m;
460 :
461 1728 : if (pytalloc_BaseObject_PyType_Ready(&PyHiveKey) < 0)
462 0 : return NULL;
463 :
464 1728 : if (pytalloc_BaseObject_PyType_Ready(&PyRegistry) < 0)
465 0 : return NULL;
466 :
467 1728 : if (pytalloc_BaseObject_PyType_Ready(&PyRegistryKey) < 0)
468 0 : return NULL;
469 :
470 1728 : m = PyModule_Create(&moduledef);
471 1728 : if (m == NULL)
472 0 : return NULL;
473 :
474 1728 : PyModule_AddObject(m, "HKEY_CLASSES_ROOT", PyLong_FromLong(HKEY_CLASSES_ROOT));
475 1728 : PyModule_AddObject(m, "HKEY_CURRENT_USER", PyLong_FromLong(HKEY_CURRENT_USER));
476 1728 : PyModule_AddObject(m, "HKEY_LOCAL_MACHINE", PyLong_FromLong(HKEY_LOCAL_MACHINE));
477 1728 : PyModule_AddObject(m, "HKEY_USERS", PyLong_FromLong(HKEY_USERS));
478 1728 : PyModule_AddObject(m, "HKEY_PERFORMANCE_DATA", PyLong_FromLong(HKEY_PERFORMANCE_DATA));
479 1728 : PyModule_AddObject(m, "HKEY_CURRENT_CONFIG", PyLong_FromLong(HKEY_CURRENT_CONFIG));
480 1728 : PyModule_AddObject(m, "HKEY_DYN_DATA", PyLong_FromLong(HKEY_DYN_DATA));
481 1728 : PyModule_AddObject(m, "HKEY_PERFORMANCE_TEXT", PyLong_FromLong(HKEY_PERFORMANCE_TEXT));
482 1728 : PyModule_AddObject(m, "HKEY_PERFORMANCE_NLSTEXT", PyLong_FromLong(HKEY_PERFORMANCE_NLSTEXT));
483 :
484 1448 : Py_INCREF(&PyRegistry);
485 1728 : PyModule_AddObject(m, "Registry", (PyObject *)&PyRegistry);
486 :
487 1448 : Py_INCREF(&PyHiveKey);
488 1728 : PyModule_AddObject(m, "HiveKey", (PyObject *)&PyHiveKey);
489 :
490 1448 : Py_INCREF(&PyRegistryKey);
491 1728 : PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);
492 :
493 1728 : return m;
494 : }
|