docs: fix a typo in history file
[bbaumbach/samba-autobuild/.git] / source3 / passdb / py_passdb.c
index 31e3907c8bc51edbc383e2de61197df7d9482459..2d5b2ab57052cb7ef822cbfb73419a6cca35a90f 100644 (file)
@@ -27,6 +27,7 @@
 #include "passdb.h"
 #include "secrets.h"
 #include "idmap.h"
+#include "lib/util/string_wrappers.h"
 
 #ifndef Py_TYPE /* Py_TYPE is only available on Python > 2.6 */
 #define Py_TYPE(ob)             (((PyObject*)(ob))->ob_type)
@@ -62,7 +63,7 @@ static PyObject *py_samu_get_logon_time(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_logon_time;
 
-       py_logon_time = PyInt_FromLong(pdb_get_logon_time(sam_acct));
+       py_logon_time = PyLong_FromLong(pdb_get_logon_time(sam_acct));
        talloc_free(frame);
        return py_logon_time;
 }
@@ -72,8 +73,8 @@ static int py_samu_set_logon_time(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_logon_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_logon_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -87,7 +88,7 @@ static PyObject *py_samu_get_logoff_time(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_logoff_time;
 
-       py_logoff_time = PyInt_FromLong(pdb_get_logoff_time(sam_acct));
+       py_logoff_time = PyLong_FromLong(pdb_get_logoff_time(sam_acct));
        talloc_free(frame);
        return py_logoff_time;
 }
@@ -97,8 +98,8 @@ static int py_samu_set_logoff_time(PyObject *obj, PyObject *value, void *closure
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_logoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_logoff_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -112,7 +113,7 @@ static PyObject *py_samu_get_kickoff_time(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_kickoff_time;
 
-       py_kickoff_time = PyInt_FromLong(pdb_get_kickoff_time(sam_acct));
+       py_kickoff_time = PyLong_FromLong(pdb_get_kickoff_time(sam_acct));
        talloc_free(frame);
        return py_kickoff_time;
 }
@@ -122,8 +123,8 @@ static int py_samu_set_kickoff_time(PyObject *obj, PyObject *value, void *closur
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_kickoff_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_kickoff_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -137,7 +138,7 @@ static PyObject *py_samu_get_bad_password_time(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_bad_password_time;
 
-       py_bad_password_time = PyInt_FromLong(pdb_get_bad_password_time(sam_acct));
+       py_bad_password_time = PyLong_FromLong(pdb_get_bad_password_time(sam_acct));
        talloc_free(frame);
        return py_bad_password_time;
 }
@@ -147,8 +148,8 @@ static int py_samu_set_bad_password_time(PyObject *obj, PyObject *value, void *c
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_bad_password_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_bad_password_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -162,7 +163,7 @@ static PyObject *py_samu_get_pass_last_set_time(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_pass_last_set_time;
 
-       py_pass_last_set_time = PyInt_FromLong(pdb_get_pass_last_set_time(sam_acct));
+       py_pass_last_set_time = PyLong_FromLong(pdb_get_pass_last_set_time(sam_acct));
        talloc_free(frame);
        return py_pass_last_set_time;
 }
@@ -172,8 +173,8 @@ static int py_samu_set_pass_last_set_time(PyObject *obj, PyObject *value, void *
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_pass_last_set_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_pass_last_set_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -187,7 +188,7 @@ static PyObject *py_samu_get_pass_can_change_time(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_pass_can_change_time;
 
-       py_pass_can_change_time = PyInt_FromLong(pdb_get_pass_can_change_time(sam_acct));
+       py_pass_can_change_time = PyLong_FromLong(pdb_get_pass_can_change_time(sam_acct));
        talloc_free(frame);
        return py_pass_can_change_time;
 }
@@ -197,8 +198,8 @@ static int py_samu_set_pass_can_change_time(PyObject *obj, PyObject *value, void
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_pass_can_change_time(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_pass_can_change_time(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -212,7 +213,7 @@ static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_pass_must_change_time;
 
-       py_pass_must_change_time = PyInt_FromLong(pdb_get_pass_must_change_time(sam_acct));
+       py_pass_must_change_time = PyLong_FromLong(pdb_get_pass_must_change_time(sam_acct));
        talloc_free(frame);
        return py_pass_must_change_time;
 }
@@ -220,7 +221,7 @@ static PyObject *py_samu_get_pass_must_change_time(PyObject *obj, void *closure)
 static int py_samu_set_pass_must_change_time(PyObject *obj, PyObject *value, void *closure)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
 
        /* TODO: make this not a get/set or give a better exception */
        talloc_free(frame);
@@ -239,7 +240,7 @@ static PyObject *py_samu_get_username(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_username = PyStr_FromString(username);
+       py_username = PyUnicode_FromString(username);
        talloc_free(frame);
        return py_username;
 }
@@ -249,8 +250,8 @@ static int py_samu_set_username(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_username(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_username(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -270,7 +271,7 @@ static PyObject *py_samu_get_domain(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_domain = PyStr_FromString(domain);
+       py_domain = PyUnicode_FromString(domain);
        talloc_free(frame);
        return py_domain;
 }
@@ -280,8 +281,8 @@ static int py_samu_set_domain(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_domain(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_domain(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -301,7 +302,7 @@ static PyObject *py_samu_get_nt_username(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_nt_username = PyStr_FromString(nt_username);
+       py_nt_username = PyUnicode_FromString(nt_username);
        talloc_free(frame);
        return py_nt_username;
 }
@@ -311,8 +312,8 @@ static int py_samu_set_nt_username(PyObject *obj, PyObject *value, void *closure
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_nt_username(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_nt_username(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -332,7 +333,7 @@ static PyObject *py_samu_get_full_name(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_full_name = PyStr_FromString(full_name);
+       py_full_name = PyUnicode_FromString(full_name);
        talloc_free(frame);
        return py_full_name;
 }
@@ -342,8 +343,8 @@ static int py_samu_set_full_name(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_fullname(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_fullname(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -363,7 +364,7 @@ static PyObject *py_samu_get_home_dir(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_home_dir = PyStr_FromString(home_dir);
+       py_home_dir = PyUnicode_FromString(home_dir);
        talloc_free(frame);
        return py_home_dir;
 }
@@ -373,8 +374,8 @@ static int py_samu_set_home_dir(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_homedir(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_homedir(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -394,7 +395,7 @@ static PyObject *py_samu_get_dir_drive(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_dir_drive = PyStr_FromString(dir_drive);
+       py_dir_drive = PyUnicode_FromString(dir_drive);
        talloc_free(frame);
        return py_dir_drive;
 }
@@ -404,8 +405,8 @@ static int py_samu_set_dir_drive(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_dir_drive(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_dir_drive(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -425,7 +426,7 @@ static PyObject *py_samu_get_logon_script(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_logon_script = PyStr_FromString(logon_script);
+       py_logon_script = PyUnicode_FromString(logon_script);
        talloc_free(frame);
        return py_logon_script;
 }
@@ -435,8 +436,8 @@ static int py_samu_set_logon_script(PyObject *obj, PyObject *value, void *closur
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_logon_script(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_logon_script(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -456,7 +457,7 @@ static PyObject *py_samu_get_profile_path(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_profile_path = PyStr_FromString(profile_path);
+       py_profile_path = PyUnicode_FromString(profile_path);
        talloc_free(frame);
        return py_profile_path;
 }
@@ -466,8 +467,8 @@ static int py_samu_set_profile_path(PyObject *obj, PyObject *value, void *closur
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_profile_path(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_profile_path(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -487,7 +488,7 @@ static PyObject *py_samu_get_acct_desc(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_acct_desc = PyStr_FromString(acct_desc);
+       py_acct_desc = PyUnicode_FromString(acct_desc);
        talloc_free(frame);
        return py_acct_desc;
 }
@@ -497,8 +498,8 @@ static int py_samu_set_acct_desc(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_acct_desc(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_acct_desc(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -518,7 +519,7 @@ static PyObject *py_samu_get_workstations(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_workstations = PyStr_FromString(workstations);
+       py_workstations = PyUnicode_FromString(workstations);
        talloc_free(frame);
        return py_workstations;
 }
@@ -528,8 +529,8 @@ static int py_samu_set_workstations(PyObject *obj, PyObject *value, void *closur
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_workstations(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_workstations(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -549,7 +550,7 @@ static PyObject *py_samu_get_comment(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_comment = PyStr_FromString(comment);
+       py_comment = PyUnicode_FromString(comment);
        talloc_free(frame);
        return py_comment;
 }
@@ -559,8 +560,8 @@ static int py_samu_set_comment(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_comment(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_comment(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -580,7 +581,7 @@ static PyObject *py_samu_get_munged_dial(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_munged_dial = PyStr_FromString(munged_dial);
+       py_munged_dial = PyUnicode_FromString(munged_dial);
        talloc_free(frame);
        return py_munged_dial;
 }
@@ -590,8 +591,8 @@ static int py_samu_set_munged_dial(PyObject *obj, PyObject *value, void *closure
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
-       if (!pdb_set_munged_dial(sam_acct, PyStr_AsString(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (!pdb_set_munged_dial(sam_acct, PyUnicode_AsUTF8(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -795,7 +796,9 @@ static PyObject *py_samu_get_plaintext_passwd(PyObject *obj, void *closure)
                Py_RETURN_NONE;
        }
 
-       py_plaintext_pw = PyStr_FromString(plaintext_pw);
+       py_plaintext_pw = PyUnicode_FromString(plaintext_pw);
+
+       BURN_STR(discard_const_p(char, plaintext_pw));
        talloc_free(frame);
        return py_plaintext_pw;
 }
@@ -805,7 +808,7 @@ static int py_samu_set_plaintext_passwd(PyObject *obj, PyObject *value, void *cl
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       if (!pdb_set_plaintext_passwd(sam_acct, PyStr_AsString(value))) {
+       if (!pdb_set_plaintext_passwd(sam_acct, PyUnicode_AsUTF8(value))) {
                talloc_free(frame);
                return -1;
        }
@@ -819,7 +822,7 @@ static PyObject *py_samu_get_acct_ctrl(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_acct_ctrl;
 
-       py_acct_ctrl = PyInt_FromLong(pdb_get_acct_ctrl(sam_acct));
+       py_acct_ctrl = PyLong_FromLong(pdb_get_acct_ctrl(sam_acct));
        talloc_free(frame);
        return py_acct_ctrl;
 }
@@ -829,8 +832,8 @@ static int py_samu_set_acct_ctrl(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_acct_ctrl(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_acct_ctrl(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -844,7 +847,7 @@ static PyObject *py_samu_get_logon_divs(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_logon_divs;
 
-       py_logon_divs = PyInt_FromLong(pdb_get_logon_divs(sam_acct));
+       py_logon_divs = PyLong_FromLong(pdb_get_logon_divs(sam_acct));
        talloc_free(frame);
        return py_logon_divs;
 }
@@ -854,8 +857,8 @@ static int py_samu_set_logon_divs(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_logon_divs(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_logon_divs(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -869,7 +872,7 @@ static PyObject *py_samu_get_hours_len(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_hours_len;
 
-       py_hours_len = PyInt_FromLong(pdb_get_hours_len(sam_acct));
+       py_hours_len = PyLong_FromLong(pdb_get_hours_len(sam_acct));
        talloc_free(frame);
        return py_hours_len;
 }
@@ -879,8 +882,8 @@ static int py_samu_set_hours_len(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_hours_len(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_hours_len(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -909,7 +912,7 @@ static PyObject *py_samu_get_hours(PyObject *obj, void *closure)
        }
 
        for (i=0; i<hours_len; i++) {
-               PyList_SetItem(py_hours, i, PyInt_FromLong(hours[i]));
+               PyList_SetItem(py_hours, i, PyLong_FromLong(hours[i]));
        }
        talloc_free(frame);
        return py_hours;
@@ -936,8 +939,8 @@ static int py_samu_set_hours(PyObject *obj, PyObject *value, void *closure)
        }
 
        for (i=0; i < hours_len; i++) {
-               PY_CHECK_TYPE(&PyInt_Type, PyList_GET_ITEM(value,i), return -1;);
-               hours[i] = PyInt_AsLong(PyList_GET_ITEM(value, i));
+               PY_CHECK_TYPE(&PyLong_Type, PyList_GET_ITEM(value,i), return -1;);
+               hours[i] = PyLong_AsLong(PyList_GET_ITEM(value, i));
        }
 
        status = pdb_set_hours(sam_acct, hours, hours_len, PDB_CHANGED);
@@ -957,7 +960,7 @@ static PyObject *py_samu_get_bad_password_count(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_bad_password_count;
 
-       py_bad_password_count = PyInt_FromLong(pdb_get_bad_password_count(sam_acct));
+       py_bad_password_count = PyLong_FromLong(pdb_get_bad_password_count(sam_acct));
        talloc_free(frame);
        return py_bad_password_count;
 }
@@ -967,8 +970,8 @@ static int py_samu_set_bad_password_count(PyObject *obj, PyObject *value, void *
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_bad_password_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_bad_password_count(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -982,7 +985,7 @@ static PyObject *py_samu_get_logon_count(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_logon_count;
 
-       py_logon_count = PyInt_FromLong(pdb_get_logon_count(sam_acct));
+       py_logon_count = PyLong_FromLong(pdb_get_logon_count(sam_acct));
        talloc_free(frame);
        return py_logon_count;
 }
@@ -992,8 +995,8 @@ static int py_samu_set_logon_count(PyObject *obj, PyObject *value, void *closure
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_logon_count(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_logon_count(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -1007,7 +1010,7 @@ static PyObject *py_samu_get_country_code(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_country_code;
 
-       py_country_code = PyInt_FromLong(pdb_get_country_code(sam_acct));
+       py_country_code = PyLong_FromLong(pdb_get_country_code(sam_acct));
        talloc_free(frame);
        return py_country_code;
 }
@@ -1017,8 +1020,8 @@ static int py_samu_set_country_code(PyObject *obj, PyObject *value, void *closur
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_country_code(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_country_code(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -1032,7 +1035,7 @@ static PyObject *py_samu_get_code_page(PyObject *obj, void *closure)
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
        PyObject *py_code_page;
 
-       py_code_page = PyInt_FromLong(pdb_get_code_page(sam_acct));
+       py_code_page = PyLong_FromLong(pdb_get_code_page(sam_acct));
        talloc_free(frame);
        return py_code_page;
 }
@@ -1042,8 +1045,8 @@ static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        struct samu *sam_acct = (struct samu *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       if (!pdb_set_code_page(sam_acct, PyInt_AsLong(value), PDB_CHANGED)) {
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       if (!pdb_set_code_page(sam_acct, PyLong_AsLong(value), PDB_CHANGED)) {
                talloc_free(frame);
                return -1;
        }
@@ -1052,40 +1055,174 @@ static int py_samu_set_code_page(PyObject *obj, PyObject *value, void *closure)
 }
 
 static PyGetSetDef py_samu_getsetters[] = {
-       { discard_const_p(char, "logon_time"), py_samu_get_logon_time, py_samu_set_logon_time },
-       { discard_const_p(char, "logoff_time"), py_samu_get_logoff_time, py_samu_set_logoff_time },
-       { discard_const_p(char, "kickoff_time"), py_samu_get_kickoff_time, py_samu_set_kickoff_time },
-       { discard_const_p(char, "bad_password_time"), py_samu_get_bad_password_time, py_samu_set_bad_password_time },
-       { discard_const_p(char, "pass_last_set_time"), py_samu_get_pass_last_set_time, py_samu_set_pass_last_set_time },
-       { discard_const_p(char, "pass_can_change_time"), py_samu_get_pass_can_change_time, py_samu_set_pass_can_change_time },
-       { discard_const_p(char, "pass_must_change_time"), py_samu_get_pass_must_change_time, py_samu_set_pass_must_change_time },
-       { discard_const_p(char, "username"), py_samu_get_username, py_samu_set_username },
-       { discard_const_p(char, "domain"), py_samu_get_domain, py_samu_set_domain },
-       { discard_const_p(char, "nt_username"), py_samu_get_nt_username, py_samu_set_nt_username },
-       { discard_const_p(char, "full_name"), py_samu_get_full_name, py_samu_set_full_name },
-       { discard_const_p(char, "home_dir"), py_samu_get_home_dir, py_samu_set_home_dir },
-       { discard_const_p(char, "dir_drive"), py_samu_get_dir_drive, py_samu_set_dir_drive },
-       { discard_const_p(char, "logon_script"), py_samu_get_logon_script, py_samu_set_logon_script },
-       { discard_const_p(char, "profile_path"), py_samu_get_profile_path, py_samu_set_profile_path },
-       { discard_const_p(char, "acct_desc"), py_samu_get_acct_desc, py_samu_set_acct_desc },
-       { discard_const_p(char, "workstations"), py_samu_get_workstations, py_samu_set_workstations },
-       { discard_const_p(char, "comment"), py_samu_get_comment, py_samu_set_comment },
-       { discard_const_p(char, "munged_dial"), py_samu_get_munged_dial, py_samu_set_munged_dial },
-       { discard_const_p(char, "user_sid"), py_samu_get_user_sid, py_samu_set_user_sid },
-       { discard_const_p(char, "group_sid"), py_samu_get_group_sid, py_samu_set_group_sid },
-       { discard_const_p(char, "lanman_passwd"), py_samu_get_lanman_passwd, py_samu_set_lanman_passwd },
-       { discard_const_p(char, "nt_passwd"), py_samu_get_nt_passwd, py_samu_set_nt_passwd },
-       { discard_const_p(char, "pw_history"), py_samu_get_pw_history, py_samu_set_pw_history },
-       { discard_const_p(char, "plaintext_passwd"), py_samu_get_plaintext_passwd, py_samu_set_plaintext_passwd },
-       { discard_const_p(char, "acct_ctrl"), py_samu_get_acct_ctrl, py_samu_set_acct_ctrl },
-       { discard_const_p(char, "logon_divs"), py_samu_get_logon_divs, py_samu_set_logon_divs },
-       { discard_const_p(char, "hours_len"), py_samu_get_hours_len, py_samu_set_hours_len },
-       { discard_const_p(char, "hours"), py_samu_get_hours, py_samu_set_hours },
-       { discard_const_p(char, "bad_password_count"), py_samu_get_bad_password_count, py_samu_set_bad_password_count },
-       { discard_const_p(char, "logon_count"), py_samu_get_logon_count, py_samu_set_logon_count },
-       { discard_const_p(char, "country_code"), py_samu_get_country_code, py_samu_set_country_code },
-       { discard_const_p(char, "code_page"), py_samu_get_code_page, py_samu_set_code_page },
-       { NULL }
+       {
+               .name = discard_const_p(char, "logon_time"),
+               .get  = py_samu_get_logon_time,
+               .set  = py_samu_set_logon_time,
+       },
+       {
+               .name = discard_const_p(char, "logoff_time"),
+               .get  = py_samu_get_logoff_time,
+               .set  = py_samu_set_logoff_time,
+       },
+       {
+               .name = discard_const_p(char, "kickoff_time"),
+               .get  = py_samu_get_kickoff_time,
+               .set  = py_samu_set_kickoff_time,
+       },
+       {
+               .name = discard_const_p(char, "bad_password_time"),
+               .get  = py_samu_get_bad_password_time,
+               .set  = py_samu_set_bad_password_time,
+       },
+       {
+               .name = discard_const_p(char, "pass_last_set_time"),
+               .get  = py_samu_get_pass_last_set_time,
+               .set  = py_samu_set_pass_last_set_time,
+       },
+       {
+               .name = discard_const_p(char, "pass_can_change_time"),
+               .get  = py_samu_get_pass_can_change_time,
+               .set  = py_samu_set_pass_can_change_time,
+       },
+       {
+               .name = discard_const_p(char, "pass_must_change_time"),
+               .get  = py_samu_get_pass_must_change_time,
+               .set  = py_samu_set_pass_must_change_time,
+       },
+       {
+               .name = discard_const_p(char, "username"),
+               .get  = py_samu_get_username,
+               .set  = py_samu_set_username,
+       },
+       {
+               .name = discard_const_p(char, "domain"),
+               .get  = py_samu_get_domain,
+               .set  = py_samu_set_domain,
+       },
+       {
+               .name = discard_const_p(char, "nt_username"),
+               .get  = py_samu_get_nt_username,
+               .set  = py_samu_set_nt_username,
+       },
+       {
+               .name = discard_const_p(char, "full_name"),
+               .get  = py_samu_get_full_name,
+               .set  = py_samu_set_full_name,
+       },
+       {
+               .name = discard_const_p(char, "home_dir"),
+               .get  = py_samu_get_home_dir,
+               .set  = py_samu_set_home_dir,
+       },
+       {
+               .name = discard_const_p(char, "dir_drive"),
+               .get  = py_samu_get_dir_drive,
+               .set  = py_samu_set_dir_drive,
+       },
+       {
+               .name = discard_const_p(char, "logon_script"),
+               .get  = py_samu_get_logon_script,
+               .set  = py_samu_set_logon_script,
+       },
+       {
+               .name = discard_const_p(char, "profile_path"),
+               .get  = py_samu_get_profile_path,
+               .set  = py_samu_set_profile_path,
+       },
+       {
+               .name = discard_const_p(char, "acct_desc"),
+               .get  = py_samu_get_acct_desc,
+               .set  = py_samu_set_acct_desc,
+       },
+       {
+               .name = discard_const_p(char, "workstations"),
+               .get  = py_samu_get_workstations,
+               .set  = py_samu_set_workstations,
+       },
+       {
+               .name = discard_const_p(char, "comment"),
+               .get  = py_samu_get_comment,
+               .set  = py_samu_set_comment,
+       },
+       {
+               .name = discard_const_p(char, "munged_dial"),
+               .get  = py_samu_get_munged_dial,
+               .set  = py_samu_set_munged_dial,
+       },
+       {
+               .name = discard_const_p(char, "user_sid"),
+               .get  = py_samu_get_user_sid,
+               .set  = py_samu_set_user_sid,
+       },
+       {
+               .name = discard_const_p(char, "group_sid"),
+               .get  = py_samu_get_group_sid,
+               .set  = py_samu_set_group_sid,
+       },
+       {
+               .name = discard_const_p(char, "lanman_passwd"),
+               .get  = py_samu_get_lanman_passwd,
+               .set  = py_samu_set_lanman_passwd,
+       },
+       {
+               .name = discard_const_p(char, "nt_passwd"),
+               .get  = py_samu_get_nt_passwd,
+               .set  = py_samu_set_nt_passwd,
+       },
+       {
+               .name = discard_const_p(char, "pw_history"),
+               .get  = py_samu_get_pw_history,
+               .set  = py_samu_set_pw_history,
+       },
+       {
+               .name = discard_const_p(char, "plaintext_passwd"),
+               .get  = py_samu_get_plaintext_passwd,
+               .set  = py_samu_set_plaintext_passwd,
+       },
+       {
+               .name = discard_const_p(char, "acct_ctrl"),
+               .get  = py_samu_get_acct_ctrl,
+               .set  = py_samu_set_acct_ctrl,
+       },
+       {
+               .name = discard_const_p(char, "logon_divs"),
+               .get  = py_samu_get_logon_divs,
+               .set  = py_samu_set_logon_divs,
+       },
+       {
+               .name = discard_const_p(char, "hours_len"),
+               .get  = py_samu_get_hours_len,
+               .set  = py_samu_set_hours_len,
+       },
+       {
+               .name = discard_const_p(char, "hours"),
+               .get  = py_samu_get_hours,
+               .set  = py_samu_set_hours,
+       },
+       {
+               .name = discard_const_p(char, "bad_password_count"),
+               .get  = py_samu_get_bad_password_count,
+               .set  = py_samu_set_bad_password_count,
+       },
+       {
+               .name = discard_const_p(char, "logon_count"),
+               .get  = py_samu_get_logon_count,
+               .set  = py_samu_set_logon_count,
+       },
+       {
+               .name = discard_const_p(char, "country_code"),
+               .get  = py_samu_get_country_code,
+               .set  = py_samu_set_country_code,
+       },
+       {
+               .name = discard_const_p(char, "code_page"),
+               .get  = py_samu_get_code_page,
+               .set  = py_samu_set_code_page,
+       },
+       {
+               .name = NULL,
+       }
 };
 
 
@@ -1133,8 +1270,8 @@ static int py_groupmap_set_gid(PyObject *obj, PyObject *value, void *closure)
        TALLOC_CTX *frame = talloc_stackframe();
        GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       group_map->gid = PyInt_AsLong(value);
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       group_map->gid = PyLong_AsLong(value);
        talloc_free(frame);
        return 0;
 }
@@ -1187,7 +1324,7 @@ static PyObject *py_groupmap_get_sid_name_use(PyObject *obj, void *closure)
        GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
        PyObject *py_sid_name_use;
 
-       py_sid_name_use = PyInt_FromLong(group_map->sid_name_use);
+       py_sid_name_use = PyLong_FromLong(group_map->sid_name_use);
        talloc_free(frame);
        return py_sid_name_use;
 }
@@ -1197,8 +1334,8 @@ static int py_groupmap_set_sid_name_use(PyObject *obj, PyObject *value, void *cl
        TALLOC_CTX *frame = talloc_stackframe();
        GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyInt_Type, value, return -1;);
-       group_map->sid_name_use = PyInt_AsLong(value);
+       PY_CHECK_TYPE(&PyLong_Type, value, return -1;);
+       group_map->sid_name_use = PyLong_AsLong(value);
        talloc_free(frame);
        return 0;
 }
@@ -1212,7 +1349,7 @@ static PyObject *py_groupmap_get_nt_name(PyObject *obj, void *closure)
                py_nt_name = Py_None;
                Py_INCREF(py_nt_name);
        } else {
-               py_nt_name = PyStr_FromString(group_map->nt_name);
+               py_nt_name = PyUnicode_FromString(group_map->nt_name);
        }
        talloc_free(frame);
        return py_nt_name;
@@ -1223,13 +1360,20 @@ static int py_groupmap_set_nt_name(PyObject *obj, PyObject *value, void *closure
        TALLOC_CTX *frame = talloc_stackframe();
        GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (group_map->nt_name != NULL) {
+               TALLOC_FREE(group_map->nt_name);
+       }
        if (value == Py_None) {
-               fstrcpy(group_map->nt_name, NULL);
+               group_map->nt_name = talloc_strdup(group_map, "");
        } else {
-               fstrcpy(group_map->nt_name, PyStr_AsString(value));
+               group_map->nt_name = talloc_strdup(group_map,
+                                                  PyUnicode_AsUTF8(value));
+       }
+       TALLOC_FREE(frame);
+       if (group_map->nt_name == NULL) {
+               return -1;
        }
-       talloc_free(frame);
        return 0;
 }
 
@@ -1242,7 +1386,7 @@ static PyObject *py_groupmap_get_comment(PyObject *obj, void *closure)
                py_comment = Py_None;
                Py_INCREF(py_comment);
        } else {
-               py_comment = PyStr_FromString(group_map->comment);
+               py_comment = PyUnicode_FromString(group_map->comment);
        }
        talloc_free(frame);
        return py_comment;
@@ -1253,23 +1397,52 @@ static int py_groupmap_set_comment(PyObject *obj, PyObject *value, void *closure
        TALLOC_CTX *frame = talloc_stackframe();
        GROUP_MAP *group_map = (GROUP_MAP *)pytalloc_get_ptr(obj);
 
-       PY_CHECK_TYPE(&PyStr_Type, value, return -1;);
+       PY_CHECK_TYPE(&PyUnicode_Type, value, return -1;);
+       if (group_map->comment != NULL) {
+               TALLOC_FREE(group_map->comment);
+       }
        if (value == Py_None) {
-               fstrcpy(group_map->comment, NULL);
+               group_map->comment = talloc_strdup(group_map, "");
        } else {
-               fstrcpy(group_map->comment, PyStr_AsString(value));
+               group_map->comment = talloc_strdup(group_map,
+                                                  PyUnicode_AsUTF8(value));
+       }
+       TALLOC_FREE(frame);
+       if (group_map->comment == NULL) {
+               return -1;
        }
-       talloc_free(frame);
        return 0;
 }
 
 static PyGetSetDef py_groupmap_getsetters[] = {
-       { discard_const_p(char, "gid"), py_groupmap_get_gid, py_groupmap_set_gid },
-       { discard_const_p(char, "sid"), py_groupmap_get_sid, py_groupmap_set_sid },
-       { discard_const_p(char, "sid_name_use"), py_groupmap_get_sid_name_use, py_groupmap_set_sid_name_use },
-       { discard_const_p(char, "nt_name"), py_groupmap_get_nt_name, py_groupmap_set_nt_name },
-       { discard_const_p(char, "comment"), py_groupmap_get_comment, py_groupmap_set_comment },
-       { NULL }
+       {
+               .name = discard_const_p(char, "gid"),
+               .get  = py_groupmap_get_gid,
+               .set  = py_groupmap_set_gid,
+       },
+       {
+               .name = discard_const_p(char, "sid"),
+               .get  = py_groupmap_get_sid,
+               .set  = py_groupmap_set_sid,
+       },
+       {
+               .name = discard_const_p(char, "sid_name_use"),
+               .get  = py_groupmap_get_sid_name_use,
+               .set  = py_groupmap_set_sid_name_use,
+       },
+       {
+               .name = discard_const_p(char, "nt_name"),
+               .get  = py_groupmap_get_nt_name,
+               .set  = py_groupmap_set_nt_name,
+       },
+       {
+               .name = discard_const_p(char, "comment"),
+               .get  = py_groupmap_get_comment,
+               .set  = py_groupmap_set_comment,
+       },
+       {
+               .name = NULL,
+       },
 };
 
 static PyObject *py_groupmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
@@ -1327,6 +1500,8 @@ static PyObject *py_pdb_domain_info(PyObject *self, PyObject *args)
        PyObject *py_domain_info;
        struct dom_sid *sid;
        struct GUID *guid;
+       PyObject *py_dom_sid = NULL;
+       PyObject *py_guid = NULL;
 
        methods = pytalloc_get_ptr(self);
 
@@ -1350,18 +1525,20 @@ static PyObject *py_pdb_domain_info(PyObject *self, PyObject *args)
        }
        *guid = domain_info->guid;
 
-       if ((py_domain_info = PyDict_New()) == NULL) {
-               PyErr_NoMemory();
-               talloc_free(frame);
-               return NULL;
-       }
+       py_dom_sid = pytalloc_steal(dom_sid_Type, sid);
+       py_guid = pytalloc_steal(guid_Type, guid);
 
-       PyDict_SetItemString(py_domain_info, "name", PyStr_FromString(domain_info->name));
-       PyDict_SetItemString(py_domain_info, "dns_domain", PyStr_FromString(domain_info->dns_domain));
-       PyDict_SetItemString(py_domain_info, "dns_forest", PyStr_FromString(domain_info->dns_forest));
-       PyDict_SetItemString(py_domain_info, "dom_sid", pytalloc_steal(dom_sid_Type, sid));
-       PyDict_SetItemString(py_domain_info, "guid", pytalloc_steal(guid_Type, guid));
+       py_domain_info = Py_BuildValue(
+               "{s:s, s:s, s:s, s:O, s:O}",
+               "name", domain_info->name,
+               "dns_domain", domain_info->dns_domain,
+               "dns_forest", domain_info->dns_forest,
+               "dom_sid", py_dom_sid,
+               "guid", py_guid);
 
+
+       Py_CLEAR(py_dom_sid);
+       Py_CLEAR(py_guid);
        talloc_free(frame);
        return py_domain_info;
 }
@@ -1471,7 +1648,7 @@ static PyObject *py_pdb_create_user(PyObject *self, PyObject *args)
        }
 
        talloc_free(frame);
-       return PyInt_FromLong(rid);
+       return PyLong_FromLong(rid);
 }
 
 static PyObject *py_pdb_delete_user(PyObject *self, PyObject *args)
@@ -1773,7 +1950,7 @@ static PyObject *py_pdb_create_dom_group(PyObject *self, PyObject *args)
        }
 
        talloc_free(frame);
-       return PyInt_FromLong(group_rid);
+       return PyLong_FromLong(group_rid);
 }
 
 
@@ -1911,11 +2088,8 @@ static PyObject *py_pdb_enum_group_mapping(PyObject *self, PyObject *args)
        struct dom_sid *domain_sid = NULL;
        GROUP_MAP **gmap = NULL;
        GROUP_MAP *group_map;
-       size_t num_entries;
+       size_t i, num_entries;
        PyObject *py_gmap_list, *py_group_map;
-       int i;
-
-       Py_INCREF(Py_None);
 
        if (!PyArg_ParseTuple(args, "|O!ii:enum_group_mapping", dom_sid_Type, &py_domain_sid,
                                        &lsa_sidtype_value, &unix_only)) {
@@ -1951,12 +2125,19 @@ static PyObject *py_pdb_enum_group_mapping(PyObject *self, PyObject *args)
        for(i=0; i<num_entries; i++) {
                py_group_map = py_groupmap_new(&PyGroupmap, NULL, NULL);
                if (py_group_map) {
+                       int res = 0;
                        group_map = pytalloc_get_ptr(py_group_map);
                        *group_map = *gmap[i];
                        talloc_steal(group_map, gmap[i]->nt_name);
                        talloc_steal(group_map, gmap[i]->comment);
 
-                       PyList_Append(py_gmap_list, py_group_map);
+                       res = PyList_Append(py_gmap_list, py_group_map);
+                       Py_CLEAR(py_group_map);
+                       if (res == -1) {
+                               Py_CLEAR(py_gmap_list);
+                               talloc_free(frame);
+                               return NULL;
+                       }
                }
        }
 
@@ -1975,10 +2156,9 @@ static PyObject *py_pdb_enum_group_members(PyObject *self, PyObject *args)
        PyObject *py_group_sid;
        struct dom_sid *group_sid;
        uint32_t *member_rids;
-       size_t num_members;
+       size_t i, num_members;
        PyObject *py_sid_list;
        struct dom_sid *domain_sid, *member_sid;
-       int i;
 
        if (!PyArg_ParseTuple(args, "O!:enum_group_members", dom_sid_Type, &py_group_sid)) {
                talloc_free(frame);
@@ -2009,8 +2189,18 @@ static PyObject *py_pdb_enum_group_members(PyObject *self, PyObject *args)
        domain_sid = get_global_sam_sid();
 
        for(i=0; i<num_members; i++) {
+               int res = 0;
+               PyObject *py_member_sid = NULL;
                member_sid = dom_sid_add_rid(frame, domain_sid, member_rids[i]);
-               PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, member_sid));
+               py_member_sid = pytalloc_steal(dom_sid_Type, member_sid);
+               res = PyList_Append(py_sid_list,
+                                   py_member_sid);
+               Py_CLEAR(py_member_sid);
+               if (res == -1) {
+                       talloc_free(frame);
+                       Py_CLEAR(py_sid_list);
+                       return NULL;
+               }
        }
 
        talloc_free(frame);
@@ -2023,7 +2213,7 @@ static PyObject *py_pdb_enum_group_memberships(PyObject *self, PyObject *args)
        TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
        struct pdb_methods *methods;
-       int i;
+       uint32_t i;
 
        struct samu *sam_acct;
        PyObject *py_sam_acct;
@@ -2059,7 +2249,11 @@ static PyObject *py_pdb_enum_group_memberships(PyObject *self, PyObject *args)
        }
 
        for(i=0; i<num_groups; i++) {
-               PyList_Append(py_sid_list, pytalloc_steal(dom_sid_Type, dom_sid_dup(NULL, &user_group_sids[i])));
+               PyObject *py_sid =
+                       pytalloc_steal(dom_sid_Type,
+                                      dom_sid_dup(NULL, &user_group_sids[i]));
+               PyList_Append(py_sid_list, py_sid);
+               Py_CLEAR(py_sid);
        }
 
        talloc_free(frame);
@@ -2149,7 +2343,7 @@ static PyObject *py_pdb_create_alias(PyObject *self, PyObject *args)
        }
 
        talloc_free(frame);
-       return PyInt_FromLong(rid);
+       return PyLong_FromLong(rid);
 }
 
 
@@ -2219,19 +2413,11 @@ static PyObject *py_pdb_get_aliasinfo(PyObject *self, PyObject *args)
                return NULL;
        }
 
-       py_alias_info = PyDict_New();
-       if (py_alias_info == NULL) {
-               PyErr_NoMemory();
-               talloc_free(frame);
-               return NULL;
-       }
-
-       PyDict_SetItemString(py_alias_info, "acct_name",
-                            PyStr_FromString(alias_info->acct_name));
-       PyDict_SetItemString(py_alias_info, "acct_desc",
-                            PyStr_FromString(alias_info->acct_desc));
-       PyDict_SetItemString(py_alias_info, "rid",
-                            PyInt_FromLong(alias_info->rid));
+       py_alias_info = Py_BuildValue(
+               "{s:s, s:s, s:l}",
+               "acct_name", alias_info->acct_name,
+               "acct_desc", alias_info->acct_desc,
+               "rid", alias_info->rid);
 
        talloc_free(frame);
        return py_alias_info;
@@ -2257,13 +2443,13 @@ static PyObject *py_pdb_set_aliasinfo(PyObject *self, PyObject *args)
 
        alias_sid = pytalloc_get_ptr(py_alias_sid);
 
-       alias_info.acct_name = talloc_strdup(frame, PyStr_AsString(PyDict_GetItemString(py_alias_info, "acct_name")));
+       alias_info.acct_name = talloc_strdup(frame, PyUnicode_AsUTF8(PyDict_GetItemString(py_alias_info, "acct_name")));
        if (alias_info.acct_name == NULL) {
                PyErr_Format(py_pdb_error, "Unable to allocate memory");
                talloc_free(frame);
                return NULL;
        }
-       alias_info.acct_desc = talloc_strdup(frame, PyStr_AsString(PyDict_GetItemString(py_alias_info, "acct_desc")));
+       alias_info.acct_desc = talloc_strdup(frame, PyUnicode_AsUTF8(PyDict_GetItemString(py_alias_info, "acct_desc")));
        if (alias_info.acct_desc == NULL) {
                PyErr_Format(py_pdb_error, "Unable to allocate memory");
                talloc_free(frame);
@@ -2358,8 +2544,7 @@ static PyObject *py_pdb_enum_aliasmem(PyObject *self, PyObject *args)
        PyObject *py_alias_sid;
        struct dom_sid *alias_sid, *member_sid, *tmp_sid;
        PyObject *py_member_list, *py_member_sid;
-       size_t num_members;
-       int i;
+       size_t i, num_members;
 
        if (!PyArg_ParseTuple(args, "O!:enum_aliasmem", dom_sid_Type, &py_alias_sid)) {
                talloc_free(frame);
@@ -2387,15 +2572,23 @@ static PyObject *py_pdb_enum_aliasmem(PyObject *self, PyObject *args)
        }
 
        for(i=0; i<num_members; i++) {
+               int res = 0;
                py_member_sid = pytalloc_new(struct dom_sid, dom_sid_Type);
                if (py_member_sid == NULL) {
                        PyErr_NoMemory();
+                       Py_CLEAR(py_member_list);
                        talloc_free(frame);
                        return NULL;
                }
                tmp_sid = pytalloc_get_ptr(py_member_sid);
                *tmp_sid = member_sid[i];
-               PyList_Append(py_member_list, py_member_sid);
+               res = PyList_Append(py_member_list, py_member_sid);
+               Py_CLEAR(py_member_sid);
+               if (res == -1) {
+                       Py_CLEAR(py_member_list);
+                       talloc_free(frame);
+                       return NULL;
+               }
        }
 
        talloc_free(frame);
@@ -2428,7 +2621,20 @@ static PyObject *py_pdb_get_account_policy(PyObject *self, PyObject *unused)
                type = account_policy_name_to_typenum(names[i]);
                status = methods->get_account_policy(methods, type, &value);
                if (NT_STATUS_IS_OK(status)) {
-                       PyDict_SetItemString(py_acct_policy, names[i], Py_BuildValue("i", value));
+                       int res = 0;
+                       PyObject *py_value = Py_BuildValue("i", value);
+                       if (py_value == NULL) {
+                               Py_CLEAR(py_acct_policy);
+                               break;
+                       }
+                       res = PyDict_SetItemString(py_acct_policy,
+                                                  names[i],
+                                                  py_value);
+                       Py_CLEAR(py_value);
+                       if (res == -1) {
+                               Py_CLEAR(py_acct_policy);
+                               break;
+                       }
                }
        }
 
@@ -2458,7 +2664,7 @@ static PyObject *py_pdb_set_account_policy(PyObject *self, PyObject *args)
        for (i=0; i<count; i++) {
                if ((py_value = PyDict_GetItemString(py_acct_policy, names[i])) != NULL) {
                        type = account_policy_name_to_typenum(names[i]);
-                       status = methods->set_account_policy(methods, type, PyInt_AsLong(py_value));
+                       status = methods->set_account_policy(methods, type, PyLong_AsLong(py_value));
                        if (!NT_STATUS_IS_OK(status)) {
                                PyErr_Format(py_pdb_error, "Error setting account policy (%s), (%d,%s)",
                                                names[i],
@@ -2516,21 +2722,30 @@ static PyObject *py_pdb_search_users(PyObject *self, PyObject *args)
        }
 
        while (search->next_entry(search, entry)) {
-               py_dict = PyDict_New();
+               int res = 1;
+               py_dict = Py_BuildValue(
+                       "{s:l, s:l, s:l, s:s, s:s, s:s}",
+                       "idx", entry->idx,
+                       "rid", entry->rid,
+                       "acct_flags", entry->acct_flags,
+                       "account_name", entry->account_name,
+                       "fullname", entry->fullname,
+                       "description", entry->description);
                if (py_dict == NULL) {
-                       PyErr_NoMemory();
-               } else {
-                       PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
-                       PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
-                       PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
-                       PyDict_SetItemString(py_dict, "account_name", PyStr_FromString(entry->account_name));
-                       PyDict_SetItemString(py_dict, "fullname", PyStr_FromString(entry->fullname));
-                       PyDict_SetItemString(py_dict, "description", PyStr_FromString(entry->description));
-                       PyList_Append(py_userlist, py_dict);
+                       Py_CLEAR(py_userlist);
+                       goto out;
+               }
+
+               res = PyList_Append(py_userlist, py_dict);
+               Py_CLEAR(py_dict);
+               if (res == -1) {
+                       Py_CLEAR(py_userlist);
+                       goto out;
                }
        }
        search->search_end(search);
 
+out:
        talloc_free(frame);
        return py_userlist;
 }
@@ -2574,21 +2789,30 @@ static PyObject *py_pdb_search_groups(PyObject *self, PyObject *unused)
        }
 
        while (search->next_entry(search, entry)) {
-               py_dict = PyDict_New();
+               int res = 0;
+               py_dict = Py_BuildValue(
+                       "{s:l, s:l, s:l, s:s, s:s, s:s}",
+                       "idx", entry->idx,
+                       "rid", entry->rid,
+                       "acct_flags", entry->acct_flags,
+                       "account_name", entry->account_name,
+                       "fullname", entry->fullname,
+                       "description", entry->description);
+
                if (py_dict == NULL) {
-                       PyErr_NoMemory();
-               } else {
-                       PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
-                       PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
-                       PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
-                       PyDict_SetItemString(py_dict, "account_name", PyStr_FromString(entry->account_name));
-                       PyDict_SetItemString(py_dict, "fullname", PyStr_FromString(entry->fullname));
-                       PyDict_SetItemString(py_dict, "description", PyStr_FromString(entry->description));
-                       PyList_Append(py_grouplist, py_dict);
+                       Py_CLEAR(py_grouplist);
+                       goto out;
+               }
+
+               res = PyList_Append(py_grouplist, py_dict);
+               Py_CLEAR(py_dict);
+               if (res == -1) {
+                       Py_CLEAR(py_grouplist);
+                       goto out;
                }
        }
        search->search_end(search);
-
+out:
        talloc_free(frame);
        return py_grouplist;
 }
@@ -2604,8 +2828,6 @@ static PyObject *py_pdb_search_aliases(PyObject *self, PyObject *args)
        PyObject *py_domain_sid = Py_None;
        struct dom_sid *domain_sid = NULL;
 
-       Py_INCREF(Py_None);
-
        if (!PyArg_ParseTuple(args, "|O!:search_aliases", dom_sid_Type, &py_domain_sid)) {
                talloc_free(frame);
                return NULL;
@@ -2645,21 +2867,30 @@ static PyObject *py_pdb_search_aliases(PyObject *self, PyObject *args)
        }
 
        while (search->next_entry(search, entry)) {
-               py_dict = PyDict_New();
+               int res = 0;
+
+               py_dict = Py_BuildValue(
+                       "{s:l, s:l, s:l, s:s, s:s, s:s}",
+                       "idx", entry->idx,
+                       "rid", entry->rid,
+                       "acct_flags", entry->acct_flags,
+                       "account_name", entry->account_name,
+                       "fullname", entry->fullname,
+                       "description", entry->description);
+
                if (py_dict == NULL) {
-                       PyErr_NoMemory();
-               } else {
-                       PyDict_SetItemString(py_dict, "idx", PyInt_FromLong(entry->idx));
-                       PyDict_SetItemString(py_dict, "rid", PyInt_FromLong(entry->rid));
-                       PyDict_SetItemString(py_dict, "acct_flags", PyInt_FromLong(entry->acct_flags));
-                       PyDict_SetItemString(py_dict, "account_name", PyStr_FromString(entry->account_name));
-                       PyDict_SetItemString(py_dict, "fullname", PyStr_FromString(entry->fullname));
-                       PyDict_SetItemString(py_dict, "description", PyStr_FromString(entry->description));
-                       PyList_Append(py_aliaslist, py_dict);
+                       Py_CLEAR(py_aliaslist);
+                       goto out;
+               }
+               res = PyList_Append(py_aliaslist, py_dict);
+               Py_CLEAR(py_dict);
+               if (res == -1) {
+                       Py_CLEAR(py_aliaslist);
+                       goto out;
                }
        }
        search->search_end(search);
-
+out:
        talloc_free(frame);
        return py_aliaslist;
 }
@@ -2786,7 +3017,7 @@ static PyObject *py_pdb_new_rid(PyObject *self, PyObject *unused)
        }
 
        talloc_free(frame);
-       return PyInt_FromLong(rid);
+       return PyLong_FromLong(rid);
 }
 
 
@@ -2828,17 +3059,13 @@ static PyObject *py_pdb_get_trusteddom_pw(PyObject *self, PyObject *args)
                return NULL;
        }
 
-       py_value = PyDict_New();
-       if (py_value == NULL) {
-               PyErr_NoMemory();
-               talloc_free(frame);
-               return NULL;
-       }
-
-       PyDict_SetItemString(py_value, "pwd", PyStr_FromString(pwd));
-       PyDict_SetItemString(py_value, "sid", py_sid);
-       PyDict_SetItemString(py_value, "last_set_tim", PyInt_FromLong(last_set_time));
+       py_value = Py_BuildValue(
+               "{s:s, s:O, s:l}",
+               "pwd", pwd,
+               "sid", py_sid,
+               "last_set_tim", last_set_time);
 
+       Py_CLEAR(py_sid);
        talloc_free(frame);
        return py_value;
 }
@@ -2903,10 +3130,9 @@ static PyObject *py_pdb_enum_trusteddoms(PyObject *self, PyObject *unused)
        TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
        struct pdb_methods *methods;
-       uint32_t num_domains;
+       uint32_t i, num_domains;
        struct trustdom_info **domains;
        PyObject *py_domain_list, *py_dict;
-       int i;
 
        methods = pytalloc_get_ptr(self);
 
@@ -2927,15 +3153,26 @@ static PyObject *py_pdb_enum_trusteddoms(PyObject *self, PyObject *unused)
        }
 
        for(i=0; i<num_domains; i++) {
-               py_dict = PyDict_New();
-               if (py_dict) {
-                       PyDict_SetItemString(py_dict, "name",
-                                       PyStr_FromString(domains[i]->name));
-                       PyDict_SetItemString(py_dict, "sid",
-                                       pytalloc_steal(dom_sid_Type, &domains[i]->sid));
+               int res = 0;
+               PyObject *py_sid =
+                       pytalloc_steal(dom_sid_Type, &domains[i]->sid);
+               py_dict = Py_BuildValue(
+                       "{s:s, s:O}",
+                       "name", domains[i]->name,
+                       "sid", py_sid);
+               Py_CLEAR(py_sid);
+               if (py_dict == NULL) {
+                       DBG_ERR("Failed to insert entry to dict\n");
+                               Py_CLEAR(py_domain_list);
+                               break;
                }
 
-               PyList_Append(py_domain_list, py_dict);
+               res = PyList_Append(py_domain_list, py_dict);
+               Py_CLEAR(py_dict);
+               if (res == -1) {
+                       Py_CLEAR(py_domain_list);
+                       break;
+               }
        }
 
        talloc_free(frame);
@@ -2951,6 +3188,7 @@ static PyObject *py_pdb_get_trusted_domain(PyObject *self, PyObject *args)
        const char *domain;
        struct pdb_trusted_domain *td;
        PyObject *py_domain_info;
+       PyObject *py_sid = NULL;
 
        if (!PyArg_ParseTuple(args, "s:get_trusted_domain", &domain)) {
                talloc_free(frame);
@@ -2968,34 +3206,30 @@ static PyObject *py_pdb_get_trusted_domain(PyObject *self, PyObject *args)
                return NULL;
        }
 
-       py_domain_info = PyDict_New();
-       if (py_domain_info == NULL) {
-               PyErr_NoMemory();
-               talloc_free(frame);
-               return NULL;
-       }
+       py_sid = pytalloc_steal(dom_sid_Type, &td->security_identifier);
 
-       PyDict_SetItemString(py_domain_info, "domain_name",
-                       PyStr_FromString(td->domain_name));
-       PyDict_SetItemString(py_domain_info, "netbios_name",
-                       PyStr_FromString(td->netbios_name));
-       PyDict_SetItemString(py_domain_info, "security_identifier",
-                       pytalloc_steal(dom_sid_Type, &td->security_identifier));
-       PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
-                       PyBytes_FromStringAndSize((const char *)td->trust_auth_incoming.data,
-                                               td->trust_auth_incoming.length));
-       PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
-                       PyBytes_FromStringAndSize((const char *)td->trust_auth_outgoing.data,
-                                               td->trust_auth_outgoing.length));
-       PyDict_SetItemString(py_domain_info, "trust_direction",
-                       PyInt_FromLong(td->trust_direction));
-       PyDict_SetItemString(py_domain_info, "trust_type",
-                       PyInt_FromLong(td->trust_type));
-       PyDict_SetItemString(py_domain_info, "trust_attributes",
-                       PyInt_FromLong(td->trust_attributes));
-       PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
-                       PyBytes_FromStringAndSize((const char *)td->trust_forest_trust_info.data,
-                                               td->trust_forest_trust_info.length));
+       py_domain_info = Py_BuildValue(
+               "{s:s, s:s, s:O,"
+                       " s:"PYARG_BYTES_LEN","
+                       " s:"PYARG_BYTES_LEN","
+                       " s:l, s:l, s:l,"
+                       " s:"PYARG_BYTES_LEN"}",
+               "domain_name", td->domain_name,
+               "netbios_name", td->netbios_name,
+               "security_identifier", py_sid,
+               "trust_auth_incoming",
+                       (const char *)td->trust_auth_incoming.data,
+                       td->trust_auth_incoming.length,
+               "trust_auth_outgoing",
+                       (const char *)td->trust_auth_outgoing.data,
+                       td->trust_auth_outgoing.length,
+               "trust_direction", td->trust_direction,
+               "trust_type", td->trust_type,
+               "trust_attributes", td->trust_attributes,
+               "trust_forest_trust_info",
+                       (const char *)td->trust_forest_trust_info.data,
+                       td->trust_forest_trust_info.length);
+       Py_CLEAR(py_sid);
 
        talloc_free(frame);
        return py_domain_info;
@@ -3011,6 +3245,7 @@ static PyObject *py_pdb_get_trusted_domain_by_sid(PyObject *self, PyObject *args
        struct dom_sid *domain_sid;
        struct pdb_trusted_domain *td;
        PyObject *py_domain_info;
+       PyObject *py_sid = NULL;
 
        if (!PyArg_ParseTuple(args, "O!:get_trusted_domain_by_sid", dom_sid_Type, &py_domain_sid)) {
                talloc_free(frame);
@@ -3030,34 +3265,30 @@ static PyObject *py_pdb_get_trusted_domain_by_sid(PyObject *self, PyObject *args
                return NULL;
        }
 
-       py_domain_info = PyDict_New();
-       if (py_domain_info == NULL) {
-               PyErr_NoMemory();
-               talloc_free(frame);
-               return NULL;
-       }
+       py_sid = pytalloc_steal(dom_sid_Type, &td->security_identifier);
 
-       PyDict_SetItemString(py_domain_info, "domain_name",
-                       PyStr_FromString(td->domain_name));
-       PyDict_SetItemString(py_domain_info, "netbios_name",
-                       PyStr_FromString(td->netbios_name));
-       PyDict_SetItemString(py_domain_info, "security_identifier",
-                       pytalloc_steal(dom_sid_Type, &td->security_identifier));
-       PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
-                       PyBytes_FromStringAndSize((char *)td->trust_auth_incoming.data,
-                                               td->trust_auth_incoming.length));
-       PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
-                       PyBytes_FromStringAndSize((char *)td->trust_auth_outgoing.data,
-                                               td->trust_auth_outgoing.length));
-       PyDict_SetItemString(py_domain_info, "trust_direction",
-                       PyInt_FromLong(td->trust_direction));
-       PyDict_SetItemString(py_domain_info, "trust_type",
-                       PyInt_FromLong(td->trust_type));
-       PyDict_SetItemString(py_domain_info, "trust_attributes",
-                       PyInt_FromLong(td->trust_attributes));
-       PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
-                       PyBytes_FromStringAndSize((char *)td->trust_forest_trust_info.data,
-                                               td->trust_forest_trust_info.length));
+       py_domain_info = Py_BuildValue(
+               "{s:s, s:s, s:O,"
+                       " s:"PYARG_BYTES_LEN","
+                       " s:"PYARG_BYTES_LEN","
+                       " s:l, s:l, s:l,"
+                       " s:"PYARG_BYTES_LEN"}",
+               "domain_name", td->domain_name,
+               "netbios_name", td->netbios_name,
+               "security_identifier", py_sid,
+               "trust_auth_incoming",
+                       (const char *)td->trust_auth_incoming.data,
+                       td->trust_auth_incoming.length,
+               "trust_auth_outgoing",
+                       (const char *)td->trust_auth_outgoing.data,
+                       td->trust_auth_outgoing.length,
+               "trust_direction", td->trust_direction,
+               "trust_type", td->trust_type,
+               "trust_attributes", td->trust_attributes,
+               "trust_forest_trust_info",
+                       (const char *)td->trust_forest_trust_info.data,
+                       td->trust_forest_trust_info.length);
+       Py_CLEAR(py_sid);
 
        talloc_free(frame);
        return py_domain_info;
@@ -3081,10 +3312,10 @@ static PyObject *py_pdb_set_trusted_domain(PyObject *self, PyObject *args)
        }
 
        py_tmp = PyDict_GetItemString(py_td_info, "domain_name");
-       td_info.domain_name = PyStr_AsString(py_tmp);
+       td_info.domain_name = discard_const_p(char, PyUnicode_AsUTF8(py_tmp));
 
        py_tmp = PyDict_GetItemString(py_td_info, "netbios_name");
-       td_info.netbios_name = PyStr_AsString(py_tmp);
+       td_info.netbios_name = discard_const_p(char, PyUnicode_AsUTF8(py_tmp));
 
        py_tmp = PyDict_GetItemString(py_td_info, "security_identifier");
        td_info.security_identifier = *pytalloc_get_type(py_tmp, struct dom_sid);
@@ -3098,13 +3329,13 @@ static PyObject *py_pdb_set_trusted_domain(PyObject *self, PyObject *args)
        td_info.trust_auth_outgoing.length = len;
 
        py_tmp = PyDict_GetItemString(py_td_info, "trust_direction");
-       td_info.trust_direction = PyInt_AsLong(py_tmp);
+       td_info.trust_direction = PyLong_AsLong(py_tmp);
 
        py_tmp = PyDict_GetItemString(py_td_info, "trust_type");
-       td_info.trust_type = PyInt_AsLong(py_tmp);
+       td_info.trust_type = PyLong_AsLong(py_tmp);
 
        py_tmp = PyDict_GetItemString(py_td_info, "trust_attributes");
-       td_info.trust_attributes = PyInt_AsLong(py_tmp);
+       td_info.trust_attributes = PyLong_AsLong(py_tmp);
 
        py_tmp = PyDict_GetItemString(py_td_info, "trust_forest_trust_info");
        PyBytes_AsStringAndSize(py_tmp, (char **)&td_info.trust_forest_trust_info.data, &len);
@@ -3159,10 +3390,9 @@ static PyObject *py_pdb_enum_trusted_domains(PyObject *self, PyObject *args)
        TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
        struct pdb_methods *methods;
-       uint32_t num_domains;
-       struct pdb_trusted_domain **td_info, *td;
+       uint32_t i, num_domains;
+       struct pdb_trusted_domain **td_info;
        PyObject *py_td_info, *py_domain_info;
-       int i;
 
        methods = pytalloc_get_ptr(self);
 
@@ -3183,39 +3413,44 @@ static PyObject *py_pdb_enum_trusted_domains(PyObject *self, PyObject *args)
        }
 
        for (i=0; i<num_domains; i++) {
+               int res = 0;
+               struct pdb_trusted_domain *td = td_info[i];
+               PyObject *py_sid =
+                       pytalloc_steal(dom_sid_Type, &td->security_identifier);
+
+               py_domain_info = Py_BuildValue(
+                       "{s:s, s:s, s:O,"
+                               " s:"PYARG_BYTES_LEN","
+                               " s:"PYARG_BYTES_LEN","
+                               " s:l, s:l, s:l,"
+                               " s:"PYARG_BYTES_LEN"}",
+                       "domain_name", td->domain_name,
+                       "netbios_name", td->netbios_name,
+                       "security_identifier", py_sid,
+                       "trust_auth_incoming",
+                               (const char *)td->trust_auth_incoming.data,
+                               td->trust_auth_incoming.length,
+                       "trust_auth_outgoing",
+                               (const char *)td->trust_auth_outgoing.data,
+                               td->trust_auth_outgoing.length,
+                       "trust_direction", td->trust_direction,
+                       "trust_type", td->trust_type,
+                       "trust_attributes", td->trust_attributes,
+                       "trust_forest_trust_info",
+                               (const char *)td->trust_forest_trust_info.data,
+                               td->trust_forest_trust_info.length);
+               Py_CLEAR(py_sid);
 
-               py_domain_info = PyDict_New();
                if (py_domain_info == NULL) {
-                       PyErr_NoMemory();
-                       Py_DECREF(py_td_info);
-                       talloc_free(frame);
-                       return NULL;
+                       Py_CLEAR(py_td_info);
+                       break;
+               }
+               res = PyList_Append(py_td_info, py_domain_info);
+               Py_CLEAR(py_domain_info);
+               if (res == -1) {
+                       Py_CLEAR(py_td_info);
+                       break;
                }
-
-               td = td_info[i];
-
-               PyDict_SetItemString(py_domain_info, "domain_name",
-                               PyStr_FromString(td->domain_name));
-               PyDict_SetItemString(py_domain_info, "netbios_name",
-                               PyStr_FromString(td->netbios_name));
-               PyDict_SetItemString(py_domain_info, "security_identifier",
-                               pytalloc_steal(dom_sid_Type, &td->security_identifier));
-               PyDict_SetItemString(py_domain_info, "trust_auth_incoming",
-                               PyBytes_FromStringAndSize((const char *)td->trust_auth_incoming.data,
-                                                       td->trust_auth_incoming.length));
-               PyDict_SetItemString(py_domain_info, "trust_auth_outgoing",
-                               PyBytes_FromStringAndSize((const char *)td->trust_auth_outgoing.data,
-                                                       td->trust_auth_outgoing.length));
-               PyDict_SetItemString(py_domain_info, "trust_direction",
-                               PyInt_FromLong(td->trust_direction));
-               PyDict_SetItemString(py_domain_info, "trust_type",
-                               PyInt_FromLong(td->trust_type));
-               PyDict_SetItemString(py_domain_info, "trust_attributes",
-                               PyInt_FromLong(td->trust_attributes));
-               PyDict_SetItemString(py_domain_info, "trust_forest_trust_info",
-                               PyBytes_FromStringAndSize((const char *)td->trust_forest_trust_info.data,
-                                                       td->trust_forest_trust_info.length));
-               PyList_Append(py_td_info, py_domain_info);
        }
 
        talloc_free(frame);
@@ -3265,24 +3500,25 @@ static PyObject *py_pdb_get_secret(PyObject *self, PyObject *args)
                return NULL;
        }
 
-       py_secret = PyDict_New();
+       py_secret = Py_BuildValue(
+               "{s:"PYARG_BYTES_LEN","
+                       " s:K"
+                       " s:"PYARG_BYTES_LEN","
+                       " s:K, s:O}",
+               "secret_current", (const char*)secret_current.data,
+                               secret_current.length,
+               "secret_current_lastchange", secret_current_lastchange,
+               "secret_old", (const char*)secret_old.data,
+                               secret_old.length,
+               "secret_old_lastchange", secret_old_lastchange,
+               "sd", py_sd);
+
+       Py_CLEAR(py_sd);
        if (py_secret == NULL) {
-               PyErr_NoMemory();
-               Py_DECREF(py_sd);
                talloc_free(frame);
                return NULL;
        }
 
-       PyDict_SetItemString(py_secret, "secret_current",
-                       PyBytes_FromStringAndSize((const char*)secret_current.data, secret_current.length));
-       PyDict_SetItemString(py_secret, "secret_current_lastchange",
-                       PyLong_FromUnsignedLongLong(secret_current_lastchange));
-       PyDict_SetItemString(py_secret, "secret_old",
-                       PyBytes_FromStringAndSize((const char*)secret_old.data, secret_old.length));
-       PyDict_SetItemString(py_secret, "secret_old_lastchange",
-                       PyLong_FromUnsignedLongLong(secret_old_lastchange));
-       PyDict_SetItemString(py_secret, "sd", py_sd);
-
        talloc_free(frame);
        return py_secret;
 }
@@ -3525,7 +3761,7 @@ static PyMethodDef py_pdb_methods[] = {
        { "delete_secret", py_pdb_delete_secret, METH_VARARGS,
                "delete_secret(secret_name) -> None\n\n \
                Delete secret information for secret_name." },
-       { NULL },
+       {0},
 };
 
 
@@ -3542,7 +3778,7 @@ static PyObject *py_pdb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs
                return NULL;
        }
 
-       /* Initalize list of methods */
+       /* Initialize list of methods */
        status = make_pdb_method_name(&methods, url);
        if (!NT_STATUS_IS_OK(status)) {
                PyErr_Format(py_pdb_error, "Cannot load backend methods for '%s' backend (%d,%s)",
@@ -3594,7 +3830,20 @@ static PyObject *py_passdb_backends(PyObject *self, PyObject *unused)
        }
 
        while(entry) {
-               PyList_Append(py_blist, PyStr_FromString(entry->name));
+               int res = 0;
+               PyObject *entry_name = PyUnicode_FromString(entry->name);
+               if (entry_name) {
+                       res = PyList_Append(py_blist, entry_name);
+               } else {
+                       Py_CLEAR(entry_name);
+                       Py_CLEAR(py_blist);
+                       break;
+               }
+               Py_CLEAR(entry_name);
+               if (res == -1) {
+                       Py_CLEAR(py_blist);
+                       break;
+               }
                entry = entry->next;
        }
 
@@ -3662,6 +3911,31 @@ static PyObject *py_reload_static_pdb(PyObject *self, PyObject *args)
        Py_RETURN_NONE;
 }
 
+static PyObject *py_get_domain_sid(PyObject *self, PyObject *unused)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct dom_sid domain_sid, *domain_sid_copy;
+       PyObject *py_dom_sid = Py_None;
+       bool ret = false;
+
+       ret = secrets_fetch_domain_sid(lp_workgroup(), &domain_sid);
+       if (!ret) {
+               talloc_free(frame);
+               return PyErr_NoMemory();
+       }
+
+       domain_sid_copy = dom_sid_dup(frame, &domain_sid);
+       if (domain_sid_copy == NULL) {
+               talloc_free(frame);
+               return PyErr_NoMemory();
+       }
+
+       py_dom_sid = pytalloc_steal(dom_sid_Type, domain_sid_copy);
+
+       talloc_free(frame);
+       return py_dom_sid;
+}
+
 static PyObject *py_get_global_sam_sid(PyObject *self, PyObject *unused)
 {
        TALLOC_CTX *frame = talloc_stackframe();
@@ -3697,10 +3971,13 @@ static PyMethodDef py_passdb_methods[] = {
        { "get_global_sam_sid", py_get_global_sam_sid, METH_NOARGS,
                "get_global_sam_sid() -> dom_sid\n\n \
                Return domain SID." },
+       { "get_domain_sid", py_get_domain_sid, METH_NOARGS,
+               "get_domain_sid() -> dom_sid\n\n \
+               Return domain SID from secrets database." },
        { "reload_static_pdb", py_reload_static_pdb, METH_NOARGS,
                "reload_static_pdb() -> None\n\n \
-               Re-initalise the static pdb used internally.  Needed if 'passdb backend' is changed." },
-       { NULL },
+               Re-initialise the static pdb used internally.  Needed if 'passdb backend' is changed." },
+       {0},
 };
 
 static struct PyModuleDef moduledef = {
@@ -3761,6 +4038,7 @@ MODULE_INIT_FUNC(passdb)
 
        dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
        if (dom_sid_Type == NULL) {
+               Py_DECREF(mod);
                talloc_free(frame);
                return NULL;
        }
@@ -3769,6 +4047,7 @@ MODULE_INIT_FUNC(passdb)
        security_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "descriptor");
        Py_DECREF(mod);
        if (security_Type == NULL) {
+               Py_DECREF(dom_sid_Type);
                talloc_free(frame);
                return NULL;
        }
@@ -3776,6 +4055,8 @@ MODULE_INIT_FUNC(passdb)
        /* Import GUID type from dcerpc.misc */
        mod = PyImport_ImportModule("samba.dcerpc.misc");
        if (mod == NULL) {
+               Py_DECREF(security_Type);
+               Py_DECREF(dom_sid_Type);
                talloc_free(frame);
                return NULL;
        }
@@ -3783,6 +4064,8 @@ MODULE_INIT_FUNC(passdb)
        guid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "GUID");
        Py_DECREF(mod);
        if (guid_Type == NULL) {
+               Py_DECREF(security_Type);
+               Py_DECREF(dom_sid_Type);
                talloc_free(frame);
                return NULL;
        }