From: Douglas Bagnall Date: Thu, 15 Feb 2024 21:20:24 +0000 (+0000) Subject: pylibs: add string_is_guid() helper. X-Git-Tag: tdb-1.4.11~1659 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0fe263a56d049b62be71ced9d8a78bc0a749c195;p=thirdparty%2Fsamba.git pylibs: add string_is_guid() helper. 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 Signed-off-by: Douglas Bagnall Signed-off-by: Andrew Bartlett Reviewed-by: Reviewed-by: Andrew Bartlett Autobuild-User(master): Andrew Bartlett Autobuild-Date(master): Thu Feb 29 02:38:07 UTC 2024 on atb-devel-224 --- diff --git a/python/samba/__init__.py b/python/samba/__init__.py index 3e6ea7d18e1..3b253f7d79c 100644 --- a/python/samba/__init__.py +++ b/python/samba/__init__.py @@ -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."""