python/samba/netcmd: Fix password usage for py2/py3 compatability
authorNoel Power <noel.power@suse.com>
Fri, 27 Apr 2018 14:11:54 +0000 (15:11 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 3 Sep 2018 01:22:23 +0000 (03:22 +0200)
getpass returns str (e.g. bytes) in python2 and str (unicode) in
py3. Adapt code to so we don't do illegal things (like try and decode)
a string in python3

Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/netcmd/domain.py
python/samba/provision/__init__.py

index f3a17eec6b8b52fc8ec328cdfdcf8063c305d142..56386c579c1be1e4e26a36264383ccee656b3d46 100644 (file)
@@ -102,6 +102,8 @@ from samba.provision.common import (
 from samba.netcmd.pso import cmd_domain_passwordsettings_pso
 from samba.netcmd.domain_backup import cmd_domain_backup
 
 from samba.netcmd.pso import cmd_domain_passwordsettings_pso
 from samba.netcmd.domain_backup import cmd_domain_backup
 
+from samba.compat import binary_type
+
 string_version_to_constant = {
     "2008_R2": DS_DOMAIN_FUNCTION_2008_R2,
     "2012": DS_DOMAIN_FUNCTION_2012,
 string_version_to_constant = {
     "2008_R2": DS_DOMAIN_FUNCTION_2008_R2,
     "2012": DS_DOMAIN_FUNCTION_2012,
@@ -570,8 +572,9 @@ class cmd_domain_provision(Command):
     def _adminpass_issue(self, adminpass):
         """Returns error string for a bad administrator password,
         or None if acceptable"""
     def _adminpass_issue(self, adminpass):
         """Returns error string for a bad administrator password,
         or None if acceptable"""
-
-        if len(adminpass.decode('utf-8')) < DEFAULT_MIN_PWD_LENGTH:
+        if isinstance(adminpass, binary_type):
+            adminpass = adminpass.decode('utf8')
+        if len(adminpass) < DEFAULT_MIN_PWD_LENGTH:
             return "Administrator password does not meet the default minimum" \
                 " password length requirement (%d characters)" \
                 % DEFAULT_MIN_PWD_LENGTH
             return "Administrator password does not meet the default minimum" \
                 " password length requirement (%d characters)" \
                 % DEFAULT_MIN_PWD_LENGTH
index 44434a215ff7590de6d4b481ea65cb3d4c5fc78e..9ecb2018060e949f11b946528e41cd814e74bef3 100644 (file)
@@ -28,6 +28,7 @@ __docformat__ = "restructuredText"
 
 from samba.compat import urllib_quote
 from samba.compat import string_types
 
 from samba.compat import urllib_quote
 from samba.compat import string_types
+from samba.compat import binary_type
 from base64 import b64encode
 import errno
 import os
 from base64 import b64encode
 import errno
 import os
@@ -2313,7 +2314,8 @@ def provision(logger, session_info, smbconf=None,
             adminpass = samba.generate_random_password(12, 32)
             adminpass_generated = True
         else:
             adminpass = samba.generate_random_password(12, 32)
             adminpass_generated = True
         else:
-            adminpass = unicode(adminpass, 'utf-8')
+            if isinstance(adminpass, binary_type):
+                adminpass = adminpass.decode('utf-8')
             adminpass_generated = False
 
         if samdb_fill == FILL_FULL:
             adminpass_generated = False
 
         if samdb_fill == FILL_FULL: