Fix enumeration of forest trusts from our root domain.
[ira/wip.git] / source3 / libnet / libnet_join.c
index 7e3e9cc93ac45b67a585140e6df736569b5b3d0d..90e1b5941e52c176397521663daa03ab95235196 100644 (file)
@@ -207,6 +207,11 @@ static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
        const char *attrs[] = { "dn", NULL };
        bool moved = false;
 
+       status = ads_check_ou_dn(mem_ctx, r->in.ads, r->in.account_ou);
+       if (!ADS_ERR_OK(status)) {
+               return status;
+       }
+
        status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
        if (!ADS_ERR_OK(status)) {
                return status;
@@ -264,10 +269,7 @@ static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
        ADS_STATUS status;
 
        if (!r->in.ads) {
-               status = libnet_unjoin_connect_ads(mem_ctx, r);
-               if (!ADS_ERR_OK(status)) {
-                       return status;
-               }
+               return libnet_unjoin_connect_ads(mem_ctx, r);
        }
 
        status = ads_leave_realm(r->in.ads, r->in.machine_name);
@@ -1201,7 +1203,7 @@ static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
        WERROR werr;
        struct smbconf_ctx *ctx;
 
-       werr = smbconf_init(r, &ctx);
+       werr = smbconf_init_reg(r, &ctx, NULL);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
        }
@@ -1233,7 +1235,7 @@ static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
        }
 
  done:
-       smbconf_close(ctx);
+       smbconf_shutdown(ctx);
        return werr;
 }
 
@@ -1245,7 +1247,7 @@ static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
        WERROR werr = WERR_OK;
        struct smbconf_ctx *ctx;
 
-       werr = smbconf_init(r, &ctx);
+       werr = smbconf_init_reg(r, &ctx, NULL);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
        }
@@ -1258,7 +1260,7 @@ static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
        }
 
  done:
-       smbconf_close(ctx);
+       smbconf_shutdown(ctx);
        return werr;
 }
 
@@ -1317,6 +1319,48 @@ static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
 /****************************************************************
 ****************************************************************/
 
+static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
+                                  const char *domain_str,
+                                  const char **domain_p,
+                                  const char **dc_p)
+{
+       char *domain = NULL;
+       char *dc = NULL;
+       const char *p = NULL;
+
+       if (!domain_str || !domain_p || !dc_p) {
+               return false;
+       }
+
+       p = strchr_m(domain_str, '\\');
+
+       if (p != NULL) {
+               domain = talloc_strndup(mem_ctx, domain_str,
+                                        PTR_DIFF(p, domain_str));
+               dc = talloc_strdup(mem_ctx, p+1);
+               if (!dc) {
+                       return false;
+               }
+       } else {
+               domain = talloc_strdup(mem_ctx, domain_str);
+               dc = NULL;
+       }
+       if (!domain) {
+               return false;
+       }
+
+       *domain_p = domain;
+
+       if (!*dc_p && dc) {
+               *dc_p = dc;
+       }
+
+       return true;
+}
+
+/****************************************************************
+****************************************************************/
+
 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
                                         struct libnet_JoinCtx *r)
 {
@@ -1326,6 +1370,14 @@ static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
                return WERR_INVALID_PARAM;
        }
 
+       if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
+                                   &r->in.domain_name,
+                                   &r->in.dc_name)) {
+               libnet_join_set_error_string(mem_ctx, r,
+                       "Failed to parse domain name");
+               return WERR_INVALID_PARAM;
+       }
+
        if (r->in.modify_config && !lp_config_backend_is_registry()) {
                libnet_join_set_error_string(mem_ctx, r,
                        "Configuration manipulation requested but not "
@@ -1657,6 +1709,14 @@ static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
                return WERR_INVALID_PARAM;
        }
 
+       if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
+                                   &r->in.domain_name,
+                                   &r->in.dc_name)) {
+               libnet_unjoin_set_error_string(mem_ctx, r,
+                       "Failed to parse domain name");
+               return WERR_INVALID_PARAM;
+       }
+
        if (r->in.modify_config && !lp_config_backend_is_registry()) {
                libnet_unjoin_set_error_string(mem_ctx, r,
                        "Configuration manipulation requested but not "