s4:libnet_join.c - always use LDB constants
[kai/samba.git] / source4 / libnet / libnet_join.c
index b0907c546192834497082ec22a0dfeffc64a932d..ea110392e43bf982a67ec41e29896cb3c23b52f1 100644 (file)
@@ -7,7 +7,7 @@
  
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -16,8 +16,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "lib/ldb/include/ldb_errors.h"
 #include "param/secrets.h"
 #include "dsdb/samdb/samdb.h"
-#include "db_wrap.h"
+#include "ldb_wrap.h"
+#include "../lib/util/util_ldb.h"
 #include "libcli/security/security.h"
 #include "auth/credentials/credentials.h"
+#include "auth/credentials/credentials_krb5.h"
 #include "librpc/gen_ndr/ndr_samr_c.h"
+#include "param/param.h"
+#include "param/provision.h"
 
 /*
  * complete a domain join, when joining to a AD domain:
@@ -61,7 +64,7 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
        struct GUID drsuapi_bind_guid;
 
        struct ldb_context *remote_ldb;
-       const struct ldb_dn *account_dn;
+       struct ldb_dn *account_dn;
        const char *account_dn_str;
        const char *remote_ldb_url;
        struct ldb_result *res;
@@ -69,12 +72,11 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
 
        int ret, rtn;
 
-       unsigned int kvno;
-       
        const char * const attrs[] = {
                "msDS-KeyVersionNumber",
                "servicePrincipalName",
                "dNSHostName",
+               "objectGUID",
                NULL,
        };
 
@@ -111,13 +113,14 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
        status = dcerpc_pipe_connect_b(tmp_ctx, 
                                       &drsuapi_pipe,
                                       drsuapi_binding,
-                                      &dcerpc_table_drsuapi,
+                                      &ndr_table_drsuapi,
                                       ctx->cred, 
-                                      ctx->event_ctx);
+                                      ctx->event_ctx,
+                                      ctx->lp_ctx);
        if (!NT_STATUS_IS_OK(status)) {
                r->out.error_string = talloc_asprintf(r,
                                        "Connection to DRSUAPI pipe of PDC of domain '%s' failed: %s",
-                                       r->in.domain_name,
+                                       r->out.domain_name,
                                        nt_errstr(status));
                talloc_free(tmp_ctx);
                return status;
@@ -130,23 +133,14 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
        r_drsuapi_bind.in.bind_info = NULL;
        r_drsuapi_bind.out.bind_handle = &drsuapi_bind_handle;
 
-       status = dcerpc_drsuapi_DsBind(drsuapi_pipe, tmp_ctx, &r_drsuapi_bind);
+       status = dcerpc_drsuapi_DsBind_r(drsuapi_pipe->binding_handle, tmp_ctx, &r_drsuapi_bind);
        if (!NT_STATUS_IS_OK(status)) {
-               if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
-                       r->out.error_string
-                               = talloc_asprintf(r,
-                                                 "dcerpc_drsuapi_DsBind failed - %s", 
-                                                 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
-                       talloc_free(tmp_ctx);
-                       return status;
-               } else {
-                       r->out.error_string
-                               = talloc_asprintf(r,
-                                                 "dcerpc_drsuapi_DsBind failed - %s", 
-                                                 nt_errstr(status));
-                       talloc_free(tmp_ctx);
-                       return status;
-               }
+               r->out.error_string
+                       = talloc_asprintf(r,
+                                         "dcerpc_drsuapi_DsBind failed - %s",
+                                         nt_errstr(status));
+               talloc_free(tmp_ctx);
+               return status;
        } else if (!W_ERROR_IS_OK(r_drsuapi_bind.out.result)) {
                r->out.error_string
                                = talloc_asprintf(r,
@@ -160,13 +154,19 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
        ZERO_STRUCT(r_crack_names);
        r_crack_names.in.bind_handle            = &drsuapi_bind_handle;
        r_crack_names.in.level                  = 1;
-       r_crack_names.in.req.req1.unknown1      = 0x000004e4;
-       r_crack_names.in.req.req1.unknown2      = 0x00000407;
-       r_crack_names.in.req.req1.count         = 1;
-       r_crack_names.in.req.req1.names         = names;
-       r_crack_names.in.req.req1.format_flags  = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
-       r_crack_names.in.req.req1.format_offered= DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY;
-       r_crack_names.in.req.req1.format_desired= DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
+       r_crack_names.in.req                    = talloc(r, union drsuapi_DsNameRequest);
+       if (!r_crack_names.in.req) {
+               r->out.error_string = NULL;
+               talloc_free(tmp_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+       r_crack_names.in.req->req1.codepage     = 1252; /* western european */
+       r_crack_names.in.req->req1.language     = 0x00000407; /* german */
+       r_crack_names.in.req->req1.count        = 1;
+       r_crack_names.in.req->req1.names        = names;
+       r_crack_names.in.req->req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
+       r_crack_names.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY;
+       r_crack_names.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
        names[0].str = dom_sid_string(tmp_ctx, r->out.account_sid);
        if (!names[0].str) {
                r->out.error_string = NULL;
@@ -174,57 +174,47 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
+       r_crack_names.out.ctr                   = talloc(r, union drsuapi_DsNameCtr);
+       r_crack_names.out.level_out             = talloc(r, uint32_t);
+       if (!r_crack_names.out.ctr || !r_crack_names.out.level_out) {
+               r->out.error_string = NULL;
+               talloc_free(tmp_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe->binding_handle, tmp_ctx, &r_crack_names);
        if (!NT_STATUS_IS_OK(status)) {
-               if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
-                       r->out.error_string
-                               = talloc_asprintf(r,
-                                                 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s", 
-                                                 names[0].str,
-                                                 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
-                       talloc_free(tmp_ctx);
-                       return status;
-               } else {
-                       r->out.error_string
-                               = talloc_asprintf(r,
-                                                 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s", 
-                                                 names[0].str,
-                                                 nt_errstr(status));
-                       talloc_free(tmp_ctx);
-                       return status;
-               }
+               r->out.error_string
+                       = talloc_asprintf(r,
+                                         "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
+                                         names[0].str,
+                                         nt_errstr(status));
+               talloc_free(tmp_ctx);
+               return status;
        } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
                r->out.error_string
                                = talloc_asprintf(r,
                                                  "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
                talloc_free(tmp_ctx);
                return NT_STATUS_UNSUCCESSFUL;
-       } else if (r_crack_names.out.level != 1 
-                  || !r_crack_names.out.ctr.ctr1 
-                  || r_crack_names.out.ctr.ctr1->count != 1) {
+       } else if (*r_crack_names.out.level_out != 1
+                  || !r_crack_names.out.ctr->ctr1
+                  || r_crack_names.out.ctr->ctr1->count != 1) {
                r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
                talloc_free(tmp_ctx);
                return NT_STATUS_INVALID_PARAMETER;
-       } else if (r_crack_names.out.ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
-               r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: %d", r_crack_names.out.ctr.ctr1->array[0].status);
+       } else if (r_crack_names.out.ctr->ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
+               r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: %d", r_crack_names.out.ctr->ctr1->array[0].status);
                talloc_free(tmp_ctx);
                return NT_STATUS_UNSUCCESSFUL;
-       } else if (r_crack_names.out.ctr.ctr1->array[0].result_name == NULL) {
+       } else if (r_crack_names.out.ctr->ctr1->array[0].result_name == NULL) {
                r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: no result name");
                talloc_free(tmp_ctx);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
        /* Store the DN of our machine account. */
-       account_dn_str = r_crack_names.out.ctr.ctr1->array[0].result_name;
-
-       account_dn = ldb_dn_explode(tmp_ctx, account_dn_str);
-       if (!account_dn) {
-               r->out.error_string = talloc_asprintf(r, "Invalid account dn: %s",
-                                                     account_dn_str);
-               talloc_free(tmp_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
+       account_dn_str = r_crack_names.out.ctr->ctr1->array[0].result_name;
 
        /* Now we know the user's DN, open with LDAP, read and modify a few things */
 
@@ -236,17 +226,26 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
                return NT_STATUS_NO_MEMORY;
        }
 
-       remote_ldb = ldb_wrap_connect(tmp_ctx, remote_ldb_url, 
-                                     NULL, ctx->cred, 0, NULL);
+       remote_ldb = ldb_wrap_connect(tmp_ctx, ctx->event_ctx, ctx->lp_ctx, 
+                                     remote_ldb_url, 
+                                     NULL, ctx->cred, 0);
        if (!remote_ldb) {
                r->out.error_string = NULL;
                talloc_free(tmp_ctx);
                return NT_STATUS_UNSUCCESSFUL;
        }
 
+       account_dn = ldb_dn_new(tmp_ctx, remote_ldb, account_dn_str);
+       if (! ldb_dn_validate(account_dn)) {
+               r->out.error_string = talloc_asprintf(r, "Invalid account dn: %s",
+                                                     account_dn_str);
+               talloc_free(tmp_ctx);
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
        /* search for the user's record */
-       ret = ldb_search(remote_ldb, account_dn, LDB_SCOPE_BASE, 
-                        NULL, attrs, &res);
+       ret = ldb_search(remote_ldb, tmp_ctx, &res,
+                        account_dn, LDB_SCOPE_BASE, attrs, NULL);
        if (ret != LDB_SUCCESS) {
                r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - %s",
                                                      account_dn_str, ldb_errstring(remote_ldb));
@@ -254,8 +253,6 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       talloc_steal(tmp_ctx, res);
-
        if (res->count != 1) {
                r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - found %d entries",
                                                      account_dn_str, res->count);
@@ -263,9 +260,6 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       /* If we have a kvno recorded in AD, we need it locally as well */
-       kvno = ldb_msg_find_attr_as_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
-
        /* Prepare a new message, for the modify */
        msg = ldb_msg_new(tmp_ctx);
        if (!msg) {
@@ -276,7 +270,7 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
        msg->dn = res->msgs[0]->dn;
 
        {
-               int i;
+               unsigned int i;
                const char *service_principal_name[6];
                const char *dns_host_name = strlower_talloc(tmp_ctx, 
                                                            talloc_asprintf(tmp_ctx, 
@@ -304,7 +298,7 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
                                return NT_STATUS_NO_MEMORY;
                        }
                        rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "servicePrincipalName", service_principal_name[i]);
-                       if (rtn == -1) {
+                       if (rtn != LDB_SUCCESS) {
                                r->out.error_string = NULL;
                                talloc_free(tmp_ctx);
                                return NT_STATUS_NO_MEMORY;
@@ -312,26 +306,58 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
                }
 
                rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "dNSHostName", dns_host_name);
-               if (rtn == -1) {
+               if (rtn != LDB_SUCCESS) {
                        r->out.error_string = NULL;
                        talloc_free(tmp_ctx);
                        return NT_STATUS_NO_MEMORY;
                }
 
-               rtn = samdb_replace(remote_ldb, tmp_ctx, msg);
-               if (rtn != 0) {
+               rtn = dsdb_replace(remote_ldb, msg, 0);
+               if (rtn != LDB_SUCCESS) {
                        r->out.error_string
                                = talloc_asprintf(r, 
                                                  "Failed to replace entries on %s", 
-                                                 ldb_dn_linearize(tmp_ctx, msg->dn));
+                                                 ldb_dn_get_linearized(msg->dn));
                        talloc_free(tmp_ctx);
                        return NT_STATUS_INTERNAL_DB_CORRUPTION;
                }
        }
                                
+       msg = ldb_msg_new(tmp_ctx);
+       if (!msg) {
+               r->out.error_string = NULL;
+               talloc_free(tmp_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+       msg->dn = res->msgs[0]->dn;
+
+       rtn = ldb_msg_add_fmt(msg, "msDS-SupportedEncryptionTypes",
+                             "%lu",
+                             (long unsigned int)(ENC_CRC32 | ENC_RSA_MD5 |
+                                                 ENC_RC4_HMAC_MD5 |
+                                                 ENC_HMAC_SHA1_96_AES128 |
+                                                 ENC_HMAC_SHA1_96_AES256));
+       if (rtn != LDB_SUCCESS) {
+               r->out.error_string = NULL;
+               talloc_free(tmp_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       rtn = dsdb_replace(remote_ldb, msg, 0);
+       /* The remote server may not support this attribute, if it
+        * isn't a modern schema */
+       if (rtn != LDB_SUCCESS && rtn != LDB_ERR_NO_SUCH_ATTRIBUTE) {
+               r->out.error_string
+                       = talloc_asprintf(r,
+                                         "Failed to replace msDS-SupportedEncryptionType on %s",
+                                         ldb_dn_get_linearized(msg->dn));
+               talloc_free(tmp_ctx);
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
        /* DsCrackNames to find out the DN of the domain. */
-       r_crack_names.in.req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
-       r_crack_names.in.req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
+       r_crack_names.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
+       r_crack_names.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
        names[0].str = talloc_asprintf(tmp_ctx, "%s\\", r->out.domain_name);
        if (!names[0].str) {
                r->out.error_string = NULL;
@@ -339,36 +365,26 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
+       status = dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe->binding_handle, tmp_ctx, &r_crack_names);
        if (!NT_STATUS_IS_OK(status)) {
-               if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
-                       r->out.error_string
-                               = talloc_asprintf(r,
-                                                 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s", 
-                                                 r->in.domain_name, 
-                                                 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
-                       talloc_free(tmp_ctx);
-                       return status;
-               } else {
-                       r->out.error_string
-                               = talloc_asprintf(r,
-                                                 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s", 
-                                                 r->in.domain_name, 
-                                                 nt_errstr(status));
-                       talloc_free(tmp_ctx);
-                       return status;
-               }
+               r->out.error_string
+                       = talloc_asprintf(r,
+                                         "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
+                                         r->in.domain_name,
+                                         nt_errstr(status));
+               talloc_free(tmp_ctx);
+               return status;
        } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
                r->out.error_string
                        = talloc_asprintf(r,
                                          "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
                talloc_free(tmp_ctx);
                return NT_STATUS_UNSUCCESSFUL;
-       } else if (r_crack_names.out.level != 1 
-                  || !r_crack_names.out.ctr.ctr1 
-                  || r_crack_names.out.ctr.ctr1->count != 1
-                  || !r_crack_names.out.ctr.ctr1->array[0].result_name           
-                  || r_crack_names.out.ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
+       } else if (*r_crack_names.out.level_out != 1
+                  || !r_crack_names.out.ctr->ctr1
+                  || r_crack_names.out.ctr->ctr1->count != 1
+                  || !r_crack_names.out.ctr->ctr1->array[0].result_name
+                  || r_crack_names.out.ctr->ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
                r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
                talloc_free(tmp_ctx);
                return NT_STATUS_UNSUCCESSFUL;
@@ -379,13 +395,18 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
        talloc_steal(r, account_dn_str);
 
        /* Store the domain DN. */
-       r->out.domain_dn_str = r_crack_names.out.ctr.ctr1->array[0].result_name;
-       talloc_steal(r, r_crack_names.out.ctr.ctr1->array[0].result_name);
+       r->out.domain_dn_str = r_crack_names.out.ctr->ctr1->array[0].result_name;
+       talloc_steal(r, r_crack_names.out.ctr->ctr1->array[0].result_name);
+
+       /* Store the KVNO of the account, critical for some kerberos
+        * operations */
+       r->out.kvno = ldb_msg_find_attr_as_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
 
-       r->out.kvno = kvno;
+       /* Store the account GUID. */
+       r->out.account_guid = samdb_result_guid(res->msgs[0], "objectGUID");
 
        if (r->in.acct_type == ACB_SVRTRUST) {
-               status = libnet_JoinSite(remote_ldb, r);
+               status = libnet_JoinSite(ctx, remote_ldb, r);
        }
        talloc_free(tmp_ctx);
 
@@ -407,9 +428,9 @@ static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_J
  * - potentially delete and recreate the user
  * - assert the account is of the right type with samrQueryUserInfo
  * 
- * - call libnet_SetPassword_samr_handle to set the password
+ * - call libnet_SetPassword_samr_handle to set the password,
+ *   and pass a samr_UserInfo21 struct to set full_name and the account flags
  *
- * - do a samrSetUserInfo to set the account flags
  * - do some ADS specific things when we join as Domain Controller,
  *    look at libnet_joinADSDomain() for the details
  */
@@ -427,14 +448,16 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
        struct samr_OpenDomain od;
        struct policy_handle d_handle;
        struct samr_LookupNames ln;
+       struct samr_Ids rids, types;
        struct samr_OpenUser ou;
        struct samr_CreateUser2 cu;
        struct policy_handle *u_handle = NULL;
        struct samr_QueryUserInfo qui;
-       struct samr_SetUserInfo sui;
-       union samr_UserInfo u_info;
+       union samr_UserInfo *uinfo;
+       struct samr_UserInfo21 u_info21;
        union libnet_SetPassword r2;
        struct samr_GetUserPwInfo pwp;
+       struct samr_PwInfo info;
        struct lsa_String samr_account_name;
        
        uint32_t acct_flags, old_acct_flags;
@@ -460,14 +483,14 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                return NT_STATUS_NO_MEMORY;
        }
        
-       connect_with_info = talloc(tmp_ctx, struct libnet_RpcConnect);
+       connect_with_info = talloc_zero(tmp_ctx, struct libnet_RpcConnect);
        if (!connect_with_info) {
                r->out.error_string = NULL;
                talloc_free(tmp_ctx);
                return NT_STATUS_NO_MEMORY;
        }
 
-       /* prepare connect to the LSA pipe of PDC */
+       /* prepare connect to the SAMR pipe of PDC */
        if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
                connect_with_info->in.binding = NULL;
                connect_with_info->in.name    = r->in.domain_name;
@@ -476,11 +499,13 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                connect_with_info->in.name    = NULL;
        }
 
+       /* This level makes a connection to the LSA pipe on the way,
+        * to get some useful bits of information about the domain */
        connect_with_info->level              = LIBNET_RPC_CONNECT_DC_INFO;
-       connect_with_info->in.dcerpc_iface    = &dcerpc_table_samr;
+       connect_with_info->in.dcerpc_iface    = &ndr_table_samr;
 
        /*
-         establish a SAMR connection, on the same CIFS transport
+         establish the SAMR connection
        */
        status = libnet_RpcConnect(ctx, tmp_ctx, connect_with_info);
        if (!NT_STATUS_IS_OK(status)) {
@@ -501,7 +526,7 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
 
        status = dcerpc_pipe_auth(tmp_ctx, &samr_pipe,
                                  connect_with_info->out.dcerpc_pipe->binding, 
-                                 &dcerpc_table_samr, ctx->cred);
+                                 &ndr_table_samr, ctx->cred, ctx->lp_ctx);
        if (!NT_STATUS_IS_OK(status)) {
                r->out.error_string = talloc_asprintf(mem_ctx,
                                                "SAMR bind failed: %s",
@@ -517,7 +542,10 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
        sc.out.connect_handle = &p_handle;
 
        /* 2. do a samr_Connect to get a policy handle */
-       status = dcerpc_samr_Connect(samr_pipe, tmp_ctx, &sc);
+       status = dcerpc_samr_Connect_r(samr_pipe->binding_handle, tmp_ctx, &sc);
+       if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(sc.out.result)) {
+               status = sc.out.result;
+       }
        if (!NT_STATUS_IS_OK(status)) {
                r->out.error_string = talloc_asprintf(mem_ctx,
                                                "samr_Connect failed: %s",
@@ -531,8 +559,9 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
                        connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
                } else {
-                       /* Bugger, we just lost our way to automaticly find the domain name */
-                       connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, lp_workgroup());
+                       /* Bugger, we just lost our way to automatically find the domain name */
+                       connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, lp_workgroup(ctx->lp_ctx));
+                       connect_with_info->out.realm = talloc_strdup(tmp_ctx, lp_realm(ctx->lp_ctx));
                }
        }
 
@@ -540,11 +569,16 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
        if (!connect_with_info->out.domain_sid) {
                struct lsa_String name;
                struct samr_LookupDomain l;
+               struct dom_sid2 *sid = NULL;
                name.string = connect_with_info->out.domain_name;
                l.in.connect_handle = &p_handle;
                l.in.domain_name = &name;
+               l.out.sid = &sid;
                
-               status = dcerpc_samr_LookupDomain(samr_pipe, tmp_ctx, &l);
+               status = dcerpc_samr_LookupDomain_r(samr_pipe->binding_handle, tmp_ctx, &l);
+               if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(l.out.result)) {
+                       status = l.out.result;
+               }
                if (!NT_STATUS_IS_OK(status)) {
                        r->out.error_string = talloc_asprintf(mem_ctx,
                                                              "SAMR LookupDomain failed: %s",
@@ -552,7 +586,7 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                        talloc_free(tmp_ctx);
                        return status;
                }
-               connect_with_info->out.domain_sid = l.out.sid;
+               connect_with_info->out.domain_sid = *l.out.sid;
        }
 
        /* prepare samr_OpenDomain */
@@ -563,7 +597,10 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
        od.out.domain_handle = &d_handle;
 
        /* do a samr_OpenDomain to get a domain handle */
-       status = dcerpc_samr_OpenDomain(samr_pipe, tmp_ctx, &od);                       
+       status = dcerpc_samr_OpenDomain_r(samr_pipe->binding_handle, tmp_ctx, &od);
+       if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(od.out.result)) {
+               status = od.out.result;
+       }
        if (!NT_STATUS_IS_OK(status)) {
                r->out.error_string = talloc_asprintf(mem_ctx,
                                                      "samr_OpenDomain for [%s] failed: %s",
@@ -585,13 +622,18 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
        cu.out.access_granted = &access_granted;
 
        /* do a samr_CreateUser2 to get an account handle, or an error */
-       cu_status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);                   
+       cu_status = dcerpc_samr_CreateUser2_r(samr_pipe->binding_handle, tmp_ctx, &cu);
+       if (NT_STATUS_IS_OK(cu_status) && !NT_STATUS_IS_OK(cu.out.result)) {
+               cu_status = cu.out.result;
+       }
        status = cu_status;
        if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
                /* prepare samr_LookupNames */
                ln.in.domain_handle = &d_handle;
                ln.in.num_names = 1;
                ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
+               ln.out.rids = &rids;
+               ln.out.types = &types;
                if (!ln.in.names) {
                        r->out.error_string = NULL;
                        talloc_free(tmp_ctx);
@@ -600,7 +642,10 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                ln.in.names[0].string = r->in.account_name;
                
                /* 5. do a samr_LookupNames to get the users rid */
-               status = dcerpc_samr_LookupNames(samr_pipe, tmp_ctx, &ln);
+               status = dcerpc_samr_LookupNames_r(samr_pipe->binding_handle, tmp_ctx, &ln);
+               if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ln.out.result)) {
+                       status = ln.out.result;
+               }
                if (!NT_STATUS_IS_OK(status)) {
                        r->out.error_string = talloc_asprintf(mem_ctx,
                                                "samr_LookupNames for [%s] failed: %s",
@@ -611,10 +656,10 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                }
                
                /* check if we got one RID for the user */
-               if (ln.out.rids.count != 1) {
+               if (ln.out.rids->count != 1) {
                        r->out.error_string = talloc_asprintf(mem_ctx,
                                                              "samr_LookupNames for [%s] returns %d RIDs",
-                                                             r->in.account_name, ln.out.rids.count);
+                                                             r->in.account_name, ln.out.rids->count);
                        talloc_free(tmp_ctx);
                        return NT_STATUS_INVALID_PARAMETER;
                }
@@ -623,12 +668,15 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                ZERO_STRUCTP(u_handle);
                ou.in.domain_handle = &d_handle;
                ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
-               ou.in.rid = ln.out.rids.ids[0];
+               ou.in.rid = ln.out.rids->ids[0];
                rid = ou.in.rid;
                ou.out.user_handle = u_handle;
                
                /* 6. do a samr_OpenUser to get a user handle */
-               status = dcerpc_samr_OpenUser(samr_pipe, tmp_ctx, &ou); 
+               status = dcerpc_samr_OpenUser_r(samr_pipe->binding_handle, tmp_ctx, &ou);
+               if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ou.out.result)) {
+                       status = ou.out.result;
+               }
                if (!NT_STATUS_IS_OK(status)) {
                        r->out.error_string = talloc_asprintf(mem_ctx,
                                                        "samr_OpenUser for [%s] failed: %s",
@@ -642,7 +690,10 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                        struct samr_DeleteUser d;
                        d.in.user_handle = u_handle;
                        d.out.user_handle = u_handle;
-                       status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
+                       status = dcerpc_samr_DeleteUser_r(samr_pipe->binding_handle, mem_ctx, &d);
+                       if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(d.out.result)) {
+                               status = d.out.result;
+                       }
                        if (!NT_STATUS_IS_OK(status)) {
                                r->out.error_string = talloc_asprintf(mem_ctx,
                                                                      "samr_DeleteUser (for recreate) of [%s] failed: %s",
@@ -655,7 +706,10 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                        /* We want to recreate, so delete and another samr_CreateUser2 */
                        
                        /* &cu filled in above */
-                       status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);                      
+                       status = dcerpc_samr_CreateUser2_r(samr_pipe->binding_handle, tmp_ctx, &cu);
+                       if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(cu.out.result)) {
+                               status = cu.out.result;
+                       }
                        if (!NT_STATUS_IS_OK(status)) {
                                r->out.error_string = talloc_asprintf(mem_ctx,
                                                                      "samr_CreateUser2 (recreate) for [%s] failed: %s",
@@ -675,8 +729,12 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
        /* prepare samr_QueryUserInfo (get flags) */
        qui.in.user_handle = u_handle;
        qui.in.level = 16;
+       qui.out.info = &uinfo;
        
-       status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
+       status = dcerpc_samr_QueryUserInfo_r(samr_pipe->binding_handle, tmp_ctx, &qui);
+       if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(qui.out.result)) {
+               status = qui.out.result;
+       }
        if (!NT_STATUS_IS_OK(status)) {
                r->out.error_string = talloc_asprintf(mem_ctx,
                                                "samr_QueryUserInfo for [%s] failed: %s",
@@ -686,7 +744,7 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                return status;
        }
        
-       if (!qui.out.info) {
+       if (!uinfo) {
                status = NT_STATUS_INVALID_PARAMETER;
                r->out.error_string
                        = talloc_asprintf(mem_ctx,
@@ -696,7 +754,7 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                return status;
        }
 
-       old_acct_flags = (qui.out.info->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
+       old_acct_flags = (uinfo->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
        /* Possibly bail if the account is of the wrong type */
        if (old_acct_flags
            != r->in.acct_type) {
@@ -752,28 +810,39 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                        return NT_STATUS_USER_EXISTS;
                }
        } else {
-               acct_flags = qui.out.info->info16.acct_flags;
+               acct_flags = uinfo->info16.acct_flags;
        }
        
-       acct_flags = (acct_flags & ~ACB_DISABLED);
+       acct_flags = (acct_flags & ~(ACB_DISABLED|ACB_PWNOTREQ));
 
        /* Find out what password policy this user has */
        pwp.in.user_handle = u_handle;
+       pwp.out.info = &info;
 
-       status = dcerpc_samr_GetUserPwInfo(samr_pipe, tmp_ctx, &pwp);                           
+       status = dcerpc_samr_GetUserPwInfo_r(samr_pipe->binding_handle, tmp_ctx, &pwp);
+       if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(pwp.out.result)) {
+               status = pwp.out.result;
+       }
        if (NT_STATUS_IS_OK(status)) {
-               policy_min_pw_len = pwp.out.info.min_password_length;
+               policy_min_pw_len = pwp.out.info->min_password_length;
        }
        
        /* Grab a password of that minimum length */
        
-       password_str = generate_random_str(tmp_ctx, MAX(8, policy_min_pw_len)); 
+       password_str = generate_random_password(tmp_ctx, MAX(8, policy_min_pw_len), 255);
+
+       /* set full_name and reset flags */
+       ZERO_STRUCT(u_info21);
+       u_info21.full_name.string       = r->in.account_name;
+       u_info21.acct_flags             = acct_flags;
+       u_info21.fields_present         = SAMR_FIELD_FULL_NAME | SAMR_FIELD_ACCT_FLAGS;
 
        r2.samr_handle.level            = LIBNET_SET_PASSWORD_SAMR_HANDLE;
        r2.samr_handle.in.account_name  = r->in.account_name;
        r2.samr_handle.in.newpassword   = password_str;
        r2.samr_handle.in.user_handle   = u_handle;
        r2.samr_handle.in.dcerpc_pipe   = samr_pipe;
+       r2.samr_handle.in.info21        = &u_info21;
 
        status = libnet_SetPassword(ctx, tmp_ctx, &r2); 
        if (!NT_STATUS_IS_OK(status)) {
@@ -782,26 +851,6 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
                return status;
        }
 
-       /* reset flags (if required) */
-       if (acct_flags != qui.out.info->info16.acct_flags) {
-               ZERO_STRUCT(u_info);
-               u_info.info16.acct_flags = acct_flags;
-
-               sui.in.user_handle = u_handle;
-               sui.in.info = &u_info;
-               sui.in.level = 16;
-               
-               dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
-               if (!NT_STATUS_IS_OK(status)) {
-                       r->out.error_string = talloc_asprintf(mem_ctx,
-                                                       "samr_SetUserInfo for [%s] failed to remove ACB_DISABLED flag: %s",
-                                                       r->in.account_name,
-                                                       nt_errstr(status));
-                       talloc_free(tmp_ctx);
-                       return status;
-               }
-       }
-
        account_sid = dom_sid_add_rid(mem_ctx, connect_with_info->out.domain_sid, rid);
        if (!account_sid) {
                r->out.error_string = NULL;
@@ -824,7 +873,7 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
        r->out.realm = connect_with_info->out.realm;
        talloc_steal(mem_ctx, r->out.realm);
        r->out.samr_pipe = samr_pipe;
-       talloc_steal(mem_ctx, samr_pipe);
+       talloc_reparent(tmp_ctx, mem_ctx, samr_pipe);
        r->out.samr_binding = samr_pipe->binding;
        talloc_steal(mem_ctx, r->out.samr_binding);
        r->out.user_handle = u_handle;
@@ -852,25 +901,12 @@ static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
        NTSTATUS status;
        TALLOC_CTX *tmp_mem;
        struct libnet_JoinDomain *r2;
-       int ret, rtn;
-       struct ldb_context *ldb;
-       const struct ldb_dn *base_dn;
-       struct ldb_message **msgs, *msg;
-       const char *sct;
-       const char * const attrs[] = {
-               "whenChanged",
-               "secret",
-               "priorSecret",
-               "priorChanged",
-               "krb5Keytab",
-               "privateKeytab",
-               NULL
-       };
+       struct provision_store_self_join_settings *set_secrets;
        uint32_t acct_type = 0;
        const char *account_name;
        const char *netbios_name;
-       char *filter;
-       
+       const char *error_string;
+
        r->out.error_string = NULL;
 
        tmp_mem = talloc_new(mem_ctx);
@@ -898,7 +934,7 @@ static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
        if (r->in.netbios_name != NULL) {
                netbios_name = r->in.netbios_name;
        } else {
-               netbios_name = talloc_reference(tmp_mem, lp_netbios_name());
+               netbios_name = talloc_strdup(tmp_mem, lp_netbios_name(ctx->lp_ctx));
                if (!netbios_name) {
                        r->out.error_string = NULL;
                        talloc_free(tmp_mem);
@@ -913,19 +949,6 @@ static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
                return NT_STATUS_NO_MEMORY;
        }
        
-       /*
-        * Local secrets are stored in secrets.ldb 
-        * open it to make sure we can write the info into it after the join
-        */
-       ldb = secrets_db_connect(tmp_mem);
-       if (!ldb) {
-               r->out.error_string
-                       = talloc_asprintf(mem_ctx, 
-                                         "Could not open secrets database");
-               talloc_free(tmp_mem);
-               return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
-       }
-
        /*
         * join the domain
         */
@@ -935,258 +958,45 @@ static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
        r2->in.netbios_name     = netbios_name;
        r2->in.level            = LIBNET_JOINDOMAIN_AUTOMATIC;
        r2->in.acct_type        = acct_type;
-       r2->in.recreate_account = False;
+       r2->in.recreate_account = false;
        status = libnet_JoinDomain(ctx, r2, r2);
        if (!NT_STATUS_IS_OK(status)) {
                r->out.error_string = talloc_steal(mem_ctx, r2->out.error_string);
                talloc_free(tmp_mem);
                return status;
        }
-       
-       /*
-        * now prepare the record for secrets.ldb
-        */
-       sct = talloc_asprintf(tmp_mem, "%d", r->in.join_type); 
-       if (!sct) {
-               r->out.error_string = NULL;
-               talloc_free(tmp_mem);
-               return NT_STATUS_NO_MEMORY;
-       }
-       
-       msg = ldb_msg_new(tmp_mem);
-       if (!msg) {
-               r->out.error_string = NULL;
-               talloc_free(tmp_mem);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       base_dn = ldb_dn_explode(tmp_mem, "cn=Primary Domains");
-       if (!base_dn) {
-               r->out.error_string = NULL;
-               talloc_free(tmp_mem);
-               return NT_STATUS_NO_MEMORY;
-       }
 
-       msg->dn = ldb_dn_build_child(tmp_mem, "flatname", r2->out.domain_name, base_dn);
-       if (!msg->dn) {
+       set_secrets = talloc(tmp_mem, struct provision_store_self_join_settings);
+       if (!set_secrets) {
                r->out.error_string = NULL;
                talloc_free(tmp_mem);
                return NT_STATUS_NO_MEMORY;
        }
        
-       rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "flatname", r2->out.domain_name);
-       if (rtn == -1) {
-               r->out.error_string = NULL;
-               talloc_free(tmp_mem);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       if (r2->out.realm) {
-               rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "realm", r2->out.realm);
-               if (rtn == -1) {
-                       r->out.error_string = NULL;
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_NO_MEMORY;
-               }
-
-               rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
-               if (rtn == -1) {
-                       r->out.error_string = NULL;
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_NO_MEMORY;
-               }
-       }
-
-       rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
-       if (rtn == -1) {
-               r->out.error_string = NULL;
-               talloc_free(tmp_mem);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
-       if (rtn == -1) {
-               r->out.error_string = NULL;
-               talloc_free(tmp_mem);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
-       if (rtn == -1) {
-               r->out.error_string = NULL;
-               talloc_free(tmp_mem);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secureChannelType", sct);
-       if (rtn == -1) {
-               r->out.error_string = NULL;
-               talloc_free(tmp_mem);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       if (r2->out.kvno) {
-               rtn = samdb_msg_add_uint(ldb, tmp_mem, msg, "msDS-KeyVersionNumber",
-                                        r2->out.kvno);
-               if (rtn == -1) {
-                       r->out.error_string = NULL;
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_NO_MEMORY;
-               }
-       }
-
-       if (r2->out.domain_sid) {
-               rtn = samdb_msg_add_dom_sid(ldb, tmp_mem, msg, "objectSid",
-                                           r2->out.domain_sid);
-               if (rtn == -1) {
-                       r->out.error_string = NULL;
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_NO_MEMORY;
-               }
-       }
-
-       /* 
-        * search for the secret record
-        * - remove the records we find
-        * - and fetch the old secret and store it under priorSecret
-        */
-       ret = gendb_search(ldb,
-                          tmp_mem, base_dn,
-                          &msgs, attrs,
-                          "(|" SECRETS_PRIMARY_DOMAIN_FILTER "(realm=%s))",
-                          r2->out.domain_name, r2->out.realm);
-       if (ret == 0) {
-               rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secretsKeytab", "secrets.keytab");
-               if (rtn == -1) {
-                       r->out.error_string = NULL;
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_NO_MEMORY;
-               }
-       } else if (ret == -1) {
-               r->out.error_string
-                       = talloc_asprintf(mem_ctx, 
-                                         "Search for domain: %s and realm: %s failed: %s", 
-                                         r2->out.domain_name, r2->out.realm, ldb_errstring(ldb));
-               talloc_free(tmp_mem);
-               return NT_STATUS_INTERNAL_DB_CORRUPTION;
-       } else {
-               const struct ldb_val *private_keytab;
-               const struct ldb_val *krb5_keytab;
-               const struct ldb_val *prior_secret;
-               const struct ldb_val *prior_modified_time;
-               int i;
-
-               for (i = 0; i < ret; i++) {
-                       ldb_delete(ldb, msgs[i]->dn);
-               }
-
-               prior_secret = ldb_msg_find_ldb_val(msgs[0], "secret");
-               if (prior_secret) {
-                       rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorSecret", prior_secret);
-                       if (rtn == -1) {
-                               r->out.error_string = NULL;
-                               talloc_free(tmp_mem);
-                               return NT_STATUS_NO_MEMORY;
-                       }
-               }
-               rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
-               if (rtn == -1) {
-                       r->out.error_string = NULL;
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_NO_MEMORY;
-               }
-
-               prior_modified_time = ldb_msg_find_ldb_val(msgs[0], 
-                                                          "whenChanged");
-               if (prior_modified_time) {
-                       rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorWhenChanged", 
-                                                 prior_modified_time);
-                       if (rtn == -1) {
-                               r->out.error_string = NULL;
-                               talloc_free(tmp_mem);
-                               return NT_STATUS_NO_MEMORY;
-                       }
-               }
-
-               rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
-               if (rtn == -1) {
-                       r->out.error_string = NULL;
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_NO_MEMORY;
-               }
-
-               rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secureChannelType", sct);
-               if (rtn == -1) {
-                       r->out.error_string = NULL;
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_NO_MEMORY;
-               }
-
-               /* We will want to keep the keytab names */
-               private_keytab = ldb_msg_find_ldb_val(msgs[0], "privateKeytab");
-               if (private_keytab) {
-                       rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "privateKeytab", private_keytab);
-                       if (rtn == -1) {
-                               r->out.error_string = NULL;
-                               talloc_free(tmp_mem);
-                               return NT_STATUS_NO_MEMORY;
-                       }
-               }
-               krb5_keytab = ldb_msg_find_ldb_val(msgs[0], "krb5Keytab");
-               if (krb5_keytab) {
-                       rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "krb5Keytab", krb5_keytab);
-                       if (rtn == -1) {
-                               r->out.error_string = NULL;
-                               talloc_free(tmp_mem);
-                               return NT_STATUS_NO_MEMORY;
-                       }
-               }
-       }
-
-       /* create the secret */
-       ret = samdb_add(ldb, tmp_mem, msg);
-       if (ret != 0) {
-               r->out.error_string = talloc_asprintf(mem_ctx, "Failed to create secret record %s", 
-                                                     ldb_dn_linearize(ldb, msg->dn));
+       ZERO_STRUCTP(set_secrets);
+       set_secrets->domain_name = r2->out.domain_name;
+       set_secrets->realm = r2->out.realm;
+       set_secrets->netbios_name = netbios_name;
+       set_secrets->secure_channel_type = r->in.join_type;
+       set_secrets->machine_password = r2->out.join_password;
+       set_secrets->key_version_number = r2->out.kvno;
+       set_secrets->domain_sid = r2->out.domain_sid;
+       
+       status = provision_store_self_join(ctx, ctx->lp_ctx, ctx->event_ctx, set_secrets, &error_string);
+       if (!NT_STATUS_IS_OK(status)) {
+               r->out.error_string = talloc_steal(mem_ctx, error_string);
                talloc_free(tmp_mem);
-               return NT_STATUS_INTERNAL_DB_CORRUPTION;
-       }
-
-       if (r2->out.realm) {
-               struct cli_credentials *creds;
-               /* Make a credentials structure from it */
-               creds = cli_credentials_init(mem_ctx);
-               if (!creds) {
-                       r->out.error_string = NULL;
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_NO_MEMORY;
-               }
-               cli_credentials_set_conf(creds);
-               filter = talloc_asprintf(mem_ctx, "dn=%s", ldb_dn_linearize(mem_ctx, msg->dn));
-               status = cli_credentials_set_secrets(creds, NULL, filter);
-               if (!NT_STATUS_IS_OK(status)) {
-                       r->out.error_string = talloc_asprintf(mem_ctx, "Failed to read secrets for keytab update for %s", 
-                                                             filter);
-                       talloc_free(tmp_mem);
-                       return status;
-               } 
-               ret = cli_credentials_update_keytab(creds);
-               if (ret != 0) {
-                       r->out.error_string = talloc_asprintf(mem_ctx, "Failed to update keytab for %s", 
-                                                             filter);
-                       talloc_free(tmp_mem);
-                       return NT_STATUS_UNSUCCESSFUL;
-               }
+               return status;
        }
 
        /* move all out parameter to the callers TALLOC_CTX */
        r->out.error_string     = NULL;
        r->out.join_password    = r2->out.join_password;
-       talloc_steal(mem_ctx, r2->out.join_password);
+       talloc_reparent(r2, mem_ctx, r2->out.join_password);
        r->out.domain_sid       = r2->out.domain_sid;
-       talloc_steal(mem_ctx, r2->out.domain_sid);
+       talloc_reparent(r2, mem_ctx, r2->out.domain_sid);
        r->out.domain_name      = r2->out.domain_name;
-       talloc_steal(mem_ctx, r2->out.domain_name);
+       talloc_reparent(r2, mem_ctx, r2->out.domain_name);
        talloc_free(tmp_mem);
        return NT_STATUS_OK;
 }
@@ -1199,6 +1009,8 @@ NTSTATUS libnet_Join(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct lib
                case SEC_CHAN_BDC:
                        return libnet_Join_primary_domain(ctx, mem_ctx, r);
                case SEC_CHAN_DOMAIN:
+               case SEC_CHAN_DNS_DOMAIN:
+               case SEC_CHAN_NULL:
                        break;
        }