pyglue: add generate_random_machine_password() wrapper
authorStefan Metzmacher <metze@samba.org>
Tue, 23 Aug 2016 07:35:50 +0000 (09:35 +0200)
committerRalph Boehme <slow@samba.org>
Tue, 21 Feb 2017 15:09:22 +0000 (16:09 +0100)
We use PyUnicode_FromString() (which is available from 2.6)
because we really have non-ascii strings.

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

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
python/pyglue.c
python/samba/__init__.py

index dbe7eb4ec690a9a0a92db7fbf53b45b8e9025ec6..0e80ba6260959b1d3970f299722a1215293e8dc1 100644 (file)
@@ -60,6 +60,23 @@ static PyObject *py_generate_random_password(PyObject *self, PyObject *args)
        return ret;
 }
 
+static PyObject *py_generate_random_machine_password(PyObject *self, PyObject *args)
+{
+       int min, max;
+       PyObject *ret;
+       char *retstr;
+       if (!PyArg_ParseTuple(args, "ii", &min, &max))
+               return NULL;
+
+       retstr = generate_random_machine_password(NULL, min, max);
+       if (retstr == NULL) {
+               return NULL;
+       }
+       ret = PyUnicode_FromString(retstr);
+       talloc_free(retstr);
+       return ret;
+}
+
 static PyObject *py_unix2nttime(PyObject *self, PyObject *args)
 {
        time_t t;
@@ -261,7 +278,14 @@ static PyMethodDef py_misc_methods[] = {
                "Generate random string with specified length." },
        { "generate_random_password", (PyCFunction)py_generate_random_password,
                METH_VARARGS, "generate_random_password(min, max) -> string\n"
-               "Generate random password with a length >= min and <= max." },
+               "Generate random password (based on printable ascii characters) "
+               "with a length >= min and <= max." },
+       { "generate_random_machine_password", (PyCFunction)py_generate_random_machine_password,
+               METH_VARARGS, "generate_random_machine_password(min, max) -> string\n"
+               "Generate random password "
+               "(based on random utf16 characters converted to utf8 or "
+               "random ascii characters if 'unix charset' is not 'utf8')"
+               "with a length >= min (at least 14) and <= max (at most 255)." },
        { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS,
                "unix2nttime(timestamp) -> nttime" },
        { "nttime2unix", (PyCFunction)py_nttime2unix, METH_VARARGS,
index 5f9153180939691c2d1133d96a942197f0616cbc..19d5e38e896fc1291a3991df75bc206badae8231 100644 (file)
@@ -396,6 +396,7 @@ nttime2string = _glue.nttime2string
 nttime2unix = _glue.nttime2unix
 unix2nttime = _glue.unix2nttime
 generate_random_password = _glue.generate_random_password
+generate_random_machine_password = _glue.generate_random_machine_password
 strcasecmp_m = _glue.strcasecmp_m
 strstr_m = _glue.strstr_m
 is_ntvfs_fileserver_built = _glue.is_ntvfs_fileserver_built