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"
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 #ifndef Py_RETURN_NONE
36 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
39 #define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_type(obj, struct loadparm_context)
41 PyAPI_DATA(PyTypeObject
) PyLoadparmContext
;
42 PyAPI_DATA(PyTypeObject
) PyLoadparmService
;
44 PyObject
*PyLoadparmService_FromService(struct loadparm_service
*service
)
46 return py_talloc_reference(&PyLoadparmService
, service
);
49 static PyObject
*py_lp_ctx_get_helper(struct loadparm_context
*lp_ctx
, const char *service_name
, const char *param_name
)
51 struct parm_struct
*parm
= NULL
;
52 void *parm_ptr
= NULL
;
55 if (service_name
!= NULL
) {
56 struct loadparm_service
*service
;
57 /* its a share parameter */
58 service
= lp_service(lp_ctx
, service_name
);
59 if (service
== NULL
) {
62 if (strchr(param_name
, ':')) {
63 /* its a parametric option on a share */
64 const char *type
= talloc_strndup(lp_ctx
,
66 strcspn(param_name
, ":"));
67 const char *option
= strchr(param_name
, ':') + 1;
69 if (type
== NULL
|| option
== NULL
) {
72 value
= lp_get_parametric(lp_ctx
, service
, type
, option
);
76 return PyString_FromString(value
);
79 parm
= lp_parm_struct(param_name
);
80 if (parm
== NULL
|| parm
->pclass
== P_GLOBAL
) {
83 parm_ptr
= lp_parm_ptr(lp_ctx
, service
, parm
);
84 } else if (strchr(param_name
, ':')) {
85 /* its a global parametric option */
86 const char *type
= talloc_strndup(lp_ctx
,
87 param_name
, strcspn(param_name
, ":"));
88 const char *option
= strchr(param_name
, ':') + 1;
90 if (type
== NULL
|| option
== NULL
) {
93 value
= lp_get_parametric(lp_ctx
, NULL
, type
, option
);
96 return PyString_FromString(value
);
98 /* its a global parameter */
99 parm
= lp_parm_struct(param_name
);
103 parm_ptr
= lp_parm_ptr(lp_ctx
, NULL
, parm
);
106 if (parm
== NULL
|| parm_ptr
== NULL
) {
110 /* construct and return the right type of python object */
111 switch (parm
->type
) {
114 return PyString_FromString(*(char **)parm_ptr
);
116 return PyBool_FromLong(*(bool *)parm_ptr
);
120 return PyLong_FromLong(*(int *)parm_ptr
);
122 for (i
=0; parm
->enum_list
[i
].name
; i
++) {
123 if (*(int *)parm_ptr
== parm
->enum_list
[i
].value
) {
124 return PyString_FromString(parm
->enum_list
[i
].name
);
131 const char **strlist
= *(const char ***)parm_ptr
;
134 if(strlist
== NULL
) {
135 return PyList_New(0);
138 pylist
= PyList_New(str_list_length(strlist
));
139 for (j
= 0; strlist
[j
]; j
++)
140 PyList_SetItem(pylist
, j
,
141 PyString_FromString(strlist
[j
]));
151 static PyObject
*py_lp_ctx_load(py_talloc_Object
*self
, PyObject
*args
)
155 if (!PyArg_ParseTuple(args
, "s", &filename
))
158 ret
= lp_load(PyLoadparmContext_AsLoadparmContext(self
), filename
);
161 PyErr_Format(PyExc_RuntimeError
, "Unable to load file %s", filename
);
167 static PyObject
*py_lp_ctx_load_default(py_talloc_Object
*self
)
170 ret
= lp_load_default(PyLoadparmContext_AsLoadparmContext(self
));
173 PyErr_SetString(PyExc_RuntimeError
, "Unable to load default file");
179 static PyObject
*py_lp_ctx_get(py_talloc_Object
*self
, PyObject
*args
)
182 char *section_name
= NULL
;
184 if (!PyArg_ParseTuple(args
, "s|s", ¶m_name
, §ion_name
))
187 ret
= py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self
), section_name
, param_name
);
193 static PyObject
*py_lp_ctx_is_myname(py_talloc_Object
*self
, PyObject
*args
)
196 if (!PyArg_ParseTuple(args
, "s", &name
))
199 return PyBool_FromLong(lp_is_myname(PyLoadparmContext_AsLoadparmContext(self
), name
));
202 static PyObject
*py_lp_ctx_is_mydomain(py_talloc_Object
*self
, PyObject
*args
)
205 if (!PyArg_ParseTuple(args
, "s", &name
))
208 return PyBool_FromLong(lp_is_mydomain(PyLoadparmContext_AsLoadparmContext(self
), name
));
211 static PyObject
*py_lp_ctx_set(py_talloc_Object
*self
, PyObject
*args
)
215 if (!PyArg_ParseTuple(args
, "ss", &name
, &value
))
218 ret
= lp_set_cmdline(PyLoadparmContext_AsLoadparmContext(self
), name
, value
);
220 PyErr_SetString(PyExc_RuntimeError
, "Unable to set parameter");
227 static PyObject
*py_lp_ctx_private_path(py_talloc_Object
*self
, PyObject
*args
)
231 if (!PyArg_ParseTuple(args
, "s", &name
))
234 path
= private_path(NULL
, PyLoadparmContext_AsLoadparmContext(self
), name
);
235 ret
= PyString_FromString(path
);
241 static PyObject
*py_lp_ctx_services(py_talloc_Object
*self
)
243 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
246 ret
= PyList_New(lp_numservices(lp_ctx
));
247 for (i
= 0; i
< lp_numservices(lp_ctx
); i
++) {
248 struct loadparm_service
*service
= lp_servicebynum(lp_ctx
, i
);
249 if (service
!= NULL
) {
250 PyList_SetItem(ret
, i
, PyString_FromString(lp_servicename(service
)));
256 static PyMethodDef py_lp_ctx_methods
[] = {
257 { "load", (PyCFunction
)py_lp_ctx_load
, METH_VARARGS
,
258 "S.load(filename) -> None\n"
259 "Load specified file." },
260 { "load_default", (PyCFunction
)py_lp_ctx_load_default
, METH_NOARGS
,
261 "S.load_default() -> None\n"
262 "Load default smb.conf file." },
263 { "is_myname", (PyCFunction
)py_lp_ctx_is_myname
, METH_VARARGS
,
264 "S.is_myname(name) -> bool\n"
265 "Check whether the specified name matches one of our netbios names." },
266 { "is_mydomain", (PyCFunction
)py_lp_ctx_is_mydomain
, METH_VARARGS
,
267 "S.is_mydomain(name) -> bool\n"
268 "Check whether the specified name matches our domain name." },
269 { "get", (PyCFunction
)py_lp_ctx_get
, METH_VARARGS
,
270 "S.get(name, service_name) -> value\n"
271 "Find specified parameter." },
272 { "set", (PyCFunction
)py_lp_ctx_set
, METH_VARARGS
,
273 "S.set(name, value) -> bool\n"
274 "Change a parameter." },
275 { "private_path", (PyCFunction
)py_lp_ctx_private_path
, METH_VARARGS
,
276 "S.private_path(name) -> path\n" },
277 { "services", (PyCFunction
)py_lp_ctx_services
, METH_NOARGS
,
278 "S.services() -> list" },
282 static PyObject
*py_lp_ctx_default_service(py_talloc_Object
*self
, void *closure
)
284 return PyLoadparmService_FromService(lp_default_service(PyLoadparmContext_AsLoadparmContext(self
)));
287 static PyObject
*py_lp_ctx_config_file(py_talloc_Object
*self
, void *closure
)
289 const char *configfile
= lp_configfile(PyLoadparmContext_AsLoadparmContext(self
));
290 if (configfile
== NULL
)
293 return PyString_FromString(configfile
);
296 static PyGetSetDef py_lp_ctx_getset
[] = {
297 { discard_const_p(char, "default_service"), (getter
)py_lp_ctx_default_service
, NULL
, NULL
},
298 { discard_const_p(char, "configfile"), (getter
)py_lp_ctx_config_file
, NULL
,
299 discard_const_p(char, "Name of last config file that was loaded.") },
303 static PyObject
*py_lp_ctx_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
305 py_talloc_Object
*ret
= (py_talloc_Object
*)type
->tp_alloc(type
, 0);
310 ret
->talloc_ctx
= talloc_new(NULL
);
311 if (ret
->talloc_ctx
== NULL
) {
315 ret
->ptr
= loadparm_init(ret
->talloc_ctx
);
316 return (PyObject
*)ret
;
319 static Py_ssize_t
py_lp_ctx_len(py_talloc_Object
*self
)
321 return lp_numservices(PyLoadparmContext_AsLoadparmContext(self
));
324 static PyObject
*py_lp_ctx_getitem(py_talloc_Object
*self
, PyObject
*name
)
326 struct loadparm_service
*service
;
327 if (!PyString_Check(name
)) {
328 PyErr_SetString(PyExc_TypeError
, "Only string subscripts are supported");
331 service
= lp_service(PyLoadparmContext_AsLoadparmContext(self
), PyString_AsString(name
));
332 if (service
== NULL
) {
333 PyErr_SetString(PyExc_KeyError
, "No such section");
336 return PyLoadparmService_FromService(service
);
339 static PyMappingMethods py_lp_ctx_mapping
= {
340 .mp_length
= (lenfunc
)py_lp_ctx_len
,
341 .mp_subscript
= (binaryfunc
)py_lp_ctx_getitem
,
344 PyTypeObject PyLoadparmContext
= {
345 .tp_name
= "LoadParm",
346 .tp_basicsize
= sizeof(py_talloc_Object
),
347 .tp_dealloc
= py_talloc_dealloc
,
348 .tp_getset
= py_lp_ctx_getset
,
349 .tp_methods
= py_lp_ctx_methods
,
350 .tp_new
= py_lp_ctx_new
,
351 .tp_as_mapping
= &py_lp_ctx_mapping
,
352 .tp_flags
= Py_TPFLAGS_DEFAULT
,
355 PyTypeObject PyLoadparmService
= {
356 .tp_name
= "LoadparmService",
357 .tp_dealloc
= py_talloc_dealloc
,
358 .tp_basicsize
= sizeof(py_talloc_Object
),
359 .tp_flags
= Py_TPFLAGS_DEFAULT
,
362 static PyObject
*py_default_path(PyObject
*self
)
364 return PyString_FromString(lp_default_path());
367 static PyMethodDef pyparam_methods
[] = {
368 { "default_path", (PyCFunction
)py_default_path
, METH_NOARGS
,
369 "Returns the default smb.conf path." },
377 if (PyType_Ready(&PyLoadparmContext
) < 0)
380 m
= Py_InitModule3("param", pyparam_methods
, "Parsing and writing Samba configuration files.");
384 Py_INCREF(&PyLoadparmContext
);
385 PyModule_AddObject(m
, "LoadParm", (PyObject
*)&PyLoadparmContext
);