From e5ce0a4d73a196ee4c92b68eb744434cfcf942ec Mon Sep 17 00:00:00 2001 From: Gary Lockyer Date: Thu, 2 Nov 2017 10:15:29 +1300 Subject: [PATCH] pyglue: Add function to generate a random byte string Adds a function to generate a random byte string using the samba random routines. Signed-off-by: Gary Lockyer Reviewed-by: Andrew Bartlett --- python/pyglue.c | 21 +++++++++++++++++++++ python/samba/__init__.py | 1 + 2 files changed, 22 insertions(+) diff --git a/python/pyglue.c b/python/pyglue.c index d928b4cda41..68b14a072e4 100644 --- a/python/pyglue.c +++ b/python/pyglue.c @@ -89,6 +89,22 @@ static PyObject *py_check_password_quality(PyObject *self, PyObject *args) return PyBool_FromLong(check_password_quality(pass)); } +static PyObject *py_generate_random_bytes(PyObject *self, PyObject *args) +{ + int len; + PyObject *ret; + uint8_t *bytes = NULL; + + if (!PyArg_ParseTuple(args, "i", &len)) + return NULL; + + bytes = talloc_zero_size(NULL, len); + generate_random_buffer(bytes, len); + ret = PyBytes_FromStringAndSize((const char *)bytes, len); + talloc_free(bytes); + return ret; +} + static PyObject *py_unix2nttime(PyObject *self, PyObject *args) { time_t t; @@ -335,6 +351,11 @@ static PyMethodDef py_misc_methods[] = { "is the NTVFS file server built in this installation?" }, { "is_heimdal_built", (PyCFunction)py_is_heimdal_built, METH_NOARGS, "is Samba built with Heimdal Kerberbos?" }, + { "generate_random_bytes", + (PyCFunction)py_generate_random_bytes, + METH_VARARGS, + "generate_random_bytes(len) -> bytes\n" + "Generate random bytes with specified length." }, { NULL } }; diff --git a/python/samba/__init__.py b/python/samba/__init__.py index 6ba7c998074..f62f5e39202 100644 --- a/python/samba/__init__.py +++ b/python/samba/__init__.py @@ -389,6 +389,7 @@ unix2nttime = _glue.unix2nttime generate_random_password = _glue.generate_random_password generate_random_machine_password = _glue.generate_random_machine_password check_password_quality = _glue.check_password_quality +generate_random_bytes = _glue.generate_random_bytes strcasecmp_m = _glue.strcasecmp_m strstr_m = _glue.strstr_m is_ntvfs_fileserver_built = _glue.is_ntvfs_fileserver_built -- 2.47.3