domain_name is a ref pointer.
[ira/wip.git] / source3 / lib / netapi / joindomain.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi Join Support
4  *  Copyright (C) Guenther Deschner 2007
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "utils/net.h"
22
23 WERROR NetJoinDomain(const char *server_name,
24                      const char *domain_name,
25                      const char *account_ou,
26                      const char *Account,
27                      const char *password,
28                      uint32_t join_flags)
29 {
30         TALLOC_CTX *mem_ctx = NULL;
31         struct cli_state *cli = NULL;
32         struct rpc_pipe_client *pipe_cli = NULL;
33         struct wkssvc_PasswordBuffer encrypted_password;
34         NTSTATUS status;
35         WERROR werr;
36
37         mem_ctx = talloc_init("NetJoinDomain");
38         if (!mem_ctx) {
39                 werr = WERR_NOMEM;
40                 goto done;
41         }
42
43         if (!server_name || is_myname_or_ipaddr(server_name)) {
44                 werr = WERR_NOT_SUPPORTED;
45                 goto done;
46         }
47
48         if (!domain_name) {
49                 werr = WERR_INVALID_PARAM;
50                 goto done;
51         }
52
53         status = net_make_ipc_connection_ex(domain_name,
54                                             server_name,
55                                             NULL, 0, &cli);
56         if (!NT_STATUS_IS_OK(status)) {
57                 werr = ntstatus_to_werror(status);
58                 goto done;
59         }
60
61         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
62                                             &status);
63         if (!pipe_cli) {
64                 werr = ntstatus_to_werror(status);
65                 goto done;
66         };
67
68         encode_wkssvc_join_password_buffer(mem_ctx,
69                                            password,
70                                            &cli->user_session_key,
71                                            &encrypted_password);
72
73         status = rpccli_wkssvc_NetrJoinDomain2(pipe_cli, mem_ctx,
74                                                server_name, domain_name,
75                                                account_ou, Account,
76                                                &encrypted_password,
77                                                join_flags);
78         if (!NT_STATUS_IS_OK(status)) {
79                 werr = ntstatus_to_werror(status);
80         }
81
82         werr = WERR_OK;
83
84  done:
85         cli_shutdown(cli);
86         TALLOC_FREE(mem_ctx);
87
88         return werr;
89 }