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.h"
24 /****************************************************************
25 ****************************************************************/
27 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
28 struct libnet_JoinCtx *r,
29 const char *format, ...)
33 if (r->out.error_string) {
37 va_start(args, format);
38 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
42 /****************************************************************
43 ****************************************************************/
45 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
46 struct libnet_UnjoinCtx *r,
47 const char *format, ...)
51 if (r->out.error_string) {
55 va_start(args, format);
56 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
62 /****************************************************************
63 ****************************************************************/
65 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
66 const char *netbios_domain_name,
68 const char *user_name,
73 ADS_STRUCT *my_ads = NULL;
75 my_ads = ads_init(dns_domain_name,
79 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
83 SAFE_FREE(my_ads->auth.user_name);
84 my_ads->auth.user_name = SMB_STRDUP(user_name);
88 SAFE_FREE(my_ads->auth.password);
89 my_ads->auth.password = SMB_STRDUP(password);
92 status = ads_connect(my_ads);
93 if (!ADS_ERR_OK(status)) {
102 /****************************************************************
103 ****************************************************************/
105 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
106 struct libnet_JoinCtx *r)
110 status = libnet_connect_ads(r->in.domain_name,
114 r->in.admin_password,
116 if (!ADS_ERR_OK(status)) {
117 libnet_join_set_error_string(mem_ctx, r,
118 "failed to connect to AD: %s",
125 /****************************************************************
126 ****************************************************************/
128 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
129 struct libnet_UnjoinCtx *r)
133 status = libnet_connect_ads(r->in.domain_name,
137 r->in.admin_password,
139 if (!ADS_ERR_OK(status)) {
140 libnet_unjoin_set_error_string(mem_ctx, r,
141 "failed to connect to AD: %s",
148 /****************************************************************
149 ****************************************************************/
151 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
152 struct libnet_JoinCtx *r)
155 LDAPMessage *res = NULL;
156 const char *attrs[] = { "dn", NULL };
158 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
159 if (!ADS_ERR_OK(status)) {
163 if (ads_count_replies(r->in.ads, res) != 1) {
164 ads_msgfree(r->in.ads, res);
165 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
168 status = ads_create_machine_acct(r->in.ads,
171 ads_msgfree(r->in.ads, res);
173 if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
174 (status.err.rc == LDAP_ALREADY_EXISTS)) {
175 status = ADS_SUCCESS;
181 /****************************************************************
182 ****************************************************************/
184 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
185 struct libnet_UnjoinCtx *r)
190 status = libnet_unjoin_connect_ads(mem_ctx, r);
191 if (!ADS_ERR_OK(status)) {
196 status = ads_leave_realm(r->in.ads, r->in.machine_name);
197 if (!ADS_ERR_OK(status)) {
198 libnet_unjoin_set_error_string(mem_ctx, r,
199 "failed to leave realm: %s",
207 /****************************************************************
208 ****************************************************************/
210 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
211 struct libnet_JoinCtx *r)
214 LDAPMessage *res = NULL;
217 if (!r->in.machine_name) {
218 return ADS_ERROR(LDAP_NO_MEMORY);
221 status = ads_find_machine_acct(r->in.ads,
224 if (!ADS_ERR_OK(status)) {
228 if (ads_count_replies(r->in.ads, res) != 1) {
229 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
233 dn = ads_get_dn(r->in.ads, res);
235 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
239 r->out.dn = talloc_strdup(mem_ctx, dn);
241 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
246 ads_msgfree(r->in.ads, res);
247 ads_memfree(r->in.ads, dn);
252 /****************************************************************
253 ****************************************************************/
255 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
256 struct libnet_JoinCtx *r)
261 const char *spn_array[3] = {NULL, NULL, NULL};
265 status = libnet_join_connect_ads(mem_ctx, r);
266 if (!ADS_ERR_OK(status)) {
271 status = libnet_join_find_machine_acct(mem_ctx, r);
272 if (!ADS_ERR_OK(status)) {
276 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
278 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
283 if (name_to_fqdn(my_fqdn, r->in.machine_name) &&
284 !strequal(my_fqdn, r->in.machine_name)) {
287 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
289 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
294 mods = ads_init_mods(mem_ctx);
296 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
299 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
300 if (!ADS_ERR_OK(status)) {
301 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
304 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
306 if (!ADS_ERR_OK(status)) {
307 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
310 return ads_gen_mod(r->in.ads, r->out.dn, mods);
313 /****************************************************************
314 ****************************************************************/
316 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
317 struct libnet_JoinCtx *r)
322 if (!r->in.create_upn) {
327 status = libnet_join_connect_ads(mem_ctx, r);
328 if (!ADS_ERR_OK(status)) {
333 status = libnet_join_find_machine_acct(mem_ctx, r);
334 if (!ADS_ERR_OK(status)) {
339 r->in.upn = talloc_asprintf(mem_ctx,
342 r->out.dns_domain_name);
344 return ADS_ERROR(LDAP_NO_MEMORY);
348 mods = ads_init_mods(mem_ctx);
350 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
353 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
354 if (!ADS_ERR_OK(status)) {
355 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
358 return ads_gen_mod(r->in.ads, r->out.dn, mods);
362 /****************************************************************
363 ****************************************************************/
365 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
366 struct libnet_JoinCtx *r)
372 if (!r->in.os_name || !r->in.os_version ) {
377 status = libnet_join_connect_ads(mem_ctx, r);
378 if (!ADS_ERR_OK(status)) {
383 status = libnet_join_find_machine_acct(mem_ctx, r);
384 if (!ADS_ERR_OK(status)) {
388 mods = ads_init_mods(mem_ctx);
390 return ADS_ERROR(LDAP_NO_MEMORY);
393 os_sp = talloc_asprintf(mem_ctx, "Samba %s", SAMBA_VERSION_STRING);
395 return ADS_ERROR(LDAP_NO_MEMORY);
398 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
400 if (!ADS_ERR_OK(status)) {
404 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
406 if (!ADS_ERR_OK(status)) {
410 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
412 if (!ADS_ERR_OK(status)) {
416 return ads_gen_mod(r->in.ads, r->out.dn, mods);
419 /****************************************************************
420 ****************************************************************/
422 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
423 struct libnet_JoinCtx *r)
425 if (!lp_use_kerberos_keytab()) {
429 if (!ads_keytab_create_default(r->in.ads)) {
436 /****************************************************************
437 ****************************************************************/
439 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
440 struct libnet_JoinCtx *r)
442 uint32_t domain_func;
444 const char *salt = NULL;
445 char *std_salt = NULL;
447 status = ads_domain_func_level(r->in.ads, &domain_func);
448 if (!ADS_ERR_OK(status)) {
449 libnet_join_set_error_string(mem_ctx, r,
450 "failed to determine domain functional level: %s",
455 std_salt = kerberos_standard_des_salt();
457 libnet_join_set_error_string(mem_ctx, r,
458 "failed to obtain standard DES salt");
462 salt = talloc_strdup(mem_ctx, std_salt);
469 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
472 upn = ads_get_upn(r->in.ads, mem_ctx,
475 salt = talloc_strdup(mem_ctx, upn);
482 return kerberos_secrets_store_des_salt(salt);
485 /****************************************************************
486 ****************************************************************/
488 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
489 struct libnet_JoinCtx *r)
493 status = libnet_join_set_machine_spn(mem_ctx, r);
494 if (!ADS_ERR_OK(status)) {
495 libnet_join_set_error_string(mem_ctx, r,
496 "failed to set machine spn: %s",
501 status = libnet_join_set_os_attributes(mem_ctx, r);
502 if (!ADS_ERR_OK(status)) {
503 libnet_join_set_error_string(mem_ctx, r,
504 "failed to set machine os attributes: %s",
509 status = libnet_join_set_machine_upn(mem_ctx, r);
510 if (!ADS_ERR_OK(status)) {
511 libnet_join_set_error_string(mem_ctx, r,
512 "failed to set machine upn: %s",
517 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
518 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
521 if (!libnet_join_create_keytab(mem_ctx, r)) {
522 libnet_join_set_error_string(mem_ctx, r,
523 "failed to create kerberos keytab");
524 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
529 #endif /* WITH_ADS */
531 /****************************************************************
532 ****************************************************************/
534 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
535 struct libnet_JoinCtx *r)
537 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
543 if (!secrets_store_machine_password(r->in.machine_password,
544 r->out.netbios_domain_name,
553 /****************************************************************
554 ****************************************************************/
556 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
557 struct libnet_JoinCtx *r)
559 struct cli_state *cli = NULL;
560 struct rpc_pipe_client *pipe_hnd = NULL;
561 POLICY_HND sam_pol, domain_pol, user_pol, lsa_pol;
562 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
564 const char *const_acct_name;
566 uint32 num_rids, *name_types, *user_rids;
567 uint32 flags = 0x3e8;
568 uint32 acb_info = ACB_WSTRUST;
569 uint32 fields_present;
571 SAM_USERINFO_CTR ctr;
572 SAM_USER_INFO_25 p25;
573 const int infolevel = 25;
574 struct MD5Context md5ctx;
576 DATA_BLOB digested_session_key;
577 uchar md4_trust_password[16];
579 if (!r->in.machine_password) {
580 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
581 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
584 status = cli_full_connection(&cli, NULL,
590 r->in.admin_password,
594 if (!NT_STATUS_IS_OK(status)) {
598 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
603 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
604 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
605 if (!NT_STATUS_IS_OK(status)) {
609 status = rpccli_lsa_query_info_policy2(pipe_hnd, mem_ctx, &lsa_pol,
611 &r->out.netbios_domain_name,
612 &r->out.dns_domain_name,
617 if (NT_STATUS_IS_OK(status)) {
618 r->out.domain_is_ad = true;
621 if (!NT_STATUS_IS_OK(status)) {
622 status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &lsa_pol,
624 &r->out.netbios_domain_name,
626 if (!NT_STATUS_IS_OK(status)) {
631 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
632 cli_rpc_pipe_close(pipe_hnd);
634 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
639 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
640 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
641 if (!NT_STATUS_IS_OK(status)) {
645 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
646 SEC_RIGHTS_MAXIMUM_ALLOWED,
649 if (!NT_STATUS_IS_OK(status)) {
653 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
654 strlower_m(acct_name);
655 const_acct_name = acct_name;
657 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
658 status = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx,
660 acct_name, ACB_WSTRUST,
661 0xe005000b, &user_pol,
663 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
664 if (!(r->in.join_flags &
665 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
670 if (NT_STATUS_IS_OK(status)) {
671 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
675 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
676 &domain_pol, flags, 1,
678 &num_rids, &user_rids, &name_types);
679 if (!NT_STATUS_IS_OK(status)) {
683 if (name_types[0] != SID_NAME_USER) {
684 status = NT_STATUS_INVALID_WORKSTATION;
688 user_rid = user_rids[0];
690 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
691 SEC_RIGHTS_MAXIMUM_ALLOWED, user_rid,
693 if (!NT_STATUS_IS_OK(status)) {
697 E_md4hash(r->in.machine_password, md4_trust_password);
698 encode_pw_buffer(pwbuf, r->in.machine_password, STR_UNICODE);
700 generate_random_buffer((uint8*)md5buffer, sizeof(md5buffer));
701 digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
704 MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
705 MD5Update(&md5ctx, cli->user_session_key.data,
706 cli->user_session_key.length);
707 MD5Final(digested_session_key.data, &md5ctx);
709 SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
710 memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
712 acb_info |= ACB_PWNOEXP;
713 if (r->out.domain_is_ad) {
714 #if !defined(ENCTYPE_ARCFOUR_HMAC)
715 acb_info |= ACB_USE_DES_KEY_ONLY;
723 fields_present = ACCT_NT_PWD_SET | ACCT_LM_PWD_SET | ACCT_FLAGS;
724 init_sam_user_info25P(&p25, fields_present, acb_info, (char *)pwbuf);
726 ctr.switch_value = infolevel;
727 ctr.info.id25 = &p25;
729 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol,
730 infolevel, &cli->user_session_key,
732 if (!NT_STATUS_IS_OK(status)) {
736 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
737 cli_rpc_pipe_close(pipe_hnd);
739 status = NT_STATUS_OK;
748 /****************************************************************
749 ****************************************************************/
751 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
752 struct libnet_UnjoinCtx *r)
754 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
758 if (!secrets_delete_domain_sid(lp_workgroup())) {
765 /****************************************************************
766 ****************************************************************/
768 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
769 struct libnet_UnjoinCtx *r)
771 struct cli_state *cli = NULL;
772 struct rpc_pipe_client *pipe_hnd = NULL;
773 POLICY_HND sam_pol, domain_pol, user_pol;
774 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
776 uint32 flags = 0x3e8;
777 const char *const_acct_name;
779 uint32 num_rids, *name_types, *user_rids;
780 SAM_USERINFO_CTR ctr, *qctr = NULL;
781 SAM_USER_INFO_16 p16;
783 status = cli_full_connection(&cli, NULL,
789 r->in.admin_password,
792 if (!NT_STATUS_IS_OK(status)) {
796 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
801 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
802 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
803 if (!NT_STATUS_IS_OK(status)) {
807 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
808 SEC_RIGHTS_MAXIMUM_ALLOWED,
811 if (!NT_STATUS_IS_OK(status)) {
815 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
816 strlower_m(acct_name);
817 const_acct_name = acct_name;
819 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
820 &domain_pol, flags, 1,
822 &num_rids, &user_rids, &name_types);
823 if (!NT_STATUS_IS_OK(status)) {
827 if (name_types[0] != SID_NAME_USER) {
828 status = NT_STATUS_INVALID_WORKSTATION;
832 user_rid = user_rids[0];
834 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
835 SEC_RIGHTS_MAXIMUM_ALLOWED,
836 user_rid, &user_pol);
837 if (!NT_STATUS_IS_OK(status)) {
841 status = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx,
842 &user_pol, 16, &qctr);
843 if (!NT_STATUS_IS_OK(status)) {
844 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
849 ctr.switch_value = 16;
850 ctr.info.id16 = &p16;
852 p16.acb_info = qctr->info.id16->acb_info | ACB_DISABLED;
854 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol, 16,
855 &cli->user_session_key, &ctr);
857 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
861 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
862 rpccli_samr_close(pipe_hnd, mem_ctx, &sam_pol);
863 cli_rpc_pipe_close(pipe_hnd);
873 /****************************************************************
874 ****************************************************************/
876 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
879 struct libnet_conf_ctx *ctx;
881 werr = libnet_conf_open(r, &ctx);
882 if (!W_ERROR_IS_OK(werr)) {
886 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
888 werr = libnet_conf_set_global_parameter(ctx, "security", "user");
889 if (!W_ERROR_IS_OK(werr)) {
893 werr = libnet_conf_set_global_parameter(ctx, "workgroup",
898 werr = libnet_conf_set_global_parameter(ctx, "security", "domain");
899 if (!W_ERROR_IS_OK(werr)) {
903 werr = libnet_conf_set_global_parameter(ctx, "workgroup",
904 r->out.netbios_domain_name);
905 if (!W_ERROR_IS_OK(werr)) {
909 if (r->out.domain_is_ad) {
910 werr = libnet_conf_set_global_parameter(ctx, "security", "ads");
911 if (!W_ERROR_IS_OK(werr)) {
915 werr = libnet_conf_set_global_parameter(ctx, "realm",
916 r->out.dns_domain_name);
920 libnet_conf_close(ctx);
924 /****************************************************************
925 ****************************************************************/
927 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
929 WERROR werr = WERR_OK;
930 struct libnet_conf_ctx *ctx;
932 werr = libnet_conf_open(r, &ctx);
933 if (!W_ERROR_IS_OK(werr)) {
937 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
939 werr = libnet_conf_set_global_parameter(ctx, "security", "user");
940 if (!W_ERROR_IS_OK(werr)) {
945 libnet_conf_delete_global_parameter(ctx, "realm");
948 libnet_conf_close(ctx);
952 /****************************************************************
953 ****************************************************************/
955 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
959 if (!W_ERROR_IS_OK(r->out.result)) {
960 return r->out.result;
963 if (!r->in.modify_config) {
967 werr = do_join_modify_vals_config(r);
968 if (!W_ERROR_IS_OK(werr)) {
972 r->out.modified_config = true;
973 r->out.result = werr;
978 /****************************************************************
979 ****************************************************************/
981 static WERROR do_UnjoinConfig(struct libnet_UnjoinCtx *r)
985 if (!W_ERROR_IS_OK(r->out.result)) {
986 return r->out.result;
989 if (!r->in.modify_config) {
993 werr = do_unjoin_modify_vals_config(r);
994 if (!W_ERROR_IS_OK(werr)) {
998 r->out.modified_config = true;
999 r->out.result = werr;
1004 /****************************************************************
1005 ****************************************************************/
1007 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1008 struct libnet_JoinCtx *r)
1011 if (!r->in.domain_name) {
1012 return WERR_INVALID_PARAM;
1015 if (r->in.modify_config && !lp_include_registry_globals()) {
1016 return WERR_NOT_SUPPORTED;
1020 return WERR_SETUP_DOMAIN_CONTROLLER;
1023 if (!secrets_init()) {
1024 libnet_join_set_error_string(mem_ctx, r,
1025 "Unable to open secrets database");
1026 return WERR_CAN_NOT_COMPLETE;
1032 /****************************************************************
1033 ****************************************************************/
1035 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1036 struct libnet_JoinCtx *r)
1040 if (!W_ERROR_IS_OK(r->out.result)) {
1041 return r->out.result;
1044 werr = do_JoinConfig(r);
1045 if (!W_ERROR_IS_OK(werr)) {
1049 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1050 saf_store(r->in.domain_name, r->in.dc_name);
1056 /****************************************************************
1057 ****************************************************************/
1059 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1062 ads_destroy(&r->in.ads);
1068 /****************************************************************
1069 ****************************************************************/
1071 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1074 ads_destroy(&r->in.ads);
1080 /****************************************************************
1081 ****************************************************************/
1083 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1084 struct libnet_JoinCtx **r)
1086 struct libnet_JoinCtx *ctx;
1088 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1093 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1095 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1096 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1103 /****************************************************************
1104 ****************************************************************/
1106 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1107 struct libnet_UnjoinCtx **r)
1109 struct libnet_UnjoinCtx *ctx;
1111 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1116 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1118 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1119 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1126 /****************************************************************
1127 ****************************************************************/
1129 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1130 struct libnet_JoinCtx *r)
1134 ADS_STATUS ads_status;
1135 #endif /* WITH_ADS */
1137 if (!r->in.dc_name) {
1138 struct DS_DOMAIN_CONTROLLER_INFO *info;
1139 status = dsgetdcname(mem_ctx,
1144 DS_DIRECTORY_SERVICE_REQUIRED |
1145 DS_WRITABLE_REQUIRED |
1148 if (!NT_STATUS_IS_OK(status)) {
1149 libnet_join_set_error_string(mem_ctx, r,
1150 "failed to find DC: %s",
1152 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1155 r->in.dc_name = talloc_strdup(mem_ctx,
1156 info->domain_controller_name);
1157 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1161 if (r->in.account_ou) {
1163 ads_status = libnet_join_connect_ads(mem_ctx, r);
1164 if (!ADS_ERR_OK(ads_status)) {
1165 return WERR_DEFAULT_JOIN_REQUIRED;
1168 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1169 if (!ADS_ERR_OK(ads_status)) {
1170 libnet_join_set_error_string(mem_ctx, r,
1171 "failed to precreate account in ou %s: %s",
1173 ads_errstr(ads_status));
1174 return WERR_DEFAULT_JOIN_REQUIRED;
1177 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1179 #endif /* WITH_ADS */
1181 status = libnet_join_joindomain_rpc(mem_ctx, r);
1182 if (!NT_STATUS_IS_OK(status)) {
1183 libnet_join_set_error_string(mem_ctx, r,
1184 "failed to join domain over rpc: %s",
1186 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1187 return WERR_SETUP_ALREADY_JOINED;
1189 return ntstatus_to_werror(status);
1192 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1193 return WERR_SETUP_NOT_JOINED;
1197 if (r->out.domain_is_ad) {
1198 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1199 if (!ADS_ERR_OK(ads_status)) {
1200 return WERR_GENERAL_FAILURE;
1203 #endif /* WITH_ADS */
1208 /****************************************************************
1209 ****************************************************************/
1211 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1212 struct libnet_JoinCtx *r)
1217 NDR_PRINT_IN_DEBUG(libnet_JoinCtx, r);
1220 werr = libnet_join_pre_processing(mem_ctx, r);
1221 if (!W_ERROR_IS_OK(werr)) {
1225 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1226 werr = libnet_DomainJoin(mem_ctx, r);
1227 if (!W_ERROR_IS_OK(werr)) {
1232 werr = libnet_join_post_processing(mem_ctx, r);
1233 if (!W_ERROR_IS_OK(werr)) {
1238 NDR_PRINT_OUT_DEBUG(libnet_JoinCtx, r);
1243 /****************************************************************
1244 ****************************************************************/
1246 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1247 struct libnet_UnjoinCtx *r)
1251 if (!r->in.dc_name) {
1252 struct DS_DOMAIN_CONTROLLER_INFO *info;
1253 status = dsgetdcname(mem_ctx,
1258 DS_DIRECTORY_SERVICE_REQUIRED |
1259 DS_WRITABLE_REQUIRED |
1262 if (!NT_STATUS_IS_OK(status)) {
1263 libnet_unjoin_set_error_string(mem_ctx, r,
1264 "failed to find DC: %s",
1266 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1269 r->in.dc_name = talloc_strdup(mem_ctx,
1270 info->domain_controller_name);
1271 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1274 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
1275 if (!NT_STATUS_IS_OK(status)) {
1276 libnet_unjoin_set_error_string(mem_ctx, r,
1277 "failed to unjoin domain: %s",
1279 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1280 return WERR_SETUP_NOT_JOINED;
1282 return ntstatus_to_werror(status);
1286 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
1287 ADS_STATUS ads_status;
1288 libnet_unjoin_connect_ads(mem_ctx, r);
1289 ads_status = libnet_unjoin_remove_machine_acct(mem_ctx, r);
1290 if (!ADS_ERR_OK(ads_status)) {
1291 libnet_unjoin_set_error_string(mem_ctx, r,
1292 "failed to remove machine account from AD: %s",
1293 ads_errstr(ads_status));
1296 #endif /* WITH_ADS */
1298 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
1303 /****************************************************************
1304 ****************************************************************/
1306 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
1307 struct libnet_UnjoinCtx *r)
1309 if (r->in.modify_config && !lp_include_registry_globals()) {
1310 return WERR_NOT_SUPPORTED;
1313 if (!secrets_init()) {
1314 libnet_unjoin_set_error_string(mem_ctx, r,
1315 "Unable to open secrets database");
1316 return WERR_CAN_NOT_COMPLETE;
1322 /****************************************************************
1323 ****************************************************************/
1325 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
1326 struct libnet_UnjoinCtx *r)
1331 NDR_PRINT_IN_DEBUG(libnet_UnjoinCtx, r);
1334 werr = libnet_unjoin_pre_processing(mem_ctx, r);
1335 if (!W_ERROR_IS_OK(werr)) {
1339 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1340 werr = libnet_DomainUnjoin(mem_ctx, r);
1341 if (!W_ERROR_IS_OK(werr)) {
1346 werr = do_UnjoinConfig(r);
1347 if (!W_ERROR_IS_OK(werr)) {
1353 NDR_PRINT_OUT_DEBUG(libnet_UnjoinCtx, r);