1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

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 <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
This commit is contained in:
Douglas Bagnall 2024-02-15 21:20:24 +00:00 committed by Andrew Bartlett
parent 7b089e1206
commit 0fe263a56d

View 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."""