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"
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
;
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
;
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
) {
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;
64 if (type
== NULL
|| option
== NULL
) {
67 value
= lpcfg_get_parametric(lp_ctx
, service
, type
, option
);
71 return PyString_FromString(value
);
74 parm
= lpcfg_parm_struct(param_name
);
75 if (parm
== NULL
|| parm
->pclass
== P_GLOBAL
) {
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;
85 if (type
== NULL
|| option
== NULL
) {
88 value
= lpcfg_get_parametric(lp_ctx
, NULL
, type
, option
);
91 return PyString_FromString(value
);
93 /* its a global parameter */
94 parm
= lpcfg_parm_struct(param_name
);
98 parm_ptr
= lpcfg_parm_ptr(lp_ctx
, NULL
, parm
);
101 if (parm
== NULL
|| parm_ptr
== NULL
) {
105 /* construct and return the right type of python object */
106 switch (parm
->type
) {
109 return PyString_FromString(*(char **)parm_ptr
);
111 return PyBool_FromLong(*(bool *)parm_ptr
);
115 return PyLong_FromLong(*(int *)parm_ptr
);
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
);
127 const char **strlist
= *(const char ***)parm_ptr
;
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
]));
147 static PyObject
*py_lp_ctx_load(py_talloc_Object
*self
, PyObject
*args
)
151 if (!PyArg_ParseTuple(args
, "s", &filename
))
154 ret
= lpcfg_load(PyLoadparmContext_AsLoadparmContext(self
), filename
);
157 PyErr_Format(PyExc_RuntimeError
, "Unable to load file %s", filename
);
163 static PyObject
*py_lp_ctx_load_default(py_talloc_Object
*self
)
166 ret
= lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self
));
169 PyErr_SetString(PyExc_RuntimeError
, "Unable to load default file");
175 static PyObject
*py_lp_ctx_get(py_talloc_Object
*self
, PyObject
*args
)
178 char *section_name
= NULL
;
180 if (!PyArg_ParseTuple(args
, "s|z", ¶m_name
, §ion_name
))
183 ret
= py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self
), section_name
, param_name
);
189 static PyObject
*py_lp_ctx_is_myname(py_talloc_Object
*self
, PyObject
*args
)
192 if (!PyArg_ParseTuple(args
, "s", &name
))
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
)
201 if (!PyArg_ParseTuple(args
, "s", &name
))
204 return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self
), name
));
207 static PyObject
*py_lp_ctx_set(py_talloc_Object
*self
, PyObject
*args
)
211 if (!PyArg_ParseTuple(args
, "ss", &name
, &value
))
214 ret
= lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self
), name
, value
);
216 PyErr_SetString(PyExc_RuntimeError
, "Unable to set parameter");
223 static PyObject
*py_lp_ctx_private_path(py_talloc_Object
*self
, PyObject
*args
)
227 if (!PyArg_ParseTuple(args
, "s", &name
))
230 path
= private_path(NULL
, PyLoadparmContext_AsLoadparmContext(self
), name
);
231 ret
= PyString_FromString(path
);
237 static PyObject
*py_lp_ctx_services(py_talloc_Object
*self
)
239 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
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
)));
252 static PyObject
*py_lp_dump(PyObject
*self
, PyObject
*args
)
255 bool show_defaults
= false;
257 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
259 if (!PyArg_ParseTuple(args
, "O|b", &py_stream
, &show_defaults
))
262 f
= PyFile_AsFile(py_stream
);
264 PyErr_SetString(PyExc_TypeError
, "Not a file stream");
268 lpcfg_dump(lp_ctx
, f
, show_defaults
, lpcfg_numservices(lp_ctx
));
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)" },
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
)
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.") },
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);
330 ret
->talloc_ctx
= talloc_new(NULL
);
331 if (ret
->talloc_ctx
== 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");
351 service
= lpcfg_service(PyLoadparmContext_AsLoadparmContext(self
), PyString_AsString(name
));
352 if (service
== NULL
) {
353 PyErr_SetString(PyExc_KeyError
, "No such section");
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
)
377 bool show_defaults
= false;
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
,
387 f
= PyFile_AsFile(py_stream
);
389 PyErr_SetString(PyExc_TypeError
, "Not a file stream");
393 if (!PyObject_TypeCheck(py_default_service
, &PyLoadparmService
)) {
394 PyErr_SetNone(PyExc_TypeError
);
398 default_service
= PyLoadparmService_AsLoadparmService(py_default_service
);
400 lpcfg_dump_one(f
, show_defaults
, service
, default_service
);
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)" },
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." },
439 PyTypeObject
*talloc_type
= PyTalloc_GetObjectType();
440 if (talloc_type
== NULL
)
443 PyLoadparmContext
.tp_base
= talloc_type
;
444 PyLoadparmService
.tp_base
= talloc_type
;
446 if (PyType_Ready(&PyLoadparmContext
) < 0)
449 if (PyType_Ready(&PyLoadparmService
) < 0)
452 m
= Py_InitModule3("param", pyparam_methods
, "Parsing and writing Samba configuration files.");
456 Py_INCREF(&PyLoadparmContext
);
457 PyModule_AddObject(m
, "LoadParm", (PyObject
*)&PyLoadparmContext
);