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/>.
20 #include "lib/replace/system/python.h"
21 #include "python/py3compat.h"
23 #include "param/param.h"
24 #include "param/loadparm.h"
26 #include "dynconfig/dynconfig.h"
28 #define PyLoadparmContext_AsLoadparmContext(obj) pytalloc_get_type(obj, struct loadparm_context)
29 #define PyLoadparmService_AsLoadparmService(obj) pytalloc_get_type(obj, struct loadparm_service)
31 extern PyTypeObject PyLoadparmContext
;
32 extern PyTypeObject PyLoadparmService
;
34 static PyObject
*PyLoadparmService_FromService(struct loadparm_service
*service
)
36 return pytalloc_reference(&PyLoadparmService
, service
);
39 static PyObject
*py_lp_ctx_get_helper(struct loadparm_context
*lp_ctx
, const char *service_name
, const char *param_name
)
41 struct parm_struct
*parm
= NULL
;
42 void *parm_ptr
= NULL
;
45 if (service_name
!= NULL
&& strwicmp(service_name
, GLOBAL_NAME
) &&
46 strwicmp(service_name
, GLOBAL_NAME2
)) {
47 struct loadparm_service
*service
;
48 /* its a share parameter */
49 service
= lpcfg_service(lp_ctx
, service_name
);
50 if (service
== NULL
) {
53 if (strchr(param_name
, ':')) {
54 /* its a parametric option on a share */
55 const char *type
= talloc_strndup(lp_ctx
, param_name
,
56 strcspn(param_name
, ":"));
57 const char *option
= strchr(param_name
, ':') + 1;
59 if (type
== NULL
|| option
== NULL
) {
62 value
= lpcfg_get_parametric(lp_ctx
, service
, type
, option
);
66 return PyUnicode_FromString(value
);
69 parm
= lpcfg_parm_struct(lp_ctx
, param_name
);
70 if (parm
== NULL
|| parm
->p_class
== P_GLOBAL
) {
73 parm_ptr
= lpcfg_parm_ptr(lp_ctx
, service
, parm
);
74 } else if (strchr(param_name
, ':')) {
75 /* its a global parametric option */
76 const char *type
= talloc_strndup(lp_ctx
,
77 param_name
, strcspn(param_name
, ":"));
78 const char *option
= strchr(param_name
, ':') + 1;
80 if (type
== NULL
|| option
== NULL
) {
83 value
= lpcfg_get_parametric(lp_ctx
, NULL
, type
, option
);
86 return PyUnicode_FromString(value
);
88 /* its a global parameter */
89 parm
= lpcfg_parm_struct(lp_ctx
, param_name
);
93 parm_ptr
= lpcfg_parm_ptr(lp_ctx
, NULL
, parm
);
96 if (parm
== NULL
|| parm_ptr
== NULL
) {
100 /* construct and return the right type of python object */
101 switch (parm
->type
) {
103 return PyUnicode_FromFormat("%c", *(char *)parm_ptr
);
106 return PyUnicode_FromString(*(char **)parm_ptr
);
108 return PyBool_FromLong(*(bool *)parm_ptr
);
110 return PyBool_FromLong(!(*(bool *)parm_ptr
));
114 return PyLong_FromLong(*(int *)parm_ptr
);
116 for (i
=0; parm
->enum_list
[i
].name
; i
++) {
117 if (*(int *)parm_ptr
== parm
->enum_list
[i
].value
) {
118 return PyUnicode_FromString(parm
->enum_list
[i
].name
);
126 const char **strlist
= *(const char ***)parm_ptr
;
129 if(strlist
== NULL
) {
130 return PyList_New(0);
133 pylist
= PyList_New(str_list_length(strlist
));
134 for (j
= 0; strlist
[j
]; j
++)
135 PyList_SetItem(pylist
, j
,
136 PyUnicode_FromString(strlist
[j
]));
144 static PyObject
*py_lp_ctx_load(PyObject
*self
, PyObject
*args
)
148 if (!PyArg_ParseTuple(args
, "s", &filename
))
151 ret
= lpcfg_load(PyLoadparmContext_AsLoadparmContext(self
), filename
);
154 PyErr_Format(PyExc_RuntimeError
, "Unable to load file %s", filename
);
160 static PyObject
*py_lp_ctx_load_default(PyObject
*self
, PyObject
*unused
)
163 ret
= lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self
));
166 PyErr_SetString(PyExc_RuntimeError
, "Unable to load default file");
172 static PyObject
*py_lp_ctx_get(PyObject
*self
, PyObject
*args
)
175 char *section_name
= NULL
;
177 if (!PyArg_ParseTuple(args
, "s|z", ¶m_name
, §ion_name
))
180 ret
= py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self
), section_name
, param_name
);
186 static PyObject
*py_lp_ctx_is_myname(PyObject
*self
, PyObject
*args
)
189 if (!PyArg_ParseTuple(args
, "s", &name
))
192 return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self
), name
));
195 static PyObject
*py_lp_ctx_is_mydomain(PyObject
*self
, PyObject
*args
)
198 if (!PyArg_ParseTuple(args
, "s", &name
))
201 return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self
), name
));
204 static PyObject
*py_lp_ctx_set(PyObject
*self
, PyObject
*args
)
208 if (!PyArg_ParseTuple(args
, "ss", &name
, &value
))
211 ret
= lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self
), name
, value
);
213 PyErr_SetString(PyExc_RuntimeError
, "Unable to set parameter");
220 static PyObject
*py_lp_ctx_private_path(PyObject
*self
, PyObject
*args
)
224 if (!PyArg_ParseTuple(args
, "s", &name
))
227 path
= lpcfg_private_path(NULL
, PyLoadparmContext_AsLoadparmContext(self
), name
);
228 ret
= PyUnicode_FromString(path
);
234 static PyObject
*py_lp_ctx_services(PyObject
*self
, PyObject
*unused
)
236 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
239 ret
= PyList_New(lpcfg_numservices(lp_ctx
));
240 for (i
= 0; i
< lpcfg_numservices(lp_ctx
); i
++) {
241 struct loadparm_service
*service
= lpcfg_servicebynum(lp_ctx
, i
);
242 if (service
!= NULL
) {
243 PyList_SetItem(ret
, i
, PyUnicode_FromString(lpcfg_servicename(service
)));
249 static PyObject
*py_lp_ctx_server_role(PyObject
*self
, PyObject
*unused
)
251 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
253 const char *role_str
;
255 role
= lpcfg_server_role(lp_ctx
);
256 role_str
= server_role_str(role
);
258 return PyUnicode_FromString(role_str
);
261 static PyObject
*py_lp_dump(PyObject
*self
, PyObject
*args
)
263 bool show_defaults
= false;
264 const char *file_name
= "";
265 const char *mode
= "w";
267 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
269 if (!PyArg_ParseTuple(args
, "|bss", &show_defaults
, &file_name
, &mode
))
272 if (file_name
[0] == '\0') {
275 f
= fopen(file_name
, mode
);
279 PyErr_SetFromErrno(PyExc_IOError
);
283 lpcfg_dump(lp_ctx
, f
, show_defaults
, lpcfg_numservices(lp_ctx
));
292 static PyObject
*py_lp_dump_globals(PyObject
*self
, PyObject
*args
)
294 bool show_defaults
= false;
295 const char *file_name
= "";
296 const char *mode
= "w";
298 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
300 if (!PyArg_ParseTuple(args
, "|bss", &show_defaults
, &file_name
, &mode
))
303 if (file_name
[0] == '\0') {
306 f
= fopen(file_name
, mode
);
310 PyErr_SetFromErrno(PyExc_IOError
);
314 lpcfg_dump_globals(lp_ctx
, f
, show_defaults
);
323 static PyObject
*py_lp_dump_a_parameter(PyObject
*self
, PyObject
*args
)
326 const char *section_name
= NULL
;
327 const char *file_name
= "";
328 const char *mode
= "w";
330 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
331 struct loadparm_service
*service
;
334 if (!PyArg_ParseTuple(args
, "s|zss", ¶m_name
, §ion_name
, &file_name
, &mode
))
337 if (file_name
[0] == '\0') {
340 f
= fopen(file_name
, mode
);
347 if (section_name
!= NULL
&& strwicmp(section_name
, GLOBAL_NAME
) &&
348 strwicmp(section_name
, GLOBAL_NAME2
)) {
349 /* it's a share parameter */
350 service
= lpcfg_service(lp_ctx
, section_name
);
351 if (service
== NULL
) {
352 PyErr_Format(PyExc_RuntimeError
, "Unknown section %s", section_name
);
358 section_name
= "global";
361 ret
= lpcfg_dump_a_parameter(lp_ctx
, service
, param_name
, f
);
364 PyErr_Format(PyExc_RuntimeError
, "Parameter %s unknown for section %s", param_name
, section_name
);
379 static PyObject
*py_lp_log_level(PyObject
*self
, PyObject
*unused
)
381 int ret
= debuglevel_get();
382 return PyLong_FromLong(ret
);
386 static PyObject
*py_samdb_url(PyObject
*self
, PyObject
*unused
)
388 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
389 return PyUnicode_FromFormat("tdb://%s/sam.ldb", lpcfg_private_dir(lp_ctx
));
392 static PyObject
*py_cache_path(PyObject
*self
, PyObject
*args
)
394 struct loadparm_context
*lp_ctx
= PyLoadparmContext_AsLoadparmContext(self
);
397 PyObject
*ret
= NULL
;
399 if (!PyArg_ParseTuple(args
, "s", &name
)) {
403 path
= lpcfg_cache_path(NULL
, lp_ctx
, name
);
405 PyErr_Format(PyExc_RuntimeError
,
406 "Unable to access cache %s", name
);
409 ret
= PyUnicode_FromString(path
);
415 static PyObject
*py_state_path(PyObject
*self
, PyObject
*args
)
417 struct loadparm_context
*lp_ctx
=
418 PyLoadparmContext_AsLoadparmContext(self
);
421 PyObject
*ret
= NULL
;
423 if (!PyArg_ParseTuple(args
, "s", &name
)) {
427 path
= lpcfg_state_path(NULL
, lp_ctx
, name
);
429 PyErr_Format(PyExc_RuntimeError
,
430 "Unable to access cache %s", name
);
433 ret
= PyUnicode_FromString(path
);
439 static PyMethodDef py_lp_ctx_methods
[] = {
440 { "load", py_lp_ctx_load
, METH_VARARGS
,
441 "S.load(filename) -> None\n"
442 "Load specified file." },
443 { "load_default", py_lp_ctx_load_default
, METH_NOARGS
,
444 "S.load_default() -> None\n"
445 "Load default smb.conf file." },
446 { "is_myname", py_lp_ctx_is_myname
, METH_VARARGS
,
447 "S.is_myname(name) -> bool\n"
448 "Check whether the specified name matches one of our netbios names." },
449 { "is_mydomain", py_lp_ctx_is_mydomain
, METH_VARARGS
,
450 "S.is_mydomain(name) -> bool\n"
451 "Check whether the specified name matches our domain name." },
452 { "get", py_lp_ctx_get
, METH_VARARGS
,
453 "S.get(name, service_name) -> value\n"
454 "Find specified parameter." },
455 { "set", py_lp_ctx_set
, METH_VARARGS
,
456 "S.set(name, value) -> bool\n"
457 "Change a parameter." },
458 { "private_path", py_lp_ctx_private_path
, METH_VARARGS
,
459 "S.private_path(name) -> path\n" },
460 { "services", py_lp_ctx_services
, METH_NOARGS
,
461 "S.services() -> list" },
462 { "server_role", py_lp_ctx_server_role
, METH_NOARGS
,
463 "S.server_role() -> value\n"
464 "Get the server role." },
465 { "dump", py_lp_dump
, METH_VARARGS
,
466 "S.dump(show_defaults=False, file_name='', mode='w')" },
467 { "dump_globals", py_lp_dump_globals
, METH_VARARGS
,
468 "S.dump_globals(show_defaults=False, file_name='', mode='w')" },
469 { "dump_a_parameter", py_lp_dump_a_parameter
, METH_VARARGS
,
470 "S.dump_a_parameter(name, service_name, file_name='', mode='w')" },
471 { "log_level", py_lp_log_level
, METH_NOARGS
,
472 "S.log_level() -> int\n Get the active log level" },
473 { "samdb_url", py_samdb_url
, METH_NOARGS
,
474 "S.samdb_url() -> string\n"
475 "Returns the current URL for sam.ldb." },
476 { "cache_path", py_cache_path
, METH_VARARGS
,
477 "S.cache_path(name) -> string\n"
478 "Returns a path in the Samba cache directory." },
479 { "state_path", py_state_path
, METH_VARARGS
,
480 "S.state_path(name) -> string\n"
481 "Returns a path in the Samba state directory." },
485 static PyObject
*py_lp_ctx_default_service(PyObject
*self
, void *closure
)
487 return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self
)));
490 static PyObject
*py_lp_ctx_config_file(PyObject
*self
, void *closure
)
492 const char *configfile
= lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self
));
493 if (configfile
== NULL
)
496 return PyUnicode_FromString(configfile
);
499 static PyObject
*py_lp_ctx_weak_crypto(PyObject
*self
, void *closure
)
501 enum samba_weak_crypto weak_crypto
=
502 lpcfg_weak_crypto(PyLoadparmContext_AsLoadparmContext(self
));
504 switch(weak_crypto
) {
505 case SAMBA_WEAK_CRYPTO_UNKNOWN
:
507 case SAMBA_WEAK_CRYPTO_ALLOWED
:
508 return PyUnicode_FromString("allowed");
509 case SAMBA_WEAK_CRYPTO_DISALLOWED
:
510 return PyUnicode_FromString("disallowed");
516 static PyGetSetDef py_lp_ctx_getset
[] = {
518 .name
= discard_const_p(char, "default_service"),
519 .get
= (getter
)py_lp_ctx_default_service
,
522 .name
= discard_const_p(char, "configfile"),
523 .get
= (getter
)py_lp_ctx_config_file
,
524 .doc
= discard_const_p(char, "Name of last config file that was loaded.")
527 .name
= discard_const_p(char, "weak_crypto"),
528 .get
= (getter
)py_lp_ctx_weak_crypto
,
529 .doc
= discard_const_p(char, "If weak crypto is allowed.")
534 static PyObject
*py_lp_ctx_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
536 const char *kwnames
[] = {"filename_for_non_global_lp", NULL
};
538 const char *non_global_conf
= NULL
;
539 struct loadparm_context
*ctx
;
541 if (!PyArg_ParseTupleAndKeywords(args
,
544 discard_const_p(char *,
551 * by default, any LoadParm python objects map to a single global
552 * underlying object. The filename_for_non_global_lp arg overrides this
553 * default behaviour and creates a separate underlying LoadParm object.
555 if (non_global_conf
!= NULL
) {
557 ctx
= loadparm_init(NULL
);
563 lp_ctx
= pytalloc_reference(type
, ctx
);
564 if (lp_ctx
== NULL
) {
569 ok
= lpcfg_load_no_global(
570 PyLoadparmContext_AsLoadparmContext(lp_ctx
),
573 PyErr_Format(PyExc_ValueError
,
574 "Could not load non-global conf %s",
580 return pytalloc_reference(type
, loadparm_init_global(false));
584 static Py_ssize_t
py_lp_ctx_len(PyObject
*self
)
586 return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self
));
589 static PyObject
*py_lp_ctx_getitem(PyObject
*self
, PyObject
*name
)
591 struct loadparm_service
*service
;
592 if (!PyUnicode_Check(name
)) {
593 PyErr_SetString(PyExc_TypeError
, "Only string subscripts are supported");
596 service
= lpcfg_service(PyLoadparmContext_AsLoadparmContext(self
), PyUnicode_AsUTF8(name
));
597 if (service
== NULL
) {
598 PyErr_SetString(PyExc_KeyError
, "No such section");
601 return PyLoadparmService_FromService(service
);
604 static PyMappingMethods py_lp_ctx_mapping
= {
605 .mp_length
= (lenfunc
)py_lp_ctx_len
,
606 .mp_subscript
= (binaryfunc
)py_lp_ctx_getitem
,
609 PyTypeObject PyLoadparmContext
= {
610 .tp_name
= "param.LoadParm",
611 .tp_getset
= py_lp_ctx_getset
,
612 .tp_methods
= py_lp_ctx_methods
,
613 .tp_new
= py_lp_ctx_new
,
614 .tp_as_mapping
= &py_lp_ctx_mapping
,
615 .tp_flags
= Py_TPFLAGS_DEFAULT
,
618 static PyObject
*py_lp_service_dump(PyObject
*self
, PyObject
*args
)
620 bool show_defaults
= false;
622 const char *file_name
= "";
623 const char *mode
= "w";
624 struct loadparm_service
*service
= PyLoadparmService_AsLoadparmService(self
);
625 struct loadparm_service
*default_service
;
626 PyObject
*py_default_service
;
628 if (!PyArg_ParseTuple(args
, "O|bss", &py_default_service
, &show_defaults
, &file_name
, &mode
))
631 if (file_name
[0] == '\0') {
634 f
= fopen(file_name
, mode
);
641 if (!PyObject_TypeCheck(py_default_service
, &PyLoadparmService
)) {
642 PyErr_SetNone(PyExc_TypeError
);
649 default_service
= PyLoadparmService_AsLoadparmService(py_default_service
);
651 lpcfg_dump_one(f
, show_defaults
, service
, default_service
);
660 static PyMethodDef py_lp_service_methods
[] = {
661 { "dump", (PyCFunction
)py_lp_service_dump
, METH_VARARGS
,
662 "S.dump(default_service, show_defaults=False, file_name='', mode='w')" },
666 PyTypeObject PyLoadparmService
= {
667 .tp_name
= "param.LoadparmService",
668 .tp_methods
= py_lp_service_methods
,
669 .tp_flags
= Py_TPFLAGS_DEFAULT
,
672 static PyObject
*py_data_dir(PyObject
*self
, PyObject
*Py_UNUSED(ignored
))
674 return PyUnicode_FromString(dyn_DATADIR
);
677 static PyObject
*py_default_path(PyObject
*self
, PyObject
*Py_UNUSED(ignored
))
679 return PyUnicode_FromString(lp_default_path());
682 static PyObject
*py_setup_dir(PyObject
*self
, PyObject
*Py_UNUSED(ignored
))
684 return PyUnicode_FromString(dyn_SETUPDIR
);
687 static PyObject
*py_modules_dir(PyObject
*self
, PyObject
*Py_UNUSED(ignored
))
689 return PyUnicode_FromString(dyn_MODULESDIR
);
692 static PyObject
*py_bin_dir(PyObject
*self
, PyObject
*Py_UNUSED(ignored
))
694 return PyUnicode_FromString(dyn_BINDIR
);
697 static PyObject
*py_sbin_dir(PyObject
*self
, PyObject
*Py_UNUSED(ignored
))
699 return PyUnicode_FromString(dyn_SBINDIR
);
702 static PyMethodDef pyparam_methods
[] = {
703 { "data_dir", (PyCFunction
)py_data_dir
, METH_NOARGS
,
704 "Returns the compiled in location of data directory." },
705 { "default_path", (PyCFunction
)py_default_path
, METH_NOARGS
,
706 "Returns the default smb.conf path." },
707 { "setup_dir", (PyCFunction
)py_setup_dir
, METH_NOARGS
,
708 "Returns the compiled in location of provision templates." },
709 { "modules_dir", (PyCFunction
)py_modules_dir
, METH_NOARGS
,
710 "Returns the compiled in location of modules." },
711 { "bin_dir", (PyCFunction
)py_bin_dir
, METH_NOARGS
,
712 "Returns the compiled in BINDIR." },
713 { "sbin_dir", (PyCFunction
)py_sbin_dir
, METH_NOARGS
,
714 "Returns the compiled in SBINDIR." },
718 static struct PyModuleDef moduledef
= {
719 PyModuleDef_HEAD_INIT
,
721 .m_doc
= "Parsing and writing Samba configuration files.",
723 .m_methods
= pyparam_methods
,
726 MODULE_INIT_FUNC(param
)
729 PyTypeObject
*talloc_type
= pytalloc_GetObjectType();
730 if (talloc_type
== NULL
)
733 if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmContext
) < 0)
736 if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmService
) < 0)
739 m
= PyModule_Create(&moduledef
);
743 Py_INCREF(&PyLoadparmContext
);
744 PyModule_AddObject(m
, "LoadParm", (PyObject
*)&PyLoadparmContext
);