]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
pidl/Python: allow ndr_print(print_secrets=True)
authorStefan Metzmacher <metze@samba.org>
Fri, 22 Nov 2024 14:00:23 +0000 (15:00 +0100)
committerStefan Metzmacher <metze@samba.org>
Thu, 5 Dec 2024 16:46:37 +0000 (16:46 +0000)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
pidl/lib/Parse/Pidl/Samba4/Python.pm

index d7ccf8309e86868dc9f2c8405d6342866f205401..66c90cfd051a786049bc807733fcb2041f8f1a84 100644 (file)
@@ -336,14 +336,36 @@ sub PythonStruct($$$$$$)
                $self->pidl("}");
                $self->pidl("");
 
-               $self->pidl("static PyObject *py_$name\_ndr_print(PyObject *py_obj, PyObject *Py_UNUSED(ignored))");
+               $self->pidl("static PyObject *py_$name\_ndr_print(PyObject *py_obj, PyObject *args, PyObject *kwargs)");
                $self->pidl("{");
                $self->indent;
                $self->pidl("$cname *object = pytalloc_get_ptr(py_obj);");
+               $self->pidl("const char * const kwnames[] = { \"print_secrets\", NULL };");
+               $self->pidl("PyObject *py_print_secrets = NULL;");
                $self->pidl("PyObject *ret;");
                $self->pidl("char *retstr;");
+               $self->pidl("bool ok;");
                $self->pidl("");
+               $self->pidl("ok = PyArg_ParseTupleAndKeywords(args, kwargs, \"|O:__ndr_print__\",");
+               $self->indent;
+               $self->pidl("discard_const_p(char *, kwnames),");
+               $self->pidl("&py_print_secrets);");
+               $self->deindent;
+               $self->pidl("if (!ok) {");
+               $self->indent;
+               $self->pidl("return NULL;");
+               $self->deindent;
+               $self->pidl("}");
+               $self->pidl("");
+               $self->pidl("if (py_print_secrets != NULL && PyObject_IsTrue(py_print_secrets)) {");
+               $self->indent;
+               $self->pidl("retstr = ndr_print_struct_secret_string(pytalloc_get_mem_ctx(py_obj), (ndr_print_fn_t)ndr_print_$name, \"$name\", object);");
+               $self->deindent;
+               $self->pidl("} else {");
+               $self->indent;
                $self->pidl("retstr = ndr_print_struct_string(pytalloc_get_mem_ctx(py_obj), (ndr_print_fn_t)ndr_print_$name, \"$name\", object);");
+               $self->pidl("}");
+               $self->deindent;
                $self->pidl("ret = PyUnicode_FromString(retstr);");
                $self->pidl("talloc_free(retstr);");
                $self->pidl("");
@@ -357,7 +379,7 @@ sub PythonStruct($$$$$$)
                $self->indent;
                $self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.ndr_pack(object) -> blob\\nNDR pack\" },");
                $self->pidl("{ \"__ndr_unpack__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_unpack), METH_VARARGS|METH_KEYWORDS, \"S.ndr_unpack(class, blob, allow_remaining=False) -> None\\nNDR unpack\" },");
-               $self->pidl("{ \"__ndr_print__\", (PyCFunction)py_$name\_ndr_print, METH_NOARGS, \"S.ndr_print(object) -> None\\nNDR print\" },");
+               $self->pidl("{ \"__ndr_print__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_print), METH_VARARGS|METH_KEYWORDS, \"S.ndr_print(object, print_secrets=False) -> String\\nNDR print\" },");
                $self->pidl("{ NULL, NULL, 0, NULL }");
                $self->deindent;
                $self->pidl("};");
@@ -823,13 +845,27 @@ sub PythonFunctionStruct($$$$)
        $self->pidl("}");
        $self->pidl("");
 
-       $self->pidl("static PyObject *py_$name\_ndr_print(PyObject *py_obj, const char *name, ndr_flags_type ndr_inout_flags)");
+       $self->pidl("static PyObject *py_$name\_ndr_print(PyObject *py_obj, PyObject *args, PyObject *kwargs, const char *name, ndr_flags_type ndr_inout_flags)");
        $self->pidl("{");
        $self->indent;
        $self->pidl("$ndr_call");
        $self->pidl("$object_ptr");
+       $self->pidl("const char * const kwnames[] = { \"print_secrets\", NULL };");
+       $self->pidl("PyObject *py_print_secrets = NULL;");
        $self->pidl("PyObject *ret;");
        $self->pidl("char *retstr;");
+       $self->pidl("bool ok;");
+       $self->pidl("");
+       $self->pidl("ok = PyArg_ParseTupleAndKeywords(args, kwargs, \"|O:__ndr_print__\",");
+       $self->indent;
+       $self->pidl("discard_const_p(char *, kwnames),");
+       $self->pidl("&py_print_secrets);");
+       $self->deindent;
+       $self->pidl("if (!ok) {");
+       $self->indent;
+       $self->pidl("return NULL;");
+       $self->deindent;
+       $self->pidl("}");
        $self->pidl("");
        $self->pidl("if (ndr_table_$iface\.num_calls < " . ($fn->{OPNUM}+1) .
                    ") {");
@@ -840,7 +876,15 @@ sub PythonFunctionStruct($$$$)
        $self->pidl("}");
        $self->pidl("call = &ndr_table_$iface\.calls[$fn->{OPNUM}];");
        $self->pidl("");
+       $self->pidl("if (py_print_secrets != NULL && PyObject_IsTrue(py_print_secrets)) {");
+       $self->indent;
+       $self->pidl("retstr = ndr_print_function_secret_string(pytalloc_get_mem_ctx(py_obj), call->ndr_print, name, ndr_inout_flags, object);");
+       $self->deindent;
+       $self->pidl("} else {");
+       $self->indent;
        $self->pidl("retstr = ndr_print_function_string(pytalloc_get_mem_ctx(py_obj), call->ndr_print, name, ndr_inout_flags, object);");
+       $self->deindent;
+       $self->pidl("}");
        $self->pidl("ret = PyUnicode_FromString(retstr);");
        $self->pidl("TALLOC_FREE(retstr);");
        $self->pidl("");
@@ -849,18 +893,18 @@ sub PythonFunctionStruct($$$$)
        $self->pidl("}");
        $self->pidl("");
 
-       $self->pidl("static PyObject *py_$name\_ndr_print_in(PyObject *py_obj, PyObject *Py_UNUSED(ignored))");
+       $self->pidl("static PyObject *py_$name\_ndr_print_in(PyObject *py_obj, PyObject *args, PyObject *kwargs)");
        $self->pidl("{");
        $self->indent;
-       $self->pidl("return py_$name\_ndr_print(py_obj, \"$name\_in\", NDR_IN);");
+       $self->pidl("return py_$name\_ndr_print(py_obj, args, kwargs, \"$name\_in\", NDR_IN);");
        $self->deindent;
        $self->pidl("}");
        $self->pidl("");
 
-       $self->pidl("static PyObject *py_$name\_ndr_print_out(PyObject *py_obj, PyObject *Py_UNUSED(ignored))");
+       $self->pidl("static PyObject *py_$name\_ndr_print_out(PyObject *py_obj, PyObject *args, PyObject *kwargs)");
        $self->pidl("{");
        $self->indent;
-       $self->pidl("return py_$name\_ndr_print(py_obj, \"$name\_out\", NDR_OUT);");
+       $self->pidl("return py_$name\_ndr_print(py_obj, args, kwargs, \"$name\_out\", NDR_OUT);");
        $self->deindent;
        $self->pidl("}");
        $self->pidl("");
@@ -888,8 +932,14 @@ sub PythonFunctionStruct($$$$)
        $self->indent;
        $self->pidl("\"S.ndr_unpack_out(class, blob, bigendian=False, ndr64=False, allow_remaining=False) -> None\\nNDR unpack output\" },");
        $self->deindent;
-       $self->pidl("{ \"__ndr_print_in__\", (PyCFunction)py_$name\_ndr_print_in, METH_NOARGS, \"S.ndr_print_in(object) -> None\\nNDR print input\" },");
-       $self->pidl("{ \"__ndr_print_out__\", (PyCFunction)py_$name\_ndr_print_out, METH_NOARGS, \"S.ndr_print_out(object) -> None\\nNDR print output\" },");
+       $self->pidl("{ \"__ndr_print_in__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_print_in), METH_VARARGS|METH_KEYWORDS,");
+       $self->indent;
+       $self->pidl("\"S.ndr_print_in(object, print_secrets=False) -> String\\nNDR print input\" },");
+       $self->deindent;
+       $self->pidl("{ \"__ndr_print_out__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_print_out), METH_VARARGS|METH_KEYWORDS,");
+       $self->indent;
+       $self->pidl("\"S.ndr_print_out(object, print_secrets=False) -> String\\nNDR print output\" },");
+       $self->deindent;
        $self->pidl("{ NULL, NULL, 0, NULL }");
        $self->deindent;
        $self->pidl("};");