auth/credentials: PY3 set_password should decode from unicode 'utf8'
authorNoel Power <noel.power@suse.com>
Thu, 8 Nov 2018 15:03:52 +0000 (15:03 +0000)
committerNoel Power <npower@samba.org>
Mon, 10 Dec 2018 09:38:21 +0000 (10:38 +0100)
set_password processes input using ParseTuple with "s" format, this
accepts string or unicode but...

Some py2 code is incorrectly using code like

   credentials.set_password(pass.encode('utf8'))

however that won't work in PY3. We should just make sure the string
retrieved from unicode passed in is encoded with 'utf8'
Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
auth/credentials/pycredentials.c

index 56174fefbb4f9002ba0255c32e9b24999377ab43..fa8ac2bcb9f7604ce691072a5e857efce3930287 100644 (file)
@@ -171,16 +171,18 @@ static PyObject *py_creds_get_password(PyObject *self, PyObject *unused)
 
 static PyObject *py_creds_set_password(PyObject *self, PyObject *args)
 {
-       char *newval;
+       const char *newval = NULL;
        enum credentials_obtained obt = CRED_SPECIFIED;
        int _obt = obt;
-
-       if (!PyArg_ParseTuple(args, "s|i", &newval, &_obt)) {
+       PyObject *result = NULL;
+       if (!PyArg_ParseTuple(args, "es|i", "utf8", &newval, &_obt)) {
                return NULL;
        }
        obt = _obt;
 
-       return PyBool_FromLong(cli_credentials_set_password(PyCredentials_AsCliCredentials(self), newval, obt));
+       result = PyBool_FromLong(cli_credentials_set_password(PyCredentials_AsCliCredentials(self), newval, obt));
+       PyMem_Free(discard_const_p(void*, newval));
+       return result;
 }
 
 static PyObject *py_creds_set_utf16_password(PyObject *self, PyObject *args)