source4/param: Fix prototypes for all functions.
[kai/samba.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 "lib/talloc/pytalloc.h"
25 #include "dynconfig/dynconfig.h"
26
27 void initparam(void);
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_type(obj, struct loadparm_context)
36 #define PyLoadparmService_AsLoadparmService(obj) py_talloc_get_type(obj, struct loadparm_service)
37
38 extern PyTypeObject PyLoadparmContext;
39 extern PyTypeObject PyLoadparmService;
40
41 static 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 && strwicmp(service_name, GLOBAL_NAME) && 
53                 strwicmp(service_name, GLOBAL_NAME2)) {
54                 struct loadparm_service *service;
55                 /* its a share parameter */
56                 service = lpcfg_service(lp_ctx, service_name);
57                 if (service == NULL) {
58                         return NULL;
59                 }
60                 if (strchr(param_name, ':')) {
61                         /* its a parametric option on a share */
62                         const char *type = talloc_strndup(lp_ctx, 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 = lpcfg_get_parametric(lp_ctx, service, type, option);
70                         if (value == NULL) {
71                         return NULL;
72                         }
73                         return PyString_FromString(value);
74                 }
75
76                 parm = lpcfg_parm_struct(param_name);
77                 if (parm == NULL || parm->pclass == P_GLOBAL) {
78                         return NULL;
79                 }
80                 parm_ptr = lpcfg_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 = lpcfg_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 = lpcfg_parm_struct(param_name);
97                 if (parm == NULL) {
98                         return NULL;
99                 }
100                 parm_ptr = lpcfg_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_CMDLIST:
126     case P_LIST: 
127         {
128             int j;
129             const char **strlist = *(const char ***)parm_ptr;
130             PyObject *pylist;
131                 
132                 if(strlist == NULL) {
133                         return PyList_New(0);
134                 }
135                 
136                 pylist = PyList_New(str_list_length(strlist));
137             for (j = 0; strlist[j]; j++) 
138                 PyList_SetItem(pylist, j, 
139                                PyString_FromString(strlist[j]));
140             return pylist;
141         }
142
143         break;
144     }
145     return NULL;
146
147 }
148
149 static PyObject *py_lp_ctx_load(py_talloc_Object *self, PyObject *args)
150 {
151         char *filename;
152         bool ret;
153         if (!PyArg_ParseTuple(args, "s", &filename))
154                 return NULL;
155
156         ret = lpcfg_load(PyLoadparmContext_AsLoadparmContext(self), filename);
157
158         if (!ret) {
159                 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
160                 return NULL;
161         }
162         Py_RETURN_NONE;
163 }
164
165 static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
166 {
167         bool ret;
168         ret = lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self));
169
170         if (!ret) {
171                 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
172                 return NULL;
173         }
174         Py_RETURN_NONE;
175 }
176
177 static PyObject *py_lp_ctx_get(py_talloc_Object *self, PyObject *args)
178 {
179         char *param_name;
180         char *section_name = NULL;
181         PyObject *ret;
182         if (!PyArg_ParseTuple(args, "s|z", &param_name, &section_name))
183                 return NULL;
184
185         ret = py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self), section_name, param_name);
186         if (ret == NULL)
187                 Py_RETURN_NONE;
188         return ret;
189 }
190
191 static PyObject *py_lp_ctx_is_myname(py_talloc_Object *self, PyObject *args)
192 {
193         char *name;
194         if (!PyArg_ParseTuple(args, "s", &name))
195                 return NULL;
196
197         return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
198 }
199
200 static PyObject *py_lp_ctx_is_mydomain(py_talloc_Object *self, PyObject *args)
201 {
202         char *name;
203         if (!PyArg_ParseTuple(args, "s", &name))
204                 return NULL;
205
206         return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
207 }
208
209 static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
210 {
211         char *name, *value;
212         bool ret;
213         if (!PyArg_ParseTuple(args, "ss", &name, &value))
214                 return NULL;
215
216         ret = lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self), name, value);
217         if (!ret) {
218                 PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
219                 return NULL;
220         }
221
222         Py_RETURN_NONE;
223 }
224
225 static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
226 {
227         char *name, *path;
228         PyObject *ret;
229         if (!PyArg_ParseTuple(args, "s", &name))
230                 return NULL;
231
232         path = private_path(NULL, PyLoadparmContext_AsLoadparmContext(self), name);
233         ret = PyString_FromString(path);
234         talloc_free(path);
235
236         return ret;
237 }
238
239 static PyObject *py_lp_ctx_services(py_talloc_Object *self)
240 {
241         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
242         PyObject *ret;
243         int i;
244         ret = PyList_New(lpcfg_numservices(lp_ctx));
245         for (i = 0; i < lpcfg_numservices(lp_ctx); i++) {
246                 struct loadparm_service *service = lpcfg_servicebynum(lp_ctx, i);
247                 if (service != NULL) {
248                         PyList_SetItem(ret, i, PyString_FromString(lpcfg_servicename(service)));
249                 }
250         }
251         return ret;
252 }
253
254 static PyObject *py_lp_dump(PyObject *self, PyObject *args)
255 {
256         PyObject *py_stream;
257         bool show_defaults = false;
258         FILE *f;
259         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
260
261         if (!PyArg_ParseTuple(args, "O|b", &py_stream, &show_defaults))
262                 return NULL;
263
264         f = PyFile_AsFile(py_stream);
265         if (f == NULL) {
266                 PyErr_SetString(PyExc_TypeError, "Not a file stream");
267                 return NULL;
268         }
269
270         lpcfg_dump(lp_ctx, f, show_defaults, lpcfg_numservices(lp_ctx));
271
272         Py_RETURN_NONE;
273 }
274
275
276 static PyMethodDef py_lp_ctx_methods[] = {
277         { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS, 
278                 "S.load(filename) -> None\n"
279                 "Load specified file." },
280         { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,
281                 "S.load_default() -> None\n"
282                 "Load default smb.conf file." },
283         { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,
284                 "S.is_myname(name) -> bool\n"
285                 "Check whether the specified name matches one of our netbios names." },
286         { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,
287                 "S.is_mydomain(name) -> bool\n"
288                 "Check whether the specified name matches our domain name." },
289         { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,
290                 "S.get(name, service_name) -> value\n"
291                 "Find specified parameter." },
292         { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,
293                 "S.set(name, value) -> bool\n"
294                 "Change a parameter." },
295         { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
296                 "S.private_path(name) -> path\n" },
297         { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,
298                 "S.services() -> list" },
299         { "dump", (PyCFunction)py_lp_dump, METH_VARARGS, 
300                 "S.dump(stream, show_defaults=False)" },
301         { NULL }
302 };
303
304 static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
305 {
306         return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self)));
307 }
308
309 static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
310 {
311         const char *configfile = lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self));
312         if (configfile == NULL)
313                 Py_RETURN_NONE;
314         else
315                 return PyString_FromString(configfile);
316 }
317
318 static PyGetSetDef py_lp_ctx_getset[] = {
319         { discard_const_p(char, "default_service"), (getter)py_lp_ctx_default_service, NULL, NULL },
320         { discard_const_p(char, "configfile"), (getter)py_lp_ctx_config_file, NULL,
321           discard_const_p(char, "Name of last config file that was loaded.") },
322         { NULL }
323 };
324
325 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
326 {
327         py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
328         if (ret == NULL) {
329                 PyErr_NoMemory();
330                 return NULL;
331         }
332         ret->talloc_ctx = talloc_new(NULL);
333         if (ret->talloc_ctx == NULL) {
334                 PyErr_NoMemory();
335                 return NULL;
336         }
337         ret->ptr = loadparm_init_global(false);
338         return (PyObject *)ret;
339 }
340
341 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
342 {
343         return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self));
344 }
345
346 static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
347 {
348         struct loadparm_service *service;
349         if (!PyString_Check(name)) {
350                 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
351                 return NULL;
352         }
353         service = lpcfg_service(PyLoadparmContext_AsLoadparmContext(self), PyString_AsString(name));
354         if (service == NULL) {
355                 PyErr_SetString(PyExc_KeyError, "No such section");
356                 return NULL;
357         }
358         return PyLoadparmService_FromService(service);
359 }
360
361 static PyMappingMethods py_lp_ctx_mapping = {
362         .mp_length = (lenfunc)py_lp_ctx_len,
363         .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
364 };
365
366 PyTypeObject PyLoadparmContext = {
367         .tp_name = "LoadParm",
368         .tp_basicsize = sizeof(py_talloc_Object),
369         .tp_getset = py_lp_ctx_getset,
370         .tp_methods = py_lp_ctx_methods,
371         .tp_new = py_lp_ctx_new,
372         .tp_as_mapping = &py_lp_ctx_mapping,
373         .tp_flags = Py_TPFLAGS_DEFAULT,
374 };
375
376 static PyObject *py_lp_service_dump(PyObject *self, PyObject *args)
377 {
378         PyObject *py_stream;
379         bool show_defaults = false;
380         FILE *f;
381         struct loadparm_service *service = PyLoadparmService_AsLoadparmService(self);
382         struct loadparm_service *default_service;
383         PyObject *py_default_service;
384
385         if (!PyArg_ParseTuple(args, "OO|b", &py_stream, &py_default_service,
386                                                   &show_defaults))
387                 return NULL;
388
389         f = PyFile_AsFile(py_stream);
390         if (f == NULL) {
391                 PyErr_SetString(PyExc_TypeError, "Not a file stream");
392                 return NULL;
393         }
394
395         if (!PyObject_TypeCheck(py_default_service, &PyLoadparmService)) {
396                 PyErr_SetNone(PyExc_TypeError);
397                 return NULL;
398         }
399
400         default_service = PyLoadparmService_AsLoadparmService(py_default_service);
401
402         lpcfg_dump_one(f, show_defaults, service, default_service);
403
404         Py_RETURN_NONE;
405 }
406
407 static PyMethodDef py_lp_service_methods[] = {
408         { "dump", (PyCFunction)py_lp_service_dump, METH_VARARGS, 
409                 "S.dump(f, default_service, show_defaults=False)" },
410         { NULL }
411 };
412
413 PyTypeObject PyLoadparmService = {
414         .tp_name = "LoadparmService",
415         .tp_basicsize = sizeof(py_talloc_Object),
416         .tp_methods = py_lp_service_methods,
417         .tp_flags = Py_TPFLAGS_DEFAULT,
418 };
419
420 static PyObject *py_default_path(PyObject *self)
421 {
422     return PyString_FromString(lp_default_path());
423 }
424
425 static PyObject *py_setup_dir(PyObject *self)
426 {
427     return PyString_FromString(dyn_SETUPDIR);
428 }
429
430 static PyMethodDef pyparam_methods[] = {
431     { "default_path", (PyCFunction)py_default_path, METH_NOARGS, 
432         "Returns the default smb.conf path." },
433     { "setup_dir", (PyCFunction)py_setup_dir, METH_NOARGS,
434         "Returns the compiled in location of provision tempates." },
435     { NULL }
436 };
437
438 void initparam(void)
439 {
440         PyObject *m;
441         PyTypeObject *talloc_type = PyTalloc_GetObjectType();
442         if (talloc_type == NULL)
443                 return;
444
445         PyLoadparmContext.tp_base = talloc_type;
446         PyLoadparmService.tp_base = talloc_type;
447
448         if (PyType_Ready(&PyLoadparmContext) < 0)
449                 return;
450
451         if (PyType_Ready(&PyLoadparmService) < 0)
452                 return;
453
454         m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
455         if (m == NULL)
456                 return;
457
458         Py_INCREF(&PyLoadparmContext);
459         PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);
460 }