s4/libnet: Allow passwords containing non ascii characters to be passed
authorNoel Power <noel.power@suse.com>
Wed, 16 May 2018 15:46:41 +0000 (16:46 +0100)
committerNoel Power <npower@samba.org>
Thu, 17 May 2018 09:31:28 +0000 (11:31 +0200)
Although we can pass unicode to py_net_change_password unfortunately in
Python2 unicode strings are encoded with the default encoding (e.g. ascii)
 when extracting the unicode string to buffer.
In Python3 the default encoding for "s" format is utf8. Use the "es"
format instead of "s" so we can specify the encoding so behaviour is
correct in py2/py3.

Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source4/libnet/py_net.c

index 0567dbd6353d0c6328f8ac6ee532782e7fd514cb..6aff373b359e57f1946b758e48c9420cffc1893f 100644 (file)
@@ -155,21 +155,26 @@ static PyObject *py_net_change_password(py_net_Object *self, PyObject *args, PyO
 {
        union libnet_ChangePassword r;
        NTSTATUS status;
-       TALLOC_CTX *mem_ctx;
-       struct tevent_context *ev;
+       TALLOC_CTX *mem_ctx = NULL;
+       struct tevent_context *ev = NULL;
        const char *kwnames[] = { "newpassword", "oldpassword", "domain", "username", NULL };
-
+       const char *newpass = NULL;
+       const char *oldpass = NULL;
        ZERO_STRUCT(r);
-
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|sss:change_password",
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "es|esss:change_password",
                                         discard_const_p(char *, kwnames),
-                                        &r.generic.in.newpassword,
-                                        &r.generic.in.oldpassword,
+                                        "utf8",
+                                        &newpass,
+                                        "utf8",
+                                        &oldpass,
                                         &r.generic.in.domain_name,
                                         &r.generic.in.account_name)) {
                return NULL;
        }
 
+       r.generic.in.newpassword = newpass;
+       r.generic.in.oldpassword = oldpass;
+
        r.generic.level = LIBNET_CHANGE_PASSWORD_GENERIC;
        if (r.generic.in.account_name == NULL) {
                r.generic.in.account_name
@@ -200,12 +205,12 @@ static PyObject *py_net_change_password(py_net_Object *self, PyObject *args, PyO
                                             r.generic.out.error_string
                                             ? r.generic.out.error_string
                                             : nt_errstr(status));
-               talloc_free(mem_ctx);
                return NULL;
        }
 
        talloc_free(mem_ctx);
-
+       PyMem_Free(discard_const_p(char,newpass));
+       PyMem_Free(discard_const_p(char,oldpass));
        Py_RETURN_NONE;
 }