Let dsgetdcname() return a struct netr_DsRGetDCNameInfo.
[gd/samba/.git] / source / libnet / libnet_join.c
index 2d00fb094f7cb0cc4a54f8a52d588ad1842e9af1..623ca39f715b1a470123176f3047ad1b1f783d66 100644 (file)
@@ -152,9 +152,24 @@ static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
                libnet_join_set_error_string(mem_ctx, r,
                        "failed to connect to AD: %s",
                        ads_errstr(status));
+               return status;
        }
 
-       return status;
+       if (!r->out.netbios_domain_name) {
+               r->out.netbios_domain_name = talloc_strdup(mem_ctx,
+                                                          r->in.ads->server.workgroup);
+               ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
+       }
+
+       if (!r->out.dns_domain_name) {
+               r->out.dns_domain_name = talloc_strdup(mem_ctx,
+                                                      r->in.ads->config.realm);
+               ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
+       }
+
+       r->out.domain_is_ad = true;
+
+       return ADS_SUCCESS;
 }
 
 /****************************************************************
@@ -788,6 +803,132 @@ 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)
+{
+       uint32_t neg_flags = NETLOGON_NEG_AUTH2_FLAGS |
+                            NETLOGON_NEG_SCHANNEL;
+       /* FIXME: NETLOGON_NEG_SELECT_AUTH2_FLAGS */
+       struct cli_state *cli = NULL;
+       struct rpc_pipe_client *pipe_hnd = NULL;
+       struct rpc_pipe_client *netlogon_pipe = NULL;
+       NTSTATUS status;
+       char *machine_password = NULL;
+       char *machine_account = NULL;
+
+       if (!dc_name) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (!secrets_init()) {
+               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;
+       }
+
+       asprintf(&machine_account, "%s$", machine_name);
+       if (!machine_account) {
+               SAFE_FREE(machine_password);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = cli_full_connection(&cli, NULL,
+                                    dc_name,
+                                    NULL, 0,
+                                    "IPC$", "IPC",
+                                    machine_account,
+                                    NULL,
+                                    machine_password,
+                                    0,
+                                    Undefined, NULL);
+       free(machine_account);
+       free(machine_password);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               status = cli_full_connection(&cli, NULL,
+                                            dc_name,
+                                            NULL, 0,
+                                            "IPC$", "IPC",
+                                            "",
+                                            NULL,
+                                            "",
+                                            0,
+                                            Undefined, NULL);
+       }
+
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       netlogon_pipe = get_schannel_session_key(cli,
+                                                netbios_domain_name,
+                                                &neg_flags, &status);
+       if (!netlogon_pipe) {
+               if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
+                       cli_shutdown(cli);
+                       return NT_STATUS_OK;
+               }
+
+               DEBUG(0,("libnet_join_ok: failed to get schannel session "
+                       "key from server %s for domain %s. Error was %s\n",
+               cli->desthost, netbios_domain_name, nt_errstr(status)));
+               cli_shutdown(cli);
+               return status;
+       }
+
+       if (!lp_client_schannel()) {
+               cli_shutdown(cli);
+               return NT_STATUS_OK;
+       }
+
+       pipe_hnd = cli_rpc_pipe_open_schannel_with_key(cli, PI_NETLOGON,
+                                                      PIPE_AUTH_LEVEL_PRIVACY,
+                                                      netbios_domain_name,
+                                                      netlogon_pipe->dc,
+                                                      &status);
+
+       cli_shutdown(cli);
+
+       if (!pipe_hnd) {
+               DEBUG(0,("libnet_join_ok: failed to open schannel session "
+                       "on netlogon pipe to server %s for domain %s. "
+                       "Error was %s\n",
+                       cli->desthost, netbios_domain_name, nt_errstr(status)));
+               return status;
+       }
+
+       return NT_STATUS_OK;
+}
+
+/****************************************************************
+****************************************************************/
+
+static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
+                                     struct libnet_JoinCtx *r)
+{
+       NTSTATUS status;
+
+       status = libnet_join_ok(r->out.netbios_domain_name,
+                               r->in.machine_name,
+                               r->in.dc_name);
+       if (!NT_STATUS_IS_OK(status)) {
+               libnet_join_set_error_string(mem_ctx, r,
+                       "failed to verify domain membership after joining: %s",
+                       get_friendly_nt_error_msg(status));
+               return WERR_SETUP_NOT_JOINED;
+       }
+
+       return WERR_OK;
+}
+
+/****************************************************************
+****************************************************************/
+
 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
                                                    struct libnet_UnjoinCtx *r)
 {
@@ -1016,7 +1157,7 @@ static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
 /****************************************************************
 ****************************************************************/
 
-static WERROR do_UnjoinConfig(struct libnet_UnjoinCtx *r)
+static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
 {
        WERROR werr;
 
@@ -1045,8 +1186,9 @@ static WERROR do_UnjoinConfig(struct libnet_UnjoinCtx *r)
 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
                                         struct libnet_JoinCtx *r)
 {
-
        if (!r->in.domain_name) {
+               libnet_join_set_error_string(mem_ctx, r,
+                       "No domain name defined");
                return WERR_INVALID_PARAM;
        }
 
@@ -1173,7 +1315,7 @@ static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
 #endif /* WITH_ADS */
 
        if (!r->in.dc_name) {
-               struct DS_DOMAIN_CONTROLLER_INFO *info;
+               struct netr_DsRGetDCNameInfo *info;
                status = dsgetdcname(mem_ctx,
                                     r->in.domain_name,
                                     NULL,
@@ -1191,7 +1333,7 @@ static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
                }
 
                r->in.dc_name = talloc_strdup(mem_ctx,
-                                             info->domain_controller_name);
+                                             info->dc_unc);
                W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
        }
 
@@ -1265,6 +1407,11 @@ WERROR libnet_Join(TALLOC_CTX *mem_ctx,
                if (!W_ERROR_IS_OK(werr)) {
                        goto done;
                }
+
+               werr = libnet_join_post_verify(mem_ctx, r);
+               if (!W_ERROR_IS_OK(werr)) {
+                       goto done;
+               }
        }
 
        werr = libnet_join_post_processing(mem_ctx, r);
@@ -1300,7 +1447,7 @@ static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
        }
 
        if (!r->in.dc_name) {
-               struct DS_DOMAIN_CONTROLLER_INFO *info;
+               struct netr_DsRGetDCNameInfo *info;
                status = dsgetdcname(mem_ctx,
                                     r->in.domain_name,
                                     NULL,
@@ -1318,7 +1465,7 @@ static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
                }
 
                r->in.dc_name = talloc_strdup(mem_ctx,
-                                             info->domain_controller_name);
+                                             info->dc_unc);
                W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
        }
 
@@ -1333,6 +1480,8 @@ static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
                return ntstatus_to_werror(status);
        }
 
+       r->out.disabled_machine_account = true;
+
 #ifdef WITH_ADS
        if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
                ADS_STATUS ads_status;
@@ -1342,6 +1491,12 @@ static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
                        libnet_unjoin_set_error_string(mem_ctx, r,
                                "failed to remove machine account from AD: %s",
                                ads_errstr(ads_status));
+               } else {
+                       r->out.deleted_machine_account = true;
+                       /* dirty hack */
+                       r->out.dns_domain_name = talloc_strdup(mem_ctx,
+                                                              r->in.ads->server.realm);
+                       W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
                }
        }
 #endif /* WITH_ADS */
@@ -1357,6 +1512,12 @@ static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
                                           struct libnet_UnjoinCtx *r)
 {
+       if (!r->in.domain_name) {
+               libnet_unjoin_set_error_string(mem_ctx, r,
+                       "No domain name defined");
+               return WERR_INVALID_PARAM;
+       }
+
        if (r->in.modify_config && !lp_config_backend_is_registry()) {
                return WERR_NOT_SUPPORTED;
        }
@@ -1370,6 +1531,17 @@ static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
        return WERR_OK;
 }
 
+/****************************************************************
+****************************************************************/
+
+static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
+                                           struct libnet_UnjoinCtx *r)
+{
+       saf_delete(r->out.netbios_domain_name);
+       saf_delete(r->out.dns_domain_name);
+
+       return libnet_unjoin_config(r);
+}
 
 /****************************************************************
 ****************************************************************/
@@ -1391,11 +1563,12 @@ WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
        if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
                werr = libnet_DomainUnjoin(mem_ctx, r);
                if (!W_ERROR_IS_OK(werr)) {
+                       libnet_unjoin_config(r);
                        goto done;
                }
        }
 
-       werr = do_UnjoinConfig(r);
+       werr = libnet_unjoin_post_processing(mem_ctx, r);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
        }