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