s3-kerberos: remove unused kdc_name from create_local_private_krb5_conf_for_domain().
[samba.git] / source3 / libnet / libnet_join.c
index 5a36d3422d536d7338041c0f13aa3e61de4ea465..68884cd8a86d71bd8a1bd9c6416e831b8c96f317 100644 (file)
 #include "../libcli/security/security.h"
 #include "passdb.h"
 #include "libsmb/libsmb.h"
+#include "../libcli/smb/smbXcli_base.h"
+#include "lib/param/loadparm.h"
+#include "libcli/auth/netlogon_creds_cli.h"
+#include "auth/credentials/credentials.h"
 
 /****************************************************************
 ****************************************************************/
@@ -134,7 +138,10 @@ static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
                        *cp++ = '\0';
                        SAFE_FREE(my_ads->auth.realm);
                        my_ads->auth.realm = smb_xstrdup(cp);
-                       strupper_m(my_ads->auth.realm);
+                       if (!strupper_m(my_ads->auth.realm)) {
+                               ads_destroy(&my_ads);
+                               return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
+                       }
                }
        }
 
@@ -379,7 +386,9 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
        if (!spn) {
                return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
        }
-       strupper_m(spn);
+       if (!strupper_m(spn)) {
+               return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
+       }
        spn_array[0] = spn;
 
        if (!name_to_fqdn(my_fqdn, r->in.machine_name)
@@ -388,7 +397,9 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
                             r->out.dns_domain_name);
        }
 
-       strlower_m(my_fqdn);
+       if (!strlower_m(my_fqdn)) {
+               return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
+       }
 
        if (!strequal(my_fqdn, r->in.machine_name)) {
                spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
@@ -440,10 +451,25 @@ static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
        }
 
        if (!r->in.upn) {
+               const char *realm = r->out.dns_domain_name;
+
+               /* in case we are about to generate a keytab during the join
+                * make sure the default upn we create is usable with kinit -k.
+                * gd */
+
+               if (USE_KERBEROS_KEYTAB) {
+                       realm = talloc_strdup_upper(mem_ctx,
+                                                   r->out.dns_domain_name);
+               }
+
+               if (!realm) {
+                       return ADS_ERROR(LDAP_NO_MEMORY);
+               }
+
                r->in.upn = talloc_asprintf(mem_ctx,
                                            "host/%s@%s",
                                            r->in.machine_name,
-                                           r->out.dns_domain_name);
+                                           realm);
                if (!r->in.upn) {
                        return ADS_ERROR(LDAP_NO_MEMORY);
                }
@@ -677,6 +703,7 @@ static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
                                           const char *user,
+                                          const char *domain,
                                           const char *pass,
                                           bool use_kerberos,
                                           struct cli_state **cli)
@@ -696,7 +723,7 @@ static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
                                   NULL, 0,
                                   "IPC$", "IPC",
                                   user,
-                                  NULL,
+                                  domain,
                                   pass,
                                   flags,
                                   SMB_SIGNING_DEFAULT);
@@ -718,6 +745,7 @@ static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
 
        status = libnet_join_connect_dc_ipc(r->in.dc_name,
                                            r->in.admin_account,
+                                           r->in.admin_domain,
                                            r->in.admin_password,
                                            r->in.use_kerberos,
                                            cli);
@@ -725,7 +753,7 @@ static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
                goto done;
        }
 
-       status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
+       status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
                                          &pipe_hnd);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
@@ -789,46 +817,73 @@ static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
                                                    struct libnet_JoinCtx *r,
                                                    struct cli_state *cli)
 {
-       struct rpc_pipe_client *pipe_hnd = NULL;
-       unsigned char orig_trust_passwd_hash[16];
-       unsigned char new_trust_passwd_hash[16];
-       fstring trust_passwd;
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct rpc_pipe_client *netlogon_pipe = NULL;
+       struct netlogon_creds_cli_context *netlogon_creds = NULL;
+       struct samr_Password current_nt_hash;
+       const char *account_name = NULL;
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
-                                         &pipe_hnd);
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
+                                         &netlogon_pipe);
        if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
                return status;
        }
 
        if (!r->in.machine_password) {
-               r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
-               NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
+               r->in.machine_password = generate_random_password(mem_ctx,
+                               DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
+                               DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
+               if (r->in.machine_password == NULL) {
+                       TALLOC_FREE(frame);
+                       return NT_STATUS_NO_MEMORY;
+               }
        }
 
-       E_md4hash(r->in.machine_password, new_trust_passwd_hash);
-
        /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
-       fstrcpy(trust_passwd, r->in.admin_password);
-       strlower_m(trust_passwd);
+       E_md4hash(r->in.admin_password, current_nt_hash.hash);
 
-       /*
-        * Machine names can be 15 characters, but the max length on
-        * a password is 14.  --jerry
-        */
+       account_name = talloc_asprintf(frame, "%s$",
+                                      r->in.machine_name);
+       if (account_name == NULL) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       trust_passwd[14] = '\0';
+       status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
+                                             r->in.domain_name,
+                                             account_name,
+                                             r->in.secure_channel_type,
+                                             r->in.msg_ctx,
+                                             frame,
+                                             &netlogon_creds);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
+               return status;
+       }
 
-       E_md4hash(trust_passwd, orig_trust_passwd_hash);
+       status = rpccli_setup_netlogon_creds(cli,
+                                            netlogon_creds,
+                                            true, /* force_reauth */
+                                            current_nt_hash,
+                                            NULL); /* previous_nt_hash */
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
+               return status;
+       }
 
-       status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
-                                                   r->in.machine_name,
-                                                   orig_trust_passwd_hash,
-                                                   r->in.machine_password,
-                                                   new_trust_passwd_hash,
-                                                   r->in.secure_channel_type);
+       status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
+                                                     netlogon_pipe->binding_handle,
+                                                     r->in.machine_password,
+                                                     NULL); /* new_version */
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
+               return status;
+       }
 
-       return status;
+       TALLOC_FREE(frame);
+       return NT_STATUS_OK;
 }
 
 /****************************************************************
@@ -850,7 +905,9 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
        struct samr_Ids name_types;
        union samr_UserInfo user_info;
        struct dcerpc_binding_handle *b = NULL;
+       unsigned int old_timeout = 0;
 
+       DATA_BLOB session_key = data_blob_null;
        struct samr_CryptPassword crypt_pwd;
        struct samr_CryptPasswordEx crypt_pwd_ex;
 
@@ -870,13 +927,15 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
        }
 
        if (!r->in.machine_password) {
-               r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
+               r->in.machine_password = generate_random_password(mem_ctx,
+                               DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
+                               DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
                NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
        }
 
        /* Open the domain */
 
-       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
                                          &pipe_hnd);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
@@ -886,6 +945,13 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
 
        b = pipe_hnd->binding_handle;
 
+       status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
+                       nt_errstr(status)));
+               goto done;
+       }
+
        status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      SAMR_ACCESS_ENUM_DOMAINS
@@ -919,7 +985,10 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
        /* Create domain user */
 
        acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
-       strlower_m(acct_name);
+       if (!strlower_m(acct_name)) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto done;
+       }
 
        init_lsa_String(&lsa_acct_name, acct_name);
 
@@ -996,6 +1065,14 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
                status = result;
                goto done;
        }
+       if (user_rids.count != 1) {
+               status = NT_STATUS_INVALID_NETWORK_RESPONSE;
+               goto done;
+       }
+       if (name_types.count != 1) {
+               status = NT_STATUS_INVALID_NETWORK_RESPONSE;
+               goto done;
+       }
 
        if (name_types.ids[0] != SID_NAME_USER) {
                DEBUG(0,("%s is not a user account (type=%d)\n",
@@ -1061,8 +1138,14 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
 
        /* Set password on machine account - first try level 26 */
 
+       /*
+        * increase the timeout as password filter modules on the DC
+        * might delay the operation for a significant amount of time
+        */
+       old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
+
        init_samr_CryptPasswordEx(r->in.machine_password,
-                                 &cli->user_session_key,
+                                 &session_key,
                                  &crypt_pwd_ex);
 
        user_info.info26.password = crypt_pwd_ex;
@@ -1079,7 +1162,7 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
                /* retry with level 24 */
 
                init_samr_CryptPassword(r->in.machine_password,
-                                       &cli->user_session_key,
+                                       &session_key,
                                        &crypt_pwd);
 
                user_info.info24.password = crypt_pwd;
@@ -1092,6 +1175,8 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
                                                  &result);
        }
 
+       old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
+
        if (!NT_STATUS_IS_OK(status)) {
 
                dcerpc_samr_DeleteUser(b, mem_ctx,
@@ -1123,6 +1208,8 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
                return status;
        }
 
+       data_blob_clear_free(&session_key);
+
        if (is_valid_policy_hnd(&sam_pol)) {
                dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
        }
@@ -1140,35 +1227,56 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS libnet_join_ok(const char *netbios_domain_name,
-                       const char *machine_name,
-                       const char *dc_name)
+NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
+                       const char *netbios_domain_name,
+                       const char *dc_name,
+                       const bool use_kerberos)
 {
-       uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
+       TALLOC_CTX *frame = talloc_stackframe();
        struct cli_state *cli = NULL;
-       struct rpc_pipe_client *pipe_hnd = NULL;
        struct rpc_pipe_client *netlogon_pipe = NULL;
+       struct netlogon_creds_cli_context *netlogon_creds = NULL;
+       struct netlogon_creds_CredentialState *creds = NULL;
+       uint32_t netlogon_flags = 0;
+       enum netr_SchannelType sec_chan_type = 0;
        NTSTATUS status;
        char *machine_password = NULL;
-       char *machine_account = NULL;
+       const char *machine_name = NULL;
+       const char *machine_account = NULL;
+       int flags = 0;
+       struct samr_Password current_nt_hash;
+       struct samr_Password *previous_nt_hash = NULL;
+       bool ok;
 
        if (!dc_name) {
+               TALLOC_FREE(frame);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
        if (!secrets_init()) {
+               TALLOC_FREE(frame);
                return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
        }
 
-       machine_password = secrets_fetch_machine_password(netbios_domain_name,
-                                                         NULL, NULL);
-       if (!machine_password) {
-               return NT_STATUS_NO_TRUST_LSA_SECRET;
+       ok = get_trust_pw_clear(netbios_domain_name,
+                               &machine_password,
+                               &machine_name,
+                               &sec_chan_type);
+       if (!ok) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
        }
 
-       if (asprintf(&machine_account, "%s$", machine_name) == -1) {
+       machine_account = talloc_asprintf(frame, "%s$", machine_name);
+       if (machine_account == NULL) {
                SAFE_FREE(machine_password);
-               return NT_STATUS_NO_MEMORY;
+               SAFE_FREE(previous_nt_hash);
+               TALLOC_FREE(frame);
+               return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
+       }
+
+       if (use_kerberos) {
+               flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
        }
 
        status = cli_full_connection(&cli, NULL,
@@ -1176,12 +1284,13 @@ NTSTATUS libnet_join_ok(const char *netbios_domain_name,
                                     NULL, 0,
                                     "IPC$", "IPC",
                                     machine_account,
-                                    NULL,
+                                    netbios_domain_name,
                                     machine_password,
-                                    0,
+                                    flags,
                                     SMB_SIGNING_DEFAULT);
-       free(machine_account);
-       free(machine_password);
+
+       E_md4hash(machine_password, current_nt_hash.hash);
+       SAFE_FREE(machine_password);
 
        if (!NT_STATUS_IS_OK(status)) {
                status = cli_full_connection(&cli, NULL,
@@ -1196,46 +1305,79 @@ NTSTATUS libnet_join_ok(const char *netbios_domain_name,
        }
 
        if (!NT_STATUS_IS_OK(status)) {
+               SAFE_FREE(previous_nt_hash);
+               TALLOC_FREE(frame);
                return status;
        }
 
-       status = get_schannel_session_key(cli, netbios_domain_name,
-                                         &neg_flags, &netlogon_pipe);
+       status = rpccli_create_netlogon_creds(dc_name,
+                                             netbios_domain_name,
+                                             machine_account,
+                                             sec_chan_type,
+                                             msg_ctx,
+                                             frame,
+                                             &netlogon_creds);
        if (!NT_STATUS_IS_OK(status)) {
-               if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
-                       cli_shutdown(cli);
-                       return NT_STATUS_OK;
-               }
+               SAFE_FREE(previous_nt_hash);
+               cli_shutdown(cli);
+               TALLOC_FREE(frame);
+               return status;
+       }
 
-               DEBUG(0,("libnet_join_ok: failed to get schannel session "
-                       "key from server %s for domain %s. Error was %s\n",
-                       cli_state_remote_name(cli),
-                       netbios_domain_name, nt_errstr(status)));
+       status = rpccli_setup_netlogon_creds(cli,
+                                            netlogon_creds,
+                                            true, /* force_reauth */
+                                            current_nt_hash,
+                                            previous_nt_hash);
+       SAFE_FREE(previous_nt_hash);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("connect_to_domain_password_server: "
+                        "unable to open the domain client session to "
+                        "machine %s. Flags[0x%08X] Error was : %s.\n",
+                        dc_name, (unsigned)netlogon_flags,
+                        nt_errstr(status)));
+               cli_shutdown(cli);
+               TALLOC_FREE(frame);
+               return status;
+       }
+
+       status = netlogon_creds_cli_get(netlogon_creds,
+                                       talloc_tos(),
+                                       &creds);
+       if (!NT_STATUS_IS_OK(status)) {
                cli_shutdown(cli);
+               TALLOC_FREE(frame);
                return status;
        }
+       netlogon_flags = creds->negotiate_flags;
+       TALLOC_FREE(creds);
 
-       if (!lp_client_schannel()) {
+       if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
                cli_shutdown(cli);
+               TALLOC_FREE(frame);
                return NT_STATUS_OK;
        }
 
        status = cli_rpc_pipe_open_schannel_with_key(
-               cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
-               DCERPC_AUTH_LEVEL_PRIVACY,
-               netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
+               cli, &ndr_table_netlogon, NCACN_NP,
+               netbios_domain_name,
+               netlogon_creds, &netlogon_pipe);
 
-       cli_shutdown(cli);
+       TALLOC_FREE(netlogon_pipe);
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("libnet_join_ok: failed to open schannel session "
                        "on netlogon pipe to server %s for domain %s. "
                        "Error was %s\n",
-                       cli_state_remote_name(cli),
+                       smbXcli_conn_remote_name(cli->conn),
                        netbios_domain_name, nt_errstr(status)));
+               cli_shutdown(cli);
+               TALLOC_FREE(frame);
                return status;
        }
 
+       cli_shutdown(cli);
+       TALLOC_FREE(frame);
        return NT_STATUS_OK;
 }
 
@@ -1247,9 +1389,10 @@ static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
 {
        NTSTATUS status;
 
-       status = libnet_join_ok(r->out.netbios_domain_name,
-                               r->in.machine_name,
-                               r->in.dc_name);
+       status = libnet_join_ok(r->in.msg_ctx,
+                               r->out.netbios_domain_name,
+                               r->in.dc_name,
+                               r->in.use_kerberos);
        if (!NT_STATUS_IS_OK(status)) {
                libnet_join_set_error_string(mem_ctx, r,
                        "failed to verify domain membership after joining: %s",
@@ -1301,6 +1444,7 @@ static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
 
        status = libnet_join_connect_dc_ipc(r->in.dc_name,
                                            r->in.admin_account,
+                                           r->in.admin_domain,
                                            r->in.admin_password,
                                            r->in.use_kerberos,
                                            &cli);
@@ -1310,7 +1454,7 @@ static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
 
        /* Open the domain */
 
-       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
                                          &pipe_hnd);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
@@ -1350,7 +1494,10 @@ static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
        /* Create domain user */
 
        acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
-       strlower_m(acct_name);
+       if (!strlower_m(acct_name)) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto done;
+       }
 
        init_lsa_String(&lsa_acct_name, acct_name);
 
@@ -1369,6 +1516,14 @@ static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
                status = result;
                goto done;
        }
+       if (user_rids.count != 1) {
+               status = NT_STATUS_INVALID_NETWORK_RESPONSE;
+               goto done;
+       }
+       if (name_types.count != 1) {
+               status = NT_STATUS_INVALID_NETWORK_RESPONSE;
+               goto done;
+       }
 
        if (name_types.ids[0] != SID_NAME_USER) {
                DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
@@ -1665,6 +1820,15 @@ static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
                return WERR_INVALID_PARAM;
        }
 
+       if (strlen(r->in.machine_name) > 15) {
+               libnet_join_set_error_string(mem_ctx, r,
+                       "Our netbios name can be at most 15 chars long, "
+                         "\"%s\" is %u chars long\n",
+                         r->in.machine_name,
+                        (unsigned int)strlen(r->in.machine_name));
+               return WERR_INVALID_PARAM;
+        }
+
        if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
                                    &r->in.domain_name,
                                    &r->in.dc_name)) {
@@ -1677,6 +1841,17 @@ static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
                return WERR_SETUP_DOMAIN_CONTROLLER;
        }
 
+       if (!r->in.admin_domain) {
+               char *admin_domain = NULL;
+               char *admin_account = NULL;
+               split_domain_user(mem_ctx,
+                                 r->in.admin_account,
+                                 &admin_domain,
+                                 &admin_account);
+               r->in.admin_domain = admin_domain;
+               r->in.admin_account = admin_account;
+       }
+
        if (!secrets_init()) {
                libnet_join_set_error_string(mem_ctx, r,
                        "Unable to open secrets database");
@@ -1892,7 +2067,6 @@ static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
                if (!valid_security) {
                        const char *sec = NULL;
                        switch (lp_security()) {
-                       case SEC_SHARE: sec = "share"; break;
                        case SEC_USER:  sec = "user"; break;
                        case SEC_DOMAIN: sec = "domain"; break;
                        case SEC_ADS: sec = "ads"; break;
@@ -1978,8 +2152,7 @@ static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
 
        create_local_private_krb5_conf_for_domain(
                r->out.dns_domain_name, r->out.netbios_domain_name,
-               NULL, cli_state_remote_sockaddr(cli),
-               cli_state_remote_name(cli));
+               NULL, smbXcli_conn_remote_sockaddr(cli->conn));
 
        if (r->out.domain_is_ad && r->in.account_ou &&
            !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
@@ -2054,6 +2227,7 @@ static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
        u->in.admin_account     = r->in.admin_account;
        u->in.admin_password    = r->in.admin_password;
        u->in.modify_config     = r->in.modify_config;
+       u->in.use_kerberos      = r->in.use_kerberos;
        u->in.unjoin_flags      = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
                                  WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
 
@@ -2238,6 +2412,17 @@ static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
                return WERR_SETUP_DOMAIN_CONTROLLER;
        }
 
+       if (!r->in.admin_domain) {
+               char *admin_domain = NULL;
+               char *admin_account = NULL;
+               split_domain_user(mem_ctx,
+                                 r->in.admin_account,
+                                 &admin_domain,
+                                 &admin_account);
+               r->in.admin_domain = admin_domain;
+               r->in.admin_account = admin_account;
+       }
+
        if (!secrets_init()) {
                libnet_unjoin_set_error_string(mem_ctx, r,
                        "Unable to open secrets database");