Merge commit 'samba/v3-2-test' into v3-2-stable
[ira/wip.git] / source3 / lib / netapi / joindomain.c
index 0c8d645db975acd1e4434502a2615e8aaa3afd3e..b268e41a2a4e4a9548397327d789ec4b36460eeb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  Unix SMB/CIFS implementation.
  *  NetApi Join Support
- *  Copyright (C) Guenther Deschner 2007
+ *  Copyright (C) Guenther Deschner 2007-2008
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -33,13 +33,13 @@ static WERROR NetJoinDomainLocal(struct libnetapi_ctx *mem_ctx,
        struct libnet_JoinCtx *r = NULL;
        WERROR werr;
 
-       werr = libnet_init_JoinCtx(mem_ctx, &r);
-       W_ERROR_NOT_OK_RETURN(werr);
-
        if (!domain_name) {
                return WERR_INVALID_PARAM;
        }
 
+       werr = libnet_init_JoinCtx(mem_ctx, &r);
+       W_ERROR_NOT_OK_RETURN(werr);
+
        r->in.domain_name = talloc_strdup(mem_ctx, domain_name);
        W_ERROR_HAVE_NO_MEMORY(r->in.domain_name);
 
@@ -49,13 +49,14 @@ static WERROR NetJoinDomainLocal(struct libnetapi_ctx *mem_ctx,
                uint32_t flags = DS_DIRECTORY_SERVICE_REQUIRED |
                                 DS_WRITABLE_REQUIRED |
                                 DS_RETURN_DNS_NAME;
-               status = DsGetDcName(mem_ctx, NULL, domain_name,
+               status = dsgetdcname(mem_ctx, NULL, domain_name,
                                     NULL, NULL, flags, &info);
                if (!NT_STATUS_IS_OK(status)) {
                        return ntstatus_to_werror(status);
                }
-               r->in.server_name = talloc_strdup(mem_ctx, info->domain_controller_name);
-               W_ERROR_HAVE_NO_MEMORY(r->in.server_name);
+               r->in.dc_name = talloc_strdup(mem_ctx,
+                                             info->domain_controller_name);
+               W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
        }
 
        if (account_ou) {
@@ -69,14 +70,20 @@ static WERROR NetJoinDomainLocal(struct libnetapi_ctx *mem_ctx,
        }
 
        if (password) {
-               r->in.password = talloc_strdup(mem_ctx, password);
-               W_ERROR_HAVE_NO_MEMORY(r->in.password);
+               r->in.admin_password = talloc_strdup(mem_ctx, password);
+               W_ERROR_HAVE_NO_MEMORY(r->in.admin_password);
        }
 
        r->in.join_flags = join_flags;
        r->in.modify_config = true;
 
-       return libnet_Join(mem_ctx, r);
+       werr = libnet_Join(mem_ctx, r);
+       if (!W_ERROR_IS_OK(werr) && r->out.error_string) {
+               libnetapi_set_error_string(mem_ctx, r->out.error_string);
+       }
+       TALLOC_FREE(r);
+
+       return werr;
 }
 
 static WERROR NetJoinDomainRemote(struct libnetapi_ctx *ctx,
@@ -89,13 +96,11 @@ static WERROR NetJoinDomainRemote(struct libnetapi_ctx *ctx,
 {
        struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
-       struct wkssvc_PasswordBuffer encrypted_password;
+       struct wkssvc_PasswordBuffer *encrypted_password = NULL;
        NTSTATUS status;
        WERROR werr;
        unsigned int old_timeout = 0;
 
-       ZERO_STRUCT(encrypted_password);
-
        status = cli_full_connection(&cli, NULL, server_name,
                                     NULL, 0,
                                     "IPC$", "IPC",
@@ -109,14 +114,12 @@ static WERROR NetJoinDomainRemote(struct libnetapi_ctx *ctx,
                goto done;
        }
 
-       old_timeout = cli_set_timeout(cli, 60000);
-
        pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
                                            &status);
        if (!pipe_cli) {
                werr = ntstatus_to_werror(status);
                goto done;
-       };
+       }
 
        if (password) {
                encode_wkssvc_join_password_buffer(ctx,
@@ -130,7 +133,7 @@ static WERROR NetJoinDomainRemote(struct libnetapi_ctx *ctx,
        status = rpccli_wkssvc_NetrJoinDomain2(pipe_cli, ctx,
                                               server_name, domain_name,
                                               account_ou, Account,
-                                              &encrypted_password,
+                                              encrypted_password,
                                               join_flags, &werr);
        if (!NT_STATUS_IS_OK(status)) {
                werr = ntstatus_to_werror(status);
@@ -226,8 +229,8 @@ static WERROR NetUnjoinDomainLocal(struct libnetapi_ctx *mem_ctx,
        W_ERROR_NOT_OK_RETURN(werr);
 
        if (server_name) {
-               r->in.server_name = talloc_strdup(mem_ctx, server_name);
-               W_ERROR_HAVE_NO_MEMORY(r->in.server_name);
+               r->in.dc_name = talloc_strdup(mem_ctx, server_name);
+               W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
        } else {
 
                NTSTATUS status;
@@ -235,20 +238,20 @@ static WERROR NetUnjoinDomainLocal(struct libnetapi_ctx *mem_ctx,
                struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
                uint32_t flags = DS_DIRECTORY_SERVICE_REQUIRED |
                                 DS_WRITABLE_REQUIRED |
-                                DS_IS_FLAT_NAME |
                                 DS_RETURN_DNS_NAME;
                if (lp_realm()) {
                        domain = lp_realm();
                } else {
                        domain = lp_workgroup();
                }
-               status = DsGetDcName(mem_ctx, NULL, domain,
+               status = dsgetdcname(mem_ctx, NULL, domain,
                                     NULL, NULL, flags, &info);
                if (!NT_STATUS_IS_OK(status)) {
                        return ntstatus_to_werror(status);
                }
-               r->in.server_name = talloc_strdup(mem_ctx, info->domain_controller_name);
-               W_ERROR_HAVE_NO_MEMORY(r->in.server_name);
+               r->in.dc_name = talloc_strdup(mem_ctx,
+                                             info->domain_controller_name);
+               W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
        }
 
        if (account) {
@@ -257,8 +260,8 @@ static WERROR NetUnjoinDomainLocal(struct libnetapi_ctx *mem_ctx,
        }
 
        if (password) {
-               r->in.password = talloc_strdup(mem_ctx, password);
-               W_ERROR_HAVE_NO_MEMORY(r->in.password);
+               r->in.admin_password = talloc_strdup(mem_ctx, password);
+               W_ERROR_HAVE_NO_MEMORY(r->in.admin_password);
        }
 
        r->in.unjoin_flags = unjoin_flags;
@@ -278,13 +281,11 @@ static WERROR NetUnjoinDomainRemote(struct libnetapi_ctx *ctx,
 {
        struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
-       struct wkssvc_PasswordBuffer encrypted_password;
+       struct wkssvc_PasswordBuffer *encrypted_password = NULL;
        NTSTATUS status;
        WERROR werr;
        unsigned int old_timeout = 0;
 
-       ZERO_STRUCT(encrypted_password);
-
        status = cli_full_connection(&cli, NULL, server_name,
                                     NULL, 0,
                                     "IPC$", "IPC",
@@ -298,14 +299,12 @@ static WERROR NetUnjoinDomainRemote(struct libnetapi_ctx *ctx,
                goto done;
        }
 
-       old_timeout = cli_set_timeout(cli, 60000);
-
        pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
                                            &status);
        if (!pipe_cli) {
                werr = ntstatus_to_werror(status);
                goto done;
-       };
+       }
 
        if (password) {
                encode_wkssvc_join_password_buffer(ctx,
@@ -319,7 +318,7 @@ static WERROR NetUnjoinDomainRemote(struct libnetapi_ctx *ctx,
        status = rpccli_wkssvc_NetrUnjoinDomain2(pipe_cli, ctx,
                                                 server_name,
                                                 account,
-                                                &encrypted_password,
+                                                encrypted_password,
                                                 unjoin_flags,
                                                 &werr);
        if (!NT_STATUS_IS_OK(status)) {
@@ -412,7 +411,7 @@ static WERROR NetGetJoinInformationRemote(struct libnetapi_ctx *ctx,
        if (!pipe_cli) {
                werr = ntstatus_to_werror(status);
                goto done;
-       };
+       }
 
        status = rpccli_wkssvc_NetrGetJoinInformation(pipe_cli, ctx,
                                                      server_name,
@@ -438,9 +437,9 @@ static WERROR NetGetJoinInformationLocal(struct libnetapi_ctx *ctx,
                                         uint16_t *name_type)
 {
        if ((lp_security() == SEC_ADS) && lp_realm()) {
-               *name_buffer = SMB_STRDUP(lp_realm());
+               *name_buffer = talloc_strdup(ctx, lp_realm());
        } else {
-               *name_buffer = SMB_STRDUP(lp_workgroup());
+               *name_buffer = talloc_strdup(ctx, lp_workgroup());
        }
        if (!*name_buffer) {
                return WERR_NOMEM;
@@ -461,10 +460,10 @@ static WERROR NetGetJoinInformationLocal(struct libnetapi_ctx *ctx,
        return WERR_OK;
 }
 
-WERROR libnetapi_NetGetJoinInformation(struct libnetapi_ctx *ctx,
-                                      const char *server_name,
-                                      const char **name_buffer,
-                                      uint16_t *name_type)
+static WERROR libnetapi_NetGetJoinInformation(struct libnetapi_ctx *ctx,
+                                             const char *server_name,
+                                             const char **name_buffer,
+                                             uint16_t *name_type)
 {
        if (!server_name || is_myname_or_ipaddr(server_name)) {
                return NetGetJoinInformationLocal(ctx,