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