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"
25 #include "dynconfig/dynconfig.h"
29 #define PyLoadparmContext_AsLoadparmContext(obj) pytalloc_get_type(obj, struct loadparm_context)
30 #define PyLoadparmService_AsLoadparmService(obj) pytalloc_get_type(obj, struct loadparm_service)
32 extern PyTypeObject PyLoadparmContext
;
33 extern PyTypeObject PyLoadparmService
;
35 static PyObject
*PyLoadparmService_FromService(struct loadparm_service
*service
)
37 return pytalloc_reference(&PyLoadparmService
, service
);
40 static PyObject
*py_lp_ctx_get_helper(struct loadparm_context
*lp_ctx
, const char *service_name
, const char *param_name
)
42 struct parm_struct
*parm
= NULL
;
43 void *parm_ptr
= NULL
;
46 if (service_name
!= NULL
&& strwicmp(service_name
, GLOBAL_NAME
) &&
47 strwicmp(service_name
, GLOBAL_NAME2
)) {
48 struct loadparm_service
*service
;
49 /* its a share parameter */
50 service
= lpcfg_service(lp_ctx
, service_name
);
51 if (service
== NULL
) {
54 if (strchr(param_name
, ':')) {
55 /* its a parametric option on a share */
56 const char *type
= talloc_strndup(lp_ctx
, param_name
,
57 strcspn(param_name
, ":"));
58 const char *option
= strchr(param_name
, ':') + 1;
60 if (type
== NULL
|| option
== NULL
) {
63 value
= lpcfg_get_parametric(lp_ctx
, service
, type
, option
);
67 return PyString_FromString(value
);
70 parm
= lpcfg_parm_struct(lp_ctx
, param_name
);
71 if (parm
== NULL
|| parm
->p_class
== P_GLOBAL
) {
74 parm_ptr
= lpcfg_parm_ptr(lp_ctx
, service
, parm
);
75 } else if (strchr(param_name
, ':')) {
76 /* its a global parametric option */
77 const char *type
= talloc_strndup(lp_ctx
,
78 param_name
, strcspn(param_name
, ":"));
79 const char *option
= strchr(param_name
, ':') + 1;
81 if (type
== NULL
|| option
== NULL
) {
84 value
= lpcfg_get_parametric(lp_ctx
, NULL
, type
, option
);
87 return PyString_FromString(value
);
89 /* its a global parameter */
90 parm
= lpcfg_parm_struct(lp_ctx
, param_name
);
94 parm_ptr
= lpcfg_parm_ptr(lp_ctx
, NULL
, parm
);
97 if (parm
== NULL
|| parm_ptr
== NULL
) {
101 /* construct and return the right type of python object */
102 switch (parm
->type
) {
104 return PyString_FromFormat("%c", *(char *)parm_ptr
);
107 return PyString_FromString(*(char **)parm_ptr
);
109 return PyBool_FromLong(*(bool *)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
]));
145 static PyObject
*py_lp_ctx_load(PyObject
*self
, PyObject
*args
)
149 if (!PyArg_ParseTuple(args
, "s", &filename
))
152 ret
= lpcfg_load(PyLoadparmContext_AsLoadparmContext(self
), filename
);
155 PyErr_Format(PyExc_RuntimeError
, "Unable to load file %s", filename
);
161 static PyObject
*py_lp_ctx_load_default(PyObject
*self
, PyObject
*unused
)
164 ret
= lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self
));
167 PyErr_SetString(PyExc_RuntimeError
, "Unable to load default file");
173 static PyObject
*py_lp_ctx_get(PyObject
*self
, PyObject
*args
)
176 char *section_name
= NULL
;
178 if (!PyArg_ParseTuple(args
, "s|z", ¶m_name
, §ion_name
))
181 ret
= py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self
), section_name
, param_name
);
187 static PyObject
*py_lp_ctx_is_myname(PyObject
*self
, PyObject
*args
)
190 if (!PyArg_ParseTuple(args
, "s", &name
))
193 return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self
), name
));
196 static PyObject
*py_lp_ctx_is_mydomain(PyObject
*self
, PyObject
*args
)
199 if (!PyArg_ParseTuple(args
, "s", &name
))
202 return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self
), name
));
205 static PyObject
*py_lp_ctx_set(PyObject
*self
, PyObject
*args
)
209 if (!PyArg_ParseTuple(args
, "ss", &name
, &value
))
212 ret
= lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self
), name
, value
);
214 PyErr_SetString(PyExc_RuntimeError
, "Unable to set parameter");
221 static PyObject
*py_lp_ctx_private_path(PyObject
*self
, PyObject
*args
)
225 if (!PyArg_ParseTuple(args
, "s", &name
))
228 path
= lpcfg_private_path(NULL
, PyLoadparmContext_AsLoadparmContext(self
), name
);
229 ret
= PyString_FromString(path
);
235 static PyObject
*py_lp_ctx_services(PyObject
*self
, PyObject
*unused
)
237 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
240 ret
= PyList_New(lpcfg_numservices(lp_ctx
));
241 for (i
= 0; i
< lpcfg_numservices(lp_ctx
); i
++) {
242 struct loadparm_service
*service
= lpcfg_servicebynum(lp_ctx
, i
);
243 if (service
!= NULL
) {
244 PyList_SetItem(ret
, i
, PyString_FromString(lpcfg_servicename(service
)));
250 static PyObject
*py_lp_ctx_server_role(PyObject
*self
, PyObject
*unused
)
252 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
254 const char *role_str
;
256 role
= lpcfg_server_role(lp_ctx
);
257 role_str
= server_role_str(role
);
259 return PyString_FromString(role_str
);
262 static PyObject
*py_lp_dump(PyObject
*self
, PyObject
*args
)
265 bool show_defaults
= false;
267 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
269 if (!PyArg_ParseTuple(args
, "O|b", &py_stream
, &show_defaults
))
272 f
= PyFile_AsFile(py_stream
);
277 lpcfg_dump(lp_ctx
, f
, show_defaults
, lpcfg_numservices(lp_ctx
));
282 static PyObject
*py_lp_dump_a_parameter(PyObject
*self
, PyObject
*args
)
286 const char *section_name
= NULL
;
288 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
289 struct loadparm_service
*service
;
292 if (!PyArg_ParseTuple(args
, "Os|z", &py_stream
, ¶m_name
, §ion_name
))
295 f
= PyFile_AsFile(py_stream
);
300 if (section_name
!= NULL
&& strwicmp(section_name
, GLOBAL_NAME
) &&
301 strwicmp(section_name
, GLOBAL_NAME2
)) {
302 /* it's a share parameter */
303 service
= lpcfg_service(lp_ctx
, section_name
);
304 if (service
== NULL
) {
305 PyErr_Format(PyExc_RuntimeError
, "Unknown section %s", section_name
);
311 section_name
= "global";
314 ret
= lpcfg_dump_a_parameter(lp_ctx
, service
, param_name
, f
);
317 PyErr_Format(PyExc_RuntimeError
, "Parameter %s unknown for section %s", param_name
, section_name
);
325 static PyObject
*py_lp_log_level(PyObject
*self
, PyObject
*unused
)
327 int ret
= DEBUGLEVEL_CLASS
[DBGC_CLASS
];
328 return PyInt_FromLong(ret
);
332 static PyObject
*py_samdb_url(PyObject
*self
, PyObject
*unused
)
334 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
335 return PyString_FromFormat("tdb://%s/sam.ldb", lpcfg_private_dir(lp_ctx
));
339 static PyMethodDef py_lp_ctx_methods
[] = {
340 { "load", py_lp_ctx_load
, METH_VARARGS
,
341 "S.load(filename) -> None\n"
342 "Load specified file." },
343 { "load_default", py_lp_ctx_load_default
, METH_NOARGS
,
344 "S.load_default() -> None\n"
345 "Load default smb.conf file." },
346 { "is_myname", py_lp_ctx_is_myname
, METH_VARARGS
,
347 "S.is_myname(name) -> bool\n"
348 "Check whether the specified name matches one of our netbios names." },
349 { "is_mydomain", py_lp_ctx_is_mydomain
, METH_VARARGS
,
350 "S.is_mydomain(name) -> bool\n"
351 "Check whether the specified name matches our domain name." },
352 { "get", py_lp_ctx_get
, METH_VARARGS
,
353 "S.get(name, service_name) -> value\n"
354 "Find specified parameter." },
355 { "set", py_lp_ctx_set
, METH_VARARGS
,
356 "S.set(name, value) -> bool\n"
357 "Change a parameter." },
358 { "private_path", py_lp_ctx_private_path
, METH_VARARGS
,
359 "S.private_path(name) -> path\n" },
360 { "services", py_lp_ctx_services
, METH_NOARGS
,
361 "S.services() -> list" },
362 { "server_role", py_lp_ctx_server_role
, METH_NOARGS
,
363 "S.server_role() -> value\n"
364 "Get the server role." },
365 { "dump", py_lp_dump
, METH_VARARGS
,
366 "S.dump(stream, show_defaults=False)" },
367 { "log_level", py_lp_log_level
, METH_NOARGS
,
368 "S.log_level() -> int\n Get the active log level" },
369 { "dump_a_parameter", py_lp_dump_a_parameter
, METH_VARARGS
,
370 "S.dump_a_parameter(stream, name, service_name)" },
371 { "samdb_url", py_samdb_url
, METH_NOARGS
,
372 "S.samdb_url() -> string\n"
373 "Returns the current URL for sam.ldb." },
377 static PyObject
*py_lp_ctx_default_service(PyObject
*self
, void *closure
)
379 return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self
)));
382 static PyObject
*py_lp_ctx_config_file(PyObject
*self
, void *closure
)
384 const char *configfile
= lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self
));
385 if (configfile
== NULL
)
388 return PyString_FromString(configfile
);
391 static PyGetSetDef py_lp_ctx_getset
[] = {
392 { discard_const_p(char, "default_service"), (getter
)py_lp_ctx_default_service
, NULL
, NULL
},
393 { discard_const_p(char, "configfile"), (getter
)py_lp_ctx_config_file
, NULL
,
394 discard_const_p(char, "Name of last config file that was loaded.") },
398 static PyObject
*py_lp_ctx_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
400 return pytalloc_reference(type
, loadparm_init_global(false));
403 static Py_ssize_t
py_lp_ctx_len(PyObject
*self
)
405 return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self
));
408 static PyObject
*py_lp_ctx_getitem(PyObject
*self
, PyObject
*name
)
410 struct loadparm_service
*service
;
411 if (!PyString_Check(name
)) {
412 PyErr_SetString(PyExc_TypeError
, "Only string subscripts are supported");
415 service
= lpcfg_service(PyLoadparmContext_AsLoadparmContext(self
), PyString_AsString(name
));
416 if (service
== NULL
) {
417 PyErr_SetString(PyExc_KeyError
, "No such section");
420 return PyLoadparmService_FromService(service
);
423 static PyMappingMethods py_lp_ctx_mapping
= {
424 .mp_length
= (lenfunc
)py_lp_ctx_len
,
425 .mp_subscript
= (binaryfunc
)py_lp_ctx_getitem
,
428 PyTypeObject PyLoadparmContext
= {
429 .tp_name
= "param.LoadParm",
430 .tp_getset
= py_lp_ctx_getset
,
431 .tp_methods
= py_lp_ctx_methods
,
432 .tp_new
= py_lp_ctx_new
,
433 .tp_as_mapping
= &py_lp_ctx_mapping
,
434 .tp_flags
= Py_TPFLAGS_DEFAULT
,
437 static PyObject
*py_lp_service_dump(PyObject
*self
, PyObject
*args
)
440 bool show_defaults
= false;
442 struct loadparm_service
*service
= PyLoadparmService_AsLoadparmService(self
);
443 struct loadparm_service
*default_service
;
444 PyObject
*py_default_service
;
446 if (!PyArg_ParseTuple(args
, "OO|b", &py_stream
, &py_default_service
,
450 f
= PyFile_AsFile(py_stream
);
455 if (!PyObject_TypeCheck(py_default_service
, &PyLoadparmService
)) {
456 PyErr_SetNone(PyExc_TypeError
);
460 default_service
= PyLoadparmService_AsLoadparmService(py_default_service
);
462 lpcfg_dump_one(f
, show_defaults
, service
, default_service
);
467 static PyMethodDef py_lp_service_methods
[] = {
468 { "dump", (PyCFunction
)py_lp_service_dump
, METH_VARARGS
,
469 "S.dump(f, default_service, show_defaults=False)" },
473 PyTypeObject PyLoadparmService
= {
474 .tp_name
= "param.LoadparmService",
475 .tp_methods
= py_lp_service_methods
,
476 .tp_flags
= Py_TPFLAGS_DEFAULT
,
479 static PyObject
*py_default_path(PyObject
*self
)
481 return PyString_FromString(lp_default_path());
484 static PyObject
*py_setup_dir(PyObject
*self
)
486 return PyString_FromString(dyn_SETUPDIR
);
489 static PyObject
*py_modules_dir(PyObject
*self
)
491 return PyString_FromString(dyn_MODULESDIR
);
494 static PyObject
*py_bin_dir(PyObject
*self
)
496 return PyString_FromString(dyn_BINDIR
);
499 static PyObject
*py_sbin_dir(PyObject
*self
)
501 return PyString_FromString(dyn_SBINDIR
);
504 static PyMethodDef pyparam_methods
[] = {
505 { "default_path", (PyCFunction
)py_default_path
, METH_NOARGS
,
506 "Returns the default smb.conf path." },
507 { "setup_dir", (PyCFunction
)py_setup_dir
, METH_NOARGS
,
508 "Returns the compiled in location of provision tempates." },
509 { "modules_dir", (PyCFunction
)py_modules_dir
, METH_NOARGS
,
510 "Returns the compiled in location of modules." },
511 { "bin_dir", (PyCFunction
)py_bin_dir
, METH_NOARGS
,
512 "Returns the compiled in BINDIR." },
513 { "sbin_dir", (PyCFunction
)py_sbin_dir
, METH_NOARGS
,
514 "Returns the compiled in SBINDIR." },
522 if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmContext
) < 0)
525 if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmService
) < 0)
528 m
= Py_InitModule3("param", pyparam_methods
, "Parsing and writing Samba configuration files.");
532 Py_INCREF(&PyLoadparmContext
);
533 PyModule_AddObject(m
, "LoadParm", (PyObject
*)&PyLoadparmContext
);