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/>.
24 #include "param/param.h"
25 #include "param/loadparm.h"
26 #include "../lib/util/python_util.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) py_talloc_get_ptr(obj)
37 PyAPI_DATA(PyTypeObject) PyLoadparmContext;
38 PyAPI_DATA(PyTypeObject) PyLoadparmService;
40 PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
42 return py_talloc_import(&PyLoadparmService, service);
45 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
47 struct parm_struct *parm = NULL;
48 void *parm_ptr = NULL;
51 if (service_name != NULL) {
52 struct loadparm_service *service;
53 /* its a share parameter */
54 service = lp_service(lp_ctx, service_name);
55 if (service == NULL) {
58 if (strchr(param_name, ':')) {
59 /* its a parametric option on a share */
60 const char *type = talloc_strndup(lp_ctx,
62 strcspn(param_name, ":"));
63 const char *option = strchr(param_name, ':') + 1;
65 if (type == NULL || option == NULL) {
68 value = lp_get_parametric(lp_ctx, service, type, option);
72 return PyString_FromString(value);
75 parm = lp_parm_struct(param_name);
76 if (parm == NULL || parm->class == P_GLOBAL) {
79 parm_ptr = lp_parm_ptr(lp_ctx, service, parm);
80 } else if (strchr(param_name, ':')) {
81 /* its a global parametric option */
82 const char *type = talloc_strndup(lp_ctx,
83 param_name, strcspn(param_name, ":"));
84 const char *option = strchr(param_name, ':') + 1;
86 if (type == NULL || option == NULL) {
89 value = lp_get_parametric(lp_ctx, NULL, type, option);
92 return PyString_FromString(value);
94 /* its a global parameter */
95 parm = lp_parm_struct(param_name);
99 parm_ptr = lp_parm_ptr(lp_ctx, NULL, parm);
102 if (parm == NULL || parm_ptr == NULL) {
106 /* construct and return the right type of python object */
107 switch (parm->type) {
110 return PyString_FromString(*(char **)parm_ptr);
112 return PyBool_FromLong(*(bool *)parm_ptr);
116 return PyLong_FromLong(*(int *)parm_ptr);
118 for (i=0; parm->enum_list[i].name; i++) {
119 if (*(int *)parm_ptr == parm->enum_list[i].value) {
120 return PyString_FromString(parm->enum_list[i].name);
127 const char **strlist = *(const char ***)parm_ptr;
128 PyObject *pylist = PyList_New(str_list_length(strlist));
129 for (j = 0; strlist[j]; j++)
130 PyList_SetItem(pylist, j,
131 PyString_FromString(strlist[j]));
141 static PyObject *py_lp_ctx_load(py_talloc_Object *self, PyObject *args)
145 if (!PyArg_ParseTuple(args, "s", &filename))
148 ret = lp_load((struct loadparm_context *)self->ptr, filename);
151 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
157 static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
160 ret = lp_load_default(self->ptr);
163 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
169 static PyObject *py_lp_ctx_get(py_talloc_Object *self, PyObject *args)
172 char *section_name = NULL;
174 if (!PyArg_ParseTuple(args, "s|s", ¶m_name, §ion_name))
177 ret = py_lp_ctx_get_helper(self->ptr, section_name, param_name);
183 static PyObject *py_lp_ctx_is_myname(py_talloc_Object *self, PyObject *args)
186 if (!PyArg_ParseTuple(args, "s", &name))
189 return PyBool_FromLong(lp_is_myname(self->ptr, name));
192 static PyObject *py_lp_ctx_is_mydomain(py_talloc_Object *self, PyObject *args)
195 if (!PyArg_ParseTuple(args, "s", &name))
198 return PyBool_FromLong(lp_is_mydomain(self->ptr, name));
201 static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
205 if (!PyArg_ParseTuple(args, "ss", &name, &value))
208 ret = lp_set_cmdline(self->ptr, name, value);
210 PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
217 static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
221 if (!PyArg_ParseTuple(args, "s", &name))
224 path = private_path(NULL, self->ptr, name);
225 ret = PyString_FromString(path);
231 static PyMethodDef py_lp_ctx_methods[] = {
232 { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS,
233 "S.load(filename) -> None\n"
234 "Load specified file." },
235 { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,
236 "S.load_default() -> None\n"
237 "Load default smb.conf file." },
238 { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,
239 "S.is_myname(name) -> bool\n"
240 "Check whether the specified name matches one of our netbios names." },
241 { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,
242 "S.is_mydomain(name) -> bool\n"
243 "Check whether the specified name matches our domain name." },
244 { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,
245 "S.get(name, service_name) -> value\n"
246 "Find specified parameter." },
247 { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,
248 "S.set(name, value) -> bool\n"
249 "Change a parameter." },
250 { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
251 "S.private_path(name) -> path\n" },
255 static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
257 return PyLoadparmService_FromService(lp_default_service((struct loadparm_context *)self->ptr));
260 static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
262 return PyString_FromString(lp_configfile(self->ptr));
265 static PyGetSetDef py_lp_ctx_getset[] = {
266 { (char *)"default_service", (getter)py_lp_ctx_default_service, NULL, NULL },
267 { (char *)"configfile", (getter)py_lp_ctx_config_file, NULL,
268 (char *)"Name of last config file that was loaded." },
272 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
274 return py_talloc_import(type, loadparm_init(NULL));
277 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
279 return lp_numservices(self->ptr);
282 static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
284 struct loadparm_service *service;
285 if (!PyString_Check(name)) {
286 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
289 service = lp_service(self->ptr, PyString_AsString(name));
290 if (service == NULL) {
291 PyErr_SetString(PyExc_KeyError, "No such section");
294 return PyLoadparmService_FromService(service);
297 static PyMappingMethods py_lp_ctx_mapping = {
298 .mp_length = (lenfunc)py_lp_ctx_len,
299 .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
302 PyTypeObject PyLoadparmContext = {
303 .tp_name = "LoadParm",
304 .tp_basicsize = sizeof(py_talloc_Object),
305 .tp_dealloc = py_talloc_dealloc,
306 .tp_getset = py_lp_ctx_getset,
307 .tp_methods = py_lp_ctx_methods,
308 .tp_new = py_lp_ctx_new,
309 .tp_as_mapping = &py_lp_ctx_mapping,
310 .tp_flags = Py_TPFLAGS_DEFAULT,
313 PyTypeObject PyLoadparmService = {
314 .tp_name = "LoadparmService",
315 .tp_dealloc = py_talloc_dealloc,
316 .tp_basicsize = sizeof(py_talloc_Object),
317 .tp_flags = Py_TPFLAGS_DEFAULT,
320 _PUBLIC_ struct loadparm_context *lp_from_py_object(PyObject *py_obj)
322 struct loadparm_context *lp_ctx;
323 if (PyString_Check(py_obj)) {
324 lp_ctx = loadparm_init(NULL);
325 if (!lp_load(lp_ctx, PyString_AsString(py_obj))) {
327 PyErr_Format(PyExc_RuntimeError,
328 "Unable to load %s", PyString_AsString(py_obj));
334 if (py_obj == Py_None) {
335 lp_ctx = loadparm_init(NULL);
336 /* We're not checking that loading the file succeeded *on purpose */
337 lp_load_default(lp_ctx);
341 return PyLoadparmContext_AsLoadparmContext(py_obj);
344 struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx)
346 struct loadparm_context *ret;
347 ret = loadparm_init(mem_ctx);
348 if (!lp_load_default(ret))
357 if (PyType_Ready(&PyLoadparmContext) < 0)
360 m = Py_InitModule3("param", NULL, "Parsing and writing Samba configuration files.");
364 Py_INCREF(&PyLoadparmContext);
365 PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);