s3:pylibsmb: Add ipc=True support for CLI_FULL_CONNECTION_IPC
[Samba.git] / source4 / param / pyparam.c
blob4023fac4dd649abc405f28b6a89467ba6f2c21c2
1 /*
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 <Python.h>
21 #include "python/py3compat.h"
22 #include "includes.h"
23 #include "param/param.h"
24 #include "param/loadparm.h"
25 #include <pytalloc.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;
43 int i;
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) {
51 return 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;
58 const char *value;
59 if (type == NULL || option == NULL) {
60 return NULL;
62 value = lpcfg_get_parametric(lp_ctx, service, type, option);
63 if (value == NULL) {
64 return NULL;
66 return PyUnicode_FromString(value);
69 parm = lpcfg_parm_struct(lp_ctx, param_name);
70 if (parm == NULL || parm->p_class == P_GLOBAL) {
71 return NULL;
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;
79 const char *value;
80 if (type == NULL || option == NULL) {
81 return NULL;
83 value = lpcfg_get_parametric(lp_ctx, NULL, type, option);
84 if (value == NULL)
85 return NULL;
86 return PyUnicode_FromString(value);
87 } else {
88 /* its a global parameter */
89 parm = lpcfg_parm_struct(lp_ctx, param_name);
90 if (parm == NULL) {
91 return NULL;
93 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, parm);
96 if (parm == NULL || parm_ptr == NULL) {
97 return NULL;
100 /* construct and return the right type of python object */
101 switch (parm->type) {
102 case P_CHAR:
103 return PyUnicode_FromFormat("%c", *(char *)parm_ptr);
104 case P_STRING:
105 case P_USTRING:
106 return PyUnicode_FromString(*(char **)parm_ptr);
107 case P_BOOL:
108 return PyBool_FromLong(*(bool *)parm_ptr);
109 case P_BOOLREV:
110 return PyBool_FromLong(!(*(bool *)parm_ptr));
111 case P_INTEGER:
112 case P_OCTAL:
113 case P_BYTES:
114 return PyLong_FromLong(*(int *)parm_ptr);
115 case P_ENUM:
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);
121 return NULL;
122 case P_CMDLIST:
123 case P_LIST:
125 int j;
126 const char **strlist = *(const char ***)parm_ptr;
127 PyObject *pylist;
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]));
137 return pylist;
140 return NULL;
144 static PyObject *py_lp_ctx_load(PyObject *self, PyObject *args)
146 char *filename;
147 bool ret;
148 if (!PyArg_ParseTuple(args, "s", &filename))
149 return NULL;
151 ret = lpcfg_load(PyLoadparmContext_AsLoadparmContext(self), filename);
153 if (!ret) {
154 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
155 return NULL;
157 Py_RETURN_NONE;
160 static PyObject *py_lp_ctx_load_default(PyObject *self, PyObject *unused)
162 bool ret;
163 ret = lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self));
165 if (!ret) {
166 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
167 return NULL;
169 Py_RETURN_NONE;
172 static PyObject *py_lp_ctx_get(PyObject *self, PyObject *args)
174 char *param_name;
175 char *section_name = NULL;
176 PyObject *ret;
177 if (!PyArg_ParseTuple(args, "s|z", &param_name, &section_name))
178 return NULL;
180 ret = py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self), section_name, param_name);
181 if (ret == NULL)
182 Py_RETURN_NONE;
183 return ret;
186 static PyObject *py_lp_ctx_is_myname(PyObject *self, PyObject *args)
188 char *name;
189 if (!PyArg_ParseTuple(args, "s", &name))
190 return NULL;
192 return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
195 static PyObject *py_lp_ctx_is_mydomain(PyObject *self, PyObject *args)
197 char *name;
198 if (!PyArg_ParseTuple(args, "s", &name))
199 return NULL;
201 return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
204 static PyObject *py_lp_ctx_set(PyObject *self, PyObject *args)
206 char *name, *value;
207 bool ret;
208 if (!PyArg_ParseTuple(args, "ss", &name, &value))
209 return NULL;
211 ret = lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self), name, value);
212 if (!ret) {
213 PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
214 return NULL;
217 Py_RETURN_NONE;
220 static PyObject *py_lp_ctx_private_path(PyObject *self, PyObject *args)
222 char *name, *path;
223 PyObject *ret;
224 if (!PyArg_ParseTuple(args, "s", &name))
225 return NULL;
227 path = lpcfg_private_path(NULL, PyLoadparmContext_AsLoadparmContext(self), name);
228 ret = PyUnicode_FromString(path);
229 talloc_free(path);
231 return ret;
234 static PyObject *py_lp_ctx_services(PyObject *self, PyObject *unused)
236 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
237 PyObject *ret;
238 int i;
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)));
246 return ret;
249 static PyObject *py_lp_ctx_server_role(PyObject *self, PyObject *unused)
251 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
252 uint32_t role;
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";
266 FILE *f;
267 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
269 if (!PyArg_ParseTuple(args, "|bss", &show_defaults, &file_name, &mode))
270 return NULL;
272 if (file_name[0] == '\0') {
273 f = stdout;
274 } else {
275 f = fopen(file_name, mode);
278 if (f == NULL) {
279 PyErr_SetFromErrno(PyExc_IOError);
280 return NULL;
283 lpcfg_dump(lp_ctx, f, show_defaults, lpcfg_numservices(lp_ctx));
285 if (f != stdout) {
286 fclose(f);
289 Py_RETURN_NONE;
292 static PyObject *py_lp_dump_a_parameter(PyObject *self, PyObject *args)
294 char *param_name;
295 const char *section_name = NULL;
296 const char *file_name = "";
297 const char *mode = "w";
298 FILE *f;
299 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
300 struct loadparm_service *service;
301 bool ret;
303 if (!PyArg_ParseTuple(args, "s|zss", &param_name, &section_name, &file_name, &mode))
304 return NULL;
306 if (file_name[0] == '\0') {
307 f = stdout;
308 } else {
309 f = fopen(file_name, mode);
312 if (f == NULL) {
313 return NULL;
316 if (section_name != NULL && strwicmp(section_name, GLOBAL_NAME) &&
317 strwicmp(section_name, GLOBAL_NAME2)) {
318 /* it's a share parameter */
319 service = lpcfg_service(lp_ctx, section_name);
320 if (service == NULL) {
321 PyErr_Format(PyExc_RuntimeError, "Unknown section %s", section_name);
322 return NULL;
324 } else {
325 /* it's global */
326 service = NULL;
327 section_name = "global";
330 ret = lpcfg_dump_a_parameter(lp_ctx, service, param_name, f);
332 if (!ret) {
333 PyErr_Format(PyExc_RuntimeError, "Parameter %s unknown for section %s", param_name, section_name);
334 if (f != stdout) {
335 fclose(f);
337 return NULL;
340 if (f != stdout) {
341 fclose(f);
344 Py_RETURN_NONE;
348 static PyObject *py_lp_log_level(PyObject *self, PyObject *unused)
350 int ret = debuglevel_get();
351 return PyLong_FromLong(ret);
355 static PyObject *py_samdb_url(PyObject *self, PyObject *unused)
357 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
358 return PyUnicode_FromFormat("tdb://%s/sam.ldb", lpcfg_private_dir(lp_ctx));
361 static PyObject *py_cache_path(PyObject *self, PyObject *args)
363 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
364 char *name = NULL;
365 char *path = NULL;
366 PyObject *ret = NULL;
368 if (!PyArg_ParseTuple(args, "s", &name)) {
369 return NULL;
372 path = lpcfg_cache_path(NULL, lp_ctx, name);
373 if (!path) {
374 PyErr_Format(PyExc_RuntimeError,
375 "Unable to access cache %s", name);
376 return NULL;
378 ret = PyUnicode_FromString(path);
379 talloc_free(path);
381 return ret;
384 static PyObject *py_state_path(PyObject *self, PyObject *args)
386 struct loadparm_context *lp_ctx =
387 PyLoadparmContext_AsLoadparmContext(self);
388 char *name = NULL;
389 char *path = NULL;
390 PyObject *ret = NULL;
392 if (!PyArg_ParseTuple(args, "s", &name)) {
393 return NULL;
396 path = lpcfg_state_path(NULL, lp_ctx, name);
397 if (!path) {
398 PyErr_Format(PyExc_RuntimeError,
399 "Unable to access cache %s", name);
400 return NULL;
402 ret = PyUnicode_FromString(path);
403 talloc_free(path);
405 return ret;
408 static PyMethodDef py_lp_ctx_methods[] = {
409 { "load", py_lp_ctx_load, METH_VARARGS,
410 "S.load(filename) -> None\n"
411 "Load specified file." },
412 { "load_default", py_lp_ctx_load_default, METH_NOARGS,
413 "S.load_default() -> None\n"
414 "Load default smb.conf file." },
415 { "is_myname", py_lp_ctx_is_myname, METH_VARARGS,
416 "S.is_myname(name) -> bool\n"
417 "Check whether the specified name matches one of our netbios names." },
418 { "is_mydomain", py_lp_ctx_is_mydomain, METH_VARARGS,
419 "S.is_mydomain(name) -> bool\n"
420 "Check whether the specified name matches our domain name." },
421 { "get", py_lp_ctx_get, METH_VARARGS,
422 "S.get(name, service_name) -> value\n"
423 "Find specified parameter." },
424 { "set", py_lp_ctx_set, METH_VARARGS,
425 "S.set(name, value) -> bool\n"
426 "Change a parameter." },
427 { "private_path", py_lp_ctx_private_path, METH_VARARGS,
428 "S.private_path(name) -> path\n" },
429 { "services", py_lp_ctx_services, METH_NOARGS,
430 "S.services() -> list" },
431 { "server_role", py_lp_ctx_server_role, METH_NOARGS,
432 "S.server_role() -> value\n"
433 "Get the server role." },
434 { "dump", py_lp_dump, METH_VARARGS,
435 "S.dump(show_defaults=False, file_name='', mode='w')" },
436 { "dump_a_parameter", py_lp_dump_a_parameter, METH_VARARGS,
437 "S.dump_a_parameter(name, service_name, file_name='', mode='w')" },
438 { "log_level", py_lp_log_level, METH_NOARGS,
439 "S.log_level() -> int\n Get the active log level" },
440 { "samdb_url", py_samdb_url, METH_NOARGS,
441 "S.samdb_url() -> string\n"
442 "Returns the current URL for sam.ldb." },
443 { "cache_path", py_cache_path, METH_VARARGS,
444 "S.cache_path(name) -> string\n"
445 "Returns a path in the Samba cache directory." },
446 { "state_path", py_state_path, METH_VARARGS,
447 "S.state_path(name) -> string\n"
448 "Returns a path in the Samba state directory." },
452 static PyObject *py_lp_ctx_default_service(PyObject *self, void *closure)
454 return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self)));
457 static PyObject *py_lp_ctx_config_file(PyObject *self, void *closure)
459 const char *configfile = lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self));
460 if (configfile == NULL)
461 Py_RETURN_NONE;
462 else
463 return PyUnicode_FromString(configfile);
466 static PyGetSetDef py_lp_ctx_getset[] = {
468 .name = discard_const_p(char, "default_service"),
469 .get = (getter)py_lp_ctx_default_service,
472 .name = discard_const_p(char, "configfile"),
473 .get = (getter)py_lp_ctx_config_file,
474 .doc = discard_const_p(char, "Name of last config file that was loaded.")
476 { .name = NULL }
479 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
481 const char *kwnames[] = {"filename_for_non_global_lp", NULL};
482 PyObject *lp_ctx;
483 const char *non_global_conf = NULL;
484 struct loadparm_context *ctx;
486 if (!PyArg_ParseTupleAndKeywords(args,
487 kwargs,
488 "|s",
489 discard_const_p(char *,
490 kwnames),
491 &non_global_conf)) {
492 return NULL;
496 * by default, any LoadParm python objects map to a single global
497 * underlying object. The filename_for_non_global_lp arg overrides this
498 * default behaviour and creates a separate underlying LoadParm object.
500 if (non_global_conf != NULL) {
501 bool ok;
502 ctx = loadparm_init(NULL);
503 if (ctx == NULL) {
504 PyErr_NoMemory();
505 return NULL;
508 lp_ctx = pytalloc_reference(type, ctx);
509 if (lp_ctx == NULL) {
510 PyErr_NoMemory();
511 return NULL;
514 ok = lpcfg_load_no_global(
515 PyLoadparmContext_AsLoadparmContext(lp_ctx),
516 non_global_conf);
517 if (!ok) {
518 PyErr_Format(PyExc_ValueError,
519 "Could not load non-global conf %s",
520 non_global_conf);
521 return NULL;
523 return lp_ctx;
524 } else{
525 return pytalloc_reference(type, loadparm_init_global(false));
529 static Py_ssize_t py_lp_ctx_len(PyObject *self)
531 return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self));
534 static PyObject *py_lp_ctx_getitem(PyObject *self, PyObject *name)
536 struct loadparm_service *service;
537 if (!PyUnicode_Check(name)) {
538 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
539 return NULL;
541 service = lpcfg_service(PyLoadparmContext_AsLoadparmContext(self), PyUnicode_AsUTF8(name));
542 if (service == NULL) {
543 PyErr_SetString(PyExc_KeyError, "No such section");
544 return NULL;
546 return PyLoadparmService_FromService(service);
549 static PyMappingMethods py_lp_ctx_mapping = {
550 .mp_length = (lenfunc)py_lp_ctx_len,
551 .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
554 PyTypeObject PyLoadparmContext = {
555 .tp_name = "param.LoadParm",
556 .tp_getset = py_lp_ctx_getset,
557 .tp_methods = py_lp_ctx_methods,
558 .tp_new = py_lp_ctx_new,
559 .tp_as_mapping = &py_lp_ctx_mapping,
560 .tp_flags = Py_TPFLAGS_DEFAULT,
563 static PyObject *py_lp_service_dump(PyObject *self, PyObject *args)
565 bool show_defaults = false;
566 FILE *f;
567 const char *file_name = "";
568 const char *mode = "w";
569 struct loadparm_service *service = PyLoadparmService_AsLoadparmService(self);
570 struct loadparm_service *default_service;
571 PyObject *py_default_service;
573 if (!PyArg_ParseTuple(args, "O|bss", &py_default_service, &show_defaults, &file_name, &mode))
574 return NULL;
576 if (file_name[0] == '\0') {
577 f = stdout;
578 } else {
579 f = fopen(file_name, mode);
582 if (f == NULL) {
583 return NULL;
586 if (!PyObject_TypeCheck(py_default_service, &PyLoadparmService)) {
587 PyErr_SetNone(PyExc_TypeError);
588 if (f != stdout) {
589 fclose(f);
591 return NULL;
594 default_service = PyLoadparmService_AsLoadparmService(py_default_service);
596 lpcfg_dump_one(f, show_defaults, service, default_service);
598 if (f != stdout) {
599 fclose(f);
602 Py_RETURN_NONE;
605 static PyMethodDef py_lp_service_methods[] = {
606 { "dump", (PyCFunction)py_lp_service_dump, METH_VARARGS,
607 "S.dump(default_service, show_defaults=False, file_name='', mode='w')" },
611 PyTypeObject PyLoadparmService = {
612 .tp_name = "param.LoadparmService",
613 .tp_methods = py_lp_service_methods,
614 .tp_flags = Py_TPFLAGS_DEFAULT,
617 static PyObject *py_data_dir(PyObject *self)
619 return PyUnicode_FromString(dyn_DATADIR);
622 static PyObject *py_default_path(PyObject *self, PyObject *Py_UNUSED(ignored))
624 return PyUnicode_FromString(lp_default_path());
627 static PyObject *py_setup_dir(PyObject *self, PyObject *Py_UNUSED(ignored))
629 return PyUnicode_FromString(dyn_SETUPDIR);
632 static PyObject *py_modules_dir(PyObject *self, PyObject *Py_UNUSED(ignored))
634 return PyUnicode_FromString(dyn_MODULESDIR);
637 static PyObject *py_bin_dir(PyObject *self, PyObject *Py_UNUSED(ignored))
639 return PyUnicode_FromString(dyn_BINDIR);
642 static PyObject *py_sbin_dir(PyObject *self, PyObject *Py_UNUSED(ignored))
644 return PyUnicode_FromString(dyn_SBINDIR);
647 static PyMethodDef pyparam_methods[] = {
648 { "data_dir", (PyCFunction)py_data_dir, METH_NOARGS,
649 "Returns the compiled in location of data directory." },
650 { "default_path", (PyCFunction)py_default_path, METH_NOARGS,
651 "Returns the default smb.conf path." },
652 { "setup_dir", (PyCFunction)py_setup_dir, METH_NOARGS,
653 "Returns the compiled in location of provision templates." },
654 { "modules_dir", (PyCFunction)py_modules_dir, METH_NOARGS,
655 "Returns the compiled in location of modules." },
656 { "bin_dir", (PyCFunction)py_bin_dir, METH_NOARGS,
657 "Returns the compiled in BINDIR." },
658 { "sbin_dir", (PyCFunction)py_sbin_dir, METH_NOARGS,
659 "Returns the compiled in SBINDIR." },
663 static struct PyModuleDef moduledef = {
664 PyModuleDef_HEAD_INIT,
665 .m_name = "param",
666 .m_doc = "Parsing and writing Samba configuration files.",
667 .m_size = -1,
668 .m_methods = pyparam_methods,
671 MODULE_INIT_FUNC(param)
673 PyObject *m;
674 PyTypeObject *talloc_type = pytalloc_GetObjectType();
675 if (talloc_type == NULL)
676 return NULL;
678 if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmContext) < 0)
679 return NULL;
681 if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmService) < 0)
682 return NULL;
684 m = PyModule_Create(&moduledef);
685 if (m == NULL)
686 return NULL;
688 Py_INCREF(&PyLoadparmContext);
689 PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);
690 return m;