s4:password_hash LDB module - substitute "lp_workgroup" with "lp_sam_name"
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / password_hash.c
index fc8fb775eeaa1eeb08f6cad54453d7385abbfccf..a3ff5f87d97d82050f1a1e40a84ace7b7ea03998 100644 (file)
@@ -33,9 +33,7 @@
 
 #include "includes.h"
 #include "libcli/ldap/ldap_ndr.h"
-#include "ldb/include/ldb_errors.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_private.h"
+#include "ldb_module.h"
 #include "librpc/gen_ndr/misc.h"
 #include "librpc/gen_ndr/samr.h"
 #include "libcli/auth/libcli_auth.h"
@@ -44,7 +42,7 @@
 #include "auth/kerberos/kerberos.h"
 #include "system/time.h"
 #include "dsdb/samdb/samdb.h"
-#include "dsdb/common/flags.h"
+#include "../libds/common/flags.h"
 #include "dsdb/samdb/ldb_modules/password_modules.h"
 #include "librpc/ndr/libndr.h"
 #include "librpc/gen_ndr/ndr_drsblobs.h"
@@ -89,9 +87,9 @@ struct domain_data {
        bool store_cleartext;
        uint_t pwdProperties;
        uint_t pwdHistoryLength;
-       char *netbios_domain;
-       char *dns_domain;
-       char *realm;
+       const char *netbios_domain;
+       const char *dns_domain;
+       const char *realm;
 };
 
 struct setup_password_fields_io {
@@ -111,6 +109,7 @@ struct setup_password_fields_io {
        struct {
                const struct ldb_val *cleartext_utf8;
                const struct ldb_val *cleartext_utf16;
+               struct ldb_val quoted_utf16;
                struct samr_Password *nt_hash;
                struct samr_Password *lm_hash;
        } n;
@@ -150,9 +149,11 @@ struct setup_password_fields_io {
 
 static int setup_nt_fields(struct setup_password_fields_io *io)
 {
+       struct ldb_context *ldb;
        uint32_t i;
 
        io->g.nt_hash = io->n.nt_hash;
+       ldb = ldb_module_get_ctx(io->ac->module);
 
        if (io->domain->pwdHistoryLength == 0) {
                return LDB_SUCCESS;
@@ -163,7 +164,7 @@ static int setup_nt_fields(struct setup_password_fields_io *io)
                                        struct samr_Password,
                                        io->domain->pwdHistoryLength);
        if (!io->g.nt_history) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -190,9 +191,11 @@ static int setup_nt_fields(struct setup_password_fields_io *io)
 
 static int setup_lm_fields(struct setup_password_fields_io *io)
 {
+       struct ldb_context *ldb;
        uint32_t i;
 
        io->g.lm_hash = io->n.lm_hash;
+       ldb = ldb_module_get_ctx(io->ac->module);
 
        if (io->domain->pwdHistoryLength == 0) {
                return LDB_SUCCESS;
@@ -203,7 +206,7 @@ static int setup_lm_fields(struct setup_password_fields_io *io)
                                        struct samr_Password,
                                        io->domain->pwdHistoryLength);
        if (!io->g.lm_history) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -223,12 +226,14 @@ static int setup_lm_fields(struct setup_password_fields_io *io)
 
 static int setup_kerberos_keys(struct setup_password_fields_io *io)
 {
+       struct ldb_context *ldb;
        krb5_error_code krb5_ret;
        Principal *salt_principal;
        krb5_salt salt;
        krb5_keyblock key;
        krb5_data cleartext_data;
 
+       ldb = ldb_module_get_ctx(io->ac->module);
        cleartext_data.data = io->n.cleartext_utf8->data;
        cleartext_data.length = io->n.cleartext_utf8->length;
 
@@ -243,9 +248,9 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                char *name;
                char *saltbody;
 
-               name = talloc_strdup(io->ac, io->u.sAMAccountName);
+               name = strlower_talloc(io->ac, io->u.sAMAccountName);
                if (!name) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
 
@@ -255,7 +260,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
 
                saltbody = talloc_asprintf(io->ac, "%s.%s", name, io->domain->dns_domain);
                if (!saltbody) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
                
@@ -269,7 +274,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
 
                user_principal_name = talloc_strdup(io->ac, io->u.user_principal_name);
                if (!user_principal_name) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
 
@@ -289,7 +294,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                               NULL);
        }
        if (krb5_ret) {
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_kerberos_keys: "
                                       "generation of a salting principal failed: %s",
                                       smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
@@ -303,7 +308,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                    salt_principal, &salt);
        krb5_free_principal(io->smb_krb5_context->krb5_context, salt_principal);
        if (krb5_ret) {
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_kerberos_keys: "
                                       "generation of krb5_salt failed: %s",
                                       smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
@@ -315,7 +320,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                    salt.saltvalue.length);
        krb5_free_salt(io->smb_krb5_context->krb5_context, salt);
        if (!io->g.salt) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
        salt.saltvalue.data     = discard_const(io->g.salt);
@@ -331,7 +336,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                                salt,
                                                &key);
        if (krb5_ret) {
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_kerberos_keys: "
                                       "generation of a aes256-cts-hmac-sha1-96 key failed: %s",
                                       smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
@@ -342,7 +347,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                         key.keyvalue.length);
        krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
        if (!io->g.aes_256.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -356,7 +361,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                                salt,
                                                &key);
        if (krb5_ret) {
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_kerberos_keys: "
                                       "generation of a aes128-cts-hmac-sha1-96 key failed: %s",
                                       smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
@@ -367,7 +372,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                         key.keyvalue.length);
        krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
        if (!io->g.aes_128.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -381,7 +386,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                                salt,
                                                &key);
        if (krb5_ret) {
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_kerberos_keys: "
                                       "generation of a des-cbc-md5 key failed: %s",
                                       smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
@@ -392,7 +397,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                         key.keyvalue.length);
        krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
        if (!io->g.des_md5.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -406,7 +411,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                                salt,
                                                &key);
        if (krb5_ret) {
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_kerberos_keys: "
                                       "generation of a des-cbc-crc key failed: %s",
                                       smb_get_krb5_error_message(io->smb_krb5_context->krb5_context, krb5_ret, io->ac));
@@ -417,7 +422,7 @@ static int setup_kerberos_keys(struct setup_password_fields_io *io)
                                         key.keyvalue.length);
        krb5_free_keyblock_contents(io->smb_krb5_context->krb5_context, &key);
        if (!io->g.des_crc.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -428,6 +433,7 @@ static int setup_primary_kerberos(struct setup_password_fields_io *io,
                                  const struct supplementalCredentialsBlob *old_scb,
                                  struct package_PrimaryKerberosBlob *pkb)
 {
+       struct ldb_context *ldb;
        struct package_PrimaryKerberosCtr3 *pkb3 = &pkb->ctr.ctr3;
        struct supplementalCredentialsPackage *old_scp = NULL;
        struct package_PrimaryKerberosBlob _old_pkb;
@@ -435,6 +441,8 @@ static int setup_primary_kerberos(struct setup_password_fields_io *io,
        uint32_t i;
        enum ndr_err_code ndr_err;
 
+       ldb = ldb_module_get_ctx(io->ac->module);
+
        /*
         * prepare generation of keys
         *
@@ -448,7 +456,7 @@ static int setup_primary_kerberos(struct setup_password_fields_io *io,
                                               struct package_PrimaryKerberosKey3,
                                               pkb3->num_keys);
        if (!pkb3->keys) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -484,16 +492,16 @@ static int setup_primary_kerberos(struct setup_password_fields_io *io,
 
                blob = strhex_to_data_blob(io->ac, old_scp->data);
                if (!blob.data) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
 
                /* TODO: use ndr_pull_struct_blob_all(), when the ndr layer handles it correct with relative pointers */
-               ndr_err = ndr_pull_struct_blob(&blob, io->ac, lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")), &_old_pkb,
+               ndr_err = ndr_pull_struct_blob(&blob, io->ac, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), &_old_pkb,
                                               (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                        NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
-                       ldb_asprintf_errstring(io->ac->module->ldb,
+                       ldb_asprintf_errstring(ldb,
                                               "setup_primary_kerberos: "
                                               "failed to pull old package_PrimaryKerberosBlob: %s",
                                               nt_errstr(status));
@@ -501,7 +509,7 @@ static int setup_primary_kerberos(struct setup_password_fields_io *io,
                }
 
                if (_old_pkb.version != 3) {
-                       ldb_asprintf_errstring(io->ac->module->ldb,
+                       ldb_asprintf_errstring(ldb,
                                               "setup_primary_kerberos: "
                                               "package_PrimaryKerberosBlob version[%u] expected[3]",
                                               _old_pkb.version);
@@ -527,6 +535,7 @@ static int setup_primary_kerberos_newer(struct setup_password_fields_io *io,
                                        const struct supplementalCredentialsBlob *old_scb,
                                        struct package_PrimaryKerberosBlob *pkb)
 {
+       struct ldb_context *ldb;
        struct package_PrimaryKerberosCtr4 *pkb4 = &pkb->ctr.ctr4;
        struct supplementalCredentialsPackage *old_scp = NULL;
        struct package_PrimaryKerberosBlob _old_pkb;
@@ -534,6 +543,8 @@ static int setup_primary_kerberos_newer(struct setup_password_fields_io *io,
        uint32_t i;
        enum ndr_err_code ndr_err;
 
+       ldb = ldb_module_get_ctx(io->ac->module);
+
        /*
         * prepare generation of keys
         *
@@ -551,7 +562,7 @@ static int setup_primary_kerberos_newer(struct setup_password_fields_io *io,
                                  struct package_PrimaryKerberosKey4,
                                  pkb4->num_keys);
        if (!pkb4->keys) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -597,18 +608,18 @@ static int setup_primary_kerberos_newer(struct setup_password_fields_io *io,
 
                blob = strhex_to_data_blob(io->ac, old_scp->data);
                if (!blob.data) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
 
                /* TODO: use ndr_pull_struct_blob_all(), when the ndr layer handles it correct with relative pointers */
                ndr_err = ndr_pull_struct_blob(&blob, io->ac,
-                                              lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
+                                              lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                               &_old_pkb,
                                               (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                        NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
-                       ldb_asprintf_errstring(io->ac->module->ldb,
+                       ldb_asprintf_errstring(ldb,
                                               "setup_primary_kerberos_newer: "
                                               "failed to pull old package_PrimaryKerberosBlob: %s",
                                               nt_errstr(status));
@@ -616,7 +627,7 @@ static int setup_primary_kerberos_newer(struct setup_password_fields_io *io,
                }
 
                if (_old_pkb.version != 4) {
-                       ldb_asprintf_errstring(io->ac->module->ldb,
+                       ldb_asprintf_errstring(ldb,
                                               "setup_primary_kerberos_newer: "
                                               "package_PrimaryKerberosBlob version[%u] expected[4]",
                                               _old_pkb.version);
@@ -644,6 +655,7 @@ static int setup_primary_wdigest(struct setup_password_fields_io *io,
                                 const struct supplementalCredentialsBlob *old_scb,
                                 struct package_PrimaryWDigestBlob *pdb)
 {
+       struct ldb_context *ldb = ldb_module_get_ctx(io->ac->module);
        DATA_BLOB sAMAccountName;
        DATA_BLOB sAMAccountName_l;
        DATA_BLOB sAMAccountName_u;
@@ -890,12 +902,12 @@ static int setup_primary_wdigest(struct setup_password_fields_io *io,
        sAMAccountName          = data_blob_string_const(io->u.sAMAccountName);
        sAMAccountName_l        = data_blob_string_const(strlower_talloc(io->ac, io->u.sAMAccountName));
        if (!sAMAccountName_l.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
        sAMAccountName_u        = data_blob_string_const(strupper_talloc(io->ac, io->u.sAMAccountName));
        if (!sAMAccountName_u.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -905,31 +917,31 @@ static int setup_primary_wdigest(struct setup_password_fields_io *io,
                                                      io->u.sAMAccountName,
                                                      io->domain->dns_domain);
                if (!user_principal_name) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }       
        }
        userPrincipalName       = data_blob_string_const(user_principal_name);
        userPrincipalName_l     = data_blob_string_const(strlower_talloc(io->ac, user_principal_name));
        if (!userPrincipalName_l.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
        userPrincipalName_u     = data_blob_string_const(strupper_talloc(io->ac, user_principal_name));
        if (!userPrincipalName_u.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        netbios_domain          = data_blob_string_const(io->domain->netbios_domain);
        netbios_domain_l        = data_blob_string_const(strlower_talloc(io->ac, io->domain->netbios_domain));
        if (!netbios_domain_l.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
        netbios_domain_u        = data_blob_string_const(strupper_talloc(io->ac, io->domain->netbios_domain));
        if (!netbios_domain_u.data) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -945,7 +957,7 @@ static int setup_primary_wdigest(struct setup_password_fields_io *io,
        pdb->num_hashes = ARRAY_SIZE(wdigest);
        pdb->hashes     = talloc_array(io->ac, struct package_PrimaryWDigestHash, pdb->num_hashes);
        if (!pdb->hashes) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -971,6 +983,7 @@ static int setup_primary_wdigest(struct setup_password_fields_io *io,
 
 static int setup_supplemental_field(struct setup_password_fields_io *io)
 {
+       struct ldb_context *ldb;
        struct supplementalCredentialsBlob scb;
        struct supplementalCredentialsBlob _old_scb;
        struct supplementalCredentialsBlob *old_scb = NULL;
@@ -1013,10 +1026,13 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
        uint8_t zero16[16];
        bool do_newer_keys = false;
        bool do_cleartext = false;
+       int *domainFunctionality;
 
        ZERO_STRUCT(zero16);
        ZERO_STRUCT(names);
 
+       ldb = ldb_module_get_ctx(io->ac->module);
+
        if (!io->n.cleartext_utf8) {
                /* 
                 * when we don't have a cleartext password
@@ -1028,12 +1044,12 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
        /* if there's an old supplementaCredentials blob then parse it */
        if (io->o.supplemental) {
                ndr_err = ndr_pull_struct_blob_all(io->o.supplemental, io->ac,
-                                                  lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
+                                                  lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                                   &_old_scb,
                                                   (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                        NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
-                       ldb_asprintf_errstring(io->ac->module->ldb,
+                       ldb_asprintf_errstring(ldb,
                                               "setup_supplemental_field: "
                                               "failed to pull old supplementalCredentialsBlob: %s",
                                               nt_errstr(status));
@@ -1043,16 +1059,17 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
                if (_old_scb.sub.signature == SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
                        old_scb = &_old_scb;
                } else {
-                       ldb_debug(io->ac->module->ldb, LDB_DEBUG_ERROR,
+                       ldb_debug(ldb, LDB_DEBUG_ERROR,
                                               "setup_supplemental_field: "
                                               "supplementalCredentialsBlob signature[0x%04X] expected[0x%04X]",
                                               _old_scb.sub.signature, SUPPLEMENTAL_CREDENTIALS_SIGNATURE);
                }
        }
+       /* Per MS-SAMR 3.1.1.8.11.6 we create AES keys if our domain functionality level is 2008 or higher */
+       domainFunctionality = talloc_get_type(ldb_get_opaque(ldb, "domainFunctionality"), int);
 
-       /* TODO: do the correct check for this, it maybe depends on the functional level? */
-       do_newer_keys = lp_parm_bool(ldb_get_opaque(io->ac->module->ldb, "loadparm"),
-                                    NULL, "password_hash", "create_aes_key", false);
+       do_newer_keys = *domainFunctionality &&
+               (*domainFunctionality >= DS_DOMAIN_FUNCTION_2008);
 
        if (io->domain->store_cleartext &&
            (io->u.user_account_control & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED)) {
@@ -1110,12 +1127,12 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
                }
 
                ndr_err = ndr_push_struct_blob(&pknb_blob, io->ac,
-                                              lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
+                                              lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                               &pknb,
                                               (ndr_push_flags_fn_t)ndr_push_package_PrimaryKerberosBlob);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                        NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
-                       ldb_asprintf_errstring(io->ac->module->ldb,
+                       ldb_asprintf_errstring(ldb,
                                               "setup_supplemental_field: "
                                               "failed to push package_PrimaryKerberosNeverBlob: %s",
                                               nt_errstr(status));
@@ -1123,7 +1140,7 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
                }
                pknb_hexstr = data_blob_hex_string(io->ac, &pknb_blob);
                if (!pknb_hexstr) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
                pkn->name       = "Primary:Kerberos-Newer-Keys";
@@ -1142,12 +1159,12 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
        }
 
        ndr_err = ndr_push_struct_blob(&pkb_blob, io->ac, 
-                                      lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
+                                      lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                       &pkb,
                                       (ndr_push_flags_fn_t)ndr_push_package_PrimaryKerberosBlob);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_supplemental_field: "
                                       "failed to push package_PrimaryKerberosBlob: %s",
                                       nt_errstr(status));
@@ -1155,7 +1172,7 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
        }
        pkb_hexstr = data_blob_hex_string(io->ac, &pkb_blob);
        if (!pkb_hexstr) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
        pk->name        = "Primary:Kerberos";
@@ -1173,12 +1190,12 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
        }
 
        ndr_err = ndr_push_struct_blob(&pdb_blob, io->ac, 
-                                      lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
+                                      lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                       &pdb,
                                       (ndr_push_flags_fn_t)ndr_push_package_PrimaryWDigestBlob);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_supplemental_field: "
                                       "failed to push package_PrimaryWDigestBlob: %s",
                                       nt_errstr(status));
@@ -1186,7 +1203,7 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
        }
        pdb_hexstr = data_blob_hex_string(io->ac, &pdb_blob);
        if (!pdb_hexstr) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
        pd->name        = "Primary:WDigest";
@@ -1202,12 +1219,12 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
                pcb.cleartext   = *io->n.cleartext_utf16;
 
                ndr_err = ndr_push_struct_blob(&pcb_blob, io->ac, 
-                                              lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
+                                              lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                               &pcb,
                                               (ndr_push_flags_fn_t)ndr_push_package_PrimaryCLEARTEXTBlob);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                        NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
-                       ldb_asprintf_errstring(io->ac->module->ldb,
+                       ldb_asprintf_errstring(ldb,
                                               "setup_supplemental_field: "
                                               "failed to push package_PrimaryCLEARTEXTBlob: %s",
                                               nt_errstr(status));
@@ -1215,7 +1232,7 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
                }
                pcb_hexstr = data_blob_hex_string(io->ac, &pcb_blob);
                if (!pcb_hexstr) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
                pc->name        = "Primary:CLEARTEXT";
@@ -1228,12 +1245,12 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
         */
        pb.names = names;
        ndr_err = ndr_push_struct_blob(&pb_blob, io->ac, 
-                                      lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")), 
+                                      lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
                                       &pb,
                                       (ndr_push_flags_fn_t)ndr_push_package_PackagesBlob);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_supplemental_field: "
                                       "failed to push package_PackagesBlob: %s",
                                       nt_errstr(status));
@@ -1241,7 +1258,7 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
        }
        pb_hexstr = data_blob_hex_string(io->ac, &pb_blob);
        if (!pb_hexstr) {
-               ldb_oom(io->ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
        pp->name        = "Packages";
@@ -1256,12 +1273,12 @@ static int setup_supplemental_field(struct setup_password_fields_io *io)
        scb.sub.packages        = packages;
 
        ndr_err = ndr_push_struct_blob(&io->g.supplemental, io->ac, 
-                                      lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")),
+                                      lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                       &scb,
                                       (ndr_push_flags_fn_t)ndr_push_supplementalCredentialsBlob);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_supplemental_field: "
                                       "failed to push supplementalCredentialsBlob: %s",
                                       nt_errstr(status));
@@ -1289,23 +1306,26 @@ static int setup_kvno_field(struct setup_password_fields_io *io)
 
 static int setup_password_fields(struct setup_password_fields_io *io)
 {
+       struct ldb_context *ldb;
        bool ok;
        int ret;
-       ssize_t converted_pw_len;
-               
+       size_t converted_pw_len;
+
+       ldb = ldb_module_get_ctx(io->ac->module);
+
        /*
         * refuse the change if someone want to change the cleartext
         * and supply his own hashes at the same time...
         */
        if ((io->n.cleartext_utf8 || io->n.cleartext_utf16) && (io->n.nt_hash || io->n.lm_hash)) {
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_password_fields: "
                                       "it's only allowed to set the cleartext password or the password hashes");
                return LDB_ERR_UNWILLING_TO_PERFORM;
        }
        
        if (io->n.cleartext_utf8 && io->n.cleartext_utf16) {
-               ldb_asprintf_errstring(io->ac->module->ldb,
+               ldb_asprintf_errstring(ldb,
                                       "setup_password_fields: "
                                       "it's only allowed to set the cleartext password as userPassword or clearTextPasssword, not both at once");
                return LDB_ERR_UNWILLING_TO_PERFORM;
@@ -1316,14 +1336,13 @@ static int setup_password_fields(struct setup_password_fields_io *io)
                struct ldb_val *cleartext_utf16_blob;
                io->n.cleartext_utf16 = cleartext_utf16_blob = talloc(io->ac, struct ldb_val);
                if (!io->n.cleartext_utf16) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
-               converted_pw_len = convert_string_talloc_convenience(io->ac, lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")), 
+               if (!convert_string_talloc_convenience(io->ac, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
                                                         CH_UTF8, CH_UTF16, io->n.cleartext_utf8->data, io->n.cleartext_utf8->length, 
-                                                        (void **)&cleartext_utf16_str);
-               if (converted_pw_len == -1) {
-                       ldb_asprintf_errstring(io->ac->module->ldb,
+                                                        (void **)&cleartext_utf16_str, &converted_pw_len, false)) {
+                       ldb_asprintf_errstring(ldb,
                                               "setup_password_fields: "
                                               "failed to generate UTF16 password from cleartext UTF8 password");
                        return LDB_ERR_OPERATIONS_ERROR;
@@ -1334,13 +1353,12 @@ static int setup_password_fields(struct setup_password_fields_io *io)
                struct ldb_val *cleartext_utf8_blob;
                io->n.cleartext_utf8 = cleartext_utf8_blob = talloc(io->ac, struct ldb_val);
                if (!io->n.cleartext_utf8) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
-               converted_pw_len = convert_string_talloc_convenience(io->ac, lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")), 
-                                                        CH_UTF16, CH_UTF8, io->n.cleartext_utf16->data, io->n.cleartext_utf16->length, 
-                                                        (void **)&cleartext_utf8_str);
-               if (converted_pw_len == -1) {
+               if (!convert_string_talloc_convenience(io->ac, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
+                                                        CH_UTF16MUNGED, CH_UTF8, io->n.cleartext_utf16->data, io->n.cleartext_utf16->length, 
+                                                        (void **)&cleartext_utf8_str, &converted_pw_len, false)) {
                        /* We can't bail out entirely, as these unconvertable passwords are frustratingly valid */
                        io->n.cleartext_utf8 = NULL;    
                        talloc_free(cleartext_utf8_blob);
@@ -1351,7 +1369,7 @@ static int setup_password_fields(struct setup_password_fields_io *io)
                struct samr_Password *nt_hash;
                nt_hash = talloc(io->ac, struct samr_Password);
                if (!nt_hash) {
-                       ldb_oom(io->ac->module->ldb);
+                       ldb_oom(ldb);
                        return LDB_ERR_OPERATIONS_ERROR;
                }
                io->n.nt_hash = nt_hash;
@@ -1363,13 +1381,13 @@ static int setup_password_fields(struct setup_password_fields_io *io)
        if (io->n.cleartext_utf8) {
                struct samr_Password *lm_hash;
                char *cleartext_unix;
-               converted_pw_len = convert_string_talloc_convenience(io->ac, lp_iconv_convenience(ldb_get_opaque(io->ac->module->ldb, "loadparm")), 
+               if (lp_lanman_auth(ldb_get_opaque(ldb, "loadparm")) &&
+                   convert_string_talloc_convenience(io->ac, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
                                                         CH_UTF8, CH_UNIX, io->n.cleartext_utf8->data, io->n.cleartext_utf8->length, 
-                                                        (void **)&cleartext_unix);
-               if (converted_pw_len != -1) {
+                                                        (void **)&cleartext_unix, &converted_pw_len, false)) {
                        lm_hash = talloc(io->ac, struct samr_Password);
                        if (!lm_hash) {
-                               ldb_oom(io->ac->module->ldb);
+                               ldb_oom(ldb);
                                return LDB_ERR_OPERATIONS_ERROR;
                        }
                        
@@ -1383,47 +1401,111 @@ static int setup_password_fields(struct setup_password_fields_io *io)
                }
 
                ret = setup_kerberos_keys(io);
-               if (ret != 0) {
+               if (ret != LDB_SUCCESS) {
                        return ret;
                }
        }
 
        ret = setup_nt_fields(io);
-       if (ret != 0) {
+       if (ret != LDB_SUCCESS) {
                return ret;
        }
 
        ret = setup_lm_fields(io);
-       if (ret != 0) {
+       if (ret != LDB_SUCCESS) {
                return ret;
        }
 
        ret = setup_supplemental_field(io);
-       if (ret != 0) {
+       if (ret != LDB_SUCCESS) {
                return ret;
        }
 
        ret = setup_last_set_field(io);
-       if (ret != 0) {
+       if (ret != LDB_SUCCESS) {
                return ret;
        }
 
        ret = setup_kvno_field(io);
-       if (ret != 0) {
+       if (ret != LDB_SUCCESS) {
                return ret;
        }
 
        return LDB_SUCCESS;
 }
 
+static int setup_io(struct ph_context *ac, 
+                   const struct ldb_message *new_msg, 
+                   const struct ldb_message *searched_msg, 
+                   struct setup_password_fields_io *io) 
+{ 
+       const struct ldb_val *quoted_utf16;
+       struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
+
+       ZERO_STRUCTP(io);
+
+       /* Some operations below require kerberos contexts */
+       if (smb_krb5_init_context(ac,
+                                 ldb_get_event_context(ldb),
+                                 (struct loadparm_context *)ldb_get_opaque(ldb, "loadparm"),
+                                 &io->smb_krb5_context) != 0) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       io->ac                          = ac;
+       io->domain                      = ac->domain;
+
+       io->u.user_account_control      = samdb_result_uint(searched_msg, "userAccountControl", 0);
+       io->u.sAMAccountName            = samdb_result_string(searched_msg, "samAccountName", NULL);
+       io->u.user_principal_name       = samdb_result_string(searched_msg, "userPrincipalName", NULL);
+       io->u.is_computer               = ldb_msg_check_string_attribute(searched_msg, "objectClass", "computer");
+
+       io->n.cleartext_utf8            = ldb_msg_find_ldb_val(new_msg, "userPassword");
+       io->n.cleartext_utf16           = ldb_msg_find_ldb_val(new_msg, "clearTextPassword");
+
+       /* this rather strange looking piece of code is there to
+          handle a ldap client setting a password remotely using the
+          unicodePwd ldap field. The syntax is that the password is
+          in UTF-16LE, with a " at either end. Unfortunately the
+          unicodePwd field is also used to store the nt hashes
+          internally in Samba, and is used in the nt hash format on
+          the wire in DRS replication, so we have a single name for
+          two distinct values. The code below leaves us with a small
+          chance (less than 1 in 2^32) of a mixup, if someone manages
+          to create a MD4 hash which starts and ends in 0x22 0x00, as
+          that would then be treated as a UTF16 password rather than
+          a nthash */
+       quoted_utf16                    = ldb_msg_find_ldb_val(new_msg, "unicodePwd");
+       if (quoted_utf16 && 
+           quoted_utf16->length >= 4 &&
+           quoted_utf16->data[0] == '"' && 
+           quoted_utf16->data[1] == 0 && 
+           quoted_utf16->data[quoted_utf16->length-2] == '"' && 
+           quoted_utf16->data[quoted_utf16->length-1] == 0) {
+               io->n.quoted_utf16.data = talloc_memdup(io->ac, quoted_utf16->data+2, quoted_utf16->length-4);
+               io->n.quoted_utf16.length = quoted_utf16->length-4;
+               io->n.cleartext_utf16 = &io->n.quoted_utf16;
+               io->n.nt_hash = NULL;
+       } else {
+               io->n.nt_hash           = samdb_result_hash(io->ac, new_msg, "unicodePwd");
+       }
+
+       io->n.lm_hash                   = samdb_result_hash(io->ac, new_msg, "dBCSPwd");
+
+       return LDB_SUCCESS;
+}
+
 static struct ph_context *ph_init_context(struct ldb_module *module,
                                          struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        struct ph_context *ac;
 
+       ldb = ldb_module_get_ctx(module);
+
        ac = talloc_zero(req, struct ph_context);
        if (ac == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
+               ldb_set_errstring(ldb, "Out of Memory");
                return NULL;
        }
 
@@ -1467,13 +1549,14 @@ static int password_hash_mod_do_mod(struct ph_context *ac);
 static int get_domain_data_callback(struct ldb_request *req,
                                    struct ldb_reply *ares)
 {
+       struct ldb_context *ldb;
        struct domain_data *data;
        struct ph_context *ac;
+       struct loadparm_context *lp_ctx;
        int ret;
-       char *tmp;
-       char *p;
 
        ac = talloc_get_type(req->context, struct ph_context);
+       ldb = ldb_module_get_ctx(ac->module);
 
        if (!ares) {
                return ldb_module_done(ac->req, NULL, NULL,
@@ -1487,7 +1570,7 @@ static int get_domain_data_callback(struct ldb_request *req,
        switch (ares->type) {
        case LDB_REPLY_ENTRY:
                if (ac->domain != NULL) {
-                       ldb_set_errstring(ac->module->ldb, "Too many results");
+                       ldb_set_errstring(ldb, "Too many results");
                        return ldb_module_done(ac->req, NULL, NULL,
                                                LDB_ERR_OPERATIONS_ERROR);
                }
@@ -1507,43 +1590,13 @@ static int get_domain_data_callback(struct ldb_request *req,
                 * but that doesn't really matter, as it's just used for salt
                 * and kerberos principals, which don't exist here */
 
-               tmp = ldb_dn_canonical_string(data, ares->message->dn);
-               if (!tmp) {
-                       return ldb_module_done(ac->req, NULL, NULL,
-                                               LDB_ERR_OPERATIONS_ERROR);
-               }
+               lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
+                                        struct loadparm_context);
 
-               /* But it puts a trailing (or just before 'builtin') / on things, so kill that */
-               p = strchr(tmp, '/');
-               if (p) {
-                       p[0] = '\0';
-               }
+               data->dns_domain = lp_dnsdomain(lp_ctx);
+               data->realm = lp_realm(lp_ctx);
+               data->netbios_domain = lp_sam_name(lp_ctx);
 
-               data->dns_domain = strlower_talloc(data, tmp);
-               if (data->dns_domain == NULL) {
-                       ldb_oom(ac->module->ldb);
-                       return ldb_module_done(ac->req, NULL, NULL,
-                                               LDB_ERR_OPERATIONS_ERROR);
-               }
-               data->realm = strupper_talloc(data, tmp);
-               if (data->realm == NULL) {
-                       ldb_oom(ac->module->ldb);
-                       return ldb_module_done(ac->req, NULL, NULL,
-                                               LDB_ERR_OPERATIONS_ERROR);
-               }
-               /* FIXME: NetbIOS name is *always* the first domain component ?? -SSS */
-               p = strchr(tmp, '.');
-               if (p) {
-                       p[0] = '\0';
-               }
-               data->netbios_domain = strupper_talloc(data, tmp);
-               if (data->netbios_domain == NULL) {
-                       ldb_oom(ac->module->ldb);
-                       return ldb_module_done(ac->req, NULL, NULL,
-                                               LDB_ERR_OPERATIONS_ERROR);
-               }
-
-               talloc_free(tmp);
                ac->domain = data;
                break;
 
@@ -1566,6 +1619,7 @@ static int get_domain_data_callback(struct ldb_request *req,
                if (ret != LDB_SUCCESS) {
                        return ldb_module_done(ac->req, NULL, NULL, ret);
                }
+               break;
 
        case LDB_REPLY_REFERRAL:
                /* ignore */
@@ -1581,20 +1635,23 @@ static int build_domain_data_request(struct ph_context *ac)
        /* attrs[] is returned from this function in
           ac->dom_req->op.search.attrs, so it must be static, as
           otherwise the compiler can put it on the stack */
+       struct ldb_context *ldb;
        static const char * const attrs[] = { "pwdProperties", "pwdHistoryLength", NULL };
        char *filter;
 
+       ldb = ldb_module_get_ctx(ac->module);
+
        filter = talloc_asprintf(ac,
-                               "(&(objectSid=%s)(|(|(objectClass=domain)(objectClass=builtinDomain))(objectClass=samba4LocalDomain)))",
+                                "(objectSid=%s)",
                                 ldap_encode_ndr_dom_sid(ac, ac->domain_sid));
        if (filter == NULL) {
-               ldb_oom(ac->module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       return ldb_build_search_req(&ac->dom_req, ac->module->ldb, ac,
-                                   ldb_get_default_basedn(ac->module->ldb),
-                                   LDB_SCOPE_SUBTREE,
+       return ldb_build_search_req(&ac->dom_req, ldb, ac,
+                                   ldb_get_default_basedn(ldb),
+                                   LDB_SCOPE_BASE,
                                    filter, attrs,
                                    NULL,
                                    ac, get_domain_data_callback,
@@ -1603,6 +1660,7 @@ static int build_domain_data_request(struct ph_context *ac)
 
 static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        struct ph_context *ac;
        struct ldb_message_element *sambaAttr;
        struct ldb_message_element *clearTextPasswordAttr;
@@ -1610,14 +1668,16 @@ static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
        struct ldb_message_element *lmAttr;
        int ret;
 
-       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_add\n");
+       ldb = ldb_module_get_ctx(module);
+
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "password_hash_add\n");
 
        if (ldb_dn_is_special(req->op.add.message->dn)) { /* do not manipulate our control entries */
                return ldb_next_request(module, req);
        }
 
        /* If the caller is manipulating the local passwords directly, let them pass */
-       if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
+       if (ldb_dn_compare_base(ldb_dn_new(req, ldb, LOCAL_BASE),
                                req->op.add.message->dn) == 0) {
                return ldb_next_request(module, req);
        }
@@ -1641,53 +1701,53 @@ static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
        ntAttr = ldb_msg_find_element(req->op.mod.message, "unicodePwd");
        lmAttr = ldb_msg_find_element(req->op.mod.message, "dBCSPwd");
 
-       if ((!sambaAttr) && (!ntAttr) && (!lmAttr)) {
+       if ((!sambaAttr) && (!clearTextPasswordAttr) && (!ntAttr) && (!lmAttr)) {
                return ldb_next_request(module, req);
        }
 
        /* if it is not an entry of type person its an error */
        /* TODO: remove this when userPassword will be in schema */
        if (!ldb_msg_check_string_attribute(req->op.add.message, "objectClass", "person")) {
-               ldb_set_errstring(module->ldb, "Cannot set a password on entry that does not have objectClass 'person'");
+               ldb_set_errstring(ldb, "Cannot set a password on entry that does not have objectClass 'person'");
                return LDB_ERR_OBJECT_CLASS_VIOLATION;
        }
 
        /* check userPassword is single valued here */
        /* TODO: remove this when userPassword will be single valued in schema */
        if (sambaAttr && sambaAttr->num_values > 1) {
-               ldb_set_errstring(module->ldb, "mupltiple values for userPassword not allowed!\n");
+               ldb_set_errstring(ldb, "mupltiple values for userPassword not allowed!\n");
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
        if (clearTextPasswordAttr && clearTextPasswordAttr->num_values > 1) {
-               ldb_set_errstring(module->ldb, "mupltiple values for clearTextPassword not allowed!\n");
+               ldb_set_errstring(ldb, "mupltiple values for clearTextPassword not allowed!\n");
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
 
        if (ntAttr && (ntAttr->num_values > 1)) {
-               ldb_set_errstring(module->ldb, "mupltiple values for unicodePwd not allowed!\n");
+               ldb_set_errstring(ldb, "mupltiple values for unicodePwd not allowed!\n");
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
        if (lmAttr && (lmAttr->num_values > 1)) {
-               ldb_set_errstring(module->ldb, "mupltiple values for dBCSPwd not allowed!\n");
+               ldb_set_errstring(ldb, "mupltiple values for dBCSPwd not allowed!\n");
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
 
        if (sambaAttr && sambaAttr->num_values == 0) {
-               ldb_set_errstring(module->ldb, "userPassword must have a value!\n");
+               ldb_set_errstring(ldb, "userPassword must have a value!\n");
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
 
        if (clearTextPasswordAttr && clearTextPasswordAttr->num_values == 0) {
-               ldb_set_errstring(module->ldb, "clearTextPassword must have a value!\n");
+               ldb_set_errstring(ldb, "clearTextPassword must have a value!\n");
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
 
        if (ntAttr && (ntAttr->num_values == 0)) {
-               ldb_set_errstring(module->ldb, "unicodePwd must have a value!\n");
+               ldb_set_errstring(ldb, "unicodePwd must have a value!\n");
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
        if (lmAttr && (lmAttr->num_values == 0)) {
-               ldb_set_errstring(module->ldb, "dBCSPwd must have a value!\n");
+               ldb_set_errstring(ldb, "dBCSPwd must have a value!\n");
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
 
@@ -1699,7 +1759,7 @@ static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
        /* get user domain data */
        ac->domain_sid = samdb_result_sid_prefix(ac, req->op.add.message, "objectSid");
        if (ac->domain_sid == NULL) {
-               ldb_debug(module->ldb, LDB_DEBUG_ERROR,
+               ldb_debug(ldb, LDB_DEBUG_ERROR,
                          "can't handle entry with missing objectSid!\n");
                return LDB_ERR_OPERATIONS_ERROR;
        }
@@ -1712,65 +1772,50 @@ static int password_hash_add(struct ldb_module *module, struct ldb_request *req)
        return ldb_next_request(module, ac->dom_req);
 }
 
-static int password_hash_add_do_add(struct ph_context *ac) {
-
+static int password_hash_add_do_add(struct ph_context *ac)
+{
+       struct ldb_context *ldb;
        struct ldb_request *down_req;
-       struct smb_krb5_context *smb_krb5_context;
        struct ldb_message *msg;
        struct setup_password_fields_io io;
        int ret;
 
-       msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
-       if (msg == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
+       /* Prepare the internal data structure containing the passwords */
+       ret = setup_io(ac, ac->req->op.add.message, ac->req->op.add.message, &io);
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
 
-       /* Some operations below require kerberos contexts */
-       if (smb_krb5_init_context(ac,
-                                 ldb_get_event_context(ac->module->ldb),
-                                 (struct loadparm_context *)ldb_get_opaque(ac->module->ldb, "loadparm"),
-                                 &smb_krb5_context) != 0) {
+       msg = ldb_msg_copy_shallow(ac, ac->req->op.add.message);
+       if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       ZERO_STRUCT(io);
-       io.ac                           = ac;
-       io.domain                       = ac->domain;
-       io.smb_krb5_context             = smb_krb5_context;
-
-       io.u.user_account_control       = samdb_result_uint(msg, "userAccountControl", 0);
-       io.u.sAMAccountName             = samdb_result_string(msg, "samAccountName", NULL);
-       io.u.user_principal_name        = samdb_result_string(msg, "userPrincipalName", NULL);
-       io.u.is_computer                = ldb_msg_check_string_attribute(msg, "objectClass", "computer");
-
-       io.n.cleartext_utf8             = ldb_msg_find_ldb_val(msg, "userPassword");
-       io.n.cleartext_utf16            = ldb_msg_find_ldb_val(msg, "clearTextPassword");
-       io.n.nt_hash                    = samdb_result_hash(io.ac, msg, "unicodePwd");
-       io.n.lm_hash                    = samdb_result_hash(io.ac, msg, "dBCSPwd");
-
-       /* remove attributes */
-       if (io.n.cleartext_utf8) ldb_msg_remove_attr(msg, "userPassword");
-       if (io.n.cleartext_utf16) ldb_msg_remove_attr(msg, "clearTextPassword");
-       if (io.n.nt_hash) ldb_msg_remove_attr(msg, "unicodePwd");
-       if (io.n.lm_hash) ldb_msg_remove_attr(msg, "dBCSPwd");
+       /* remove attributes that we just read into 'io' */
+       ldb_msg_remove_attr(msg, "userPassword");
+       ldb_msg_remove_attr(msg, "clearTextPassword");
+       ldb_msg_remove_attr(msg, "unicodePwd");
+       ldb_msg_remove_attr(msg, "dBCSPwd");
        ldb_msg_remove_attr(msg, "pwdLastSet");
        io.o.kvno = samdb_result_uint(msg, "msDs-KeyVersionNumber", 1) - 1;
        ldb_msg_remove_attr(msg, "msDs-KeyVersionNumber");
 
+       ldb = ldb_module_get_ctx(ac->module);
+
        ret = setup_password_fields(&io);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
 
        if (io.g.nt_hash) {
-               ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
+               ret = samdb_msg_add_hash(ldb, ac, msg,
                                         "unicodePwd", io.g.nt_hash);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
        }
        if (io.g.lm_hash) {
-               ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
+               ret = samdb_msg_add_hash(ldb, ac, msg,
                                         "dBCSPwd", io.g.lm_hash);
                if (ret != LDB_SUCCESS) {
                        return ret;
@@ -1801,20 +1846,20 @@ static int password_hash_add_do_add(struct ph_context *ac) {
                        return ret;
                }
        }
-       ret = samdb_msg_add_uint64(ac->module->ldb, ac, msg,
+       ret = samdb_msg_add_uint64(ldb, ac, msg,
                                   "pwdLastSet",
                                   io.g.last_set);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
-       ret = samdb_msg_add_uint(ac->module->ldb, ac, msg,
+       ret = samdb_msg_add_uint(ldb, ac, msg,
                                 "msDs-KeyVersionNumber",
                                 io.g.kvno);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
 
-       ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
+       ret = ldb_build_add_req(&down_req, ldb, ac,
                                msg,
                                ac->req->controls,
                                ac, ph_op_callback,
@@ -1828,6 +1873,7 @@ static int password_hash_add_do_add(struct ph_context *ac) {
 
 static int password_hash_modify(struct ldb_module *module, struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        struct ph_context *ac;
        struct ldb_message_element *sambaAttr;
        struct ldb_message_element *clearTextAttr;
@@ -1837,14 +1883,16 @@ static int password_hash_modify(struct ldb_module *module, struct ldb_request *r
        struct ldb_request *down_req;
        int ret;
 
-       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "password_hash_modify\n");
+       ldb = ldb_module_get_ctx(module);
+
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "password_hash_modify\n");
 
        if (ldb_dn_is_special(req->op.mod.message->dn)) { /* do not manipulate our control entries */
                return ldb_next_request(module, req);
        }
        
        /* If the caller is manipulating the local passwords directly, let them pass */
-       if (ldb_dn_compare_base(ldb_dn_new(req, module->ldb, LOCAL_BASE),
+       if (ldb_dn_compare_base(ldb_dn_new(req, ldb, LOCAL_BASE),
                                req->op.mod.message->dn) == 0) {
                return ldb_next_request(module, req);
        }
@@ -1877,27 +1925,32 @@ static int password_hash_modify(struct ldb_module *module, struct ldb_request *r
        /* check passwords are single valued here */
        /* TODO: remove this when passwords will be single valued in schema */
        if (sambaAttr && (sambaAttr->num_values > 1)) {
+               DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
        if (clearTextAttr && (clearTextAttr->num_values > 1)) {
+               DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
        if (ntAttr && (ntAttr->num_values > 1)) {
+               DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
        if (lmAttr && (lmAttr->num_values > 1)) {
+               DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
 
        ac = ph_init_context(module, req);
        if (!ac) {
+               DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        /* use a new message structure so that we can modify it */
        msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
        if (msg == NULL) {
-               ldb_oom(module->ldb);
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -1913,7 +1966,7 @@ static int password_hash_modify(struct ldb_module *module, struct ldb_request *r
                return password_hash_mod_search_self(ac);
        }
 
-       ret = ldb_build_mod_req(&down_req, module->ldb, ac,
+       ret = ldb_build_mod_req(&down_req, ldb, ac,
                                msg,
                                req->controls,
                                ac, ph_modify_callback,
@@ -1958,10 +2011,12 @@ static int ph_modify_callback(struct ldb_request *req, struct ldb_reply *ares)
 
 static int ph_mod_search_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
+       struct ldb_context *ldb;
        struct ph_context *ac;
        int ret;
 
        ac = talloc_get_type(req->context, struct ph_context);
+       ldb = ldb_module_get_ctx(ac->module);
 
        if (!ares) {
                return ldb_module_done(ac->req, NULL, NULL,
@@ -1977,7 +2032,7 @@ static int ph_mod_search_callback(struct ldb_request *req, struct ldb_reply *are
        case LDB_REPLY_ENTRY:
 
                if (ac->search_res != NULL) {
-                       ldb_set_errstring(ac->module->ldb, "Too many results");
+                       ldb_set_errstring(ldb, "Too many results");
                        talloc_free(ares);
                        return ldb_module_done(ac->req, NULL, NULL,
                                                LDB_ERR_OPERATIONS_ERROR);
@@ -1986,7 +2041,7 @@ static int ph_mod_search_callback(struct ldb_request *req, struct ldb_reply *are
                /* if it is not an entry of type person this is an error */
                /* TODO: remove this when sambaPassword will be in schema */
                if (!ldb_msg_check_string_attribute(ares->message, "objectClass", "person")) {
-                       ldb_set_errstring(ac->module->ldb, "Object class violation");
+                       ldb_set_errstring(ldb, "Object class violation");
                        talloc_free(ares);
                        return ldb_module_done(ac->req, NULL, NULL,
                                        LDB_ERR_OBJECT_CLASS_VIOLATION);
@@ -2002,7 +2057,7 @@ static int ph_mod_search_callback(struct ldb_request *req, struct ldb_reply *are
                                                        ac->search_res->message,
                                                        "objectSid");
                if (ac->domain_sid == NULL) {
-                       ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR,
+                       ldb_debug(ldb, LDB_DEBUG_ERROR,
                                  "can't handle entry without objectSid!\n");
                        return ldb_module_done(ac->req, NULL, NULL,
                                                LDB_ERR_OPERATIONS_ERROR);
@@ -2025,8 +2080,9 @@ static int ph_mod_search_callback(struct ldb_request *req, struct ldb_reply *are
        return LDB_SUCCESS;
 }
 
-static int password_hash_mod_search_self(struct ph_context *ac) {
-
+static int password_hash_mod_search_self(struct ph_context *ac)
+{
+       struct ldb_context *ldb;
        static const char * const attrs[] = { "userAccountControl", "lmPwdHistory", 
                                              "ntPwdHistory", 
                                              "objectSid", "msDS-KeyVersionNumber", 
@@ -2038,7 +2094,9 @@ static int password_hash_mod_search_self(struct ph_context *ac) {
        struct ldb_request *search_req;
        int ret;
 
-       ret = ldb_build_search_req(&search_req, ac->module->ldb, ac,
+       ldb = ldb_module_get_ctx(ac->module);
+
+       ret = ldb_build_search_req(&search_req, ldb, ac,
                                   ac->req->op.mod.message->dn,
                                   LDB_SCOPE_BASE,
                                   "(objectclass=*)",
@@ -2054,16 +2112,17 @@ static int password_hash_mod_search_self(struct ph_context *ac) {
        return ldb_next_request(ac->module, search_req);
 }
 
-static int password_hash_mod_do_mod(struct ph_context *ac) {
-
+static int password_hash_mod_do_mod(struct ph_context *ac)
+{
+       struct ldb_context *ldb;
        struct ldb_request *mod_req;
-       struct smb_krb5_context *smb_krb5_context;
        struct ldb_message *msg;
-       struct ldb_message *orig_msg;
-       struct ldb_message *searched_msg;
+       const struct ldb_message *searched_msg;
        struct setup_password_fields_io io;
        int ret;
 
+       ldb = ldb_module_get_ctx(ac->module);
+
        /* use a new message structure so that we can modify it */
        msg = ldb_msg_new(ac);
        if (msg == NULL) {
@@ -2073,32 +2132,18 @@ static int password_hash_mod_do_mod(struct ph_context *ac) {
        /* modify dn */
        msg->dn = ac->req->op.mod.message->dn;
 
-       /* Some operations below require kerberos contexts */
-       if (smb_krb5_init_context(ac,
-                                 ldb_get_event_context(ac->module->ldb),
-                                 (struct loadparm_context *)ldb_get_opaque(ac->module->ldb, "loadparm"),
-                                 &smb_krb5_context) != 0) {
-               return LDB_ERR_OPERATIONS_ERROR;
+       /* Prepare the internal data structure containing the passwords */
+       ret = setup_io(ac, 
+                      ac->req->op.mod.message, 
+                      ac->search_res->message, 
+                      &io);
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
+       
+       searched_msg = ac->search_res->message;
 
-       orig_msg        = discard_const(ac->req->op.mod.message);
-       searched_msg    = ac->search_res->message;
-
-       ZERO_STRUCT(io);
-       io.ac                           = ac;
-       io.domain                       = ac->domain;
-       io.smb_krb5_context             = smb_krb5_context;
-
-       io.u.user_account_control       = samdb_result_uint(searched_msg, "userAccountControl", 0);
-       io.u.sAMAccountName             = samdb_result_string(searched_msg, "samAccountName", NULL);
-       io.u.user_principal_name        = samdb_result_string(searched_msg, "userPrincipalName", NULL);
-       io.u.is_computer                = ldb_msg_check_string_attribute(searched_msg, "objectClass", "computer");
-
-       io.n.cleartext_utf8             = ldb_msg_find_ldb_val(orig_msg, "userPassword");
-       io.n.cleartext_utf16            = ldb_msg_find_ldb_val(orig_msg, "clearTextPassword");
-       io.n.nt_hash                    = samdb_result_hash(io.ac, orig_msg, "unicodePwd");
-       io.n.lm_hash                    = samdb_result_hash(io.ac, orig_msg, "dBCSPwd");
-
+       /* Fill in some final details (only relevent once the password has been set) */
        io.o.kvno                       = samdb_result_uint(searched_msg, "msDs-KeyVersionNumber", 0);
        io.o.nt_history_len             = samdb_result_hashes(io.ac, searched_msg, "ntPwdHistory", &io.o.nt_history);
        io.o.lm_history_len             = samdb_result_hashes(io.ac, searched_msg, "lmPwdHistory", &io.o.lm_history);
@@ -2119,14 +2164,14 @@ static int password_hash_mod_do_mod(struct ph_context *ac) {
        ret = ldb_msg_add_empty(msg, "msDs-KeyVersionNumber", LDB_FLAG_MOD_REPLACE, NULL);
 
        if (io.g.nt_hash) {
-               ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
+               ret = samdb_msg_add_hash(ldb, ac, msg,
                                         "unicodePwd", io.g.nt_hash);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
        }
        if (io.g.lm_hash) {
-               ret = samdb_msg_add_hash(ac->module->ldb, ac, msg,
+               ret = samdb_msg_add_hash(ldb, ac, msg,
                                         "dBCSPwd", io.g.lm_hash);
                if (ret != LDB_SUCCESS) {
                        return ret;
@@ -2157,20 +2202,20 @@ static int password_hash_mod_do_mod(struct ph_context *ac) {
                        return ret;
                }
        }
-       ret = samdb_msg_add_uint64(ac->module->ldb, ac, msg,
+       ret = samdb_msg_add_uint64(ldb, ac, msg,
                                   "pwdLastSet",
                                   io.g.last_set);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
-       ret = samdb_msg_add_uint(ac->module->ldb, ac, msg,
+       ret = samdb_msg_add_uint(ldb, ac, msg,
                                 "msDs-KeyVersionNumber",
                                 io.g.kvno);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
 
-       ret = ldb_build_mod_req(&mod_req, ac->module->ldb, ac,
+       ret = ldb_build_mod_req(&mod_req, ldb, ac,
                                msg,
                                ac->req->controls,
                                ac, ph_op_callback,