Merge branch 'master' of ssh://jra@git.samba.org/data/git/samba
[ira/wip.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 "includes.h"
21 #include <Python.h>
22 #include "libcli/util/pyerrors.h"
23 #include "lib/registry/registry.h"
24 #include "scripting/python/modules.h" /* for py_iconv_convenience() */
25 #include <pytalloc.h>
26 #include <tevent.h>
27
28 extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);
29 extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
30
31 PyAPI_DATA(PyTypeObject) PyRegistryKey;
32 PyAPI_DATA(PyTypeObject) PyRegistry;
33 PyAPI_DATA(PyTypeObject) PyHiveKey;
34
35 static PyObject *py_get_predefined_key_by_name(PyObject *self, PyObject *args)
36 {
37         char *name;
38         WERROR result;
39         struct registry_context *ctx = py_talloc_get_ptr(self);
40         struct registry_key *key;
41
42         if (!PyArg_ParseTuple(args, "s", &name))
43                 return NULL;
44
45         result = reg_get_predefined_key_by_name(ctx, name, &key);
46         PyErr_WERROR_IS_ERR_RAISE(result);
47
48         return py_talloc_import(&PyRegistryKey, key);
49 }
50
51 static PyObject *py_key_del_abs(PyObject *self, PyObject *args)
52 {
53         char *path;
54         WERROR result;
55         struct registry_context *ctx = py_talloc_get_ptr(self);
56
57         if (!PyArg_ParseTuple(args, "s", &path))
58                 return NULL;
59
60         result = reg_key_del_abs(ctx, path);
61         PyErr_WERROR_IS_ERR_RAISE(result);
62
63         return Py_None;
64 }
65
66 static PyObject *py_get_predefined_key(PyObject *self, PyObject *args)
67 {
68         uint32_t hkey;
69         struct registry_context *ctx = py_talloc_get_ptr(self);
70         WERROR result;
71         struct registry_key *key;
72
73         if (!PyArg_ParseTuple(args, "I", &hkey))
74                 return NULL;
75
76         result = reg_get_predefined_key(ctx, hkey, &key);
77         PyErr_WERROR_IS_ERR_RAISE(result);
78
79         return py_talloc_import(&PyRegistryKey, key);
80 }
81
82 static PyObject *py_diff_apply(PyObject *self, PyObject *args)
83 {
84         char *filename;
85         WERROR result;
86         struct registry_context *ctx = py_talloc_get_ptr(self);
87         if (!PyArg_ParseTuple(args, "s", &filename))
88                 return NULL;
89
90         result = reg_diff_apply(ctx, py_iconv_convenience(NULL), filename);
91         PyErr_WERROR_IS_ERR_RAISE(result);
92
93         return Py_None; 
94 }
95
96 static PyObject *py_mount_hive(PyObject *self, PyObject *args)
97 {
98         struct registry_context *ctx = py_talloc_get_ptr(self);
99         uint32_t hkey;
100         PyObject *py_hivekey, *py_elements = Py_None;
101         const char **elements;
102         WERROR result;
103
104         if (!PyArg_ParseTuple(args, "OI|O", &py_hivekey, &hkey, &py_elements))
105                 return NULL;
106
107         if (!PyList_Check(py_elements) && py_elements != Py_None) {
108                 PyErr_SetString(PyExc_TypeError, "Expected list of elements");
109                 return NULL;
110         }
111
112         if (py_elements == Py_None) {
113                 elements = NULL;
114         } else {
115                 int i;
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));
119         }
120
121         SMB_ASSERT(ctx != NULL);
122
123         result = reg_mount_hive(ctx, py_talloc_get_ptr(py_hivekey), hkey, elements);
124         PyErr_WERROR_IS_ERR_RAISE(result);
125
126         return Py_None;
127 }
128
129 static PyObject *registry_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
130 {
131         WERROR result;
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);
136 }
137
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." },
150         { NULL }
151 };
152
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 };
160
161 static PyObject *py_hive_key_del(PyObject *self, PyObject *args)
162 {
163         char *name;
164         struct hive_key *key = py_talloc_get_ptr(self);
165         WERROR result;
166
167         if (!PyArg_ParseTuple(args, "s", &name))
168                 return NULL;
169
170         result = hive_key_del(key, name);
171
172         PyErr_WERROR_IS_ERR_RAISE(result);
173
174         return Py_None; 
175 }
176
177 static PyObject *py_hive_key_flush(PyObject *self)
178 {
179         WERROR result;
180         struct hive_key *key = py_talloc_get_ptr(self);
181
182         result = hive_key_flush(key);
183         PyErr_WERROR_IS_ERR_RAISE(result);
184
185         return Py_None;
186 }
187
188 static PyObject *py_hive_key_del_value(PyObject *self, PyObject *args)
189 {
190         char *name;
191         WERROR result;
192         struct hive_key *key = py_talloc_get_ptr(self);
193
194         if (!PyArg_ParseTuple(args, "s", &name))
195                 return NULL;
196
197         result = hive_key_del_value(key, name);
198
199         PyErr_WERROR_IS_ERR_RAISE(result);
200
201         return Py_None; 
202 }
203
204 static PyObject *py_hive_key_set_value(PyObject *self, PyObject *args)
205 {
206         char *name;
207         uint32_t type;
208         DATA_BLOB value;
209         WERROR result;
210         struct hive_key *key = py_talloc_get_ptr(self);
211
212         if (!PyArg_ParseTuple(args, "siz#", &name, &type, &value.data, &value.length))
213                 return NULL;
214
215         if (value.data != NULL)
216                 result = hive_key_set_value(key, name, type, value);
217         else
218                 result = hive_key_del_value(key, name);
219
220         PyErr_WERROR_IS_ERR_RAISE(result);
221
222         return Py_None; 
223 }
224
225 static PyMethodDef hive_key_methods[] = {
226         { "del", py_hive_key_del, METH_VARARGS, "S.del(name) -> None\n"
227                 "Delete a subkey" },
228         { "flush", (PyCFunction)py_hive_key_flush, METH_NOARGS, "S.flush() -> None\n"
229                 "Flush this key to disk" },
230         { "del_value", py_hive_key_del_value, METH_VARARGS, "S.del_value(name) -> None\n"
231                  "Delete a value" },
232         { "set_value", py_hive_key_set_value, METH_VARARGS, "S.set_value(name, type, data) -> None\n"
233                  "Set a value" },
234         { NULL }
235 };
236
237 static PyObject *hive_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
238 {
239         /* reg_open_hive */
240         return Py_None;
241 }
242
243 PyTypeObject PyHiveKey = {
244         .tp_name = "HiveKey",
245         .tp_methods = hive_key_methods,
246         .tp_new = hive_open,
247         .tp_basicsize = sizeof(py_talloc_Object),
248         .tp_dealloc = py_talloc_dealloc,
249 };
250
251 PyTypeObject PyRegistryKey = {
252         .tp_name = "RegistryKey",
253         .tp_basicsize = sizeof(py_talloc_Object),
254         .tp_dealloc = py_talloc_dealloc,
255 };
256
257 static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
258 {
259         const char *kwnames[] = { "lp_ctx", "session_info", NULL };
260         struct registry_context *reg_ctx;
261         WERROR result;
262     struct loadparm_context *lp_ctx;
263         PyObject *py_lp_ctx, *py_session_info, *py_credentials;
264         struct auth_session_info *session_info;
265     struct cli_credentials *credentials;
266         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO", discard_const_p(char *, kwnames),
267                                          &py_lp_ctx, &py_session_info, &py_credentials))
268                 return NULL;
269
270     lp_ctx = lp_from_py_object(py_lp_ctx);
271     if (lp_ctx == NULL) {
272                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
273                 return NULL;
274     }
275
276         credentials = cli_credentials_from_py_object(py_credentials);
277         if (credentials == NULL) {
278                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
279                 return NULL;
280         }
281
282         session_info = NULL; /* FIXME */
283
284         result = reg_open_samba(NULL, &reg_ctx, NULL, 
285                                 lp_ctx, session_info, credentials);
286         if (!W_ERROR_IS_OK(result)) {
287                 PyErr_SetWERROR(result);
288                 return NULL;
289         }
290         
291         return py_talloc_import(&PyRegistry, reg_ctx);
292 }
293
294 static PyObject *py_open_directory(PyObject *self, PyObject *args)
295 {
296         char *location;
297         WERROR result;
298         struct hive_key *key;
299
300         if (!PyArg_ParseTuple(args, "s", &location))
301                 return NULL;
302
303         result = reg_open_directory(NULL, location, &key);
304         PyErr_WERROR_IS_ERR_RAISE(result);
305
306         return py_talloc_import(&PyHiveKey, key);
307 }
308
309 static PyObject *py_create_directory(PyObject *self, PyObject *args)
310 {
311         char *location;
312         WERROR result;
313         struct hive_key *key;
314
315         if (!PyArg_ParseTuple(args, "s", &location))
316                 return NULL;
317
318         result = reg_create_directory(NULL, location, &key);
319         PyErr_WERROR_IS_ERR_RAISE(result);
320
321         return py_talloc_import(&PyHiveKey, key);
322 }
323
324 static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
325 {
326         const char *kwnames[] = { "location", "session_info", "credentials", "lp_ctx", NULL };
327         PyObject *py_session_info = Py_None, *py_credentials = Py_None, *py_lp_ctx = Py_None;
328         WERROR result;
329         char *location;
330     struct loadparm_context *lp_ctx;
331     struct cli_credentials *credentials;
332         struct hive_key *key;
333         struct auth_session_info *session_info;
334
335         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO", 
336                                                                          discard_const_p(char *, kwnames), 
337                                                                          &location, 
338                                                                          &py_session_info, &py_credentials,
339                                                                          &py_lp_ctx))
340                 return NULL;
341
342     lp_ctx = lp_from_py_object(py_lp_ctx);
343     if (lp_ctx == NULL) {
344                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
345                 return NULL;
346     }
347
348         credentials = cli_credentials_from_py_object(py_credentials);
349         if (credentials == NULL) {
350                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
351                 return NULL;
352         }
353
354         session_info = NULL; /* FIXME */
355
356         result = reg_open_ldb_file(NULL, location, session_info, credentials,
357                                                            event_context_init(NULL), lp_ctx, &key);
358         PyErr_WERROR_IS_ERR_RAISE(result);
359
360         return py_talloc_import(&PyHiveKey, key);
361 }
362
363 static PyObject *py_str_regtype(PyObject *self, PyObject *args)
364 {
365         int regtype;
366
367         if (!PyArg_ParseTuple(args, "i", &regtype))
368                 return NULL;
369         
370         return PyString_FromString(str_regtype(regtype));
371 }
372
373 static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
374 {
375         uint32_t hkey;
376         const char *str;
377
378         if (!PyArg_ParseTuple(args, "I", &hkey))
379                 return NULL;
380
381         str = reg_get_predef_name(hkey);
382         if (str == NULL)
383                 return Py_None;
384         return PyString_FromString(str);
385 }
386
387 static PyMethodDef py_registry_methods[] = {
388         { "open_samba", (PyCFunction)py_open_samba, METH_VARARGS|METH_KEYWORDS, "open_samba() -> reg" },
389         { "open_directory", py_open_directory, METH_VARARGS, "open_dir(location) -> key" },
390         { "create_directory", py_create_directory, METH_VARARGS, "create_dir(location) -> key" },
391         { "open_ldb", (PyCFunction)py_open_ldb_file, METH_VARARGS|METH_KEYWORDS, "open_ldb(location, session_info=None, credentials=None, loadparm_context=None) -> key" },
392         { "str_regtype", py_str_regtype, METH_VARARGS, "str_regtype(int) -> str" },
393         { "get_predef_name", py_get_predef_name, METH_VARARGS, "get_predef_name(hkey) -> str" },
394         { NULL }
395 };
396
397 void initregistry(void)
398 {
399         PyObject *m;
400
401         if (PyType_Ready(&PyHiveKey) < 0)
402                 return;
403
404         if (PyType_Ready(&PyRegistry) < 0)
405                 return;
406
407         if (PyType_Ready(&PyRegistryKey) < 0)
408                 return;
409
410         m = Py_InitModule3("registry", py_registry_methods, "Registry");
411         if (m == NULL)
412                 return;
413
414         PyModule_AddObject(m, "HKEY_CLASSES_ROOT", PyInt_FromLong(HKEY_CLASSES_ROOT));
415         PyModule_AddObject(m, "HKEY_CURRENT_USER", PyInt_FromLong(HKEY_CURRENT_USER));
416         PyModule_AddObject(m, "HKEY_LOCAL_MACHINE", PyInt_FromLong(HKEY_LOCAL_MACHINE));
417         PyModule_AddObject(m, "HKEY_USERS", PyInt_FromLong(HKEY_USERS));
418         PyModule_AddObject(m, "HKEY_PERFORMANCE_DATA", PyInt_FromLong(HKEY_PERFORMANCE_DATA));
419         PyModule_AddObject(m, "HKEY_CURRENT_CONFIG", PyInt_FromLong(HKEY_CURRENT_CONFIG));
420         PyModule_AddObject(m, "HKEY_DYN_DATA", PyInt_FromLong(HKEY_DYN_DATA));
421         PyModule_AddObject(m, "HKEY_PERFORMANCE_TEXT", PyInt_FromLong(HKEY_PERFORMANCE_TEXT));
422         PyModule_AddObject(m, "HKEY_PERFORMANCE_NLSTEXT", PyInt_FromLong(HKEY_PERFORMANCE_NLSTEXT));
423
424         Py_INCREF(&PyRegistry);
425         PyModule_AddObject(m, "Registry", (PyObject *)&PyRegistry);
426
427         Py_INCREF(&PyHiveKey);
428         PyModule_AddObject(m, "HiveKey", (PyObject *)&PyHiveKey);
429
430         Py_INCREF(&PyRegistryKey);
431         PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);
432 }