a3ba6c535116761daf0dd3145ced286218a3974d
[samba.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    
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.
10    
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.
15    
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/>.
18 */
19
20 #include <Python.h>
21 #include "includes.h"
22 #include <tevent.h>
23 #include "libcli/util/pyerrors.h"
24 #include "lib/registry/registry.h"
25 #include "lib/talloc/pytalloc.h"
26 #include "auth/credentials/pycredentials.h"
27 #include "param/pyparam.h"
28
29 PyAPI_DATA(PyTypeObject) PyRegistryKey;
30 PyAPI_DATA(PyTypeObject) PyRegistry;
31 PyAPI_DATA(PyTypeObject) PyHiveKey;
32
33 /*#define PyRegistryKey_AsRegistryKey(obj) py_talloc_get_type(obj, struct registry_key)*/
34 #define PyRegistry_AsRegistryContext(obj) ((struct registry_context *)py_talloc_get_ptr(obj))
35 #define PyHiveKey_AsHiveKey(obj) ((struct hive_key*)py_talloc_get_ptr(obj))
36
37
38 static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
39 {
40         char *name;
41         WERROR result;
42         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
43         struct registry_key *key;
44
45         if (!PyArg_ParseTuple(args, "s", &name))
46                 return NULL;
47
48         result = reg_get_predefined_key_by_name(ctx, name, &key);
49         PyErr_WERROR_IS_ERR_RAISE(result);
50
51         return py_talloc_steal(&PyRegistryKey, key);
52 }
53
54 static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
55 {
56         char *path;
57         WERROR result;
58         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
59
60         if (!PyArg_ParseTuple(args, "s", &path))
61                 return NULL;
62
63         result = reg_key_del_abs(ctx, path);
64         PyErr_WERROR_IS_ERR_RAISE(result);
65
66         Py_RETURN_NONE;
67 }
68
69 static PyObject *py_get_predefined_key(PyObject *self, PyObject *args)
70 {
71         uint32_t hkey;
72         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
73         WERROR result;
74         struct registry_key *key;
75
76         if (!PyArg_ParseTuple(args, "I", &hkey))
77                 return NULL;
78
79         result = reg_get_predefined_key(ctx, hkey, &key);
80         PyErr_WERROR_IS_ERR_RAISE(result);
81
82         return py_talloc_steal(&PyRegistryKey, key);
83 }
84
85 static PyObject *py_diff_apply(PyObject *self, PyObject *args)
86 {
87         char *filename;
88         WERROR result;
89         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
90         if (!PyArg_ParseTuple(args, "s", &filename))
91                 return NULL;
92
93         result = reg_diff_apply(ctx, filename);
94         PyErr_WERROR_IS_ERR_RAISE(result);
95
96         Py_RETURN_NONE; 
97 }
98
99 static PyObject *py_mount_hive(PyObject *self, PyObject *args)
100 {
101         struct registry_context *ctx = PyRegistry_AsRegistryContext(self);
102         uint32_t hkey;
103         PyObject *py_hivekey, *py_elements = Py_None;
104         const char **elements;
105         WERROR result;
106
107         if (!PyArg_ParseTuple(args, "OI|O", &py_hivekey, &hkey, &py_elements))
108                 return NULL;
109
110         if (!PyList_Check(py_elements) && py_elements != Py_None) {
111                 PyErr_SetString(PyExc_TypeError, "Expected list of elements");
112                 return NULL;
113         }
114
115         if (py_elements == Py_None) {
116                 elements = NULL;
117         } else {
118                 int i;
119                 elements = talloc_array(NULL, const char *, PyList_Size(py_elements));
120                 for (i = 0; i < PyList_Size(py_elements); i++)
121                         elements[i] = PyString_AsString(PyList_GetItem(py_elements, i));
122         }
123
124         SMB_ASSERT(ctx != NULL);
125
126         result = reg_mount_hive(ctx, PyHiveKey_AsHiveKey(py_hivekey), hkey, elements);
127         PyErr_WERROR_IS_ERR_RAISE(result);
128
129         Py_RETURN_NONE;
130 }
131
132 static PyObject *registry_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
133 {
134         WERROR result;
135         struct registry_context *ctx;
136         result = reg_open_local(NULL, &ctx);
137         PyErr_WERROR_IS_ERR_RAISE(result);
138         return py_talloc_steal(&PyRegistry, ctx);
139 }
140
141 static PyMethodDef registry_methods[] = {
142         { "get_predefined_key_by_name", py_get_predefined_key_by_name, METH_VARARGS, 
143                 "S.get_predefined_key_by_name(name) -> key\n"
144                 "Find a predefined key by name" },
145         { "key_del_abs", py_key_del_abs, METH_VARARGS, "S.key_del_abs(name) -> None\n"
146                 "Delete a key by absolute path." },
147         { "get_predefined_key", py_get_predefined_key, METH_VARARGS, "S.get_predefined_key(hkey_id) -> key\n"
148                 "Find a predefined key by id" },
149         { "diff_apply", py_diff_apply, METH_VARARGS, "S.diff_apply(filename) -> None\n"
150                 "Apply the diff from the specified file" },
151         { "mount_hive", py_mount_hive, METH_VARARGS, "S.mount_hive(key, key_id, elements=None) -> None\n"
152                 "Mount the specified key at the specified path." },
153         { NULL }
154 };
155
156 PyTypeObject PyRegistry = {
157         .tp_name = "Registry",
158         .tp_methods = registry_methods,
159         .tp_new = registry_new,
160         .tp_basicsize = sizeof(py_talloc_Object),
161         .tp_dealloc = py_talloc_dealloc,
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_IS_ERR_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_IS_ERR_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_IS_ERR_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         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
229 static 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
241 static PyObject *hive_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
242 {
243         /* reg_open_hive */
244         Py_RETURN_NONE;
245 }
246
247 PyTypeObject PyHiveKey = {
248         .tp_name = "HiveKey",
249         .tp_methods = hive_key_methods,
250         .tp_new = hive_open,
251         .tp_basicsize = sizeof(py_talloc_Object),
252         .tp_dealloc = py_talloc_dealloc,
253         .tp_flags = Py_TPFLAGS_DEFAULT,
254 };
255
256 PyTypeObject PyRegistryKey = {
257         .tp_name = "RegistryKey",
258         .tp_basicsize = sizeof(py_talloc_Object),
259         .tp_dealloc = py_talloc_dealloc,
260         .tp_flags = Py_TPFLAGS_DEFAULT,
261 };
262
263 static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
264 {
265         const char *kwnames[] = { "lp_ctx", "session_info", NULL };
266         struct registry_context *reg_ctx;
267         WERROR result;
268         struct loadparm_context *lp_ctx;
269         PyObject *py_lp_ctx, *py_session_info, *py_credentials;
270         struct auth_session_info *session_info;
271         struct cli_credentials *credentials;
272         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO",
273                                          discard_const_p(char *, kwnames),
274                                          &py_lp_ctx, &py_session_info,
275                                          &py_credentials))
276                 return NULL;
277
278         lp_ctx = lpcfg_from_py_object(NULL, py_lp_ctx); /* FIXME: leaky */
279         if (lp_ctx == NULL) {
280                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
281                 return NULL;
282         }
283
284         credentials = cli_credentials_from_py_object(py_credentials);
285         if (credentials == NULL) {
286                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
287                 return NULL;
288         }
289
290         session_info = NULL; /* FIXME */
291
292         result = reg_open_samba(NULL, &reg_ctx, NULL, 
293                                 lp_ctx, session_info, credentials);
294         if (!W_ERROR_IS_OK(result)) {
295                 PyErr_SetWERROR(result);
296                 return NULL;
297         }
298         
299         return py_talloc_steal(&PyRegistry, reg_ctx);
300 }
301
302 static PyObject *py_open_directory(PyObject *self, PyObject *args)
303 {
304         char *location;
305         WERROR result;
306         struct hive_key *key;
307
308         if (!PyArg_ParseTuple(args, "s", &location))
309                 return NULL;
310
311         result = reg_open_directory(NULL, location, &key);
312         PyErr_WERROR_IS_ERR_RAISE(result);
313
314         return py_talloc_steal(&PyHiveKey, key);
315 }
316
317 static PyObject *py_create_directory(PyObject *self, PyObject *args)
318 {
319         char *location;
320         WERROR result;
321         struct hive_key *key;
322
323         if (!PyArg_ParseTuple(args, "s", &location))
324                 return NULL;
325
326         result = reg_create_directory(NULL, location, &key);
327         PyErr_WERROR_IS_ERR_RAISE(result);
328
329         return py_talloc_steal(&PyHiveKey, key);
330 }
331
332 static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
333 {
334         const char *kwnames[] = { "location", "session_info", "credentials", "lp_ctx", NULL };
335         PyObject *py_session_info = Py_None, *py_credentials = Py_None, *py_lp_ctx = Py_None;
336         WERROR result;
337         char *location;
338         struct loadparm_context *lp_ctx;
339         struct cli_credentials *credentials;
340         struct hive_key *key;
341         struct auth_session_info *session_info;
342
343         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO", 
344                                          discard_const_p(char *, kwnames),
345                                          &location, &py_session_info,
346                                          &py_credentials, &py_lp_ctx))
347                 return NULL;
348
349         lp_ctx = lpcfg_from_py_object(NULL, py_lp_ctx); /* FIXME: leaky */
350         if (lp_ctx == NULL) {
351                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
352                 return NULL;
353         }
354
355         credentials = cli_credentials_from_py_object(py_credentials);
356         if (credentials == NULL) {
357                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
358                 return NULL;
359         }
360
361         session_info = NULL; /* FIXME */
362
363         result = reg_open_ldb_file(NULL, location, session_info, credentials,
364                                    tevent_context_init(NULL), lp_ctx, &key);
365         PyErr_WERROR_IS_ERR_RAISE(result);
366
367         return py_talloc_steal(&PyHiveKey, key);
368 }
369
370 static PyObject *py_str_regtype(PyObject *self, PyObject *args)
371 {
372         int regtype;
373
374         if (!PyArg_ParseTuple(args, "i", &regtype))
375                 return NULL;
376         
377         return PyString_FromString(str_regtype(regtype));
378 }
379
380 static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
381 {
382         uint32_t hkey;
383         const char *str;
384
385         if (!PyArg_ParseTuple(args, "I", &hkey))
386                 return NULL;
387
388         str = reg_get_predef_name(hkey);
389         if (str == NULL)
390                 Py_RETURN_NONE;
391         return PyString_FromString(str);
392 }
393
394 static PyMethodDef py_registry_methods[] = {
395         { "open_samba", (PyCFunction)py_open_samba, METH_VARARGS|METH_KEYWORDS, "open_samba() -> reg" },
396         { "open_directory", py_open_directory, METH_VARARGS, "open_dir(location) -> key" },
397         { "create_directory", py_create_directory, METH_VARARGS, "create_dir(location) -> key" },
398         { "open_ldb", (PyCFunction)py_open_ldb_file, METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
399         { "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
400         { "get_predef_name", py_get_predef_name, METH_VARARGS, "get_predef_name(hkey) -> str" },
401         { NULL }
402 };
403
404 void initregistry(void)
405 {
406         PyObject *m;
407
408         if (PyType_Ready(&PyHiveKey) < 0)
409                 return;
410
411         if (PyType_Ready(&PyRegistry) < 0)
412                 return;
413
414         if (PyType_Ready(&PyRegistryKey) < 0)
415                 return;
416
417         m = Py_InitModule3("registry", py_registry_methods, "Registry");
418         if (m == NULL)
419                 return;
420
421         PyModule_AddObject(m, "HKEY_CLASSES_ROOT", PyInt_FromLong(HKEY_CLASSES_ROOT));
422         PyModule_AddObject(m, "HKEY_CURRENT_USER", PyInt_FromLong(HKEY_CURRENT_USER));
423         PyModule_AddObject(m, "HKEY_LOCAL_MACHINE", PyInt_FromLong(HKEY_LOCAL_MACHINE));
424         PyModule_AddObject(m, "HKEY_USERS", PyInt_FromLong(HKEY_USERS));
425         PyModule_AddObject(m, "HKEY_PERFORMANCE_DATA", PyInt_FromLong(HKEY_PERFORMANCE_DATA));
426         PyModule_AddObject(m, "HKEY_CURRENT_CONFIG", PyInt_FromLong(HKEY_CURRENT_CONFIG));
427         PyModule_AddObject(m, "HKEY_DYN_DATA", PyInt_FromLong(HKEY_DYN_DATA));
428         PyModule_AddObject(m, "HKEY_PERFORMANCE_TEXT", PyInt_FromLong(HKEY_PERFORMANCE_TEXT));
429         PyModule_AddObject(m, "HKEY_PERFORMANCE_NLSTEXT", PyInt_FromLong(HKEY_PERFORMANCE_NLSTEXT));
430
431         Py_INCREF(&PyRegistry);
432         PyModule_AddObject(m, "Registry", (PyObject *)&PyRegistry);
433
434         Py_INCREF(&PyHiveKey);
435         PyModule_AddObject(m, "HiveKey", (PyObject *)&PyHiveKey);
436
437         Py_INCREF(&PyRegistryKey);
438         PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);
439 }