mirror of
https://github.com/samba-team/samba.git
synced 2025-10-22 07:33:16 +03:00
python: Generate HRESULT definitions automatically
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
This commit is contained in:
committed by
Joseph Sutton
parent
5199d788f4
commit
f30a79d7f2
@@ -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',
|
||||
|
@@ -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);
|
||||
|
@@ -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
|
||||
|
@@ -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,
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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__':
|
||||
|
||||
|
Reference in New Issue
Block a user