2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2007-2008
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 "librpc/gen_ndr/libnetapi.h"
23 #include "lib/netapi/netapi.h"
24 #include "lib/netapi/netapi_private.h"
25 #include "lib/netapi/libnetapi.h"
26 #include "libnet/libnet.h"
27 #include "libcli/auth/libcli_auth.h"
29 /****************************************************************
30 ****************************************************************/
32 WERROR NetJoinDomain_l(struct libnetapi_ctx *mem_ctx,
33 struct NetJoinDomain *r)
35 struct libnet_JoinCtx *j = NULL;
39 return WERR_INVALID_PARAM;
42 werr = libnet_init_JoinCtx(mem_ctx, &j);
43 W_ERROR_NOT_OK_RETURN(werr);
45 j->in.domain_name = talloc_strdup(mem_ctx, r->in.domain);
46 W_ERROR_HAVE_NO_MEMORY(j->in.domain_name);
48 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
50 struct netr_DsRGetDCNameInfo *info = NULL;
51 const char *dc = NULL;
52 uint32_t flags = DS_DIRECTORY_SERVICE_REQUIRED |
53 DS_WRITABLE_REQUIRED |
55 status = dsgetdcname(mem_ctx, NULL, r->in.domain,
56 NULL, NULL, flags, &info);
57 if (!NT_STATUS_IS_OK(status)) {
58 libnetapi_set_error_string(mem_ctx,
59 "%s", get_friendly_nt_error_msg(status));
60 return ntstatus_to_werror(status);
63 dc = strip_hostname(info->dc_unc);
64 j->in.dc_name = talloc_strdup(mem_ctx, dc);
65 W_ERROR_HAVE_NO_MEMORY(j->in.dc_name);
68 if (r->in.account_ou) {
69 j->in.account_ou = talloc_strdup(mem_ctx, r->in.account_ou);
70 W_ERROR_HAVE_NO_MEMORY(j->in.account_ou);
74 j->in.admin_account = talloc_strdup(mem_ctx, r->in.account);
75 W_ERROR_HAVE_NO_MEMORY(j->in.admin_account);
79 j->in.admin_password = talloc_strdup(mem_ctx, r->in.password);
80 W_ERROR_HAVE_NO_MEMORY(j->in.admin_password);
83 j->in.join_flags = r->in.join_flags;
84 j->in.modify_config = true;
87 werr = libnet_Join(mem_ctx, j);
88 if (!W_ERROR_IS_OK(werr) && j->out.error_string) {
89 libnetapi_set_error_string(mem_ctx, "%s", j->out.error_string);
96 /****************************************************************
97 ****************************************************************/
99 WERROR NetJoinDomain_r(struct libnetapi_ctx *ctx,
100 struct NetJoinDomain *r)
102 struct rpc_pipe_client *pipe_cli = NULL;
103 struct wkssvc_PasswordBuffer *encrypted_password = NULL;
106 unsigned int old_timeout = 0;
108 werr = libnetapi_open_pipe(ctx, r->in.server,
109 &ndr_table_wkssvc.syntax_id,
111 if (!W_ERROR_IS_OK(werr)) {
115 if (r->in.password) {
116 encode_wkssvc_join_password_buffer(ctx,
118 &pipe_cli->auth->user_session_key,
119 &encrypted_password);
122 old_timeout = rpccli_set_timeout(pipe_cli, 600000);
124 status = rpccli_wkssvc_NetrJoinDomain2(pipe_cli, ctx,
132 if (!NT_STATUS_IS_OK(status)) {
133 werr = ntstatus_to_werror(status);
138 if (pipe_cli && old_timeout) {
139 rpccli_set_timeout(pipe_cli, old_timeout);
144 /****************************************************************
145 ****************************************************************/
147 WERROR NetUnjoinDomain_l(struct libnetapi_ctx *mem_ctx,
148 struct NetUnjoinDomain *r)
150 struct libnet_UnjoinCtx *u = NULL;
151 struct dom_sid domain_sid;
152 const char *domain = NULL;
155 if (!secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
156 return WERR_SETUP_NOT_JOINED;
159 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
160 W_ERROR_NOT_OK_RETURN(werr);
165 domain = lp_workgroup();
168 if (r->in.server_name) {
169 u->in.dc_name = talloc_strdup(mem_ctx, r->in.server_name);
170 W_ERROR_HAVE_NO_MEMORY(u->in.dc_name);
173 struct netr_DsRGetDCNameInfo *info = NULL;
174 const char *dc = NULL;
175 uint32_t flags = DS_DIRECTORY_SERVICE_REQUIRED |
176 DS_WRITABLE_REQUIRED |
178 status = dsgetdcname(mem_ctx, NULL, domain,
179 NULL, NULL, flags, &info);
180 if (!NT_STATUS_IS_OK(status)) {
181 libnetapi_set_error_string(mem_ctx,
182 "failed to find DC for domain %s: %s",
184 get_friendly_nt_error_msg(status));
185 return ntstatus_to_werror(status);
188 dc = strip_hostname(info->dc_unc);
189 u->in.dc_name = talloc_strdup(mem_ctx, dc);
190 W_ERROR_HAVE_NO_MEMORY(u->in.dc_name);
192 u->in.domain_name = domain;
196 u->in.admin_account = talloc_strdup(mem_ctx, r->in.account);
197 W_ERROR_HAVE_NO_MEMORY(u->in.admin_account);
200 if (r->in.password) {
201 u->in.admin_password = talloc_strdup(mem_ctx, r->in.password);
202 W_ERROR_HAVE_NO_MEMORY(u->in.admin_password);
205 u->in.domain_name = domain;
206 u->in.unjoin_flags = r->in.unjoin_flags;
207 u->in.modify_config = true;
210 u->in.domain_sid = &domain_sid;
212 werr = libnet_Unjoin(mem_ctx, u);
213 if (!W_ERROR_IS_OK(werr) && u->out.error_string) {
214 libnetapi_set_error_string(mem_ctx, "%s", u->out.error_string);
221 /****************************************************************
222 ****************************************************************/
224 WERROR NetUnjoinDomain_r(struct libnetapi_ctx *ctx,
225 struct NetUnjoinDomain *r)
227 struct rpc_pipe_client *pipe_cli = NULL;
228 struct wkssvc_PasswordBuffer *encrypted_password = NULL;
231 unsigned int old_timeout = 0;
233 werr = libnetapi_open_pipe(ctx, r->in.server_name,
234 &ndr_table_wkssvc.syntax_id,
236 if (!W_ERROR_IS_OK(werr)) {
240 if (r->in.password) {
241 encode_wkssvc_join_password_buffer(ctx,
243 &pipe_cli->auth->user_session_key,
244 &encrypted_password);
247 old_timeout = rpccli_set_timeout(pipe_cli, 60000);
249 status = rpccli_wkssvc_NetrUnjoinDomain2(pipe_cli, ctx,
255 if (!NT_STATUS_IS_OK(status)) {
256 werr = ntstatus_to_werror(status);
261 if (pipe_cli && old_timeout) {
262 rpccli_set_timeout(pipe_cli, old_timeout);
268 /****************************************************************
269 ****************************************************************/
271 WERROR NetGetJoinInformation_r(struct libnetapi_ctx *ctx,
272 struct NetGetJoinInformation *r)
274 struct rpc_pipe_client *pipe_cli = NULL;
277 const char *buffer = NULL;
279 werr = libnetapi_open_pipe(ctx, r->in.server_name,
280 &ndr_table_wkssvc.syntax_id,
282 if (!W_ERROR_IS_OK(werr)) {
286 status = rpccli_wkssvc_NetrGetJoinInformation(pipe_cli, ctx,
289 (enum wkssvc_NetJoinStatus *)r->out.name_type,
291 if (!NT_STATUS_IS_OK(status)) {
292 werr = ntstatus_to_werror(status);
296 *r->out.name_buffer = talloc_strdup(ctx, buffer);
297 W_ERROR_HAVE_NO_MEMORY(*r->out.name_buffer);
303 /****************************************************************
304 ****************************************************************/
306 WERROR NetGetJoinInformation_l(struct libnetapi_ctx *ctx,
307 struct NetGetJoinInformation *r)
309 if ((lp_security() == SEC_ADS) && lp_realm()) {
310 *r->out.name_buffer = talloc_strdup(ctx, lp_realm());
312 *r->out.name_buffer = talloc_strdup(ctx, lp_workgroup());
314 if (!*r->out.name_buffer) {
318 switch (lp_server_role()) {
319 case ROLE_DOMAIN_MEMBER:
320 case ROLE_DOMAIN_PDC:
321 case ROLE_DOMAIN_BDC:
322 *r->out.name_type = NetSetupDomainName;
324 case ROLE_STANDALONE:
326 *r->out.name_type = NetSetupWorkgroupName;
333 /****************************************************************
334 ****************************************************************/
336 WERROR NetGetJoinableOUs_l(struct libnetapi_ctx *ctx,
337 struct NetGetJoinableOUs *r)
341 ADS_STATUS ads_status;
342 ADS_STRUCT *ads = NULL;
343 struct netr_DsRGetDCNameInfo *info = NULL;
344 const char *dc = NULL;
345 uint32_t flags = DS_DIRECTORY_SERVICE_REQUIRED |
348 status = dsgetdcname(ctx, NULL, r->in.domain,
349 NULL, NULL, flags, &info);
350 if (!NT_STATUS_IS_OK(status)) {
351 libnetapi_set_error_string(ctx, "%s",
352 get_friendly_nt_error_msg(status));
353 return ntstatus_to_werror(status);
356 dc = strip_hostname(info->dc_unc);
358 ads = ads_init(info->domain_name, info->domain_name, dc);
360 return WERR_GENERAL_FAILURE;
363 SAFE_FREE(ads->auth.user_name);
365 ads->auth.user_name = SMB_STRDUP(r->in.account);
366 } else if (ctx->username) {
367 ads->auth.user_name = SMB_STRDUP(ctx->username);
370 SAFE_FREE(ads->auth.password);
371 if (r->in.password) {
372 ads->auth.password = SMB_STRDUP(r->in.password);
373 } else if (ctx->password) {
374 ads->auth.password = SMB_STRDUP(ctx->password);
377 ads_status = ads_connect_user_creds(ads);
378 if (!ADS_ERR_OK(ads_status)) {
380 return WERR_DEFAULT_JOIN_REQUIRED;
383 ads_status = ads_get_joinable_ous(ads, ctx,
384 (char ***)r->out.ous,
385 (size_t *)r->out.ou_count);
386 if (!ADS_ERR_OK(ads_status)) {
388 return WERR_DEFAULT_JOIN_REQUIRED;
394 return WERR_NOT_SUPPORTED;
398 /****************************************************************
399 ****************************************************************/
401 WERROR NetGetJoinableOUs_r(struct libnetapi_ctx *ctx,
402 struct NetGetJoinableOUs *r)
404 struct rpc_pipe_client *pipe_cli = NULL;
405 struct wkssvc_PasswordBuffer *encrypted_password = NULL;
409 werr = libnetapi_open_pipe(ctx, r->in.server_name,
410 &ndr_table_wkssvc.syntax_id,
412 if (!W_ERROR_IS_OK(werr)) {
416 if (r->in.password) {
417 encode_wkssvc_join_password_buffer(ctx,
419 &pipe_cli->auth->user_session_key,
420 &encrypted_password);
423 status = rpccli_wkssvc_NetrGetJoinableOus2(pipe_cli, ctx,
431 if (!NT_STATUS_IS_OK(status)) {
432 werr = ntstatus_to_werror(status);
440 /****************************************************************
441 ****************************************************************/
443 WERROR NetRenameMachineInDomain_r(struct libnetapi_ctx *ctx,
444 struct NetRenameMachineInDomain *r)
446 struct rpc_pipe_client *pipe_cli = NULL;
447 struct wkssvc_PasswordBuffer *encrypted_password = NULL;
451 werr = libnetapi_open_pipe(ctx, r->in.server_name,
452 &ndr_table_wkssvc.syntax_id,
454 if (!W_ERROR_IS_OK(werr)) {
458 if (r->in.password) {
459 encode_wkssvc_join_password_buffer(ctx,
461 &pipe_cli->auth->user_session_key,
462 &encrypted_password);
465 status = rpccli_wkssvc_NetrRenameMachineInDomain2(pipe_cli, ctx,
467 r->in.new_machine_name,
470 r->in.rename_options,
472 if (!NT_STATUS_IS_OK(status)) {
473 werr = ntstatus_to_werror(status);
481 /****************************************************************
482 ****************************************************************/
484 WERROR NetRenameMachineInDomain_l(struct libnetapi_ctx *ctx,
485 struct NetRenameMachineInDomain *r)
487 LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetRenameMachineInDomain);