python: Generate HRESULT definitions automatically
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Thu, 11 Jan 2024 03:23:55 +0000 (16:23 +1300)
committerJoseph Sutton <jsutton@samba.org>
Mon, 15 Jan 2024 01:56:53 +0000 (01:56 +0000)
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Autobuild-User(master): Joseph Sutton <jsutton@samba.org>
Autobuild-Date(master): Mon Jan 15 01:56:53 UTC 2024 on atb-devel-224

libcli/util/wscript_build
python/pyglue.c
python/samba/__init__.py
python/samba/tests/gkdi.py
python/samba/tests/krb5/authn_policy_tests.py
python/samba/tests/krb5/gkdi_tests.py
python/samba/tests/krb5/protected_users_tests.py
source4/dsdb/tests/python/passwords.py
source4/dsdb/tests/python/sam.py
source4/scripting/bin/gen_hresult.py

index 0afce5a33fbaca620661e445be27b7ab27fc3bf6..340ea420549df0f1728cd0ea9ef50c9078278b34 100644 (file)
@@ -15,9 +15,9 @@ bld.SAMBA_LIBRARY('samba-errors',
 
 bld.SAMBA_GENERATOR('hresult_generated',
                     source='../../source4/scripting/bin/gen_hresult.py hresult_err_table.txt ../../source4/scripting/bin/gen_error_common.py',
-                    target='hresult.h hresult.c',
+                    target='hresult.h hresult.c py_hresult.c',
                     group='build_source',
-                    rule='${PYTHON} ${SRC[0].abspath(env)} ${SRC[1].abspath(env)} ${TGT[0].abspath(env)} ${TGT[1].abspath(env)}'
+                    rule='${PYTHON} ${SRC[0].abspath(env)} ${SRC[1].abspath(env)} ${TGT[0].abspath(env)} ${TGT[1].abspath(env)} ${TGT[2].abspath(env)}'
                    )
 
 bld.SAMBA_GENERATOR('ntstatus_generated',
@@ -39,6 +39,12 @@ bld.SAMBA_GENERATOR('werror_generated',
                     rule='${PYTHON} ${SRC[0].abspath(env)} ${SRC[1].abspath(env)} ${TGT[0].abspath(env)} ${TGT[1].abspath(env)} ${TGT[2].abspath(env)} ${TGT[3].abspath(env)}'
                    )
 
+bld.SAMBA_PYTHON('python_hresult',
+               source='py_hresult.c',
+               deps='samba-errors',
+               realname='samba/hresult.so'
+               )
+
 bld.SAMBA_PYTHON('python_ntstatus',
                source='py_ntstatus.c',
                deps='samba-errors',
index 77cd556e0da79bbeece703222b0c5045e27e0588..c24d1b033a486c3cea8a09407e2505dfaa5723aa 100644 (file)
@@ -27,7 +27,6 @@
 #include "lib/util/debug.h"
 #include "librpc/ndr/ndr_private.h"
 #include "lib/cmdline/cmdline.h"
-#include "libcli/util/hresult.h"
 #include "lib/crypto/gkdi.h"
 
 void init_glue(void);
@@ -636,17 +635,6 @@ MODULE_INIT_FUNC(_glue)
                PyModule_AddObject(m, "DsExtendedError", PyExc_DsExtendedError);
        }
 
-       PyModule_AddObject(m, "HRES_E_INVALIDARG",
-                          PyLong_FromUnsignedLongLong(HRES_ERROR_V(HRES_E_INVALIDARG)));
-       PyModule_AddObject(m, "HRES_NTE_BAD_KEY",
-                          PyLong_FromUnsignedLongLong(HRES_ERROR_V(HRES_NTE_BAD_KEY)));
-       PyModule_AddObject(m, "HRES_NTE_NO_KEY",
-                          PyLong_FromUnsignedLongLong(HRES_ERROR_V(HRES_NTE_NO_KEY)));
-       PyModule_AddObject(m, "HRES_SEC_E_INVALID_TOKEN",
-                          PyLong_FromUnsignedLongLong(HRES_ERROR_V(HRES_SEC_E_INVALID_TOKEN)));
-       PyModule_AddObject(m, "HRES_SEC_E_LOGON_DENIED",
-                          PyLong_FromUnsignedLongLong(HRES_ERROR_V(HRES_SEC_E_LOGON_DENIED)));
-
        ret = PyModule_AddIntConstant(m, "GKDI_L1_KEY_ITERATION", gkdi_l1_key_iteration);
        if (ret) {
                Py_DECREF(m);
index d8eb16d204ec7a7ff1d6d1805b6e95ebb5b7a380..3e6ea7d18e1bfa7520d91100a8de9a262a7e9fac 100644 (file)
@@ -398,9 +398,3 @@ NTSTATUSError = _glue.NTSTATUSError
 HRESULTError = _glue.HRESULTError
 WERRORError = _glue.WERRORError
 DsExtendedError = _glue.DsExtendedError
-
-HRES_E_INVALIDARG = _glue.HRES_E_INVALIDARG
-HRES_NTE_BAD_KEY = _glue.HRES_NTE_BAD_KEY
-HRES_NTE_NO_KEY = _glue.HRES_NTE_NO_KEY
-HRES_SEC_E_LOGON_DENIED = _glue.HRES_SEC_E_LOGON_DENIED
-HRES_SEC_E_INVALID_TOKEN = _glue.HRES_SEC_E_INVALID_TOKEN
index 623e0e5e092e62a288ddf5460b4cd45608666312..b59a837a9ca439b80f72301e7cc43113fd14a3c0 100644 (file)
@@ -34,9 +34,6 @@ from cryptography.hazmat.primitives import hashes
 from cryptography.hazmat.primitives.kdf.kbkdf import CounterLocation, KBKDFHMAC, Mode
 
 from samba import (
-    HRES_E_INVALIDARG,
-    HRES_NTE_BAD_KEY,
-    HRES_NTE_NO_KEY,
     ntstatus,
     NTSTATUSError,
     werror,
@@ -53,6 +50,11 @@ from samba.gkdi import (
     MAX_CLOCK_SKEW,
     SeedKeyPair,
 )
+from samba.hresult import (
+    HRES_E_INVALIDARG,
+    HRES_NTE_BAD_KEY,
+    HRES_NTE_NO_KEY,
+)
 from samba.ndr import ndr_pack, ndr_unpack
 from samba.nt_time import (
     nt_time_from_datetime,
index 6ce2e41d1c78b3461e31fdddeffdf96092b0993f..2f15f8b24171b0529cf19ff90196106b370e8299 100755 (executable)
@@ -38,7 +38,7 @@ from samba.netcmd.domain.models import AuthenticationPolicy, AuthenticationSilo
 
 import samba.tests
 import samba.tests.krb5.kcrypto as kcrypto
-from samba import HRES_SEC_E_INVALID_TOKEN, HRES_SEC_E_LOGON_DENIED
+from samba.hresult import HRES_SEC_E_INVALID_TOKEN, HRES_SEC_E_LOGON_DENIED
 from samba.tests.krb5.kdc_base_test import GroupType
 from samba.tests.krb5.kdc_tgs_tests import KdcTgsBaseTests
 from samba.tests.auth_log_base import AuthLogTestBase, NoMessageException
index a2a074f81ec7e5a8e6908eac7323e095ee5067c4..58a65c4c764a26ad4ffcf6a397ab41244bcf2e86 100755 (executable)
@@ -26,7 +26,6 @@ import secrets
 
 from typing import ClassVar, Optional
 
-from samba import HRES_E_INVALIDARG, HRES_NTE_BAD_KEY, HRES_NTE_NO_KEY
 from samba.dcerpc import gkdi, misc
 from samba.gkdi import (
     Algorithm,
@@ -38,6 +37,7 @@ from samba.gkdi import (
     NtTimeDelta,
     SeedKeyPair,
 )
+from samba.hresult import HRES_E_INVALIDARG, HRES_NTE_BAD_KEY, HRES_NTE_NO_KEY
 from samba.nt_time import timedelta_from_nt_time_delta
 
 from samba.tests.gkdi import GetKeyError, GkdiBaseTest, ROOT_KEY_START_TIME
index b2955a874e162a35cac1fac5fde9d754c2adb3a9..fee78ab1ae23bfa9286cd617fe649bbb903a52f3 100755 (executable)
@@ -26,8 +26,9 @@ from functools import partial
 
 import ldb
 
-from samba import generate_random_password, ntstatus, HRES_SEC_E_LOGON_DENIED
+from samba import generate_random_password, ntstatus
 from samba.dcerpc import netlogon, security
+from samba.hresult import HRES_SEC_E_LOGON_DENIED
 
 import samba.tests.krb5.kcrypto as kcrypto
 from samba.tests.krb5.kdc_base_test import KDCBaseTest
index 73fd0275a6d8509a174d1b68e4873c554db54831..d431486e06957f5ac4a1beb62f8e94174b9166ce 100755 (executable)
@@ -21,10 +21,10 @@ from samba.tests.password_test import PasswordTestCase
 
 import samba.getopt as options
 
-from samba import HRES_SEC_E_INVALID_TOKEN
 from samba.auth import system_session
 from samba.credentials import Credentials
 from samba.dcerpc import security
+from samba.hresult import HRES_SEC_E_INVALID_TOKEN
 from ldb import SCOPE_BASE, LdbError
 from ldb import ERR_ATTRIBUTE_OR_VALUE_EXISTS
 from ldb import ERR_UNWILLING_TO_PERFORM, ERR_INSUFFICIENT_ACCESS_RIGHTS
index bbc6ba8a58f653f02a5529e68462e17c317a47fc..f1c1c0f4cf69a69965896cebcbd9ba80f5d72882 100755 (executable)
@@ -9,7 +9,7 @@ import time
 
 sys.path.insert(0, "bin/python")
 import samba
-from samba import HRES_SEC_E_INVALID_TOKEN, HRES_SEC_E_LOGON_DENIED
+from samba.hresult import HRES_SEC_E_INVALID_TOKEN, HRES_SEC_E_LOGON_DENIED
 from samba.tests.subunitrun import SubunitOptions, TestProgram
 
 import samba.getopt as options
index 8929453933636c7cba456796055c64bbbc01fa4e..2ac80029270aa4b89214da90150ccb297f744d37 100755 (executable)
@@ -144,26 +144,63 @@ def generateSourceFile(out_file, errors):
     out_file.write("   return msg;\n")
     out_file.write("};\n")
 
+def generatePythonFile(out_file, errors):
+    out_file.write("/*\n")
+    out_file.write(" * Errors generated from\n")
+    out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704587.aspx\n")
+    out_file.write(" */\n")
+    out_file.write("#include \"lib/replace/system/python.h\"\n")
+    out_file.write("#include \"python/py3compat.h\"\n")
+    out_file.write("#include \"includes.h\"\n\n")
+    out_file.write("static struct PyModuleDef moduledef = {\n")
+    out_file.write("\tPyModuleDef_HEAD_INIT,\n")
+    out_file.write("\t.m_name = \"hresult\",\n")
+    out_file.write("\t.m_doc = \"HRESULT defines\",\n")
+    out_file.write("\t.m_size = -1,\n")
+    out_file.write("\t};\n\n")
+    out_file.write("MODULE_INIT_FUNC(hresult)\n")
+    out_file.write("{\n")
+    out_file.write("\tPyObject *m = NULL;\n")
+    out_file.write("\tPyObject *py_obj = NULL;\n")
+    out_file.write("\tint ret;\n\n")
+    out_file.write("\tm = PyModule_Create(&moduledef);\n")
+    out_file.write("\tif (m == NULL) {\n")
+    out_file.write("\t\treturn NULL;\n")
+    out_file.write("\t}\n\n")
+    for err in errors:
+        out_file.write(f"\tpy_obj = PyLong_FromUnsignedLongLong(HRES_ERROR_V({err.err_define}));\n")
+        out_file.write(f"\tret = PyModule_AddObject(m, \"{err.err_define}\", py_obj);\n")
+        out_file.write("\tif (ret) {\n")
+        out_file.write("\t\tPy_XDECREF(py_obj);\n")
+        out_file.write("\t\tPy_DECREF(m);\n")
+        out_file.write("\t\treturn NULL;\n")
+        out_file.write("\t}\n")
+    out_file.write("\n")
+    out_file.write("\treturn m;\n")
+    out_file.write("}\n")
+
 def transformErrorName(error_name):
     return "HRES_" + error_name
 
 # Very simple script to generate files hresult.c & hresult.h
-# This script takes three inputs:
+# This script takes four inputs:
 # [1]: The name of the text file which is the content of an HTML table
 #      (such as that found at https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/705fb797-2175-4a90-b5a3-3918024b10b8)
 #      copied and pasted.
 # [2]: The name of the output generated header file with HResult #defines
 # [3]: The name of the output generated source file with C arrays
+# [4]: The name of the output generated python file
 
 def main ():
     input_file1 = None
 
-    if len(sys.argv) == 4:
+    if len(sys.argv) == 5:
         input_file1 =  sys.argv[1]
         gen_headerfile_name = sys.argv[2]
         gen_sourcefile_name = sys.argv[3]
+        gen_pythonfile_name = sys.argv[4]
     else:
-        print("usage: %s winerrorfile headerfile sourcefile"%(sys.argv[0]))
+        print("usage: %s winerrorfile headerfile sourcefile pythonfile"%(sys.argv[0]))
         sys.exit()
 
     # read in the data
@@ -176,6 +213,9 @@ def main ():
     print(f"writing new source file: {gen_sourcefile_name}")
     with open(gen_sourcefile_name,"w") as out_file:
         generateSourceFile(out_file, errors)
+    print(f"writing new python file: {gen_pythonfile_name}")
+    with open(gen_pythonfile_name,"w") as out_file:
+        generatePythonFile(out_file, errors)
 
 if __name__ == '__main__':