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,
93 if (!NT_STATUS_IS_OK(status)) {
94 werr = ntstatus_to_werror(status);
98 old_timeout = cli_set_timeout(cli, 60000);
100 pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
103 werr = ntstatus_to_werror(status);
108 encode_wkssvc_join_password_buffer(mem_ctx,
110 &cli->user_session_key,
111 &encrypted_password);
114 old_timeout = cli_set_timeout(cli, 60000);
116 status = rpccli_wkssvc_NetrJoinDomain2(pipe_cli, mem_ctx,
117 server_name, domain_name,
121 if (!NT_STATUS_IS_OK(status)) {
122 werr = ntstatus_to_werror(status);
128 cli_set_timeout(cli, old_timeout);
135 WERROR NetJoinDomain(const char *server_name,
136 const char *domain_name,
137 const char *account_ou,
139 const char *password,
142 TALLOC_CTX *mem_ctx = NULL;
145 mem_ctx = talloc_init("NetJoinDomain");
152 werr = WERR_INVALID_PARAM;
156 if (!server_name || is_myname_or_ipaddr(server_name)) {
158 const char *dc = NULL;
160 /* FIXME: DsGetDcName */
161 if (server_name == NULL) {
167 werr = NetJoinDomainLocal(mem_ctx,
178 werr = NetJoinDomainRemote(mem_ctx,
186 TALLOC_FREE(mem_ctx);
191 WERROR NetUnjoinDomain(const char *server_name,
193 const char *password,
194 uint32_t unjoin_flags)
196 TALLOC_CTX *mem_ctx = NULL;
197 struct cli_state *cli = NULL;
198 struct rpc_pipe_client *pipe_cli = NULL;
199 struct wkssvc_PasswordBuffer encrypted_password;
202 unsigned int old_timeout = 0;
204 ZERO_STRUCT(encrypted_password);
206 mem_ctx = talloc_init("NetUnjoinDomain");
212 if (!server_name || is_myname_or_ipaddr(server_name)) {
213 werr = WERR_NOT_SUPPORTED;
217 status = cli_full_connection(&cli, NULL, server_name,
225 if (!NT_STATUS_IS_OK(status)) {
226 werr = ntstatus_to_werror(status);
230 old_timeout = cli_set_timeout(cli, 60000);
232 pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
235 werr = ntstatus_to_werror(status);
240 encode_wkssvc_join_password_buffer(mem_ctx,
242 &cli->user_session_key,
243 &encrypted_password);
246 old_timeout = cli_set_timeout(cli, 60000);
248 status = rpccli_wkssvc_NetrUnjoinDomain2(pipe_cli, mem_ctx,
254 if (!NT_STATUS_IS_OK(status)) {
255 werr = ntstatus_to_werror(status);
261 cli_set_timeout(cli, old_timeout);
264 TALLOC_FREE(mem_ctx);
269 WERROR NetGetJoinInformation(const char *server_name,
270 const char **name_buffer,
273 TALLOC_CTX *mem_ctx = NULL;
274 struct cli_state *cli = NULL;
275 struct rpc_pipe_client *pipe_cli = NULL;
279 mem_ctx = talloc_init("NetGetJoinInformation");
285 if (!server_name || is_myname_or_ipaddr(server_name)) {
286 if ((lp_security() == SEC_ADS) && lp_realm()) {
287 *name_buffer = SMB_STRDUP(lp_realm());
289 *name_buffer = SMB_STRDUP(lp_workgroup());
295 switch (lp_server_role()) {
296 case ROLE_DOMAIN_MEMBER:
297 case ROLE_DOMAIN_PDC:
298 case ROLE_DOMAIN_BDC:
299 *name_type = NetSetupDomainName;
301 case ROLE_STANDALONE:
303 *name_type = NetSetupWorkgroupName;
311 status = cli_full_connection(&cli, NULL, server_name,
319 if (!NT_STATUS_IS_OK(status)) {
320 werr = ntstatus_to_werror(status);
324 pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
327 werr = ntstatus_to_werror(status);
331 status = rpccli_wkssvc_NetrGetJoinInformation(pipe_cli, mem_ctx,
334 (enum wkssvc_NetJoinStatus *)name_type,
336 if (!NT_STATUS_IS_OK(status)) {
337 werr = ntstatus_to_werror(status);
345 TALLOC_FREE(mem_ctx);