python: Add glue.burn_commandline() method
authorAndrew Bartlett <abartlet@samba.org>
Fri, 21 Jul 2023 01:29:22 +0000 (13:29 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 21 Jul 2023 05:23:32 +0000 (05:23 +0000)
This uses samba_cmdline_burn() to as to have common
command line redaction code.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15289

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/pyglue.c
python/wscript

index 6b9814072e7cfe9902fc664b2b72480e2f773e14..901a9a37be58cd673f61cc25c8f6cfbf224d5cb2 100644 (file)
@@ -26,6 +26,7 @@
 #include "lib/socket/netif.h"
 #include "lib/util/debug.h"
 #include "librpc/ndr/ndr_private.h"
+#include "lib/cmdline/cmdline.h"
 
 void init_glue(void);
 static PyObject *PyExc_NTSTATUSError;
@@ -466,6 +467,62 @@ static PyObject *py_strstr_m(PyObject *self, PyObject *args)
        return result;
 }
 
+static PyObject *py_get_burnt_commandline(PyObject *self, PyObject *args)
+{
+       PyObject *cmdline_as_list, *ret;
+       char *burnt_cmdline = NULL;
+       Py_ssize_t i, argc;
+       char **argv = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
+       bool burnt;
+
+       if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &cmdline_as_list))
+       {
+               TALLOC_FREE(frame);
+               return NULL;
+       }
+
+       argc = PyList_GET_SIZE(cmdline_as_list);
+
+       if (argc == 0) {
+               TALLOC_FREE(frame);
+               Py_RETURN_NONE;
+       }
+
+       argv = PyList_AsStringList(frame, cmdline_as_list, "sys.argv");
+       if (argv == NULL) {
+               return NULL;
+       }
+
+       burnt = samba_cmdline_burn(argc, argv);
+       if (!burnt) {
+               TALLOC_FREE(frame);
+               Py_RETURN_NONE;
+       }
+
+       for (i = 0; i < argc; i++) {
+               if (i == 0) {
+                       burnt_cmdline = talloc_strdup(frame,
+                                                     argv[i]);
+               } else {
+                       burnt_cmdline
+                               = talloc_asprintf_append(burnt_cmdline,
+                                                        " %s",
+                                                        argv[i]);
+               }
+               if (burnt_cmdline == NULL) {
+                       PyErr_NoMemory();
+                       TALLOC_FREE(frame);
+                       return NULL;
+               }
+       }
+
+       ret = PyUnicode_FromString(burnt_cmdline);
+       TALLOC_FREE(frame);
+
+       return ret;
+}
+
 static PyMethodDef py_misc_methods[] = {
        { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS,
                "generate_random_str(len) -> string\n"
@@ -525,6 +582,8 @@ static PyMethodDef py_misc_methods[] = {
                METH_NOARGS, "is Samba built with selftest enabled?" },
        { "ndr_token_max_list_size", (PyCFunction)py_ndr_token_max_list_size,
                METH_NOARGS, "How many NDR internal tokens is too many for this build?" },
+       { "get_burnt_commandline", (PyCFunction)py_get_burnt_commandline,
+               METH_VARARGS, "Return a redacted commandline to feed to setproctitle (None if no redaction required)" },
        {0}
 };
 
index abe88b00129eaf020753c6c1bbebbf6aa8906823..a4573c57c6e5541cb5cb2036150daab91b1df24d 100644 (file)
@@ -117,6 +117,7 @@ def build(bld):
                               samba-util
                               netif
                               ndr
+                              cmdline
                               %s
                               ''' % (pyparam_util, pytalloc_util),
                      realname='samba/_glue.so')