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