2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2007
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.
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.
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/>.
21 #include "lib/netapi/joindomain.h"
23 static WERROR NetJoinDomainLocal(TALLOC_CTX *mem_ctx,
24 const char *server_name,
25 const char *domain_name,
26 const char *account_ou,
31 struct libnet_JoinCtx *r = NULL;
34 werr = libnet_init_JoinCtx(mem_ctx, &r);
35 W_ERROR_NOT_OK_RETURN(werr);
37 if (!server_name || !domain_name) {
38 return WERR_INVALID_PARAM;
41 r->in.server_name = talloc_strdup(mem_ctx, server_name);
42 W_ERROR_HAVE_NO_MEMORY(r->in.server_name);
44 r->in.domain_name = talloc_strdup(mem_ctx, domain_name);
45 W_ERROR_HAVE_NO_MEMORY(r->in.domain_name);
48 r->in.account_ou = talloc_strdup(mem_ctx, account_ou);
49 W_ERROR_HAVE_NO_MEMORY(r->in.account_ou);
53 r->in.admin_account = talloc_strdup(mem_ctx, Account);
54 W_ERROR_HAVE_NO_MEMORY(r->in.admin_account);
58 r->in.password = talloc_strdup(mem_ctx, password);
59 W_ERROR_HAVE_NO_MEMORY(r->in.password);
62 r->in.join_flags = join_flags;
63 r->in.modify_config = true;
65 return libnet_Join(mem_ctx, r);
68 static WERROR NetJoinDomainRemote(TALLOC_CTX *mem_ctx,
69 const char *server_name,
70 const char *domain_name,
71 const char *account_ou,
76 struct cli_state *cli = NULL;
77 struct rpc_pipe_client *pipe_cli = NULL;
78 struct wkssvc_PasswordBuffer encrypted_password;
81 unsigned int old_timeout = 0;
83 ZERO_STRUCT(encrypted_password);
85 status = cli_full_connection(&cli, NULL, server_name,
88 opt_user_name, opt_workgroup,
89 opt_password, 0, Undefined, NULL);
91 if (!NT_STATUS_IS_OK(status)) {
92 werr = ntstatus_to_werror(status);
96 old_timeout = cli_set_timeout(cli, 60000);
98 pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
101 werr = ntstatus_to_werror(status);
106 encode_wkssvc_join_password_buffer(mem_ctx,
108 &cli->user_session_key,
109 &encrypted_password);
112 old_timeout = cli_set_timeout(cli, 60000);
114 status = rpccli_wkssvc_NetrJoinDomain2(pipe_cli, mem_ctx,
115 server_name, domain_name,
119 if (!NT_STATUS_IS_OK(status)) {
120 werr = ntstatus_to_werror(status);
126 cli_set_timeout(cli, old_timeout);
133 WERROR NetJoinDomain(const char *server_name,
134 const char *domain_name,
135 const char *account_ou,
137 const char *password,
140 TALLOC_CTX *mem_ctx = NULL;
143 mem_ctx = talloc_init("NetJoinDomain");
150 werr = WERR_INVALID_PARAM;
154 if (!server_name || is_myname_or_ipaddr(server_name)) {
156 const char *dc = NULL;
158 /* FIXME: DsGetDcName */
159 if (server_name == NULL) {
165 werr = NetJoinDomainLocal(mem_ctx,
176 werr = NetJoinDomainRemote(mem_ctx,
184 TALLOC_FREE(mem_ctx);
189 WERROR NetUnjoinDomain(const char *server_name,
191 const char *password,
192 uint32_t unjoin_flags)
194 TALLOC_CTX *mem_ctx = NULL;
195 struct cli_state *cli = NULL;
196 struct rpc_pipe_client *pipe_cli = NULL;
197 struct wkssvc_PasswordBuffer encrypted_password;
200 unsigned int old_timeout = 0;
202 ZERO_STRUCT(encrypted_password);
204 mem_ctx = talloc_init("NetUnjoinDomain");
210 if (!server_name || is_myname_or_ipaddr(server_name)) {
211 werr = WERR_NOT_SUPPORTED;
215 status = cli_full_connection(&cli, NULL, server_name,
223 if (!NT_STATUS_IS_OK(status)) {
224 werr = ntstatus_to_werror(status);
228 old_timeout = cli_set_timeout(cli, 60000);
230 pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
233 werr = ntstatus_to_werror(status);
238 encode_wkssvc_join_password_buffer(mem_ctx,
240 &cli->user_session_key,
241 &encrypted_password);
244 old_timeout = cli_set_timeout(cli, 60000);
246 status = rpccli_wkssvc_NetrUnjoinDomain2(pipe_cli, mem_ctx,
252 if (!NT_STATUS_IS_OK(status)) {
253 werr = ntstatus_to_werror(status);
259 cli_set_timeout(cli, old_timeout);
262 TALLOC_FREE(mem_ctx);
267 WERROR NetGetJoinInformation(const char *server_name,
268 const char **name_buffer,
271 TALLOC_CTX *mem_ctx = NULL;
272 struct cli_state *cli = NULL;
273 struct rpc_pipe_client *pipe_cli = NULL;
277 mem_ctx = talloc_init("NetGetJoinInformation");
283 if (!server_name || is_myname_or_ipaddr(server_name)) {
284 if ((lp_security() == SEC_ADS) && lp_realm()) {
285 *name_buffer = SMB_STRDUP(lp_realm());
287 *name_buffer = SMB_STRDUP(lp_workgroup());
293 switch (lp_server_role()) {
294 case ROLE_DOMAIN_MEMBER:
295 case ROLE_DOMAIN_PDC:
296 case ROLE_DOMAIN_BDC:
297 *name_type = NetSetupDomainName;
299 case ROLE_STANDALONE:
301 *name_type = NetSetupWorkgroupName;
309 status = cli_full_connection(&cli, NULL, server_name,
317 if (!NT_STATUS_IS_OK(status)) {
318 werr = ntstatus_to_werror(status);
322 pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
325 werr = ntstatus_to_werror(status);
329 status = rpccli_wkssvc_NetrGetJoinInformation(pipe_cli, mem_ctx,
332 (enum wkssvc_NetJoinStatus *)name_type,
334 if (!NT_STATUS_IS_OK(status)) {
335 werr = ntstatus_to_werror(status);
343 TALLOC_FREE(mem_ctx);