]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
samba-tool user: use _glue.crypt, not crypt.crypt
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 11 Dec 2024 02:54:48 +0000 (15:54 +1300)
committerAndreas Schneider <asn@cryptomilk.org>
Fri, 20 Dec 2024 07:04:31 +0000 (07:04 +0000)
Because we know we have _glue.crypt, and we know it raises exceptions
rather than returning None, we can simplify the checks.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15756

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andreas Schneider <asn@samba.org>
python/samba/netcmd/user/readpasswords/common.py

index 1bf29fe3eca8fb30936bcb52d884e34d25e61e43..0e68d042a55f06c6f34516b0c1c2f0955319d0b1 100644 (file)
@@ -37,6 +37,7 @@ from samba.netcmd import Command, CommandError
 from samba.samdb import SamDB
 from samba.nt_time import timedelta_from_nt_time_delta, nt_time_from_datetime
 from samba.gkdi import MAX_CLOCK_SKEW
+from samba._glue import crypt
 
 # python[3]-gpgme is abandoned since ubuntu 1804 and debian 9
 # have to use python[3]-gpg instead
@@ -132,9 +133,7 @@ def get_crypt_value(alg, utf8pw, rounds=0):
     else:
         crypt_salt = "$%s$%s$" % (alg, b64salt)
 
-    crypt_value = crypt.crypt(utf8pw, crypt_salt)
-    if crypt_value is None:
-        raise NotImplementedError("crypt.crypt(%s) returned None" % (crypt_salt))
+    crypt_value = crypt(utf8pw, crypt_salt)
     expected_len = len(crypt_salt) + algs[alg]["length"]
     if len(crypt_value) != expected_len:
         raise NotImplementedError("crypt.crypt(%s) returned a value with length %d, expected length is %d" % (
@@ -156,21 +155,13 @@ except ImportError as e:
 
 for (alg, attr) in [("5", "virtualCryptSHA256"), ("6", "virtualCryptSHA512")]:
     try:
-        import crypt
         get_crypt_value(alg, "")
-        virtual_attributes[attr] = {
-        }
-    except ImportError as e:
-        reason = "crypt"
-        reason += " required"
-        disabled_virtual_attributes[attr] = {
-            "reason": reason,
-        }
-    except NotImplementedError as e:
-        reason = "modern '$%s$' salt in crypt(3) required" % (alg)
+    except (ValueError, OSError):
         disabled_virtual_attributes[attr] = {
-            "reason": reason,
+            "reason": f"modern '${alg}$' salt in crypt(3) required"
         }
+        continue
+    virtual_attributes[attr] = {}
 
 # Add the wDigest virtual attributes, virtualWDigest01 to virtualWDigest29
 for x in range(1, 30):