78b47b8286d8e03fe69ed0489ce49a78b1ebd891
[sfrench/samba-autobuild/.git] / source4 / lib / registry / pyregistry.c
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 "python/py3compat.h"
23 #include "includes.h"
24 #include "libcli/util/pyerrors.h"
25 #include "lib/registry/registry.h"
26 #include <pytalloc.h>
27 #include "lib/events/events.h"
28 #include "auth/credentials/pycredentials.h"
29 #include "param/pyparam.h"
30
31 extern PyTypeObject PyRegistryKey;
32 extern PyTypeObject PyRegistry;
33 extern PyTypeObject PyHiveKey;
34
35 /*#define PyRegistryKey_AsRegistryKey(obj) pytalloc_get_type(obj, struct registry_key)*/
36 #define PyRegistry_AsRegistryContext(obj) ((struct registry_context *)pytalloc_get_ptr(obj))
37 #define PyHiveKey_AsHiveKey(obj) ((struct hive_key*)pytalloc_get_ptr(obj))
38
39
40 static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
41 {
42         char *name;
43         WERROR result;
44         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
45         struct registry_key *key;
46
47         if (!PyArg_ParseTuple(args, "s", &name))
48                 return NULL;
49
50         result = reg_get_predefined_key_by_name(ctx, name, &key);
51         PyErr_WERROR_NOT_OK_RAISE(result);
52
53         return pytalloc_steal(&PyRegistryKey, key);
54 }
55
56 static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
57 {
58         char *path;
59         WERROR result;
60         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
61
62         if (!PyArg_ParseTuple(args, "s", &path))
63                 return NULL;
64
65         result = reg_key_del_abs(ctx, path);
66         PyErr_WERROR_NOT_OK_RAISE(result);
67
68         Py_RETURN_NONE;
69 }
70
71 static PyObject *py_get_predefined_key(PyObject *self, PyObject *args)
72 {
73         uint32_t hkey;
74         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
75         WERROR result;
76         struct registry_key *key;
77
78         if (!PyArg_ParseTuple(args, "I", &hkey))
79                 return NULL;
80
81         result = reg_get_predefined_key(ctx, hkey, &key);
82         PyErr_WERROR_NOT_OK_RAISE(result);
83
84         return pytalloc_steal(&PyRegistryKey, key);
85 }
86
87 static PyObject *py_diff_apply(PyObject *self, PyObject *args)
88 {
89         char *filename;
90         WERROR result;
91         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
92         if (!PyArg_ParseTuple(args, "s", &filename))
93                 return NULL;
94
95         result = reg_diff_apply(ctx, filename);
96         PyErr_WERROR_NOT_OK_RAISE(result);
97
98         Py_RETURN_NONE; 
99 }
100
101 static PyObject *py_mount_hive(PyObject *self, PyObject *args)
102 {
103         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
104         uint32_t hkey;
105         PyObject *py_hivekey, *py_elements = Py_None;
106         const char **elements;
107         WERROR result;
108
109         if (!PyArg_ParseTuple(args, "OI|O", &py_hivekey, &hkey, &py_elements))
110                 return NULL;
111
112         if (!PyList_Check(py_elements) && py_elements != Py_None) {
113                 PyErr_SetString(PyExc_TypeError, "Expected list of elements");
114                 return NULL;
115         }
116
117         if (py_elements == Py_None) {
118                 elements = NULL;
119         } else {
120                 int i;
121                 elements = talloc_array(NULL, const char *, PyList_Size(py_elements));
122                 for (i = 0; i < PyList_Size(py_elements); i++)
123                         elements[i] = PyStr_AsString(PyList_GetItem(py_elements, i));
124         }
125
126         SMB_ASSERT(ctx != NULL);
127
128         result = reg_mount_hive(ctx, PyHiveKey_AsHiveKey(py_hivekey), hkey, elements);
129         PyErr_WERROR_NOT_OK_RAISE(result);
130
131         Py_RETURN_NONE;
132 }
133
134 static PyObject *registry_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
135 {
136         WERROR result;
137         struct registry_context *ctx;
138         result = reg_open_local(NULL, &ctx);
139         PyErr_WERROR_NOT_OK_RAISE(result);
140         return pytalloc_steal(&PyRegistry, ctx);
141 }
142
143 static PyMethodDef registry_methods[] = {
144         { "get_predefined_key_by_name", py_get_predefined_key_by_name, METH_VARARGS, 
145                 "S.get_predefined_key_by_name(name) -> key\n"
146                 "Find a predefined key by name" },
147         { "key_del_abs", py_key_del_abs, METH_VARARGS, "S.key_del_abs(name) -> None\n"
148                 "Delete a key by absolute path." },
149         { "get_predefined_key", py_get_predefined_key, METH_VARARGS, "S.get_predefined_key(hkey_id) -> key\n"
150                 "Find a predefined key by id" },
151         { "diff_apply", py_diff_apply, METH_VARARGS, "S.diff_apply(filename) -> None\n"
152                 "Apply the diff from the specified file" },
153         { "mount_hive", py_mount_hive, METH_VARARGS, "S.mount_hive(key, key_id, elements=None) -> None\n"
154                 "Mount the specified key at the specified path." },
155         { NULL }
156 };
157
158 PyTypeObject PyRegistry = {
159         .tp_name = "Registry",
160         .tp_methods = registry_methods,
161         .tp_new = registry_new,
162         .tp_flags = Py_TPFLAGS_DEFAULT,
163 };
164
165 static 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_NOT_OK_RAISE(result);
177
178         Py_RETURN_NONE; 
179 }
180
181 static 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_NOT_OK_RAISE(result);
188
189         Py_RETURN_NONE;
190 }
191
192 static 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_NOT_OK_RAISE(result);
204
205         Py_RETURN_NONE; 
206 }
207
208 static PyObject *py_hive_key_set_value(PyObject *self, PyObject *args)
209 {
210         char *name;
211         uint32_t type;
212         DATA_BLOB value;
213         Py_ssize_t value_length = 0;
214         WERROR result;
215         struct hive_key *key = PyHiveKey_AsHiveKey(self);
216
217         if (!PyArg_ParseTuple(args, "sIz#", &name, &type, &value.data, &value_length)) {
218                 return NULL;
219         }
220         value.length = value_length;
221
222         if (value.data != NULL)
223                 result = hive_key_set_value(key, name, type, value);
224         else
225                 result = hive_key_del_value(NULL, key, name);
226
227         PyErr_WERROR_NOT_OK_RAISE(result);
228
229         Py_RETURN_NONE; 
230 }
231
232 static PyMethodDef hive_key_methods[] = {
233         { "del", py_hive_key_del, METH_VARARGS, "S.del(name) -> None\n"
234                 "Delete a subkey" },
235         { "flush", (PyCFunction)py_hive_key_flush, METH_NOARGS, "S.flush() -> None\n"
236                 "Flush this key to disk" },
237         { "del_value", py_hive_key_del_value, METH_VARARGS, "S.del_value(name) -> None\n"
238                  "Delete a value" },
239         { "set_value", py_hive_key_set_value, METH_VARARGS, "S.set_value(name, type, data) -> None\n"
240                  "Set a value" },
241         { NULL }
242 };
243
244 static PyObject *hive_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
245         Py_RETURN_NONE;
246 }
247
248 static PyObject *py_open_hive(PyTypeObject *type, PyObject *args, PyObject *kwargs)
249 {
250         const char *kwnames[] = { "location", "lp_ctx", "session_info", "credentials", NULL };
251         WERROR result;
252         struct loadparm_context *lp_ctx;
253         PyObject *py_lp_ctx = Py_None;
254         PyObject *py_session_info = Py_None;
255         PyObject *py_credentials = Py_None;
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;
261
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;
268
269         mem_ctx = talloc_new(NULL);
270         if (mem_ctx == NULL) {
271                 PyErr_NoMemory();
272                 return NULL;
273         }
274
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;
280         }
281
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;
287         }
288         session_info = NULL;
289
290         result = reg_open_hive(NULL, location, session_info, credentials,
291                                samba_tevent_context_init(NULL),
292                                lp_ctx, &hive_key);
293         talloc_free(mem_ctx);
294         PyErr_WERROR_NOT_OK_RAISE(result);
295
296         return pytalloc_steal(&PyHiveKey, hive_key);
297 }
298
299 PyTypeObject PyHiveKey = {
300         .tp_name = "HiveKey",
301         .tp_methods = hive_key_methods,
302         .tp_new = hive_new,
303         .tp_flags = Py_TPFLAGS_DEFAULT,
304 };
305
306 PyTypeObject PyRegistryKey = {
307         .tp_name = "RegistryKey",
308         .tp_flags = Py_TPFLAGS_DEFAULT,
309 };
310
311 static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
312 {
313         const char *kwnames[] = { "lp_ctx", "session_info", NULL };
314         struct registry_context *reg_ctx;
315         WERROR result;
316         struct loadparm_context *lp_ctx;
317         PyObject *py_lp_ctx = Py_None;
318         PyObject *py_session_info = Py_None;
319         PyObject *py_credentials = Py_None;
320         struct auth_session_info *session_info;
321         struct cli_credentials *credentials;
322         TALLOC_CTX *mem_ctx;
323
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;
329
330         mem_ctx = talloc_new(NULL);
331         if (mem_ctx == NULL) {
332                 PyErr_NoMemory();
333                 return NULL;
334         }
335
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;
341         }
342
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;
348         }
349
350         session_info = NULL; /* FIXME */
351
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;
358         }
359
360         return pytalloc_steal(&PyRegistry, reg_ctx);
361 }
362
363 static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
364 {
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;
374
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;
380
381         mem_ctx = talloc_new(NULL);
382         if (mem_ctx == NULL) {
383                 PyErr_NoMemory();
384                 return NULL;
385         }
386
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;
392         }
393
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;
399         }
400
401         session_info = NULL; /* FIXME */
402
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);
407
408         return pytalloc_steal(&PyHiveKey, key);
409 }
410
411 static PyObject *py_str_regtype(PyObject *self, PyObject *args)
412 {
413         int regtype;
414
415         if (!PyArg_ParseTuple(args, "i", &regtype))
416                 return NULL;
417         
418         return PyStr_FromString(str_regtype(regtype));
419 }
420
421 static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
422 {
423         uint32_t hkey;
424         const char *str;
425
426         if (!PyArg_ParseTuple(args, "I", &hkey))
427                 return NULL;
428
429         str = reg_get_predef_name(hkey);
430         if (str == NULL)
431                 Py_RETURN_NONE;
432         return PyStr_FromString(str);
433 }
434
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 }
442 };
443
444 static struct PyModuleDef moduledef = {
445     PyModuleDef_HEAD_INIT,
446     .m_name = "registry",
447     .m_doc = "Registry",
448     .m_size = -1,
449     .m_methods = py_registry_methods,
450 };
451
452 MODULE_INIT_FUNC(registry)
453 {
454         PyObject *m;
455
456         if (pytalloc_BaseObject_PyType_Ready(&PyHiveKey) < 0)
457                 return NULL;
458
459         if (pytalloc_BaseObject_PyType_Ready(&PyRegistry) < 0)
460                 return NULL;
461
462         if (pytalloc_BaseObject_PyType_Ready(&PyRegistryKey) < 0)
463                 return NULL;
464
465         m = PyModule_Create(&moduledef);
466         if (m == NULL)
467                 return NULL;
468
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));
478
479         Py_INCREF(&PyRegistry);
480         PyModule_AddObject(m, "Registry", (PyObject *)&PyRegistry);
481
482         Py_INCREF(&PyHiveKey);
483         PyModule_AddObject(m, "HiveKey", (PyObject *)&PyHiveKey);
484
485         Py_INCREF(&PyRegistryKey);
486         PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);
487
488         return m;
489 }