]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
pylibs: add string_is_guid() helper.
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Thu, 15 Feb 2024 21:20:24 +0000 (21:20 +0000)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 29 Feb 2024 02:38:06 +0000 (02:38 +0000)
In various places we use regular expressions to check for GUID-ness,
though typically we don't match GUIDs with uppercase hex digits when
we really should.

If we centralise the check, we have more chance of getting it right.

Pair-programmed-by: Andrew Bartlett <abartlet@samba.org>
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Thu Feb 29 02:38:07 UTC 2024 on atb-devel-224

python/samba/__init__.py

index 3e6ea7d18e1bfa7520d91100a8de9a262a7e9fac..3b253f7d79c04f06346b0cc17cbc543fbb0fd19a 100644 (file)
@@ -26,6 +26,7 @@ import os
 import time
 import ldb
 import samba.param
+import re
 from samba import _glue
 from samba._ldb import Ldb as _Ldb
 
@@ -356,6 +357,37 @@ def arcfour_encrypt(key, data):
     return arcfour_crypt_blob(data, key)
 
 
+GUID_RE = re.compile(
+    "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
+
+GUID_MIXCASE_RE = re.compile(
+    "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
+    flags = re.IGNORECASE)
+
+
+def string_is_guid(string, lower_case_only=False):
+    """Is the string an ordinary undecorated string GUID?
+
+    That is, like 12345678-abcd-1234-FEED-1234567890ab, and not like
+    variants which have surrounding curly brackets or lack hyphens.
+
+    If lower case_only is true, only lowercase hex characters are
+    accepted. This is tighter than we ever require, but matches what
+    we usually emit.
+    """
+    # Note: it is rightly tempting to use misc.GUID() here and catch
+    # the error, but misc.GUID is more forgiving than we want,
+    # allowing all kinds of weird variants.
+    if lower_case_only:
+        m = GUID_RE.fullmatch(string)
+    else:
+        m = GUID_MIXCASE_RE.fullmatch(string)
+
+    if m is None:
+        return False
+    return True
+
+
 def enable_net_export_keytab():
     """This function modifies the samba.net.Net class to contain
     an export_keytab() method."""