py: Properly increase the reference counter of Py_None.
[metze/samba/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 #include "param/pyparam.h"
28
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         Py_RETURN_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         Py_RETURN_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         Py_RETURN_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         .tp_flags = Py_TPFLAGS_DEFAULT,
160 };
161
162 static PyObject *py_hive_key_del(PyObject *self, PyObject *args)
163 {
164         char *name;
165         struct hive_key *key = py_talloc_get_ptr(self);
166         WERROR result;
167
168         if (!PyArg_ParseTuple(args, "s", &name))
169                 return NULL;
170
171         result = hive_key_del(key, name);
172
173         PyErr_WERROR_IS_ERR_RAISE(result);
174
175         Py_RETURN_NONE; 
176 }
177
178 static PyObject *py_hive_key_flush(PyObject *self)
179 {
180         WERROR result;
181         struct hive_key *key = py_talloc_get_ptr(self);
182
183         result = hive_key_flush(key);
184         PyErr_WERROR_IS_ERR_RAISE(result);
185
186         Py_RETURN_NONE;
187 }
188
189 static PyObject *py_hive_key_del_value(PyObject *self, PyObject *args)
190 {
191         char *name;
192         WERROR result;
193         struct hive_key *key = py_talloc_get_ptr(self);
194
195         if (!PyArg_ParseTuple(args, "s", &name))
196                 return NULL;
197
198         result = hive_key_del_value(key, name);
199
200         PyErr_WERROR_IS_ERR_RAISE(result);
201
202         Py_RETURN_NONE; 
203 }
204
205 static PyObject *py_hive_key_set_value(PyObject *self, PyObject *args)
206 {
207         char *name;
208         uint32_t type;
209         DATA_BLOB value;
210         WERROR result;
211         struct hive_key *key = py_talloc_get_ptr(self);
212
213         if (!PyArg_ParseTuple(args, "siz#", &name, &type, &value.data, &value.length))
214                 return NULL;
215
216         if (value.data != NULL)
217                 result = hive_key_set_value(key, name, type, value);
218         else
219                 result = hive_key_del_value(key, name);
220
221         PyErr_WERROR_IS_ERR_RAISE(result);
222
223         Py_RETURN_NONE; 
224 }
225
226 static PyMethodDef hive_key_methods[] = {
227         { "del", py_hive_key_del, METH_VARARGS, "S.del(name) -> None\n"
228                 "Delete a subkey" },
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"
232                  "Delete a value" },
233         { "set_value", py_hive_key_set_value, METH_VARARGS, "S.set_value(name, type, data) -> None\n"
234                  "Set a value" },
235         { NULL }
236 };
237
238 static PyObject *hive_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
239 {
240         /* reg_open_hive */
241         Py_RETURN_NONE;
242 }
243
244 PyTypeObject PyHiveKey = {
245         .tp_name = "HiveKey",
246         .tp_methods = hive_key_methods,
247         .tp_new = hive_open,
248         .tp_basicsize = sizeof(py_talloc_Object),
249         .tp_dealloc = py_talloc_dealloc,
250         .tp_flags = Py_TPFLAGS_DEFAULT,
251 };
252
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,
258 };
259
260 static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs)
261 {
262         const char *kwnames[] = { "lp_ctx", "session_info", NULL };
263         struct registry_context *reg_ctx;
264         WERROR result;
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))
271                 return NULL;
272
273     lp_ctx = lp_from_py_object(py_lp_ctx);
274     if (lp_ctx == NULL) {
275                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
276                 return NULL;
277     }
278
279         credentials = cli_credentials_from_py_object(py_credentials);
280         if (credentials == NULL) {
281                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
282                 return NULL;
283         }
284
285         session_info = NULL; /* FIXME */
286
287         result = reg_open_samba(NULL, &reg_ctx, NULL, 
288                                 lp_ctx, session_info, credentials);
289         if (!W_ERROR_IS_OK(result)) {
290                 PyErr_SetWERROR(result);
291                 return NULL;
292         }
293         
294         return py_talloc_import(&PyRegistry, reg_ctx);
295 }
296
297 static PyObject *py_open_directory(PyObject *self, PyObject *args)
298 {
299         char *location;
300         WERROR result;
301         struct hive_key *key;
302
303         if (!PyArg_ParseTuple(args, "s", &location))
304                 return NULL;
305
306         result = reg_open_directory(NULL, location, &key);
307         PyErr_WERROR_IS_ERR_RAISE(result);
308
309         return py_talloc_import(&PyHiveKey, key);
310 }
311
312 static PyObject *py_create_directory(PyObject *self, PyObject *args)
313 {
314         char *location;
315         WERROR result;
316         struct hive_key *key;
317
318         if (!PyArg_ParseTuple(args, "s", &location))
319                 return NULL;
320
321         result = reg_create_directory(NULL, location, &key);
322         PyErr_WERROR_IS_ERR_RAISE(result);
323
324         return py_talloc_import(&PyHiveKey, key);
325 }
326
327 static PyObject *py_open_ldb_file(PyObject *self, PyObject *args, PyObject *kwargs)
328 {
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;
331         WERROR result;
332         char *location;
333     struct loadparm_context *lp_ctx;
334     struct cli_credentials *credentials;
335         struct hive_key *key;
336         struct auth_session_info *session_info;
337
338         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO", 
339                                                                          discard_const_p(char *, kwnames), 
340                                                                          &location, 
341                                                                          &py_session_info, &py_credentials,
342                                                                          &py_lp_ctx))
343                 return NULL;
344
345     lp_ctx = lp_from_py_object(py_lp_ctx);
346     if (lp_ctx == NULL) {
347                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
348                 return NULL;
349     }
350
351         credentials = cli_credentials_from_py_object(py_credentials);
352         if (credentials == NULL) {
353                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
354                 return NULL;
355         }
356
357         session_info = NULL; /* FIXME */
358
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);
362
363         return py_talloc_import(&PyHiveKey, key);
364 }
365
366 static PyObject *py_str_regtype(PyObject *self, PyObject *args)
367 {
368         int regtype;
369
370         if (!PyArg_ParseTuple(args, "i", &regtype))
371                 return NULL;
372         
373         return PyString_FromString(str_regtype(regtype));
374 }
375
376 static PyObject *py_get_predef_name(PyObject *self, PyObject *args)
377 {
378         uint32_t hkey;
379         const char *str;
380
381         if (!PyArg_ParseTuple(args, "I", &hkey))
382                 return NULL;
383
384         str = reg_get_predef_name(hkey);
385         if (str == NULL)
386                 Py_RETURN_NONE;
387         return PyString_FromString(str);
388 }
389
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" },
397         { NULL }
398 };
399
400 void initregistry(void)
401 {
402         PyObject *m;
403
404         if (PyType_Ready(&PyHiveKey) < 0)
405                 return;
406
407         if (PyType_Ready(&PyRegistry) < 0)
408                 return;
409
410         if (PyType_Ready(&PyRegistryKey) < 0)
411                 return;
412
413         m = Py_InitModule3("registry", py_registry_methods, "Registry");
414         if (m == NULL)
415                 return;
416
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));
426
427         Py_INCREF(&PyRegistry);
428         PyModule_AddObject(m, "Registry", (PyObject *)&PyRegistry);
429
430         Py_INCREF(&PyHiveKey);
431         PyModule_AddObject(m, "HiveKey", (PyObject *)&PyHiveKey);
432
433         Py_INCREF(&PyRegistryKey);
434         PyModule_AddObject(m, "RegistryKey", (PyObject *)&PyRegistryKey);
435 }