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"
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;
32 #define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_type(obj, struct loadparm_context)
34 PyAPI_DATA(PyTypeObject) PyLoadparmContext;
35 PyAPI_DATA(PyTypeObject) PyLoadparmService;
37 PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
39 return py_talloc_reference(&PyLoadparmService, service);
42 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
44 struct parm_struct *parm = NULL;
45 void *parm_ptr = NULL;
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) {
55 if (strchr(param_name, ':')) {
56 /* its a parametric option on a share */
57 const char *type = talloc_strndup(lp_ctx,
59 strcspn(param_name, ":"));
60 const char *option = strchr(param_name, ':') + 1;
62 if (type == NULL || option == NULL) {
65 value = lp_get_parametric(lp_ctx, service, type, option);
69 return PyString_FromString(value);
72 parm = lp_parm_struct(param_name);
73 if (parm == NULL || parm->pclass == P_GLOBAL) {
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;
83 if (type == NULL || option == NULL) {
86 value = lp_get_parametric(lp_ctx, NULL, type, option);
89 return PyString_FromString(value);
91 /* its a global parameter */
92 parm = lp_parm_struct(param_name);
96 parm_ptr = lp_parm_ptr(lp_ctx, NULL, parm);
99 if (parm == NULL || parm_ptr == NULL) {
103 /* construct and return the right type of python object */
104 switch (parm->type) {
107 return PyString_FromString(*(char **)parm_ptr);
109 return PyBool_FromLong(*(bool *)parm_ptr);
113 return PyLong_FromLong(*(int *)parm_ptr);
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);
124 const char **strlist = *(const char ***)parm_ptr;
127 if(strlist == NULL) {
128 return PyList_New(0);
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]));
144 static PyObject *py_lp_ctx_load(py_talloc_Object *self, PyObject *args)
148 if (!PyArg_ParseTuple(args, "s", &filename))
151 ret = lp_load(PyLoadparmContext_AsLoadparmContext(self), filename);
154 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
160 static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
163 ret = lp_load_default(PyLoadparmContext_AsLoadparmContext(self));
166 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
172 static PyObject *py_lp_ctx_get(py_talloc_Object *self, PyObject *args)
175 char *section_name = NULL;
177 if (!PyArg_ParseTuple(args, "s|s", ¶m_name, §ion_name))
180 ret = py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self), section_name, param_name);
186 static PyObject *py_lp_ctx_is_myname(py_talloc_Object *self, PyObject *args)
189 if (!PyArg_ParseTuple(args, "s", &name))
192 return PyBool_FromLong(lp_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
195 static PyObject *py_lp_ctx_is_mydomain(py_talloc_Object *self, PyObject *args)
198 if (!PyArg_ParseTuple(args, "s", &name))
201 return PyBool_FromLong(lp_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
204 static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
208 if (!PyArg_ParseTuple(args, "ss", &name, &value))
211 ret = lp_set_cmdline(PyLoadparmContext_AsLoadparmContext(self), name, value);
213 PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
220 static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
224 if (!PyArg_ParseTuple(args, "s", &name))
227 path = private_path(NULL, PyLoadparmContext_AsLoadparmContext(self), name);
228 ret = PyString_FromString(path);
234 static PyObject *py_lp_ctx_services(py_talloc_Object *self)
236 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
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)));
249 static PyMethodDef py_lp_ctx_methods[] = {
250 { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS,
251 "S.load(filename) -> None\n"
252 "Load specified file." },
253 { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,
254 "S.load_default() -> None\n"
255 "Load default smb.conf file." },
256 { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,
257 "S.is_myname(name) -> bool\n"
258 "Check whether the specified name matches one of our netbios names." },
259 { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,
260 "S.is_mydomain(name) -> bool\n"
261 "Check whether the specified name matches our domain name." },
262 { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,
263 "S.get(name, service_name) -> value\n"
264 "Find specified parameter." },
265 { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,
266 "S.set(name, value) -> bool\n"
267 "Change a parameter." },
268 { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
269 "S.private_path(name) -> path\n" },
270 { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,
271 "S.services() -> list" },
275 static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
277 return PyLoadparmService_FromService(lp_default_service(PyLoadparmContext_AsLoadparmContext(self)));
280 static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
282 const char *configfile = lp_configfile(PyLoadparmContext_AsLoadparmContext(self));
283 if (configfile == NULL)
286 return PyString_FromString(configfile);
289 static PyGetSetDef py_lp_ctx_getset[] = {
290 { discard_const_p(char, "default_service"), (getter)py_lp_ctx_default_service, NULL, NULL },
291 { discard_const_p(char, "configfile"), (getter)py_lp_ctx_config_file, NULL,
292 discard_const_p(char, "Name of last config file that was loaded.") },
296 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
298 py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
303 ret->talloc_ctx = talloc_new(NULL);
304 if (ret->talloc_ctx == NULL) {
308 ret->ptr = loadparm_init(ret->talloc_ctx);
309 return (PyObject *)ret;
312 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
314 return lp_numservices(PyLoadparmContext_AsLoadparmContext(self));
317 static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
319 struct loadparm_service *service;
320 if (!PyString_Check(name)) {
321 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
324 service = lp_service(PyLoadparmContext_AsLoadparmContext(self), PyString_AsString(name));
325 if (service == NULL) {
326 PyErr_SetString(PyExc_KeyError, "No such section");
329 return PyLoadparmService_FromService(service);
332 static PyMappingMethods py_lp_ctx_mapping = {
333 .mp_length = (lenfunc)py_lp_ctx_len,
334 .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
337 PyTypeObject PyLoadparmContext = {
338 .tp_name = "LoadParm",
339 .tp_basicsize = sizeof(py_talloc_Object),
340 .tp_dealloc = py_talloc_dealloc,
341 .tp_getset = py_lp_ctx_getset,
342 .tp_methods = py_lp_ctx_methods,
343 .tp_new = py_lp_ctx_new,
344 .tp_as_mapping = &py_lp_ctx_mapping,
345 .tp_flags = Py_TPFLAGS_DEFAULT,
348 PyTypeObject PyLoadparmService = {
349 .tp_name = "LoadparmService",
350 .tp_dealloc = py_talloc_dealloc,
351 .tp_basicsize = sizeof(py_talloc_Object),
352 .tp_flags = Py_TPFLAGS_DEFAULT,
355 static PyObject *py_default_path(PyObject *self)
357 return PyString_FromString(lp_default_path());
360 static PyMethodDef pyparam_methods[] = {
361 { "default_path", (PyCFunction)py_default_path, METH_NOARGS,
362 "Returns the default smb.conf path." },
370 if (PyType_Ready(&PyLoadparmContext) < 0)
373 if (PyType_Ready(&PyLoadparmService) < 0)
376 m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
380 Py_INCREF(&PyLoadparmContext);
381 PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);