s3/s4 build: Fix Py_RETURN_NONE to work with python versions < 2.4
[gd/samba-autobuild/.git] / source4 / param / pyparam.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-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 <stdint.h>
21 #include <stdbool.h>
22
23 #include "includes.h"
24 #include "param/param.h"
25 #include "param/loadparm.h"
26 #include "../lib/util/python_util.h"
27 #include "pytalloc.h"
28
29 /* There's no Py_ssize_t in 2.4, apparently */
30 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
31 typedef int Py_ssize_t;
32 typedef inquiry lenfunc;
33 #endif
34
35 #define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_ptr(obj)
36
37 PyAPI_DATA(PyTypeObject) PyLoadparmContext;
38 PyAPI_DATA(PyTypeObject) PyLoadparmService;
39
40 PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
41 {
42         return py_talloc_import(&PyLoadparmService, service);
43 }
44
45 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
46 {
47     struct parm_struct *parm = NULL;
48     void *parm_ptr = NULL;
49     int i;
50
51     if (service_name != NULL) {
52         struct loadparm_service *service;
53         /* its a share parameter */
54         service = lp_service(lp_ctx, service_name);
55         if (service == NULL) {
56             return NULL;
57         }
58         if (strchr(param_name, ':')) {
59             /* its a parametric option on a share */
60             const char *type = talloc_strndup(lp_ctx, 
61                               param_name, 
62                               strcspn(param_name, ":"));
63             const char *option = strchr(param_name, ':') + 1;
64             const char *value;
65             if (type == NULL || option == NULL) {
66                 return NULL;
67             }
68             value = lp_get_parametric(lp_ctx, service, type, option);
69             if (value == NULL) {
70                 return NULL;
71             }
72             return PyString_FromString(value);
73         }
74
75         parm = lp_parm_struct(param_name);
76         if (parm == NULL || parm->class == P_GLOBAL) {
77             return NULL;
78         }
79         parm_ptr = lp_parm_ptr(lp_ctx, service, parm);
80     } else if (strchr(param_name, ':')) {
81         /* its a global parametric option */
82         const char *type = talloc_strndup(lp_ctx, 
83                           param_name, strcspn(param_name, ":"));
84         const char *option = strchr(param_name, ':') + 1;
85         const char *value;
86         if (type == NULL || option == NULL) {
87             return NULL;
88         }
89         value = lp_get_parametric(lp_ctx, NULL, type, option);
90         if (value == NULL)
91             return NULL;
92         return PyString_FromString(value);
93     } else {
94         /* its a global parameter */
95         parm = lp_parm_struct(param_name);
96         if (parm == NULL) {
97             return NULL;
98         }
99         parm_ptr = lp_parm_ptr(lp_ctx, NULL, parm);
100     }
101
102     if (parm == NULL || parm_ptr == NULL) {
103         return NULL;
104     }
105
106     /* construct and return the right type of python object */
107     switch (parm->type) {
108     case P_STRING:
109     case P_USTRING:
110         return PyString_FromString(*(char **)parm_ptr);
111     case P_BOOL:
112         return PyBool_FromLong(*(bool *)parm_ptr);
113     case P_INTEGER:
114     case P_OCTAL:
115     case P_BYTES:
116         return PyLong_FromLong(*(int *)parm_ptr);
117     case P_ENUM:
118         for (i=0; parm->enum_list[i].name; i++) {
119             if (*(int *)parm_ptr == parm->enum_list[i].value) {
120                 return PyString_FromString(parm->enum_list[i].name);
121             }
122         }
123         return NULL;
124     case P_LIST: 
125         {
126             int j;
127             const char **strlist = *(const char ***)parm_ptr;
128             PyObject *pylist = PyList_New(str_list_length(strlist));
129             for (j = 0; strlist[j]; j++) 
130                 PyList_SetItem(pylist, j, 
131                                PyString_FromString(strlist[j]));
132             return pylist;
133         }
134
135         break;
136     }
137     return NULL;
138
139 }
140
141 static PyObject *py_lp_ctx_load(py_talloc_Object *self, PyObject *args)
142 {
143         char *filename;
144         bool ret;
145         if (!PyArg_ParseTuple(args, "s", &filename))
146                 return NULL;
147
148         ret = lp_load((struct loadparm_context *)self->ptr, filename);
149
150         if (!ret) {
151                 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
152                 return NULL;
153         }
154         Py_RETURN_NONE;
155 }
156
157 static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
158 {
159         bool ret;
160         ret = lp_load_default(self->ptr);
161
162         if (!ret) {
163                 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
164                 return NULL;
165         }
166         Py_RETURN_NONE;
167 }
168
169 static PyObject *py_lp_ctx_get(py_talloc_Object *self, PyObject *args)
170 {
171         char *param_name;
172         char *section_name = NULL;
173         PyObject *ret;
174         if (!PyArg_ParseTuple(args, "s|s", &param_name, &section_name))
175                 return NULL;
176
177         ret = py_lp_ctx_get_helper(self->ptr, section_name, param_name);
178         if (ret == NULL)
179                 Py_RETURN_NONE;
180         return ret;
181 }
182
183 static PyObject *py_lp_ctx_is_myname(py_talloc_Object *self, PyObject *args)
184 {
185         char *name;
186         if (!PyArg_ParseTuple(args, "s", &name))
187                 return NULL;
188
189         return PyBool_FromLong(lp_is_myname(self->ptr, name));
190 }
191
192 static PyObject *py_lp_ctx_is_mydomain(py_talloc_Object *self, PyObject *args)
193 {
194         char *name;
195         if (!PyArg_ParseTuple(args, "s", &name))
196                 return NULL;
197
198         return PyBool_FromLong(lp_is_mydomain(self->ptr, name));
199 }
200
201 static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
202 {
203         char *name, *value;
204         bool ret;
205         if (!PyArg_ParseTuple(args, "ss", &name, &value))
206                 return NULL;
207
208         ret = lp_set_cmdline(self->ptr, name, value);
209         if (!ret) {
210                 PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
211                 return NULL;
212         }
213
214         Py_RETURN_NONE;
215 }
216
217 static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
218 {
219         char *name, *path;
220         PyObject *ret;
221         if (!PyArg_ParseTuple(args, "s", &name))
222                 return NULL;
223
224         path = private_path(NULL, self->ptr, name);
225         ret = PyString_FromString(path);
226         talloc_free(path);
227
228         return ret;
229 }
230
231 static PyMethodDef py_lp_ctx_methods[] = {
232         { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS, 
233                 "S.load(filename) -> None\n"
234                 "Load specified file." },
235         { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,
236                 "S.load_default() -> None\n"
237                 "Load default smb.conf file." },
238         { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,
239                 "S.is_myname(name) -> bool\n"
240                 "Check whether the specified name matches one of our netbios names." },
241         { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,
242                 "S.is_mydomain(name) -> bool\n"
243                 "Check whether the specified name matches our domain name." },
244         { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,
245                 "S.get(name, service_name) -> value\n"
246                 "Find specified parameter." },
247         { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,
248                 "S.set(name, value) -> bool\n"
249                 "Change a parameter." },
250         { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
251                 "S.private_path(name) -> path\n" },
252         { NULL }
253 };
254
255 static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
256 {
257         return PyLoadparmService_FromService(lp_default_service((struct loadparm_context *)self->ptr));
258 }
259
260 static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
261 {
262         return PyString_FromString(lp_configfile(self->ptr));
263 }
264
265 static PyGetSetDef py_lp_ctx_getset[] = {
266         { (char *)"default_service", (getter)py_lp_ctx_default_service, NULL, NULL },
267         { (char *)"configfile", (getter)py_lp_ctx_config_file, NULL, 
268                 (char *)"Name of last config file that was loaded." },
269         { NULL }
270 };
271
272 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
273 {
274         return py_talloc_import(type, loadparm_init(NULL));
275 }
276
277 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
278 {
279         return lp_numservices(self->ptr);
280 }
281
282 static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
283 {
284         struct loadparm_service *service;
285         if (!PyString_Check(name)) {
286                 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
287                 return NULL;
288         }
289         service = lp_service(self->ptr, PyString_AsString(name));
290         if (service == NULL) {
291                 PyErr_SetString(PyExc_KeyError, "No such section");
292                 return NULL;
293         }
294         return PyLoadparmService_FromService(service);
295 }
296
297 static PyMappingMethods py_lp_ctx_mapping = {
298         .mp_length = (lenfunc)py_lp_ctx_len,
299         .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
300 };
301
302 PyTypeObject PyLoadparmContext = {
303         .tp_name = "LoadParm",
304         .tp_basicsize = sizeof(py_talloc_Object),
305         .tp_dealloc = py_talloc_dealloc,
306         .tp_getset = py_lp_ctx_getset,
307         .tp_methods = py_lp_ctx_methods,
308         .tp_new = py_lp_ctx_new,
309         .tp_as_mapping = &py_lp_ctx_mapping,
310         .tp_flags = Py_TPFLAGS_DEFAULT,
311 };
312
313 PyTypeObject PyLoadparmService = {
314         .tp_name = "LoadparmService",
315         .tp_dealloc = py_talloc_dealloc,
316         .tp_basicsize = sizeof(py_talloc_Object),
317         .tp_flags = Py_TPFLAGS_DEFAULT,
318 };
319
320 _PUBLIC_ struct loadparm_context *lp_from_py_object(PyObject *py_obj)
321 {
322     struct loadparm_context *lp_ctx;
323     if (PyString_Check(py_obj)) {
324         lp_ctx = loadparm_init(NULL);
325         if (!lp_load(lp_ctx, PyString_AsString(py_obj))) {
326             talloc_free(lp_ctx);
327             PyErr_Format(PyExc_RuntimeError, 
328                          "Unable to load %s", PyString_AsString(py_obj));
329             return NULL;
330         }
331         return lp_ctx;
332     }
333
334     if (py_obj == Py_None) {
335         lp_ctx = loadparm_init(NULL);
336         /* We're not checking that loading the file succeeded *on purpose */
337         lp_load_default(lp_ctx);
338         return lp_ctx;
339     }
340
341     return PyLoadparmContext_AsLoadparmContext(py_obj);
342 }
343
344 struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx)
345 {
346     struct loadparm_context *ret;
347     ret = loadparm_init(mem_ctx);
348     if (!lp_load_default(ret))
349         return NULL;
350     return ret;
351 }
352
353 void initparam(void)
354 {
355         PyObject *m;
356
357         if (PyType_Ready(&PyLoadparmContext) < 0)
358                 return;
359
360         m = Py_InitModule3("param", NULL, "Parsing and writing Samba configuration files.");
361         if (m == NULL)
362                 return;
363
364         Py_INCREF(&PyLoadparmContext);
365         PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);
366 }