python/samba: PY3 port gpo.apply smbtorture test
authorNoel Power <noel.power@suse.com>
Tue, 6 Nov 2018 19:55:22 +0000 (19:55 +0000)
committerNoel Power <npower@samba.org>
Mon, 10 Dec 2018 09:38:22 +0000 (10:38 +0100)
1) configparser.set requires string values
2) self.gp_db.store() etc. neex to pass str object for
   xml.etree.ElementTree.Element text attribute which needs
   to be text
3) tdb delete method needs bytes key
4) configparser.write needs a file opened in text mode

Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/gp_sec_ext.py
python/samba/gpclass.py
python/samba/tests/gpo.py

index fcde46808c884b9c341a55a3f6469cfa31585206..6eab975e6feef4f5ce645bdf0ca15a9a445e2359 100644 (file)
@@ -18,6 +18,7 @@
 import os.path
 from samba.gpclass import gp_ext_setter, gp_inf_ext
 from samba.auth import system_session
+from samba.compat import get_string
 try:
     from ldb import LdbError
     from samba.samdb import SamDB
@@ -37,8 +38,8 @@ class inf_to_kdc_tdb(gp_ext_setter):
         self.logger.info('%s was changed from %s to %s' % (self.attribute,
                                                            old_val, val))
         if val is not None:
-            self.gp_db.gpostore.store(self.attribute, val)
-            self.gp_db.store(str(self), self.attribute, old_val)
+            self.gp_db.gpostore.store(self.attribute, get_string(val))
+            self.gp_db.store(str(self), self.attribute, get_string(old_val) if old_val else None)
         else:
             self.gp_db.gpostore.delete(self.attribute)
             self.gp_db.delete(str(self), self.attribute)
index 41ff3e9c50005e72ef37b38bf7a37f720cd036f6..fb7c705a47e50643fd6f85c75f21aa5eaabe552d 100644 (file)
@@ -23,6 +23,7 @@ sys.path.insert(0, "bin/python")
 from samba import NTSTATUSError
 from samba.compat import ConfigParser
 from samba.compat import StringIO
+from samba.compat import get_bytes
 from abc import ABCMeta, abstractmethod
 import xml.etree.ElementTree as etree
 import re
@@ -258,24 +259,24 @@ class GPOStorage:
 
     def get_int(self, key):
         try:
-            return int(self.log.get(key))
+            return int(self.log.get(get_bytes(key)))
         except TypeError:
             return None
 
     def get(self, key):
-        return self.log.get(key)
+        return self.log.get(get_bytes(key))
 
     def get_gplog(self, user):
-        return gp_log(user, self, self.log.get(user))
+        return gp_log(user, self, self.log.get(get_bytes(user)))
 
     def store(self, key, val):
-        self.log.store(key, val)
+        self.log.store(get_bytes(key), get_bytes(val))
 
     def cancel(self):
         self.log.transaction_cancel()
 
     def delete(self, key):
-        self.log.delete(key)
+        self.log.delete(get_bytes(key))
 
     def commit(self):
         self.log.transaction_commit()
@@ -503,7 +504,7 @@ def parse_gpext_conf(smb_conf):
 
 def atomic_write_conf(lp, parser):
     ext_conf = lp.state_path('gpext.conf')
-    with NamedTemporaryFile(delete=False, dir=os.path.dirname(ext_conf)) as f:
+    with NamedTemporaryFile(mode="w+", delete=False, dir=os.path.dirname(ext_conf)) as f:
         parser.write(f)
         os.rename(f.name, ext_conf)
 
@@ -532,8 +533,8 @@ def register_gp_extension(guid, name, path,
         parser.add_section(guid)
     parser.set(guid, 'DllName', path)
     parser.set(guid, 'ProcessGroupPolicy', name)
-    parser.set(guid, 'NoMachinePolicy', 0 if machine else 1)
-    parser.set(guid, 'NoUserPolicy', 0 if user else 1)
+    parser.set(guid, 'NoMachinePolicy', "0" if machine else "1")
+    parser.set(guid, 'NoUserPolicy', "0" if user else "1")
 
     atomic_write_conf(lp, parser)
 
index 84e59edb9d68c1cf6582ab975e19ab5c8056fc88..70dc28c4c2374e82fe7106f1db9dffed3aec5385 100644 (file)
@@ -27,6 +27,7 @@ from tempfile import NamedTemporaryFile
 from samba.gp_sec_ext import gp_sec_ext
 import logging
 from samba.credentials import Credentials
+from samba.compat import get_bytes
 
 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'
@@ -62,7 +63,7 @@ def stage_file(path, data):
     if os.path.exists(path):
         os.rename(path, '%s.bak' % path)
     with NamedTemporaryFile(delete=False, dir=os.path.dirname(path)) as f:
-        f.write(data)
+        f.write(get_bytes(data))
         os.rename(f.name, path)
         os.chmod(path, 0o644)
     return True