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/>.
22 #include "lib/netapi/netapi.h"
23 #include "libnet/libnet.h"
25 static WERROR NetJoinDomainLocal(struct libnetapi_ctx *mem_ctx,
26 const char *server_name,
27 const char *domain_name,
28 const char *account_ou,
33 struct libnet_JoinCtx *r = NULL;
36 werr = libnet_init_JoinCtx(mem_ctx, &r);
37 W_ERROR_NOT_OK_RETURN(werr);
40 return WERR_INVALID_PARAM;
43 r->in.domain_name = talloc_strdup(mem_ctx, domain_name);
44 W_ERROR_HAVE_NO_MEMORY(r->in.domain_name);
46 if (join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
48 struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
49 uint32_t flags = DS_DIRECTORY_SERVICE_REQUIRED |
50 DS_WRITABLE_REQUIRED |
52 status = DsGetDcName(mem_ctx, NULL, domain_name,
53 NULL, NULL, flags, &info);
54 if (!NT_STATUS_IS_OK(status)) {
55 return ntstatus_to_werror(status);
57 r->in.server_name = talloc_strdup(mem_ctx, info->domain_controller_name);
58 W_ERROR_HAVE_NO_MEMORY(r->in.server_name);
62 r->in.account_ou = talloc_strdup(mem_ctx, account_ou);
63 W_ERROR_HAVE_NO_MEMORY(r->in.account_ou);
67 r->in.admin_account = talloc_strdup(mem_ctx, Account);
68 W_ERROR_HAVE_NO_MEMORY(r->in.admin_account);
72 r->in.password = talloc_strdup(mem_ctx, password);
73 W_ERROR_HAVE_NO_MEMORY(r->in.password);
76 r->in.join_flags = join_flags;
77 r->in.modify_config = true;
79 return libnet_Join(mem_ctx, r);
82 static WERROR NetJoinDomainRemote(struct libnetapi_ctx *ctx,
83 const char *server_name,
84 const char *domain_name,
85 const char *account_ou,
90 struct cli_state *cli = NULL;
91 struct rpc_pipe_client *pipe_cli = NULL;
92 struct wkssvc_PasswordBuffer encrypted_password;
95 unsigned int old_timeout = 0;
97 ZERO_STRUCT(encrypted_password);
99 status = cli_full_connection(&cli, NULL, server_name,
107 if (!NT_STATUS_IS_OK(status)) {
108 werr = ntstatus_to_werror(status);
112 old_timeout = cli_set_timeout(cli, 60000);
114 pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
117 werr = ntstatus_to_werror(status);
122 encode_wkssvc_join_password_buffer(ctx,
124 &cli->user_session_key,
125 &encrypted_password);
128 old_timeout = cli_set_timeout(cli, 60000);
130 status = rpccli_wkssvc_NetrJoinDomain2(pipe_cli, ctx,
131 server_name, domain_name,
135 if (!NT_STATUS_IS_OK(status)) {
136 werr = ntstatus_to_werror(status);
142 cli_set_timeout(cli, old_timeout);
149 static WERROR libnetapi_NetJoinDomain(struct libnetapi_ctx *ctx,
150 const char *server_name,
151 const char *domain_name,
152 const char *account_ou,
154 const char *password,
158 return WERR_INVALID_PARAM;
161 if (!server_name || is_myname_or_ipaddr(server_name)) {
163 return NetJoinDomainLocal(ctx,
172 return NetJoinDomainRemote(ctx,
181 NET_API_STATUS NetJoinDomain(const char *server_name,
182 const char *domain_name,
183 const char *account_ou,
185 const char *password,
188 struct libnetapi_ctx *ctx = NULL;
189 NET_API_STATUS status;
192 status = libnetapi_getctx(&ctx);
197 werr = libnetapi_NetJoinDomain(ctx,
204 if (!W_ERROR_IS_OK(werr)) {
205 return W_ERROR_V(werr);
211 static WERROR libnetapi_NetUnjoinDomain(struct libnetapi_ctx *ctx,
212 const char *server_name,
214 const char *password,
215 uint32_t unjoin_flags)
217 struct cli_state *cli = NULL;
218 struct rpc_pipe_client *pipe_cli = NULL;
219 struct wkssvc_PasswordBuffer encrypted_password;
222 unsigned int old_timeout = 0;
224 ZERO_STRUCT(encrypted_password);
226 status = cli_full_connection(&cli, NULL, server_name,
234 if (!NT_STATUS_IS_OK(status)) {
235 werr = ntstatus_to_werror(status);
239 old_timeout = cli_set_timeout(cli, 60000);
241 pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
244 werr = ntstatus_to_werror(status);
249 encode_wkssvc_join_password_buffer(ctx,
251 &cli->user_session_key,
252 &encrypted_password);
255 old_timeout = cli_set_timeout(cli, 60000);
257 status = rpccli_wkssvc_NetrUnjoinDomain2(pipe_cli, ctx,
263 if (!NT_STATUS_IS_OK(status)) {
264 werr = ntstatus_to_werror(status);
270 cli_set_timeout(cli, old_timeout);
277 NET_API_STATUS NetUnjoinDomain(const char *server_name,
279 const char *password,
280 uint32_t unjoin_flags)
282 struct libnetapi_ctx *ctx = NULL;
283 NET_API_STATUS status;
286 status = libnetapi_getctx(&ctx);
291 werr = libnetapi_NetUnjoinDomain(ctx,
296 if (!W_ERROR_IS_OK(werr)) {
297 return W_ERROR_V(werr);
304 WERROR libnetapi_NetGetJoinInformation(struct libnetapi_ctx *ctx,
305 const char *server_name,
306 const char **name_buffer,
309 TALLOC_CTX *mem_ctx = NULL;
310 struct cli_state *cli = NULL;
311 struct rpc_pipe_client *pipe_cli = NULL;
315 mem_ctx = talloc_init("NetGetJoinInformation");
321 if (!server_name || is_myname_or_ipaddr(server_name)) {
322 if ((lp_security() == SEC_ADS) && lp_realm()) {
323 *name_buffer = SMB_STRDUP(lp_realm());
325 *name_buffer = SMB_STRDUP(lp_workgroup());
331 switch (lp_server_role()) {
332 case ROLE_DOMAIN_MEMBER:
333 case ROLE_DOMAIN_PDC:
334 case ROLE_DOMAIN_BDC:
335 *name_type = NetSetupDomainName;
337 case ROLE_STANDALONE:
339 *name_type = NetSetupWorkgroupName;
347 status = cli_full_connection(&cli, NULL, server_name,
355 if (!NT_STATUS_IS_OK(status)) {
356 werr = ntstatus_to_werror(status);
360 pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_WKSSVC,
363 werr = ntstatus_to_werror(status);
367 status = rpccli_wkssvc_NetrGetJoinInformation(pipe_cli, mem_ctx,
370 (enum wkssvc_NetJoinStatus *)name_type,
372 if (!NT_STATUS_IS_OK(status)) {
373 werr = ntstatus_to_werror(status);
381 TALLOC_FREE(mem_ctx);
386 NET_API_STATUS NetGetJoinInformation(const char *server_name,
387 const char **name_buffer,
390 struct libnetapi_ctx *ctx = NULL;
391 NET_API_STATUS status;
394 status = libnetapi_getctx(&ctx);
399 werr = libnetapi_NetGetJoinInformation(ctx,
403 if (!W_ERROR_IS_OK(werr)) {
404 return W_ERROR_V(werr);