c07892cc541626dc1f05d97e690658ff21786252
[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(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_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 = lpcfg_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 static PyObject *py_samdb_url(PyObject *self)
276 {
277         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
278         return PyString_FromFormat("tdb://%s/sam.ldb", lpcfg_private_dir(lp_ctx));
279 }
280
281
282 static PyMethodDef py_lp_ctx_methods[] = {
283         { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS, 
284                 "S.load(filename) -> None\n"
285                 "Load specified file." },
286         { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,
287                 "S.load_default() -> None\n"
288                 "Load default smb.conf file." },
289         { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,
290                 "S.is_myname(name) -> bool\n"
291                 "Check whether the specified name matches one of our netbios names." },
292         { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,
293                 "S.is_mydomain(name) -> bool\n"
294                 "Check whether the specified name matches our domain name." },
295         { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,
296                 "S.get(name, service_name) -> value\n"
297                 "Find specified parameter." },
298         { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,
299                 "S.set(name, value) -> bool\n"
300                 "Change a parameter." },
301         { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
302                 "S.private_path(name) -> path\n" },
303         { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,
304                 "S.services() -> list" },
305         { "dump", (PyCFunction)py_lp_dump, METH_VARARGS, 
306                 "S.dump(stream, show_defaults=False)" },
307         { "samdb_url", (PyCFunction)py_samdb_url, METH_NOARGS,
308                 "S.samdb_url() -> string\n"
309                 "Returns the current URL for sam.ldb." },
310         { NULL }
311 };
312
313 static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
314 {
315         return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self)));
316 }
317
318 static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
319 {
320         const char *configfile = lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self));
321         if (configfile == NULL)
322                 Py_RETURN_NONE;
323         else
324                 return PyString_FromString(configfile);
325 }
326
327 static PyGetSetDef py_lp_ctx_getset[] = {
328         { discard_const_p(char, "default_service"), (getter)py_lp_ctx_default_service, NULL, NULL },
329         { discard_const_p(char, "configfile"), (getter)py_lp_ctx_config_file, NULL,
330           discard_const_p(char, "Name of last config file that was loaded.") },
331         { NULL }
332 };
333
334 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
335 {
336         py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
337         if (ret == NULL) {
338                 PyErr_NoMemory();
339                 return NULL;
340         }
341         ret->talloc_ctx = talloc_new(NULL);
342         if (ret->talloc_ctx == NULL) {
343                 PyErr_NoMemory();
344                 return NULL;
345         }
346         ret->ptr = loadparm_init_global(false);
347         if (ret->ptr == NULL) {
348                 PyErr_NoMemory();
349                 return NULL;
350         }
351         return (PyObject *)ret;
352 }
353
354 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
355 {
356         return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self));
357 }
358
359 static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
360 {
361         struct loadparm_service *service;
362         if (!PyString_Check(name)) {
363                 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
364                 return NULL;
365         }
366         service = lpcfg_service(PyLoadparmContext_AsLoadparmContext(self), PyString_AsString(name));
367         if (service == NULL) {
368                 PyErr_SetString(PyExc_KeyError, "No such section");
369                 return NULL;
370         }
371         return PyLoadparmService_FromService(service);
372 }
373
374 static PyMappingMethods py_lp_ctx_mapping = {
375         .mp_length = (lenfunc)py_lp_ctx_len,
376         .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
377 };
378
379 PyTypeObject PyLoadparmContext = {
380         .tp_name = "LoadParm",
381         .tp_basicsize = sizeof(py_talloc_Object),
382         .tp_getset = py_lp_ctx_getset,
383         .tp_methods = py_lp_ctx_methods,
384         .tp_new = py_lp_ctx_new,
385         .tp_as_mapping = &py_lp_ctx_mapping,
386         .tp_flags = Py_TPFLAGS_DEFAULT,
387 };
388
389 static PyObject *py_lp_service_dump(PyObject *self, PyObject *args)
390 {
391         PyObject *py_stream;
392         bool show_defaults = false;
393         FILE *f;
394         struct loadparm_service *service = PyLoadparmService_AsLoadparmService(self);
395         struct loadparm_service *default_service;
396         PyObject *py_default_service;
397
398         if (!PyArg_ParseTuple(args, "OO|b", &py_stream, &py_default_service,
399                                                   &show_defaults))
400                 return NULL;
401
402         f = PyFile_AsFile(py_stream);
403         if (f == NULL) {
404                 PyErr_SetString(PyExc_TypeError, "Not a file stream");
405                 return NULL;
406         }
407
408         if (!PyObject_TypeCheck(py_default_service, &PyLoadparmService)) {
409                 PyErr_SetNone(PyExc_TypeError);
410                 return NULL;
411         }
412
413         default_service = PyLoadparmService_AsLoadparmService(py_default_service);
414
415         lpcfg_dump_one(f, show_defaults, service, default_service);
416
417         Py_RETURN_NONE;
418 }
419
420 static PyMethodDef py_lp_service_methods[] = {
421         { "dump", (PyCFunction)py_lp_service_dump, METH_VARARGS, 
422                 "S.dump(f, default_service, show_defaults=False)" },
423         { NULL }
424 };
425
426 PyTypeObject PyLoadparmService = {
427         .tp_name = "LoadparmService",
428         .tp_basicsize = sizeof(py_talloc_Object),
429         .tp_methods = py_lp_service_methods,
430         .tp_flags = Py_TPFLAGS_DEFAULT,
431 };
432
433 static PyObject *py_default_path(PyObject *self)
434 {
435     return PyString_FromString(lp_default_path());
436 }
437
438 static PyObject *py_setup_dir(PyObject *self)
439 {
440     return PyString_FromString(dyn_SETUPDIR);
441 }
442
443 static PyObject *py_modules_dir(PyObject *self)
444 {
445     return PyString_FromString(dyn_MODULESDIR);
446 }
447
448 static PyMethodDef pyparam_methods[] = {
449     { "default_path", (PyCFunction)py_default_path, METH_NOARGS, 
450         "Returns the default smb.conf path." },
451     { "setup_dir", (PyCFunction)py_setup_dir, METH_NOARGS,
452         "Returns the compiled in location of provision tempates." },
453     { "modules_dir", (PyCFunction)py_modules_dir, METH_NOARGS,
454         "Returns the compiled in location of modules." },
455     { NULL }
456 };
457
458 void initparam(void)
459 {
460         PyObject *m;
461         PyTypeObject *talloc_type = PyTalloc_GetObjectType();
462         if (talloc_type == NULL)
463                 return;
464
465         PyLoadparmContext.tp_base = talloc_type;
466         PyLoadparmService.tp_base = talloc_type;
467
468         if (PyType_Ready(&PyLoadparmContext) < 0)
469                 return;
470
471         if (PyType_Ready(&PyLoadparmService) < 0)
472                 return;
473
474         m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
475         if (m == NULL)
476                 return;
477
478         Py_INCREF(&PyLoadparmContext);
479         PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);
480 }