2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Gerald (Jerry) Carter 2006
5 * Copyright (C) Guenther Deschner 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "libnet/libnet_join.h"
23 #include "libnet/libnet_proto.h"
25 /****************************************************************
26 ****************************************************************/
28 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
29 struct libnet_JoinCtx *r,
30 const char *format, ...)
35 va_start(args, format);
36 tmp = talloc_vasprintf(mem_ctx, format, args);
39 TALLOC_FREE(r->out.error_string);
40 r->out.error_string = tmp;
43 /****************************************************************
44 ****************************************************************/
46 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
47 struct libnet_UnjoinCtx *r,
48 const char *format, ...)
53 va_start(args, format);
54 tmp = talloc_vasprintf(mem_ctx, format, args);
57 TALLOC_FREE(r->out.error_string);
58 r->out.error_string = tmp;
61 /****************************************************************
62 ****************************************************************/
64 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
65 const char *netbios_domain_name,
67 const char *user_name,
72 ADS_STRUCT *my_ads = NULL;
74 my_ads = ads_init(dns_domain_name,
78 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
82 SAFE_FREE(my_ads->auth.user_name);
83 my_ads->auth.user_name = SMB_STRDUP(user_name);
87 SAFE_FREE(my_ads->auth.password);
88 my_ads->auth.password = SMB_STRDUP(password);
91 status = ads_connect(my_ads);
92 if (!ADS_ERR_OK(status)) {
101 /****************************************************************
102 ****************************************************************/
104 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
105 struct libnet_JoinCtx *r)
110 ads_destroy(&r->in.ads);
113 status = libnet_connect_ads(r->in.domain_name,
117 r->in.admin_password,
119 if (!ADS_ERR_OK(status)) {
120 libnet_join_set_error_string(mem_ctx, r,
121 "failed to connect to AD: %s\n",
128 /****************************************************************
129 ****************************************************************/
131 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
132 struct libnet_UnjoinCtx *r)
137 ads_destroy(&r->in.ads);
140 status = libnet_connect_ads(r->in.domain_name,
144 r->in.admin_password,
146 if (!ADS_ERR_OK(status)) {
147 libnet_unjoin_set_error_string(mem_ctx, r,
148 "failed to connect to AD: %s\n",
155 /****************************************************************
156 ****************************************************************/
158 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
159 struct libnet_JoinCtx *r)
162 LDAPMessage *res = NULL;
163 const char *attrs[] = { "dn", NULL };
165 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
166 if (!ADS_ERR_OK(status)) {
170 if (ads_count_replies(r->in.ads, res) != 1) {
171 ads_msgfree(r->in.ads, res);
172 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
175 status = ads_create_machine_acct(r->in.ads,
178 ads_msgfree(r->in.ads, res);
180 if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
181 (status.err.rc == LDAP_ALREADY_EXISTS)) {
182 status = ADS_SUCCESS;
188 /****************************************************************
189 ****************************************************************/
191 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
192 struct libnet_JoinCtx *r)
194 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
200 if (!secrets_store_machine_password(r->in.machine_password,
201 r->out.netbios_domain_name,
210 /****************************************************************
211 ****************************************************************/
213 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
214 struct libnet_JoinCtx *r)
216 struct cli_state *cli = NULL;
217 struct rpc_pipe_client *pipe_hnd = NULL;
218 POLICY_HND sam_pol, domain_pol, user_pol, lsa_pol;
219 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
221 const char *const_acct_name;
223 uint32 num_rids, *name_types, *user_rids;
224 uint32 flags = 0x3e8;
225 uint32 acb_info = ACB_WSTRUST;
226 uint32 fields_present;
228 SAM_USERINFO_CTR ctr;
229 SAM_USER_INFO_25 p25;
230 const int infolevel = 25;
231 struct MD5Context md5ctx;
233 DATA_BLOB digested_session_key;
234 uchar md4_trust_password[16];
236 if (!r->in.machine_password) {
237 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
238 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
241 status = cli_full_connection(&cli, NULL,
247 r->in.admin_password,
251 if (!NT_STATUS_IS_OK(status)) {
255 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
260 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
261 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
262 if (!NT_STATUS_IS_OK(status)) {
266 status = rpccli_lsa_query_info_policy2(pipe_hnd, mem_ctx, &lsa_pol,
268 &r->out.netbios_domain_name,
269 &r->out.dns_domain_name,
274 if (!NT_STATUS_IS_OK(status)) {
275 status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &lsa_pol,
277 &r->out.netbios_domain_name,
279 if (!NT_STATUS_IS_OK(status)) {
284 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
285 cli_rpc_pipe_close(pipe_hnd);
287 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
292 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
293 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
294 if (!NT_STATUS_IS_OK(status)) {
298 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
299 SEC_RIGHTS_MAXIMUM_ALLOWED,
302 if (!NT_STATUS_IS_OK(status)) {
306 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
307 strlower_m(acct_name);
308 const_acct_name = acct_name;
310 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
311 status = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx,
313 acct_name, ACB_WSTRUST,
314 0xe005000b, &user_pol,
316 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
317 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
322 if (NT_STATUS_IS_OK(status)) {
323 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
327 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
328 &domain_pol, flags, 1,
330 &num_rids, &user_rids, &name_types);
331 if (!NT_STATUS_IS_OK(status)) {
335 if (name_types[0] != SID_NAME_USER) {
336 status = NT_STATUS_INVALID_WORKSTATION;
340 user_rid = user_rids[0];
342 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
343 SEC_RIGHTS_MAXIMUM_ALLOWED, user_rid,
345 if (!NT_STATUS_IS_OK(status)) {
349 E_md4hash(r->in.machine_password, md4_trust_password);
350 encode_pw_buffer(pwbuf, r->in.machine_password, STR_UNICODE);
352 generate_random_buffer((uint8*)md5buffer, sizeof(md5buffer));
353 digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
356 MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
357 MD5Update(&md5ctx, cli->user_session_key.data,
358 cli->user_session_key.length);
359 MD5Final(digested_session_key.data, &md5ctx);
361 SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
362 memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
364 acb_info |= ACB_PWNOEXP;
366 if ( dom_type == ND_TYPE_AD ) {
367 #if !defined(ENCTYPE_ARCFOUR_HMAC)
368 acb_info |= ACB_USE_DES_KEY_ONLY;
376 fields_present = ACCT_NT_PWD_SET | ACCT_LM_PWD_SET | ACCT_FLAGS;
377 init_sam_user_info25P(&p25, fields_present, acb_info, (char *)pwbuf);
379 ctr.switch_value = infolevel;
380 ctr.info.id25 = &p25;
382 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol,
383 infolevel, &cli->user_session_key,
385 if (!NT_STATUS_IS_OK(status)) {
389 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
390 cli_rpc_pipe_close(pipe_hnd);
392 status = NT_STATUS_OK;
401 /****************************************************************
402 ****************************************************************/
404 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
405 struct libnet_UnjoinCtx *r)
407 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
411 if (!secrets_delete_domain_sid(lp_workgroup())) {
418 /****************************************************************
419 ****************************************************************/
421 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
422 struct libnet_UnjoinCtx *r)
424 struct cli_state *cli = NULL;
425 struct rpc_pipe_client *pipe_hnd = NULL;
426 POLICY_HND sam_pol, domain_pol, user_pol;
427 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
429 uint32 flags = 0x3e8;
430 const char *const_acct_name;
432 uint32 num_rids, *name_types, *user_rids;
433 SAM_USERINFO_CTR ctr, *qctr = NULL;
434 SAM_USER_INFO_16 p16;
436 status = cli_full_connection(&cli, NULL,
442 r->in.admin_password,
445 if (!NT_STATUS_IS_OK(status)) {
449 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
454 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
455 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
456 if (!NT_STATUS_IS_OK(status)) {
460 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
461 SEC_RIGHTS_MAXIMUM_ALLOWED,
464 if (!NT_STATUS_IS_OK(status)) {
468 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
469 strlower_m(acct_name);
470 const_acct_name = acct_name;
472 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
473 &domain_pol, flags, 1,
475 &num_rids, &user_rids, &name_types);
476 if (!NT_STATUS_IS_OK(status)) {
480 if (name_types[0] != SID_NAME_USER) {
481 status = NT_STATUS_INVALID_WORKSTATION;
485 user_rid = user_rids[0];
487 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
488 SEC_RIGHTS_MAXIMUM_ALLOWED,
489 user_rid, &user_pol);
490 if (!NT_STATUS_IS_OK(status)) {
494 status = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx,
495 &user_pol, 16, &qctr);
496 if (!NT_STATUS_IS_OK(status)) {
497 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
502 ctr.switch_value = 16;
503 ctr.info.id16 = &p16;
505 p16.acb_info = qctr->info.id16->acb_info | ACB_DISABLED;
507 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol, 16,
508 &cli->user_session_key, &ctr);
510 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
514 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
515 rpccli_samr_close(pipe_hnd, mem_ctx, &sam_pol);
516 cli_rpc_pipe_close(pipe_hnd);
526 /****************************************************************
527 ****************************************************************/
529 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
534 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
536 werr = libnet_conf_set_global_parameter("security", "user");
537 W_ERROR_NOT_OK_RETURN(werr);
539 werr = libnet_conf_set_global_parameter("workgroup",
544 if (r->out.dns_domain_name) {
548 werr = libnet_conf_set_global_parameter("security", "domain");
549 W_ERROR_NOT_OK_RETURN(werr);
551 werr = libnet_conf_set_global_parameter("workgroup",
552 r->out.netbios_domain_name);
553 W_ERROR_NOT_OK_RETURN(werr);
556 werr = libnet_conf_set_global_parameter("security", "ads");
557 W_ERROR_NOT_OK_RETURN(werr);
559 werr = libnet_conf_set_global_parameter("realm",
560 r->out.dns_domain_name);
561 W_ERROR_NOT_OK_RETURN(werr);
567 /****************************************************************
568 ****************************************************************/
570 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
572 WERROR werr = WERR_OK;
574 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
576 werr = libnet_conf_set_global_parameter("security", "user");
577 W_ERROR_NOT_OK_RETURN(werr);
580 werr = libnet_conf_delete_parameter(GLOBAL_NAME, "realm");
585 /****************************************************************
586 ****************************************************************/
588 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
592 if (!W_ERROR_IS_OK(r->out.result)) {
593 return r->out.result;
596 if (!r->in.modify_config) {
600 werr = do_join_modify_vals_config(r);
601 if (!W_ERROR_IS_OK(werr)) {
605 r->out.modified_config = true;
606 r->out.result = werr;
611 /****************************************************************
612 ****************************************************************/
614 static WERROR do_UnjoinConfig(struct libnet_UnjoinCtx *r)
618 if (!W_ERROR_IS_OK(r->out.result)) {
619 return r->out.result;
622 if (!r->in.modify_config) {
626 werr = do_unjoin_modify_vals_config(r);
627 if (!W_ERROR_IS_OK(werr)) {
631 r->out.modified_config = true;
632 r->out.result = werr;
637 /****************************************************************
638 ****************************************************************/
640 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
643 ads_destroy(&r->in.ads);
649 /****************************************************************
650 ****************************************************************/
652 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
655 ads_destroy(&r->in.ads);
661 /****************************************************************
662 ****************************************************************/
664 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
665 struct libnet_JoinCtx **r)
667 struct libnet_JoinCtx *ctx;
669 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
674 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
676 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
677 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
684 /****************************************************************
685 ****************************************************************/
687 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
688 struct libnet_UnjoinCtx **r)
690 struct libnet_UnjoinCtx *ctx;
692 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
697 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
699 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
700 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
707 /****************************************************************
708 ****************************************************************/
710 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
711 struct libnet_JoinCtx *r)
714 ADS_STATUS ads_status;
716 if (r->in.account_ou) {
717 ads_status = libnet_join_connect_ads(mem_ctx, r);
718 if (!ADS_ERR_OK(ads_status)) {
719 return WERR_GENERAL_FAILURE;
721 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
722 if (!ADS_ERR_OK(ads_status)) {
723 libnet_join_set_error_string(mem_ctx, r,
724 "failed to precreate account in ou %s: %s\n",
726 ads_errstr(ads_status));
727 return WERR_GENERAL_FAILURE;
730 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
733 status = libnet_join_joindomain_rpc(mem_ctx, r);
734 if (!NT_STATUS_IS_OK(status)) {
735 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
736 return WERR_SETUP_ALREADY_JOINED;
738 return ntstatus_to_werror(status);
741 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
742 return WERR_SETUP_NOT_JOINED;
748 /****************************************************************
749 ****************************************************************/
751 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
752 struct libnet_JoinCtx *r)
756 if (!r->in.domain_name) {
757 return WERR_INVALID_PARAM;
760 if (r->in.modify_config && !lp_include_registry_globals()) {
761 return WERR_NOT_SUPPORTED;
765 return WERR_SETUP_DOMAIN_CONTROLLER;
768 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
769 werr = libnet_DomainJoin(mem_ctx, r);
770 if (!W_ERROR_IS_OK(werr)) {
775 werr = do_JoinConfig(r);
776 if (!W_ERROR_IS_OK(werr)) {
783 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
784 struct libnet_UnjoinCtx *r)
789 if (r->in.modify_config && !lp_include_registry_globals()) {
790 return WERR_NOT_SUPPORTED;
793 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
795 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
796 if (!NT_STATUS_IS_OK(status)) {
797 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
798 return WERR_SETUP_NOT_JOINED;
800 return ntstatus_to_werror(status);
803 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
806 werr = do_UnjoinConfig(r);
807 if (!W_ERROR_IS_OK(werr)) {