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/>.
22 #include "param/param.h"
23 #include "param/loadparm.h"
25 #include "dynconfig/dynconfig.h"
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;
35 #define PyLoadparmContext_AsLoadparmContext(obj) pytalloc_get_type(obj, struct loadparm_context)
36 #define PyLoadparmService_AsLoadparmService(obj) pytalloc_get_type(obj, struct loadparm_service)
38 extern PyTypeObject PyLoadparmContext;
39 extern PyTypeObject PyLoadparmService;
41 static PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
43 return pytalloc_reference(&PyLoadparmService, service);
46 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
48 struct parm_struct *parm = NULL;
49 void *parm_ptr = NULL;
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) {
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;
66 if (type == NULL || option == NULL) {
69 value = lpcfg_get_parametric(lp_ctx, service, type, option);
73 return PyString_FromString(value);
76 parm = lpcfg_parm_struct(lp_ctx, param_name);
77 if (parm == NULL || parm->p_class == P_GLOBAL) {
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;
87 if (type == NULL || option == NULL) {
90 value = lpcfg_get_parametric(lp_ctx, NULL, type, option);
93 return PyString_FromString(value);
95 /* its a global parameter */
96 parm = lpcfg_parm_struct(lp_ctx, param_name);
100 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, parm);
103 if (parm == NULL || parm_ptr == NULL) {
107 /* construct and return the right type of python object */
108 switch (parm->type) {
110 return PyString_FromFormat("%c", *(char *)parm_ptr);
113 return PyString_FromString(*(char **)parm_ptr);
115 return PyBool_FromLong(*(bool *)parm_ptr);
117 return PyBool_FromLong(!(*(bool *)parm_ptr));
121 return PyLong_FromLong(*(int *)parm_ptr);
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);
133 const char **strlist = *(const char ***)parm_ptr;
136 if(strlist == NULL) {
137 return PyList_New(0);
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]));
147 return NULL; /* this stands for a separator, can be ignored */
153 static PyObject *py_lp_ctx_load(pytalloc_Object *self, PyObject *args)
157 if (!PyArg_ParseTuple(args, "s", &filename))
160 ret = lpcfg_load(PyLoadparmContext_AsLoadparmContext(self), filename);
163 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
169 static PyObject *py_lp_ctx_load_default(pytalloc_Object *self)
172 ret = lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self));
175 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
181 static PyObject *py_lp_ctx_get(pytalloc_Object *self, PyObject *args)
184 char *section_name = NULL;
186 if (!PyArg_ParseTuple(args, "s|z", ¶m_name, §ion_name))
189 ret = py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self), section_name, param_name);
195 static PyObject *py_lp_ctx_is_myname(pytalloc_Object *self, PyObject *args)
198 if (!PyArg_ParseTuple(args, "s", &name))
201 return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
204 static PyObject *py_lp_ctx_is_mydomain(pytalloc_Object *self, PyObject *args)
207 if (!PyArg_ParseTuple(args, "s", &name))
210 return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
213 static PyObject *py_lp_ctx_set(pytalloc_Object *self, PyObject *args)
217 if (!PyArg_ParseTuple(args, "ss", &name, &value))
220 ret = lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self), name, value);
222 PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
229 static PyObject *py_lp_ctx_private_path(pytalloc_Object *self, PyObject *args)
233 if (!PyArg_ParseTuple(args, "s", &name))
236 path = lpcfg_private_path(NULL, PyLoadparmContext_AsLoadparmContext(self), name);
237 ret = PyString_FromString(path);
243 static PyObject *py_lp_ctx_services(pytalloc_Object *self)
245 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
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)));
258 static PyObject *py_lp_ctx_server_role(pytalloc_Object *self)
260 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
262 const char *role_str;
264 role = lpcfg_server_role(lp_ctx);
265 role_str = server_role_str(role);
267 return PyString_FromString(role_str);
270 static PyObject *py_lp_dump(PyObject *self, PyObject *args)
273 bool show_defaults = false;
275 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
277 if (!PyArg_ParseTuple(args, "O|b", &py_stream, &show_defaults))
280 f = PyFile_AsFile(py_stream);
285 lpcfg_dump(lp_ctx, f, show_defaults, lpcfg_numservices(lp_ctx));
290 static PyObject *py_lp_dump_a_parameter(PyObject *self, PyObject *args)
294 const char *section_name = NULL;
296 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
297 struct loadparm_service *service;
300 if (!PyArg_ParseTuple(args, "Os|z", &py_stream, ¶m_name, §ion_name))
303 f = PyFile_AsFile(py_stream);
308 if (section_name != NULL && strwicmp(section_name, GLOBAL_NAME) &&
309 strwicmp(section_name, GLOBAL_NAME2)) {
310 /* it's a share parameter */
311 service = lpcfg_service(lp_ctx, section_name);
312 if (service == NULL) {
313 PyErr_Format(PyExc_RuntimeError, "Unknown section %s", section_name);
319 section_name = "global";
322 ret = lpcfg_dump_a_parameter(lp_ctx, service, param_name, f);
325 PyErr_Format(PyExc_RuntimeError, "Parameter %s unknown for section %s", param_name, section_name);
333 static PyObject *py_samdb_url(PyObject *self)
335 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
336 return PyString_FromFormat("tdb://%s/sam.ldb", lpcfg_private_dir(lp_ctx));
340 static PyMethodDef py_lp_ctx_methods[] = {
341 { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS,
342 "S.load(filename) -> None\n"
343 "Load specified file." },
344 { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,
345 "S.load_default() -> None\n"
346 "Load default smb.conf file." },
347 { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,
348 "S.is_myname(name) -> bool\n"
349 "Check whether the specified name matches one of our netbios names." },
350 { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,
351 "S.is_mydomain(name) -> bool\n"
352 "Check whether the specified name matches our domain name." },
353 { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,
354 "S.get(name, service_name) -> value\n"
355 "Find specified parameter." },
356 { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,
357 "S.set(name, value) -> bool\n"
358 "Change a parameter." },
359 { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
360 "S.private_path(name) -> path\n" },
361 { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,
362 "S.services() -> list" },
363 { "server_role", (PyCFunction)py_lp_ctx_server_role, METH_NOARGS,
364 "S.server_role() -> value\n"
365 "Get the server role." },
366 { "dump", (PyCFunction)py_lp_dump, METH_VARARGS,
367 "S.dump(stream, show_defaults=False)" },
368 { "dump_a_parameter", (PyCFunction)py_lp_dump_a_parameter, METH_VARARGS,
369 "S.dump_a_parameter(stream, name, service_name)" },
370 { "samdb_url", (PyCFunction)py_samdb_url, METH_NOARGS,
371 "S.samdb_url() -> string\n"
372 "Returns the current URL for sam.ldb." },
376 static PyObject *py_lp_ctx_default_service(pytalloc_Object *self, void *closure)
378 return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self)));
381 static PyObject *py_lp_ctx_config_file(pytalloc_Object *self, void *closure)
383 const char *configfile = lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self));
384 if (configfile == NULL)
387 return PyString_FromString(configfile);
390 static PyGetSetDef py_lp_ctx_getset[] = {
391 { discard_const_p(char, "default_service"), (getter)py_lp_ctx_default_service, NULL, NULL },
392 { discard_const_p(char, "configfile"), (getter)py_lp_ctx_config_file, NULL,
393 discard_const_p(char, "Name of last config file that was loaded.") },
397 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
399 pytalloc_Object *ret = (pytalloc_Object *)type->tp_alloc(type, 0);
404 ret->talloc_ctx = talloc_new(NULL);
405 if (ret->talloc_ctx == NULL) {
409 ret->ptr = loadparm_init_global(false);
410 if (ret->ptr == NULL) {
414 return (PyObject *)ret;
417 static Py_ssize_t py_lp_ctx_len(pytalloc_Object *self)
419 return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self));
422 static PyObject *py_lp_ctx_getitem(pytalloc_Object *self, PyObject *name)
424 struct loadparm_service *service;
425 if (!PyString_Check(name)) {
426 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
429 service = lpcfg_service(PyLoadparmContext_AsLoadparmContext(self), PyString_AsString(name));
430 if (service == NULL) {
431 PyErr_SetString(PyExc_KeyError, "No such section");
434 return PyLoadparmService_FromService(service);
437 static PyMappingMethods py_lp_ctx_mapping = {
438 .mp_length = (lenfunc)py_lp_ctx_len,
439 .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
442 PyTypeObject PyLoadparmContext = {
443 .tp_name = "param.LoadParm",
444 .tp_basicsize = sizeof(pytalloc_Object),
445 .tp_getset = py_lp_ctx_getset,
446 .tp_methods = py_lp_ctx_methods,
447 .tp_new = py_lp_ctx_new,
448 .tp_as_mapping = &py_lp_ctx_mapping,
449 .tp_flags = Py_TPFLAGS_DEFAULT,
452 static PyObject *py_lp_service_dump(PyObject *self, PyObject *args)
455 bool show_defaults = false;
457 struct loadparm_service *service = PyLoadparmService_AsLoadparmService(self);
458 struct loadparm_service *default_service;
459 PyObject *py_default_service;
461 if (!PyArg_ParseTuple(args, "OO|b", &py_stream, &py_default_service,
465 f = PyFile_AsFile(py_stream);
470 if (!PyObject_TypeCheck(py_default_service, &PyLoadparmService)) {
471 PyErr_SetNone(PyExc_TypeError);
475 default_service = PyLoadparmService_AsLoadparmService(py_default_service);
477 lpcfg_dump_one(f, show_defaults, service, default_service);
482 static PyMethodDef py_lp_service_methods[] = {
483 { "dump", (PyCFunction)py_lp_service_dump, METH_VARARGS,
484 "S.dump(f, default_service, show_defaults=False)" },
488 PyTypeObject PyLoadparmService = {
489 .tp_name = "param.LoadparmService",
490 .tp_basicsize = sizeof(pytalloc_Object),
491 .tp_methods = py_lp_service_methods,
492 .tp_flags = Py_TPFLAGS_DEFAULT,
495 static PyObject *py_default_path(PyObject *self)
497 return PyString_FromString(lp_default_path());
500 static PyObject *py_setup_dir(PyObject *self)
502 return PyString_FromString(dyn_SETUPDIR);
505 static PyObject *py_modules_dir(PyObject *self)
507 return PyString_FromString(dyn_MODULESDIR);
510 static PyObject *py_bin_dir(PyObject *self)
512 return PyString_FromString(dyn_BINDIR);
515 static PyObject *py_sbin_dir(PyObject *self)
517 return PyString_FromString(dyn_SBINDIR);
520 static PyMethodDef pyparam_methods[] = {
521 { "default_path", (PyCFunction)py_default_path, METH_NOARGS,
522 "Returns the default smb.conf path." },
523 { "setup_dir", (PyCFunction)py_setup_dir, METH_NOARGS,
524 "Returns the compiled in location of provision tempates." },
525 { "modules_dir", (PyCFunction)py_modules_dir, METH_NOARGS,
526 "Returns the compiled in location of modules." },
527 { "bin_dir", (PyCFunction)py_bin_dir, METH_NOARGS,
528 "Returns the compiled in BINDIR." },
529 { "sbin_dir", (PyCFunction)py_sbin_dir, METH_NOARGS,
530 "Returns the compiled in SBINDIR." },
537 PyTypeObject *talloc_type = pytalloc_GetObjectType();
538 if (talloc_type == NULL)
541 PyLoadparmContext.tp_base = talloc_type;
542 PyLoadparmService.tp_base = talloc_type;
544 if (PyType_Ready(&PyLoadparmContext) < 0)
547 if (PyType_Ready(&PyLoadparmService) < 0)
550 m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
554 Py_INCREF(&PyLoadparmContext);
555 PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);