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