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