From 59c648fef6b1828befdbf4d73b9e604a12c2394a Mon Sep 17 00:00:00 2001 From: David Mulder Date: Tue, 30 Jan 2018 12:31:42 -0700 Subject: [PATCH 1/2] libgpo: port samba.gpo to python3 Signed-off-by: David Mulder --- libgpo/pygpo.c | 30 +++++++++++++++++++++--------- libgpo/wscript_build | 9 +++++---- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/libgpo/pygpo.c b/libgpo/pygpo.c index b6b53b76212..ac6e3237a82 100644 --- a/libgpo/pygpo.c +++ b/libgpo/pygpo.c @@ -27,6 +27,7 @@ #include "librpc/rpc/pyrpc_util.h" #include "auth/credentials/pycredentials.h" #include "libcli/util/pyerrors.h" +#include "python/py3compat.h" /* A Python C API module to use LIBGPO */ @@ -37,7 +38,7 @@ static PyObject* GPO_get_##ATTR(PyObject *self, void *closure) \ = pytalloc_get_ptr(self); \ \ if (gpo_ptr->ATTR) \ - return PyString_FromString(gpo_ptr->ATTR); \ + return PyStr_FromString(gpo_ptr->ATTR); \ else \ return Py_None; \ } @@ -110,7 +111,7 @@ static PyObject *py_gpo_get_unix_path(PyObject *self, PyObject *args, goto out; } - ret = PyString_FromString(unix_path); + ret = PyStr_FromString(unix_path); out: return ret; @@ -475,7 +476,7 @@ static PyMethodDef ADS_methods[] = { { "connect", (PyCFunction)py_ads_connect, METH_NOARGS, "Connect to the LDAP server" }, #ifdef HAVE_ADS - { "get_gpo_list", (PyCFunction)py_ads_get_gpo_list, METH_KEYWORDS, + { "get_gpo_list", (PyCFunction)py_ads_get_gpo_list, METH_VARARGS | METH_KEYWORDS, NULL }, #endif { NULL } @@ -499,34 +500,45 @@ static PyMethodDef py_gpo_methods[] = { {NULL} }; +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + .m_name = "gpo", + .m_doc = "libgpo python bindings", + .m_size = -1, + .m_methods = py_gpo_methods, +}; + /* Will be called by python when loading this module */ -void initgpo(void) +void initgpo(void); + +MODULE_INIT_FUNC(gpo) { PyObject *m; debug_setup_talloc_log(); /* Instantiate the types */ - m = Py_InitModule3("gpo", py_gpo_methods, "libgpo python bindings"); + m = PyModule_Create(&moduledef); if (m == NULL) { - return; + return m; } PyModule_AddObject(m, "version", - PyString_FromString(SAMBA_VERSION_STRING)); + PyStr_FromString(SAMBA_VERSION_STRING)); if (PyType_Ready(&ads_ADSType) < 0) { - return; + return m; } PyModule_AddObject(m, "ADS_STRUCT", (PyObject *)&ads_ADSType); if (pytalloc_BaseObject_PyType_Ready(&GPOType) < 0) { - return; + return m; } Py_INCREF((PyObject *)(void *)&GPOType); PyModule_AddObject(m, "GROUP_POLICY_OBJECT", (PyObject *)&GPOType); + return m; } diff --git a/libgpo/wscript_build b/libgpo/wscript_build index 2ef66f7fa9d..f456d417d8a 100644 --- a/libgpo/wscript_build +++ b/libgpo/wscript_build @@ -7,7 +7,8 @@ bld.SAMBA3_LIBRARY('gpext', deps='talloc ads TOKEN_UTIL auth', private_library=True) -bld.SAMBA3_PYTHON('python_samba_libgpo', 'pygpo.c', - deps='''pyparam_util gpext talloc ads TOKEN_UTIL - auth pyrpc_util''', - realname='samba/gpo.so') +for env in bld.gen_python_environments(): + bld.SAMBA3_PYTHON('python_samba_libgpo', 'pygpo.c', + deps='''pyparam_util gpext talloc ads TOKEN_UTIL + auth pyrpc_util''', + realname='samba/gpo.so') From d9d17aafac9b65b64230e9dc4a86702bbccff676 Mon Sep 17 00:00:00 2001 From: David Mulder Date: Mon, 12 Feb 2018 12:21:42 -0700 Subject: [PATCH 2/2] Add python tests for samba.gpo module Signed-off-by: David Mulder --- python/samba/tests/gpo.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++ source4/selftest/tests.py | 3 +++ 2 files changed, 57 insertions(+) create mode 100644 python/samba/tests/gpo.py diff --git a/python/samba/tests/gpo.py b/python/samba/tests/gpo.py new file mode 100644 index 00000000000..814d63eb40a --- /dev/null +++ b/python/samba/tests/gpo.py @@ -0,0 +1,54 @@ +import os +from samba import gpo, tests +from samba.param import LoadParm + +poldir = r'\\addom.samba.example.com\sysvol\addom.samba.example.com\Policies' +dspath = 'CN=Policies,CN=System,DC=addom,DC=samba,DC=example,DC=com' +gpt_data = '[General]\nVersion=%d' + +class GPOTests(tests.TestCase): + def setUp(self): + super(GPOTests, self).setUp() + self.server = os.environ["SERVER"] + self.lp = LoadParm() + self.lp.load_default() + self.creds = self.insta_creds(template=self.get_credentials()) + + def tearDown(self): + super(GPOTests, self).tearDown() + + def test_gpo_list(self): + global poldir, dspath + ads = gpo.ADS_STRUCT(self.server, self.lp, self.creds) + if ads.connect(): + gpos = ads.get_gpo_list(self.creds.get_username()) + guid = '{31B2F340-016D-11D2-945F-00C04FB984F9}' + names = ['Local Policy', guid] + file_sys_paths = [None, '%s\\%s' % (poldir, guid)] + ds_paths = [None, 'CN=%s,%s' % (guid, dspath)] + for i in range(0, len(gpos)): + assert gpos[i].name == names[i], \ + 'The gpo name did not match expected name %s' % gpos[i].name + assert gpos[i].file_sys_path == file_sys_paths[i], \ + 'file_sys_path did not match expected %s' % gpos[i].file_sys_path + assert gpos[i].ds_path == ds_paths[i], \ + 'ds_path did not match expected %s' % gpos[i].ds_path + + def test_gpt_version(self): + global gpt_data + local_path = self.lp.get("path", "sysvol") + policies = 'addom.samba.example.com/Policies' + guid = '{31B2F340-016D-11D2-945F-00C04FB984F9}' + gpo_path = os.path.join(local_path, policies, guid) + old_vers = gpo.gpo_get_sysvol_gpt_version(gpo_path)[1] + + with open(os.path.join(gpo_path, 'GPT.INI'), 'w') as gpt: + gpt.write(gpt_data % 42) + assert gpo.gpo_get_sysvol_gpt_version(gpo_path)[1] == 42, \ + 'gpo_get_sysvol_gpt_version() did not return the expected version' + + with open(os.path.join(gpo_path, 'GPT.INI'), 'w') as gpt: + gpt.write(gpt_data % old_vers) + assert gpo.gpo_get_sysvol_gpt_version(gpo_path)[1] == old_vers, \ + 'gpo_get_sysvol_gpt_version() did not return the expected version' + diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py index 4e397a8f134..02c3e17f048 100755 --- a/source4/selftest/tests.py +++ b/source4/selftest/tests.py @@ -613,6 +613,9 @@ def planoldpythontestsuite(env, module, name=None, extra_path=[], environ={}, ex planpythontestsuite("ad_dc:local", "samba.tests.samba_tool.dnscmd") planpythontestsuite("ad_dc_ntvfs:local", "samba.tests.dcerpc.rpcecho") + +planoldpythontestsuite("ad_dc:local", "samba.tests.gpo", extra_args=['-U"$USERNAME%$PASSWORD"'], py3_compatible=True) + planoldpythontestsuite("ad_dc_ntvfs:local", "samba.tests.dcerpc.registry", extra_args=['-U"$USERNAME%$PASSWORD"']) planoldpythontestsuite("ad_dc_ntvfs", "samba.tests.dcerpc.dnsserver", extra_args=['-U"$USERNAME%$PASSWORD"']) planoldpythontestsuite("ad_dc", "samba.tests.dcerpc.dnsserver", extra_args=['-U"$USERNAME%$PASSWORD"'])