WHATSNEW: Add another change.
[Samba.git] / source4 / param / pyparam.c
blobbf4d1d0cbc466f015b253e5bd7f2a6bdaed31180
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
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.
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.
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/>.
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"
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
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)
36 extern PyTypeObject PyLoadparmContext;
37 extern PyTypeObject PyLoadparmService;
39 PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
41 return py_talloc_reference(&PyLoadparmService, service);
44 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
46 struct parm_struct *parm = NULL;
47 void *parm_ptr = NULL;
48 int i;
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;
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;
67 value = lpcfg_get_parametric(lp_ctx, service, type, option);
68 if (value == NULL) {
69 return NULL;
71 return PyString_FromString(value);
74 parm = lpcfg_parm_struct(param_name);
75 if (parm == NULL || parm->pclass == P_GLOBAL) {
76 return NULL;
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;
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;
98 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, parm);
101 if (parm == NULL || parm_ptr == NULL) {
102 return NULL;
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);
122 return NULL;
123 case P_CMDLIST:
124 case P_LIST:
126 int j;
127 const char **strlist = *(const char ***)parm_ptr;
128 PyObject *pylist;
130 if(strlist == NULL) {
131 return PyList_New(0);
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;
141 break;
143 return NULL;
147 static PyObject *py_lp_ctx_load(py_talloc_Object *self, PyObject *args)
149 char *filename;
150 bool ret;
151 if (!PyArg_ParseTuple(args, "s", &filename))
152 return NULL;
154 ret = lpcfg_load(PyLoadparmContext_AsLoadparmContext(self), filename);
156 if (!ret) {
157 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
158 return NULL;
160 Py_RETURN_NONE;
163 static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
165 bool ret;
166 ret = lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self));
168 if (!ret) {
169 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
170 return NULL;
172 Py_RETURN_NONE;
175 static PyObject *py_lp_ctx_get(py_talloc_Object *self, PyObject *args)
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;
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;
189 static PyObject *py_lp_ctx_is_myname(py_talloc_Object *self, PyObject *args)
191 char *name;
192 if (!PyArg_ParseTuple(args, "s", &name))
193 return NULL;
195 return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
198 static PyObject *py_lp_ctx_is_mydomain(py_talloc_Object *self, PyObject *args)
200 char *name;
201 if (!PyArg_ParseTuple(args, "s", &name))
202 return NULL;
204 return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
207 static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
209 char *name, *value;
210 bool ret;
211 if (!PyArg_ParseTuple(args, "ss", &name, &value))
212 return NULL;
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;
220 Py_RETURN_NONE;
223 static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
225 char *name, *path;
226 PyObject *ret;
227 if (!PyArg_ParseTuple(args, "s", &name))
228 return NULL;
230 path = private_path(NULL, PyLoadparmContext_AsLoadparmContext(self), name);
231 ret = PyString_FromString(path);
232 talloc_free(path);
234 return ret;
237 static PyObject *py_lp_ctx_services(py_talloc_Object *self)
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)));
249 return ret;
252 static PyObject *py_lp_dump(PyObject *self, PyObject *args)
254 PyObject *py_stream;
255 bool show_defaults = false;
256 FILE *f;
257 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
259 if (!PyArg_ParseTuple(args, "O|b", &py_stream, &show_defaults))
260 return NULL;
262 f = PyFile_AsFile(py_stream);
263 if (f == NULL) {
264 PyErr_SetString(PyExc_TypeError, "Not a file stream");
265 return NULL;
268 lpcfg_dump(lp_ctx, f, show_defaults, lpcfg_numservices(lp_ctx));
270 Py_RETURN_NONE;
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 }
302 static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
304 return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self)));
307 static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
309 const char *configfile = lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self));
310 if (configfile == NULL)
311 Py_RETURN_NONE;
312 else
313 return PyString_FromString(configfile);
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 }
323 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
325 py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
326 if (ret == NULL) {
327 PyErr_NoMemory();
328 return NULL;
330 ret->talloc_ctx = talloc_new(NULL);
331 if (ret->talloc_ctx == NULL) {
332 PyErr_NoMemory();
333 return NULL;
335 ret->ptr = loadparm_init_global(false);
336 return (PyObject *)ret;
339 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
341 return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self));
344 static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
346 struct loadparm_service *service;
347 if (!PyString_Check(name)) {
348 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
349 return NULL;
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;
356 return PyLoadparmService_FromService(service);
359 static PyMappingMethods py_lp_ctx_mapping = {
360 .mp_length = (lenfunc)py_lp_ctx_len,
361 .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
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,
374 static PyObject *py_lp_service_dump(PyObject *self, PyObject *args)
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;
383 if (!PyArg_ParseTuple(args, "OO|b", &py_stream, &py_default_service,
384 &show_defaults))
385 return NULL;
387 f = PyFile_AsFile(py_stream);
388 if (f == NULL) {
389 PyErr_SetString(PyExc_TypeError, "Not a file stream");
390 return NULL;
393 if (!PyObject_TypeCheck(py_default_service, &PyLoadparmService)) {
394 PyErr_SetNone(PyExc_TypeError);
395 return NULL;
398 default_service = PyLoadparmService_AsLoadparmService(py_default_service);
400 lpcfg_dump_one(f, show_defaults, service, default_service);
402 Py_RETURN_NONE;
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 }
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,
418 static PyObject *py_default_path(PyObject *self)
420 return PyString_FromString(lp_default_path());
423 static PyObject *py_setup_dir(PyObject *self)
425 return PyString_FromString(dyn_SETUPDIR);
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 }
436 void initparam(void)
438 PyObject *m;
439 PyTypeObject *talloc_type = PyTalloc_GetObjectType();
440 if (talloc_type == NULL)
441 return;
443 PyLoadparmContext.tp_base = talloc_type;
444 PyLoadparmService.tp_base = talloc_type;
446 if (PyType_Ready(&PyLoadparmContext) < 0)
447 return;
449 if (PyType_Ready(&PyLoadparmService) < 0)
450 return;
452 m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
453 if (m == NULL)
454 return;
456 Py_INCREF(&PyLoadparmContext);
457 PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);