Fix enumeration of forest trusts from our root domain.
[ira/wip.git] / source3 / libnet / libnet_join.c
index 1a8486f5b515fa7ddbd3e491229032302b61360a..90e1b5941e52c176397521663daa03ab95235196 100644 (file)
@@ -29,7 +29,7 @@
                char *str = NULL; \
                str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
                DEBUG(1,("libnet_Join:\n%s", str)); \
-               talloc_free(str); \
+               TALLOC_FREE(str); \
        } while (0)
 
 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
@@ -42,7 +42,7 @@
                char *str = NULL; \
                str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
                DEBUG(1,("libnet_Unjoin:\n%s", str)); \
-               talloc_free(str); \
+               TALLOC_FREE(str); \
        } while (0)
 
 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
@@ -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);
@@ -630,7 +632,7 @@ static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
 
        if (!secrets_store_machine_password(r->in.machine_password,
                                            r->out.netbios_domain_name,
-                                           SEC_CHAN_WKSTA))
+                                           r->in.secure_channel_type))
        {
                DEBUG(1,("Failed to save machine password\n"));
                return false;
@@ -928,9 +930,8 @@ 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 |
+       uint32_t neg_flags = NETLOGON_NEG_SELECT_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;
@@ -1200,41 +1201,41 @@ done:
 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
 {
        WERROR werr;
-       struct libnet_conf_ctx *ctx;
+       struct smbconf_ctx *ctx;
 
-       werr = libnet_conf_open(r, &ctx);
+       werr = smbconf_init_reg(r, &ctx, NULL);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
        }
 
        if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
 
-               werr = libnet_conf_set_global_parameter(ctx, "security", "user");
+               werr = smbconf_set_global_parameter(ctx, "security", "user");
                W_ERROR_NOT_OK_GOTO_DONE(werr);
 
-               werr = libnet_conf_set_global_parameter(ctx, "workgroup",
-                                                       r->in.domain_name);
+               werr = smbconf_set_global_parameter(ctx, "workgroup",
+                                                   r->in.domain_name);
                goto done;
        }
 
-       werr = libnet_conf_set_global_parameter(ctx, "security", "domain");
+       werr = smbconf_set_global_parameter(ctx, "security", "domain");
        W_ERROR_NOT_OK_GOTO_DONE(werr);
 
-       werr = libnet_conf_set_global_parameter(ctx, "workgroup",
-                                               r->out.netbios_domain_name);
+       werr = smbconf_set_global_parameter(ctx, "workgroup",
+                                           r->out.netbios_domain_name);
        W_ERROR_NOT_OK_GOTO_DONE(werr);
 
        if (r->out.domain_is_ad) {
-               werr = libnet_conf_set_global_parameter(ctx, "security", "ads");
+               werr = smbconf_set_global_parameter(ctx, "security", "ads");
                W_ERROR_NOT_OK_GOTO_DONE(werr);
 
-               werr = libnet_conf_set_global_parameter(ctx, "realm",
-                                                       r->out.dns_domain_name);
+               werr = smbconf_set_global_parameter(ctx, "realm",
+                                                   r->out.dns_domain_name);
                W_ERROR_NOT_OK_GOTO_DONE(werr);
        }
 
  done:
-       libnet_conf_close(ctx);
+       smbconf_shutdown(ctx);
        return werr;
 }
 
@@ -1244,22 +1245,22 @@ static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
 {
        WERROR werr = WERR_OK;
-       struct libnet_conf_ctx *ctx;
+       struct smbconf_ctx *ctx;
 
-       werr = libnet_conf_open(r, &ctx);
+       werr = smbconf_init_reg(r, &ctx, NULL);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
        }
 
        if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
 
-               werr = libnet_conf_set_global_parameter(ctx, "security", "user");
+               werr = smbconf_set_global_parameter(ctx, "security", "user");
                W_ERROR_NOT_OK_GOTO_DONE(werr);
-               libnet_conf_delete_global_parameter(ctx, "realm");
+               smbconf_delete_global_parameter(ctx, "realm");
        }
 
  done:
-       libnet_conf_close(ctx);
+       smbconf_shutdown(ctx);
        return werr;
 }
 
@@ -1318,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)
 {
@@ -1327,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 "
@@ -1413,6 +1464,8 @@ WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
        ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
        W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
 
+       ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
+
        *r = ctx;
 
        return WERR_OK;
@@ -1656,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 "