Add NetUnjoinDomain().
[samba.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         unsigned int old_timeout;
37
38         ZERO_STRUCT(encrypted_password);
39
40         mem_ctx = talloc_init("NetJoinDomain");
41         if (!mem_ctx) {
42                 werr = WERR_NOMEM;
43                 goto done;
44         }
45
46         if (!server_name || is_myname_or_ipaddr(server_name)) {
47                 werr = WERR_NOT_SUPPORTED;
48                 goto done;
49         }
50
51         if (!domain_name) {
52                 werr = WERR_INVALID_PARAM;
53                 goto done;
54         }
55
56         status = net_make_ipc_connection_ex(domain_name,
57                                             server_name,
58                                             NULL, 0, &cli);
59         if (!NT_STATUS_IS_OK(status)) {
60                 werr = ntstatus_to_werror(status);
61                 goto done;
62         }
63
64         old_timeout = cli_set_timeout(cli, 60000);
65
66         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
67                                             &status);
68         if (!pipe_cli) {
69                 werr = ntstatus_to_werror(status);
70                 goto done;
71         };
72
73         if (password) {
74                 encode_wkssvc_join_password_buffer(mem_ctx,
75                                                    password,
76                                                    &cli->user_session_key,
77                                                    &encrypted_password);
78         }
79
80         old_timeout = cli_set_timeout(cli, 60000);
81
82         status = rpccli_wkssvc_NetrJoinDomain2(pipe_cli, mem_ctx,
83                                                server_name, domain_name,
84                                                account_ou, Account,
85                                                &encrypted_password,
86                                                join_flags);
87         if (!NT_STATUS_IS_OK(status)) {
88                 werr = ntstatus_to_werror(status);
89                 goto done;
90         }
91
92         werr = WERR_OK;
93
94  done:
95         if (cli) {
96                 cli_set_timeout(cli, old_timeout);
97                 cli_shutdown(cli);
98         }
99         TALLOC_FREE(mem_ctx);
100
101         return werr;
102 }
103
104 WERROR NetUnjoinDomain(const char *server_name,
105                        const char *account,
106                        const char *password,
107                        uint32_t unjoin_flags)
108 {
109         TALLOC_CTX *mem_ctx = NULL;
110         struct cli_state *cli = NULL;
111         struct rpc_pipe_client *pipe_cli = NULL;
112         struct wkssvc_PasswordBuffer encrypted_password;
113         NTSTATUS status;
114         WERROR werr;
115         unsigned int old_timeout;
116
117         ZERO_STRUCT(encrypted_password);
118
119         mem_ctx = talloc_init("NetUnjoinDomain");
120         if (!mem_ctx) {
121                 werr = WERR_NOMEM;
122                 goto done;
123         }
124
125         if (!server_name || is_myname_or_ipaddr(server_name)) {
126                 werr = WERR_NOT_SUPPORTED;
127                 goto done;
128         }
129
130         status = net_make_ipc_connection_ex(NULL,
131                                             server_name,
132                                             NULL, 0, &cli);
133         if (!NT_STATUS_IS_OK(status)) {
134                 werr = ntstatus_to_werror(status);
135                 goto done;
136         }
137
138         old_timeout = cli_set_timeout(cli, 60000);
139
140         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
141                                             &status);
142         if (!pipe_cli) {
143                 werr = ntstatus_to_werror(status);
144                 goto done;
145         };
146
147         if (password) {
148                 encode_wkssvc_join_password_buffer(mem_ctx,
149                                                    password,
150                                                    &cli->user_session_key,
151                                                    &encrypted_password);
152         }
153
154         old_timeout = cli_set_timeout(cli, 60000);
155
156         status = rpccli_wkssvc_NetrUnjoinDomain2(pipe_cli, mem_ctx,
157                                                  server_name,
158                                                  account,
159                                                  &encrypted_password,
160                                                  unjoin_flags);
161         if (!NT_STATUS_IS_OK(status)) {
162                 werr = ntstatus_to_werror(status);
163                 goto done;
164         }
165
166         werr = WERR_OK;
167
168  done:
169         if (cli) {
170                 cli_set_timeout(cli, old_timeout);
171                 cli_shutdown(cli);
172         }
173         TALLOC_FREE(mem_ctx);
174
175         return werr;
176 }