]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
samba-tool: try to present diagnostics for SDDL errors.
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Fri, 27 Oct 2023 00:16:56 +0000 (13:16 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 1 Nov 2023 20:10:46 +0000 (20:10 +0000)
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/netcmd/__init__.py
selftest/knownfail.d/samba-tool-sddl-errors [new file with mode: 0644]

index 31541a983474bd8149323799eaa3a06a5480dc44..7ddc1dc08289e18e74fe2e5ddb3b4b7c99c93617 100644 (file)
@@ -29,6 +29,7 @@ from samba.auth import system_session
 from samba.getopt import Option, OptionParser
 from samba.logger import get_samba_logger
 from samba.samdb import SamDB
+from samba.dcerpc.security import SDDLValueError
 
 from .encoders import JSONEncoder
 
@@ -115,6 +116,29 @@ class Command(object):
         else:
             print(f"{err}{klass}: {msg} - {evalue}", file=self.errf)
 
+    def _print_sddl_value_error(self, e):
+        generic_msg, specific_msg, position, sddl = e.args
+        print(f"{colour.c_DARK_RED('ERROR')}: {generic_msg}\n",
+              file=self.errf)
+        print(f' {sddl}', file=self.errf)
+        # If the SDDL contains non-ascii characters, the byte offset
+        # provided by the exception won't agree with the visual offset
+        # because those characters will be encoded as multiple bytes.
+        #
+        # To account for this we'll attempt to measure the string
+        # length of the specified number of bytes. That is not quite
+        # the same as the visual length, because the SDDL could
+        # contain zero-width, full-width, or combining characters, but
+        # it is closer.
+        try:
+            position = len((sddl.encode()[:position]).decode())
+        except ValueError:
+            # use the original position
+            pass
+
+        print(f"{colour.c_DARK_YELLOW('^'):>{position + 2}}", file=self.errf)
+        print(f' {specific_msg}', file=self.errf)
+
     def ldb_connect(self, hostopts, sambaopts, credopts):
         """Helper to connect to Ldb database using command line opts."""
         lp = sambaopts.get_loadparm()
@@ -163,6 +187,10 @@ class Command(object):
             else:
                 self._print_error(message, ldb_emsg, 'ldb')
 
+        elif isinstance(inner_exception, SDDLValueError):
+            self._print_sddl_value_error(inner_exception)
+            force_traceback = False
+
         elif isinstance(inner_exception, AssertionError):
             self._print_error(message, klass='assert')
             force_traceback = True
diff --git a/selftest/knownfail.d/samba-tool-sddl-errors b/selftest/knownfail.d/samba-tool-sddl-errors
new file mode 100644 (file)
index 0000000..a1b9e1a
--- /dev/null
@@ -0,0 +1 @@
+samba.tests.samba_tool.domain_auth_policy.samba.tests.samba_tool.domain_auth_policy.AuthPolicyCmdTestCase.test_authentication_policy_create_invalid_sddl