2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "../lib/util/python_util.h"
22 #include "libcli/util/pyerrors.h"
23 #include "lib/registry/registry.h"
24 #include "scripting/python/modules.h" /* for py_iconv_convenience() */
27 #include "param/pyparam.h"
29 extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
31 PyAPI_DATA(PyTypeObject) PyRegistryKey;
32 PyAPI_DATA(PyTypeObject) PyRegistry;
33 PyAPI_DATA(PyTypeObject) PyHiveKey;
35 static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
39 struct registry_context *ctx = py_talloc_get_ptr(self);
40 struct registry_key *key;
42 if (!PyArg_ParseTuple(args, "s", &name))
45 result = reg_get_predefined_key_by_name(ctx, name, &key);
46 PyErr_WERROR_IS_ERR_RAISE(result);
48 return py_talloc_import(&PyRegistryKey, key);
51 static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
55 struct registry_context *ctx = py_talloc_get_ptr(self);
57 if (!PyArg_ParseTuple(args, "s", &path))
60 result = reg_key_del_abs(ctx, path);
61 PyErr_WERROR_IS_ERR_RAISE(result);
66 static PyObject *py_get_predefined_key(PyObject *self, PyObject *args)
69 struct registry_context *ctx = py_talloc_get_ptr(self);
71 struct registry_key *key;
73 if (!PyArg_ParseTuple(args, "I", &hkey))
76 result = reg_get_predefined_key(ctx, hkey, &key);
77 PyErr_WERROR_IS_ERR_RAISE(result);
79 return py_talloc_import(&PyRegistryKey, key);
82 static PyObject *py_diff_apply(PyObject *self, PyObject *args)
86 struct registry_context *ctx = py_talloc_get_ptr(self);
87 if (!PyArg_ParseTuple(args, "s", &filename))
90 result = reg_diff_apply(ctx, py_iconv_convenience(NULL), filename);
91 PyErr_WERROR_IS_ERR_RAISE(result);
96 static PyObject *py_mount_hive(PyObject *self, PyObject *args)
98 struct registry_context *ctx = py_talloc_get_ptr(self);
100 PyObject *py_hivekey, *py_elements = Py_None;
101 const char **elements;
104 if (!PyArg_ParseTuple(args, "OI|O", &py_hivekey, &hkey, &py_elements))
107 if (!PyList_Check(py_elements) && py_elements != Py_None) {
108 PyErr_SetString(PyExc_TypeError, "Expected list of elements");
112 if (py_elements == Py_None) {
116 elements = talloc_array(NULL, const char *, PyList_Size(py_elements));
117 for (i = 0; i < PyList_Size(py_elements); i++)
118 elements[i] = PyString_AsString(PyList_GetItem(py_elements, i));
121 SMB_ASSERT(ctx != NULL);
123 result = reg_mount_hive(ctx, py_talloc_get_ptr(py_hivekey), hkey, elements);
124 PyErr_WERROR_IS_ERR_RAISE(result);
129 static PyObject *registry_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
132 struct registry_context *ctx;
133 result = reg_open_local(NULL, &ctx);
134 PyErr_WERROR_IS_ERR_RAISE(result);
135 return py_talloc_import(&PyRegistry, ctx);
138 static PyMethodDef registry_methods[] = {
139 { "get_predefined_key_by_name", py_get_predefined_key_by_name, METH_VARARGS,
140 "S.get_predefined_key_by_name(name) -> key\n"
141 "Find a predefined key by name" },
142 { "key_del_abs", py_key_del_abs, METH_VARARGS, "S.key_del_abs(name) -> None\n"
143 "Delete a key by absolute path." },
144 { "get_predefined_key", py_get_predefined_key, METH_VARARGS, "S.get_predefined_key(hkey_id) -> key\n"
145 "Find a predefined key by id" },
146 { "diff_apply", py_diff_apply, METH_VARARGS, "S.diff_apply(filename) -> None\n"
147 "Apply the diff from the specified file" },
148 { "mount_hive", py_mount_hive, METH_VARARGS, "S.mount_hive(key, key_id, elements=None) -> None\n"
149 "Mount the specified key at the specified path." },
153 PyTypeObject PyRegistry = {
154 .tp_name = "Registry",
155 .tp_methods = registry_methods,
156 .tp_new = registry_new,
157 .tp_basicsize = sizeof(py_talloc_Object),
158 .tp_dealloc = py_talloc_dealloc,
159 .tp_flags = Py_TPFLAGS_DEFAULT,
162 static PyObject *py_hive_key_del(PyObject *self, PyObject *args)
165 struct hive_key *key = py_talloc_get_ptr(self);
168 if (!PyArg_ParseTuple(args, "s", &name))
171 result = hive_key_del(key, name);
173 PyErr_WERROR_IS_ERR_RAISE(result);
178 static PyObject *py_hive_key_flush(PyObject *self)
181 struct hive_key *key = py_talloc_get_ptr(self);
183 result = hive_key_flush(key);
184 PyErr_WERROR_IS_ERR_RAISE(result);
189 static PyObject *py_hive_key_del_value(PyObject *self, PyObject *args)
193 struct hive_key *key = py_talloc_get_ptr(self);
195 if (!PyArg_ParseTuple(args, "s", &name))
198 result = hive_key_del_value(key, name);
200 PyErr_WERROR_IS_ERR_RAISE(result);
205 static PyObject *py_hive_key_set_value(PyObject *self, PyObject *args)
211 struct hive_key *key = py_talloc_get_ptr(self);
213 if (!PyArg_ParseTuple(args, "siz#", &name, &type, &value.data, &value.length))
216 if (value.data != NULL)
217 result = hive_key_set_value(key, name, type, value);
219 result = hive_key_del_value(key, name);
221 PyErr_WERROR_IS_ERR_RAISE(result);
226 static PyMethodDef hive_key_methods[] = {
227 { "del", py_hive_key_del, METH_VARARGS, "S.del(name) -> None\n"
229 { "flush", (PyCFunction)py_hive_key_flush, METH_NOARGS, "S.flush() -> None\n"
230 "Flush this key to disk" },
231 { "del_value", py_hive_key_del_value, METH_VARARGS, "S.del_value(name) -> None\n"
233 { "set_value", py_hive_key_set_value, METH_VARARGS, "S.set_value(name, type, data) -> None\n"
238 static PyObject *hive_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
244 PyTypeObject PyHiveKey = {
245 .tp_name = "HiveKey",
246 .tp_methods = hive_key_methods,
248 .tp_basicsize = sizeof(py_talloc_Object),
249 .tp_dealloc = py_talloc_dealloc,
250 .tp_flags = Py_TPFLAGS_DEFAULT,
253 PyTypeObject PyRegistryKey = {
254 .tp_name = "RegistryKey",
255 .tp_basicsize = sizeof(py_talloc_Object),
256 .tp_dealloc = py_talloc_dealloc,
257 .tp_flags = Py_TPFLAGS_DEFAULT,
260 static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
262 const char *kwnames[] = { "lp_ctx", "session_info", NULL };
263 struct registry_context *reg_ctx;
265 struct loadparm_context *lp_ctx;
266 PyObject *py_lp_ctx, *py_session_info, *py_credentials;
267 struct auth_session_info *session_info;
268 struct cli_credentials *credentials;
269 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO", discard_const_p(char *, kwnames),
270 &py_lp_ctx, &py_session_info, &py_credentials))
273 lp_ctx = lp_from_py_object(py_lp_ctx);
274 if (lp_ctx == NULL) {
275 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
279 credentials = cli_credentials_from_py_object(py_credentials);
280 if (credentials == NULL) {
281 PyErr_SetString(PyExc_TypeError, "Expected credentials");
285 session_info = NULL; /* FIXME */
287 result = reg_open_samba(NULL, ®_ctx, NULL,
288 lp_ctx, session_info, credentials);
289 if (!W_ERROR_IS_OK(result)) {
290 PyErr_SetWERROR(result);
294 return py_talloc_import(&PyRegistry, reg_ctx);
297 static PyObject *py_open_directory(PyObject *self, PyObject *args)
301 struct hive_key *key;
303 if (!PyArg_ParseTuple(args, "s", &location))
306 result = reg_open_directory(NULL, location, &key);
307 PyErr_WERROR_IS_ERR_RAISE(result);
309 return py_talloc_import(&PyHiveKey, key);
312 static PyObject *py_create_directory(PyObject *self, PyObject *args)
316 struct hive_key *key;
318 if (!PyArg_ParseTuple(args, "s", &location))
321 result = reg_create_directory(NULL, location, &key);
322 PyErr_WERROR_IS_ERR_RAISE(result);
324 return py_talloc_import(&PyHiveKey, key);
327 static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
329 const char *kwnames[] = { "location", "session_info", "credentials", "lp_ctx", NULL };
330 PyObject *py_session_info = Py_None, *py_credentials = Py_None, *py_lp_ctx = Py_None;
333 struct loadparm_context *lp_ctx;
334 struct cli_credentials *credentials;
335 struct hive_key *key;
336 struct auth_session_info *session_info;
338 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO",
339 discard_const_p(char *, kwnames),
341 &py_session_info, &py_credentials,
345 lp_ctx = lp_from_py_object(py_lp_ctx);
346 if (lp_ctx == NULL) {
347 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
351 credentials = cli_credentials_from_py_object(py_credentials);
352 if (credentials == NULL) {
353 PyErr_SetString(PyExc_TypeError, "Expected credentials");
357 session_info = NULL; /* FIXME */
359 result = reg_open_ldb_file(NULL, location, session_info, credentials,
360 tevent_context_init(NULL), lp_ctx, &key);
361 PyErr_WERROR_IS_ERR_RAISE(result);
363 return py_talloc_import(&PyHiveKey, key);
366 static PyObject *py_str_regtype(PyObject *self, PyObject *args)
370 if (!PyArg_ParseTuple(args, "i", ®type))
373 return PyString_FromString(str_regtype(regtype));
376 static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
381 if (!PyArg_ParseTuple(args, "I", &hkey))
384 str = reg_get_predef_name(hkey);
387 return PyString_FromString(str);
390 static PyMethodDef py_registry_methods[] = {
391 { "open_samba", (PyCFunction)py_open_samba, METH_VARARGS|METH_KEYWORDS, "open_samba() -> reg" },
392 { "open_directory", py_open_directory, METH_VARARGS, "open_dir(location) -> key" },
393 { "create_directory", py_create_directory, METH_VARARGS, "create_dir(location) -> key" },
394 { "open_ldb", (PyCFunction)py_open_ldb_file, METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
395 { "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
396 { "get_predef_name", py_get_predef_name, METH_VARARGS, "get_predef_name(hkey) -> str" },
400 void initregistry(void)
404 if (PyType_Ready(&PyHiveKey) < 0)
407 if (PyType_Ready(&PyRegistry) < 0)
410 if (PyType_Ready(&PyRegistryKey) < 0)
413 m = Py_InitModule3("registry", py_registry_methods, "Registry");
417 PyModule_AddObject(m, "HKEY_CLASSES_ROOT", PyInt_FromLong(HKEY_CLASSES_ROOT));
418 PyModule_AddObject(m, "HKEY_CURRENT_USER", PyInt_FromLong(HKEY_CURRENT_USER));
419 PyModule_AddObject(m, "HKEY_LOCAL_MACHINE", PyInt_FromLong(HKEY_LOCAL_MACHINE));
420 PyModule_AddObject(m, "HKEY_USERS", PyInt_FromLong(HKEY_USERS));
421 PyModule_AddObject(m, "HKEY_PERFORMANCE_DATA", PyInt_FromLong(HKEY_PERFORMANCE_DATA));
422 PyModule_AddObject(m, "HKEY_CURRENT_CONFIG", PyInt_FromLong(HKEY_CURRENT_CONFIG));
423 PyModule_AddObject(m, "HKEY_DYN_DATA", PyInt_FromLong(HKEY_DYN_DATA));
424 PyModule_AddObject(m, "HKEY_PERFORMANCE_TEXT", PyInt_FromLong(HKEY_PERFORMANCE_TEXT));
425 PyModule_AddObject(m, "HKEY_PERFORMANCE_NLSTEXT", PyInt_FromLong(HKEY_PERFORMANCE_NLSTEXT));
427 Py_INCREF(&PyRegistry);
428 PyModule_AddObject(m, "Registry", (PyObject *)&PyRegistry);
430 Py_INCREF(&PyHiveKey);
431 PyModule_AddObject(m, "HiveKey", (PyObject *)&PyHiveKey);
433 Py_INCREF(&PyRegistryKey);
434 PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);