libnet_join: fix "net rpc oldjoin"
[nivanova/samba-autobuild/.git] / source3 / libnet / libnet_join.c
index 36b9addcf4f263a592b02322652d4d28878e10c2..0595cfe64abca60167e2f30c590ec681c271ca28 100644 (file)
@@ -1044,12 +1044,23 @@ static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
        NTSTATUS status, result;
        union lsa_PolicyInformation *info = NULL;
        struct dcerpc_binding_handle *b;
+       const char *account = r->in.admin_account;
+       const char *domain = r->in.admin_domain;
+       const char *password = r->in.admin_password;
+       bool use_kerberos = r->in.use_kerberos;
+
+       if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) {
+               account = "";
+               domain = "";
+               password = NULL;
+               use_kerberos = false;
+       }
 
        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,
+                                           account,
+                                           domain,
+                                           password,
+                                           use_kerberos,
                                            cli);
        if (!NT_STATUS_IS_OK(status)) {
                goto done;
@@ -1121,16 +1132,19 @@ static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
                                                    struct cli_state *cli)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct rpc_pipe_client *netlogon_pipe = NULL;
+       struct rpc_pipe_client *authenticate_pipe = NULL;
+       struct rpc_pipe_client *passwordset_pipe = NULL;
+       struct cli_credentials *cli_creds;
        struct netlogon_creds_cli_context *netlogon_creds = NULL;
-       struct samr_Password current_nt_hash;
+       struct netlogon_creds_CredentialState *creds = NULL;
+       uint32_t netlogon_flags = 0;
        size_t len = 0;
        bool ok;
        DATA_BLOB new_trust_blob = data_blob_null;
        NTSTATUS status;
 
        status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
-                                         &netlogon_pipe);
+                                         &authenticate_pipe);
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(frame);
                return status;
@@ -1148,32 +1162,63 @@ static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
                }
        }
 
+       cli_creds = cli_credentials_init(talloc_tos());
+       if (cli_creds == NULL) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       cli_credentials_set_username(cli_creds, r->out.account_name,
+                                    CRED_SPECIFIED);
+       cli_credentials_set_domain(cli_creds, r->in.domain_name,
+                                  CRED_SPECIFIED);
+       cli_credentials_set_realm(cli_creds, "", CRED_SPECIFIED);
+       cli_credentials_set_secure_channel_type(cli_creds,
+                                               r->in.secure_channel_type);
+
        /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
-       E_md4hash(r->in.admin_password, current_nt_hash.hash);
-
-       status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
-                                             r->in.domain_name,
-                                             "", /* Never unsecure in AD */
-                                             r->out.account_name,
-                                             r->in.secure_channel_type,
-                                             r->in.msg_ctx,
-                                             frame,
-                                             &netlogon_creds);
+       cli_credentials_set_password(cli_creds, r->in.admin_password,
+                                    CRED_SPECIFIED);
+
+       status = rpccli_create_netlogon_creds_ctx(
+               cli_creds, authenticate_pipe->desthost, r->in.msg_ctx,
+               frame, &netlogon_creds);
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(frame);
                return status;
        }
 
-       status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
-                                            netlogon_creds,
-                                            true, /* force_reauth */
-                                            current_nt_hash,
-                                            NULL); /* previous_nt_hash */
+       status = rpccli_setup_netlogon_creds(
+               cli, NCACN_NP, netlogon_creds, true /* force_reauth */,
+               cli_creds);
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(frame);
                return status;
        }
 
+       status = netlogon_creds_cli_get(netlogon_creds, frame, &creds);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
+               return status;
+       }
+
+       netlogon_flags = creds->negotiate_flags;
+       TALLOC_FREE(creds);
+
+       if (netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC) {
+               status = cli_rpc_pipe_open_schannel_with_creds(cli,
+                                                              &ndr_table_netlogon,
+                                                              NCACN_NP,
+                                                              netlogon_creds,
+                                                              &passwordset_pipe);
+               if (!NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(frame);
+                       return status;
+               }
+       } else {
+               passwordset_pipe = authenticate_pipe;
+       }
+
        len = strlen(r->in.machine_password);
        ok = convert_string_talloc(frame, CH_UNIX, CH_UTF16,
                                   r->in.machine_password, len,
@@ -1189,7 +1234,7 @@ static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
        }
 
        status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
-                                                     netlogon_pipe->binding_handle,
+                                                     passwordset_pipe->binding_handle,
                                                      &new_trust_blob,
                                                      NULL); /* new_version */
        if (!NT_STATUS_IS_OK(status)) {
@@ -1609,21 +1654,21 @@ NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
                return status;
        }
 
-       status = rpccli_create_netlogon_creds_with_creds(cli_creds,
-                                                        dc_name,
-                                                        msg_ctx,
-                                                        frame,
-                                                        &netlogon_creds);
+       status = rpccli_create_netlogon_creds_ctx(cli_creds,
+                                                 dc_name,
+                                                 msg_ctx,
+                                                 frame,
+                                                 &netlogon_creds);
        if (!NT_STATUS_IS_OK(status)) {
                cli_shutdown(cli);
                TALLOC_FREE(frame);
                return status;
        }
 
-       status = rpccli_setup_netlogon_creds_with_creds(cli, NCACN_NP,
-                                                       netlogon_creds,
-                                                       true, /* force_reauth */
-                                                       cli_creds);
+       status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
+                                            netlogon_creds,
+                                            true, /* force_reauth */
+                                            cli_creds);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("connect_to_domain_password_server: "
                         "unable to open the domain client session to "
@@ -1654,7 +1699,6 @@ NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
 
        status = cli_rpc_pipe_open_schannel_with_creds(
                cli, &ndr_table_netlogon, NCACN_NP,
-               cli_creds,
                netlogon_creds, &netlogon_pipe);
 
        TALLOC_FREE(netlogon_pipe);