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"
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_type(obj, struct loadparm_context)
36 #define PyLoadparmService_AsLoadparmService(obj) py_talloc_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 py_talloc_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
) {
111 return PyString_FromString(*(char **)parm_ptr
);
113 return PyBool_FromLong(*(bool *)parm_ptr
);
117 return PyLong_FromLong(*(int *)parm_ptr
);
119 for (i
=0; parm
->enum_list
[i
].name
; i
++) {
120 if (*(int *)parm_ptr
== parm
->enum_list
[i
].value
) {
121 return PyString_FromString(parm
->enum_list
[i
].name
);
129 const char **strlist
= *(const char ***)parm_ptr
;
132 if(strlist
== NULL
) {
133 return PyList_New(0);
136 pylist
= PyList_New(str_list_length(strlist
));
137 for (j
= 0; strlist
[j
]; j
++)
138 PyList_SetItem(pylist
, j
,
139 PyString_FromString(strlist
[j
]));
149 static PyObject
*py_lp_ctx_load(py_talloc_Object
*self
, PyObject
*args
)
153 if (!PyArg_ParseTuple(args
, "s", &filename
))
156 ret
= lpcfg_load(PyLoadparmContext_AsLoadparmContext(self
), filename
);
159 PyErr_Format(PyExc_RuntimeError
, "Unable to load file %s", filename
);
165 static PyObject
*py_lp_ctx_load_default(py_talloc_Object
*self
)
168 ret
= lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self
));
171 PyErr_SetString(PyExc_RuntimeError
, "Unable to load default file");
177 static PyObject
*py_lp_ctx_get(py_talloc_Object
*self
, PyObject
*args
)
180 char *section_name
= NULL
;
182 if (!PyArg_ParseTuple(args
, "s|z", ¶m_name
, §ion_name
))
185 ret
= py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self
), section_name
, param_name
);
191 static PyObject
*py_lp_ctx_is_myname(py_talloc_Object
*self
, PyObject
*args
)
194 if (!PyArg_ParseTuple(args
, "s", &name
))
197 return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self
), name
));
200 static PyObject
*py_lp_ctx_is_mydomain(py_talloc_Object
*self
, PyObject
*args
)
203 if (!PyArg_ParseTuple(args
, "s", &name
))
206 return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self
), name
));
209 static PyObject
*py_lp_ctx_set(py_talloc_Object
*self
, PyObject
*args
)
213 if (!PyArg_ParseTuple(args
, "ss", &name
, &value
))
216 ret
= lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self
), name
, value
);
218 PyErr_SetString(PyExc_RuntimeError
, "Unable to set parameter");
225 static PyObject
*py_lp_ctx_private_path(py_talloc_Object
*self
, PyObject
*args
)
229 if (!PyArg_ParseTuple(args
, "s", &name
))
232 path
= lpcfg_private_path(NULL
, PyLoadparmContext_AsLoadparmContext(self
), name
);
233 ret
= PyString_FromString(path
);
239 static PyObject
*py_lp_ctx_services(py_talloc_Object
*self
)
241 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
244 ret
= PyList_New(lpcfg_numservices(lp_ctx
));
245 for (i
= 0; i
< lpcfg_numservices(lp_ctx
); i
++) {
246 struct loadparm_service
*service
= lpcfg_servicebynum(lp_ctx
, i
);
247 if (service
!= NULL
) {
248 PyList_SetItem(ret
, i
, PyString_FromString(lpcfg_servicename(service
)));
254 static PyObject
*py_lp_dump(PyObject
*self
, PyObject
*args
)
257 bool show_defaults
= false;
259 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
261 if (!PyArg_ParseTuple(args
, "O|b", &py_stream
, &show_defaults
))
264 f
= PyFile_AsFile(py_stream
);
266 PyErr_SetString(PyExc_TypeError
, "Not a file stream");
270 lpcfg_dump(lp_ctx
, f
, show_defaults
, lpcfg_numservices(lp_ctx
));
275 static PyObject
*py_samdb_url(PyObject
*self
)
277 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
278 return PyString_FromFormat("tdb://%s/sam.ldb", lpcfg_private_dir(lp_ctx
));
282 static PyMethodDef py_lp_ctx_methods
[] = {
283 { "load", (PyCFunction
)py_lp_ctx_load
, METH_VARARGS
,
284 "S.load(filename) -> None\n"
285 "Load specified file." },
286 { "load_default", (PyCFunction
)py_lp_ctx_load_default
, METH_NOARGS
,
287 "S.load_default() -> None\n"
288 "Load default smb.conf file." },
289 { "is_myname", (PyCFunction
)py_lp_ctx_is_myname
, METH_VARARGS
,
290 "S.is_myname(name) -> bool\n"
291 "Check whether the specified name matches one of our netbios names." },
292 { "is_mydomain", (PyCFunction
)py_lp_ctx_is_mydomain
, METH_VARARGS
,
293 "S.is_mydomain(name) -> bool\n"
294 "Check whether the specified name matches our domain name." },
295 { "get", (PyCFunction
)py_lp_ctx_get
, METH_VARARGS
,
296 "S.get(name, service_name) -> value\n"
297 "Find specified parameter." },
298 { "set", (PyCFunction
)py_lp_ctx_set
, METH_VARARGS
,
299 "S.set(name, value) -> bool\n"
300 "Change a parameter." },
301 { "private_path", (PyCFunction
)py_lp_ctx_private_path
, METH_VARARGS
,
302 "S.private_path(name) -> path\n" },
303 { "services", (PyCFunction
)py_lp_ctx_services
, METH_NOARGS
,
304 "S.services() -> list" },
305 { "dump", (PyCFunction
)py_lp_dump
, METH_VARARGS
,
306 "S.dump(stream, show_defaults=False)" },
307 { "samdb_url", (PyCFunction
)py_samdb_url
, METH_NOARGS
,
308 "S.samdb_url() -> string\n"
309 "Returns the current URL for sam.ldb." },
313 static PyObject
*py_lp_ctx_default_service(py_talloc_Object
*self
, void *closure
)
315 return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self
)));
318 static PyObject
*py_lp_ctx_config_file(py_talloc_Object
*self
, void *closure
)
320 const char *configfile
= lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self
));
321 if (configfile
== NULL
)
324 return PyString_FromString(configfile
);
327 static PyGetSetDef py_lp_ctx_getset
[] = {
328 { discard_const_p(char, "default_service"), (getter
)py_lp_ctx_default_service
, NULL
, NULL
},
329 { discard_const_p(char, "configfile"), (getter
)py_lp_ctx_config_file
, NULL
,
330 discard_const_p(char, "Name of last config file that was loaded.") },
334 static PyObject
*py_lp_ctx_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
336 py_talloc_Object
*ret
= (py_talloc_Object
*)type
->tp_alloc(type
, 0);
341 ret
->talloc_ctx
= talloc_new(NULL
);
342 if (ret
->talloc_ctx
== NULL
) {
346 ret
->ptr
= loadparm_init_global(false);
347 if (ret
->ptr
== NULL
) {
351 return (PyObject
*)ret
;
354 static Py_ssize_t
py_lp_ctx_len(py_talloc_Object
*self
)
356 return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self
));
359 static PyObject
*py_lp_ctx_getitem(py_talloc_Object
*self
, PyObject
*name
)
361 struct loadparm_service
*service
;
362 if (!PyString_Check(name
)) {
363 PyErr_SetString(PyExc_TypeError
, "Only string subscripts are supported");
366 service
= lpcfg_service(PyLoadparmContext_AsLoadparmContext(self
), PyString_AsString(name
));
367 if (service
== NULL
) {
368 PyErr_SetString(PyExc_KeyError
, "No such section");
371 return PyLoadparmService_FromService(service
);
374 static PyMappingMethods py_lp_ctx_mapping
= {
375 .mp_length
= (lenfunc
)py_lp_ctx_len
,
376 .mp_subscript
= (binaryfunc
)py_lp_ctx_getitem
,
379 PyTypeObject PyLoadparmContext
= {
380 .tp_name
= "LoadParm",
381 .tp_basicsize
= sizeof(py_talloc_Object
),
382 .tp_getset
= py_lp_ctx_getset
,
383 .tp_methods
= py_lp_ctx_methods
,
384 .tp_new
= py_lp_ctx_new
,
385 .tp_as_mapping
= &py_lp_ctx_mapping
,
386 .tp_flags
= Py_TPFLAGS_DEFAULT
,
389 static PyObject
*py_lp_service_dump(PyObject
*self
, PyObject
*args
)
392 bool show_defaults
= false;
394 struct loadparm_service
*service
= PyLoadparmService_AsLoadparmService(self
);
395 struct loadparm_service
*default_service
;
396 PyObject
*py_default_service
;
398 if (!PyArg_ParseTuple(args
, "OO|b", &py_stream
, &py_default_service
,
402 f
= PyFile_AsFile(py_stream
);
404 PyErr_SetString(PyExc_TypeError
, "Not a file stream");
408 if (!PyObject_TypeCheck(py_default_service
, &PyLoadparmService
)) {
409 PyErr_SetNone(PyExc_TypeError
);
413 default_service
= PyLoadparmService_AsLoadparmService(py_default_service
);
415 lpcfg_dump_one(f
, show_defaults
, service
, default_service
);
420 static PyMethodDef py_lp_service_methods
[] = {
421 { "dump", (PyCFunction
)py_lp_service_dump
, METH_VARARGS
,
422 "S.dump(f, default_service, show_defaults=False)" },
426 PyTypeObject PyLoadparmService
= {
427 .tp_name
= "LoadparmService",
428 .tp_basicsize
= sizeof(py_talloc_Object
),
429 .tp_methods
= py_lp_service_methods
,
430 .tp_flags
= Py_TPFLAGS_DEFAULT
,
433 static PyObject
*py_default_path(PyObject
*self
)
435 return PyString_FromString(lp_default_path());
438 static PyObject
*py_setup_dir(PyObject
*self
)
440 return PyString_FromString(dyn_SETUPDIR
);
443 static PyObject
*py_modules_dir(PyObject
*self
)
445 return PyString_FromString(dyn_MODULESDIR
);
448 static PyMethodDef pyparam_methods
[] = {
449 { "default_path", (PyCFunction
)py_default_path
, METH_NOARGS
,
450 "Returns the default smb.conf path." },
451 { "setup_dir", (PyCFunction
)py_setup_dir
, METH_NOARGS
,
452 "Returns the compiled in location of provision tempates." },
453 { "modules_dir", (PyCFunction
)py_modules_dir
, METH_NOARGS
,
454 "Returns the compiled in location of modules." },
461 PyTypeObject
*talloc_type
= PyTalloc_GetObjectType();
462 if (talloc_type
== NULL
)
465 PyLoadparmContext
.tp_base
= talloc_type
;
466 PyLoadparmService
.tp_base
= talloc_type
;
468 if (PyType_Ready(&PyLoadparmContext
) < 0)
471 if (PyType_Ready(&PyLoadparmService
) < 0)
474 m
= Py_InitModule3("param", pyparam_methods
, "Parsing and writing Samba configuration files.");
478 Py_INCREF(&PyLoadparmContext
);
479 PyModule_AddObject(m
, "LoadParm", (PyObject
*)&PyLoadparmContext
);