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 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
30 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
31 DEBUG(1,("libnet_Join:\n%s", str)); \
35 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
36 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
37 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
38 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
40 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
43 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
44 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
48 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
49 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
50 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
51 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
53 /****************************************************************
54 ****************************************************************/
56 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
57 struct libnet_JoinCtx *r,
58 const char *format, ...)
62 if (r->out.error_string) {
66 va_start(args, format);
67 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
71 /****************************************************************
72 ****************************************************************/
74 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
75 struct libnet_UnjoinCtx *r,
76 const char *format, ...)
80 if (r->out.error_string) {
84 va_start(args, format);
85 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
91 /****************************************************************
92 ****************************************************************/
94 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
95 const char *netbios_domain_name,
97 const char *user_name,
102 ADS_STRUCT *my_ads = NULL;
104 my_ads = ads_init(dns_domain_name,
108 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
112 SAFE_FREE(my_ads->auth.user_name);
113 my_ads->auth.user_name = SMB_STRDUP(user_name);
117 SAFE_FREE(my_ads->auth.password);
118 my_ads->auth.password = SMB_STRDUP(password);
121 status = ads_connect(my_ads);
122 if (!ADS_ERR_OK(status)) {
123 ads_destroy(&my_ads);
131 /****************************************************************
132 ****************************************************************/
134 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
135 struct libnet_JoinCtx *r)
139 status = libnet_connect_ads(r->in.domain_name,
143 r->in.admin_password,
145 if (!ADS_ERR_OK(status)) {
146 libnet_join_set_error_string(mem_ctx, r,
147 "failed to connect to AD: %s",
154 /****************************************************************
155 ****************************************************************/
157 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
158 struct libnet_UnjoinCtx *r)
162 status = libnet_connect_ads(r->in.domain_name,
166 r->in.admin_password,
168 if (!ADS_ERR_OK(status)) {
169 libnet_unjoin_set_error_string(mem_ctx, r,
170 "failed to connect to AD: %s",
177 /****************************************************************
178 ****************************************************************/
180 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
181 struct libnet_JoinCtx *r)
184 LDAPMessage *res = NULL;
185 const char *attrs[] = { "dn", NULL };
187 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
188 if (!ADS_ERR_OK(status)) {
192 if (ads_count_replies(r->in.ads, res) != 1) {
193 ads_msgfree(r->in.ads, res);
194 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
197 status = ads_create_machine_acct(r->in.ads,
200 ads_msgfree(r->in.ads, res);
202 if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
203 (status.err.rc == LDAP_ALREADY_EXISTS)) {
204 status = ADS_SUCCESS;
210 /****************************************************************
211 ****************************************************************/
213 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
214 struct libnet_UnjoinCtx *r)
219 status = libnet_unjoin_connect_ads(mem_ctx, r);
220 if (!ADS_ERR_OK(status)) {
225 status = ads_leave_realm(r->in.ads, r->in.machine_name);
226 if (!ADS_ERR_OK(status)) {
227 libnet_unjoin_set_error_string(mem_ctx, r,
228 "failed to leave realm: %s",
236 /****************************************************************
237 ****************************************************************/
239 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
240 struct libnet_JoinCtx *r)
243 LDAPMessage *res = NULL;
246 if (!r->in.machine_name) {
247 return ADS_ERROR(LDAP_NO_MEMORY);
250 status = ads_find_machine_acct(r->in.ads,
253 if (!ADS_ERR_OK(status)) {
257 if (ads_count_replies(r->in.ads, res) != 1) {
258 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
262 dn = ads_get_dn(r->in.ads, res);
264 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
268 r->out.dn = talloc_strdup(mem_ctx, dn);
270 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
275 ads_msgfree(r->in.ads, res);
276 ads_memfree(r->in.ads, dn);
281 /****************************************************************
282 ****************************************************************/
284 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
285 struct libnet_JoinCtx *r)
290 const char *spn_array[3] = {NULL, NULL, NULL};
294 status = libnet_join_connect_ads(mem_ctx, r);
295 if (!ADS_ERR_OK(status)) {
300 status = libnet_join_find_machine_acct(mem_ctx, r);
301 if (!ADS_ERR_OK(status)) {
305 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
307 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
312 if (name_to_fqdn(my_fqdn, r->in.machine_name) &&
313 !strequal(my_fqdn, r->in.machine_name)) {
316 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
318 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
323 mods = ads_init_mods(mem_ctx);
325 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
328 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
329 if (!ADS_ERR_OK(status)) {
330 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
333 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
335 if (!ADS_ERR_OK(status)) {
336 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
339 return ads_gen_mod(r->in.ads, r->out.dn, mods);
342 /****************************************************************
343 ****************************************************************/
345 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
346 struct libnet_JoinCtx *r)
351 if (!r->in.create_upn) {
356 status = libnet_join_connect_ads(mem_ctx, r);
357 if (!ADS_ERR_OK(status)) {
362 status = libnet_join_find_machine_acct(mem_ctx, r);
363 if (!ADS_ERR_OK(status)) {
368 r->in.upn = talloc_asprintf(mem_ctx,
371 r->out.dns_domain_name);
373 return ADS_ERROR(LDAP_NO_MEMORY);
377 mods = ads_init_mods(mem_ctx);
379 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
382 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
383 if (!ADS_ERR_OK(status)) {
384 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
387 return ads_gen_mod(r->in.ads, r->out.dn, mods);
391 /****************************************************************
392 ****************************************************************/
394 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
395 struct libnet_JoinCtx *r)
401 if (!r->in.os_name || !r->in.os_version ) {
406 status = libnet_join_connect_ads(mem_ctx, r);
407 if (!ADS_ERR_OK(status)) {
412 status = libnet_join_find_machine_acct(mem_ctx, r);
413 if (!ADS_ERR_OK(status)) {
417 mods = ads_init_mods(mem_ctx);
419 return ADS_ERROR(LDAP_NO_MEMORY);
422 os_sp = talloc_asprintf(mem_ctx, "Samba %s", SAMBA_VERSION_STRING);
424 return ADS_ERROR(LDAP_NO_MEMORY);
427 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
429 if (!ADS_ERR_OK(status)) {
433 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
435 if (!ADS_ERR_OK(status)) {
439 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
441 if (!ADS_ERR_OK(status)) {
445 return ads_gen_mod(r->in.ads, r->out.dn, mods);
448 /****************************************************************
449 ****************************************************************/
451 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
452 struct libnet_JoinCtx *r)
454 if (!lp_use_kerberos_keytab()) {
458 if (!ads_keytab_create_default(r->in.ads)) {
465 /****************************************************************
466 ****************************************************************/
468 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
469 struct libnet_JoinCtx *r)
471 uint32_t domain_func;
473 const char *salt = NULL;
474 char *std_salt = NULL;
476 status = ads_domain_func_level(r->in.ads, &domain_func);
477 if (!ADS_ERR_OK(status)) {
478 libnet_join_set_error_string(mem_ctx, r,
479 "failed to determine domain functional level: %s",
484 std_salt = kerberos_standard_des_salt();
486 libnet_join_set_error_string(mem_ctx, r,
487 "failed to obtain standard DES salt");
491 salt = talloc_strdup(mem_ctx, std_salt);
498 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
501 upn = ads_get_upn(r->in.ads, mem_ctx,
504 salt = talloc_strdup(mem_ctx, upn);
511 return kerberos_secrets_store_des_salt(salt);
514 /****************************************************************
515 ****************************************************************/
517 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
518 struct libnet_JoinCtx *r)
522 status = libnet_join_set_machine_spn(mem_ctx, r);
523 if (!ADS_ERR_OK(status)) {
524 libnet_join_set_error_string(mem_ctx, r,
525 "failed to set machine spn: %s",
530 status = libnet_join_set_os_attributes(mem_ctx, r);
531 if (!ADS_ERR_OK(status)) {
532 libnet_join_set_error_string(mem_ctx, r,
533 "failed to set machine os attributes: %s",
538 status = libnet_join_set_machine_upn(mem_ctx, r);
539 if (!ADS_ERR_OK(status)) {
540 libnet_join_set_error_string(mem_ctx, r,
541 "failed to set machine upn: %s",
546 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
547 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
550 if (!libnet_join_create_keytab(mem_ctx, r)) {
551 libnet_join_set_error_string(mem_ctx, r,
552 "failed to create kerberos keytab");
553 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
558 #endif /* WITH_ADS */
560 /****************************************************************
561 ****************************************************************/
563 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
564 struct libnet_JoinCtx *r)
566 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
572 if (!secrets_store_machine_password(r->in.machine_password,
573 r->out.netbios_domain_name,
582 /****************************************************************
583 ****************************************************************/
585 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
586 struct libnet_JoinCtx *r)
588 struct cli_state *cli = NULL;
589 struct rpc_pipe_client *pipe_hnd = NULL;
590 POLICY_HND sam_pol, domain_pol, user_pol, lsa_pol;
591 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
593 const char *const_acct_name;
595 uint32 num_rids, *name_types, *user_rids;
596 uint32 flags = 0x3e8;
597 uint32 acb_info = ACB_WSTRUST;
598 uint32 fields_present;
600 SAM_USERINFO_CTR ctr;
601 SAM_USER_INFO_25 p25;
602 const int infolevel = 25;
603 struct MD5Context md5ctx;
605 DATA_BLOB digested_session_key;
606 uchar md4_trust_password[16];
608 if (!r->in.machine_password) {
609 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
610 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
613 status = cli_full_connection(&cli, NULL,
619 r->in.admin_password,
623 if (!NT_STATUS_IS_OK(status)) {
627 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
632 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
633 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
634 if (!NT_STATUS_IS_OK(status)) {
638 status = rpccli_lsa_query_info_policy2(pipe_hnd, mem_ctx, &lsa_pol,
640 &r->out.netbios_domain_name,
641 &r->out.dns_domain_name,
646 if (NT_STATUS_IS_OK(status)) {
647 r->out.domain_is_ad = true;
650 if (!NT_STATUS_IS_OK(status)) {
651 status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &lsa_pol,
653 &r->out.netbios_domain_name,
655 if (!NT_STATUS_IS_OK(status)) {
660 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
661 cli_rpc_pipe_close(pipe_hnd);
663 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
668 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
669 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
670 if (!NT_STATUS_IS_OK(status)) {
674 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
675 SEC_RIGHTS_MAXIMUM_ALLOWED,
678 if (!NT_STATUS_IS_OK(status)) {
682 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
683 strlower_m(acct_name);
684 const_acct_name = acct_name;
686 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
687 uint32_t acct_flags =
688 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
689 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
690 SAMR_USER_ACCESS_SET_PASSWORD |
691 SAMR_USER_ACCESS_GET_ATTRIBUTES |
692 SAMR_USER_ACCESS_SET_ATTRIBUTES;
694 status = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx,
696 acct_name, ACB_WSTRUST,
697 acct_flags, &user_pol,
699 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
700 if (!(r->in.join_flags &
701 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
706 if (NT_STATUS_IS_OK(status)) {
707 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
711 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
712 &domain_pol, flags, 1,
714 &num_rids, &user_rids, &name_types);
715 if (!NT_STATUS_IS_OK(status)) {
719 if (name_types[0] != SID_NAME_USER) {
720 status = NT_STATUS_INVALID_WORKSTATION;
724 user_rid = user_rids[0];
726 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
727 SEC_RIGHTS_MAXIMUM_ALLOWED, user_rid,
729 if (!NT_STATUS_IS_OK(status)) {
733 E_md4hash(r->in.machine_password, md4_trust_password);
734 encode_pw_buffer(pwbuf, r->in.machine_password, STR_UNICODE);
736 generate_random_buffer((uint8*)md5buffer, sizeof(md5buffer));
737 digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
740 MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
741 MD5Update(&md5ctx, cli->user_session_key.data,
742 cli->user_session_key.length);
743 MD5Final(digested_session_key.data, &md5ctx);
745 SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
746 memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
748 acb_info |= ACB_PWNOEXP;
749 if (r->out.domain_is_ad) {
750 #if !defined(ENCTYPE_ARCFOUR_HMAC)
751 acb_info |= ACB_USE_DES_KEY_ONLY;
759 fields_present = ACCT_NT_PWD_SET | ACCT_LM_PWD_SET | ACCT_FLAGS;
760 init_sam_user_info25P(&p25, fields_present, acb_info, (char *)pwbuf);
762 ctr.switch_value = infolevel;
763 ctr.info.id25 = &p25;
765 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol,
766 infolevel, &cli->user_session_key,
768 if (!NT_STATUS_IS_OK(status)) {
772 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
773 cli_rpc_pipe_close(pipe_hnd);
775 status = NT_STATUS_OK;
784 /****************************************************************
785 ****************************************************************/
787 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
788 struct libnet_UnjoinCtx *r)
790 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
794 if (!secrets_delete_domain_sid(lp_workgroup())) {
801 /****************************************************************
802 ****************************************************************/
804 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
805 struct libnet_UnjoinCtx *r)
807 struct cli_state *cli = NULL;
808 struct rpc_pipe_client *pipe_hnd = NULL;
809 POLICY_HND sam_pol, domain_pol, user_pol;
810 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
812 uint32 flags = 0x3e8;
813 const char *const_acct_name;
815 uint32 num_rids, *name_types, *user_rids;
816 SAM_USERINFO_CTR ctr, *qctr = NULL;
817 SAM_USER_INFO_16 p16;
819 status = cli_full_connection(&cli, NULL,
825 r->in.admin_password,
828 if (!NT_STATUS_IS_OK(status)) {
832 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
837 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
838 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
839 if (!NT_STATUS_IS_OK(status)) {
843 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
844 SEC_RIGHTS_MAXIMUM_ALLOWED,
847 if (!NT_STATUS_IS_OK(status)) {
851 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
852 strlower_m(acct_name);
853 const_acct_name = acct_name;
855 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
856 &domain_pol, flags, 1,
858 &num_rids, &user_rids, &name_types);
859 if (!NT_STATUS_IS_OK(status)) {
863 if (name_types[0] != SID_NAME_USER) {
864 status = NT_STATUS_INVALID_WORKSTATION;
868 user_rid = user_rids[0];
870 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
871 SEC_RIGHTS_MAXIMUM_ALLOWED,
872 user_rid, &user_pol);
873 if (!NT_STATUS_IS_OK(status)) {
877 status = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx,
878 &user_pol, 16, &qctr);
879 if (!NT_STATUS_IS_OK(status)) {
880 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
885 ctr.switch_value = 16;
886 ctr.info.id16 = &p16;
888 p16.acb_info = qctr->info.id16->acb_info | ACB_DISABLED;
890 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol, 16,
891 &cli->user_session_key, &ctr);
893 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
897 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
898 rpccli_samr_close(pipe_hnd, mem_ctx, &sam_pol);
899 cli_rpc_pipe_close(pipe_hnd);
909 /****************************************************************
910 ****************************************************************/
912 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
915 struct libnet_conf_ctx *ctx;
917 werr = libnet_conf_open(r, &ctx);
918 if (!W_ERROR_IS_OK(werr)) {
922 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
924 werr = libnet_conf_set_global_parameter(ctx, "security", "user");
925 if (!W_ERROR_IS_OK(werr)) {
929 werr = libnet_conf_set_global_parameter(ctx, "workgroup",
934 werr = libnet_conf_set_global_parameter(ctx, "security", "domain");
935 if (!W_ERROR_IS_OK(werr)) {
939 werr = libnet_conf_set_global_parameter(ctx, "workgroup",
940 r->out.netbios_domain_name);
941 if (!W_ERROR_IS_OK(werr)) {
945 if (r->out.domain_is_ad) {
946 werr = libnet_conf_set_global_parameter(ctx, "security", "ads");
947 if (!W_ERROR_IS_OK(werr)) {
951 werr = libnet_conf_set_global_parameter(ctx, "realm",
952 r->out.dns_domain_name);
956 libnet_conf_close(ctx);
960 /****************************************************************
961 ****************************************************************/
963 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
965 WERROR werr = WERR_OK;
966 struct libnet_conf_ctx *ctx;
968 werr = libnet_conf_open(r, &ctx);
969 if (!W_ERROR_IS_OK(werr)) {
973 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
975 werr = libnet_conf_set_global_parameter(ctx, "security", "user");
976 if (!W_ERROR_IS_OK(werr)) {
981 libnet_conf_delete_global_parameter(ctx, "realm");
984 libnet_conf_close(ctx);
988 /****************************************************************
989 ****************************************************************/
991 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
995 if (!W_ERROR_IS_OK(r->out.result)) {
996 return r->out.result;
999 if (!r->in.modify_config) {
1003 werr = do_join_modify_vals_config(r);
1004 if (!W_ERROR_IS_OK(werr)) {
1008 r->out.modified_config = true;
1009 r->out.result = werr;
1014 /****************************************************************
1015 ****************************************************************/
1017 static WERROR do_UnjoinConfig(struct libnet_UnjoinCtx *r)
1021 if (!W_ERROR_IS_OK(r->out.result)) {
1022 return r->out.result;
1025 if (!r->in.modify_config) {
1029 werr = do_unjoin_modify_vals_config(r);
1030 if (!W_ERROR_IS_OK(werr)) {
1034 r->out.modified_config = true;
1035 r->out.result = werr;
1040 /****************************************************************
1041 ****************************************************************/
1043 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1044 struct libnet_JoinCtx *r)
1047 if (!r->in.domain_name) {
1048 return WERR_INVALID_PARAM;
1051 if (r->in.modify_config && !lp_config_backend_is_registry()) {
1052 return WERR_NOT_SUPPORTED;
1056 return WERR_SETUP_DOMAIN_CONTROLLER;
1059 if (!secrets_init()) {
1060 libnet_join_set_error_string(mem_ctx, r,
1061 "Unable to open secrets database");
1062 return WERR_CAN_NOT_COMPLETE;
1068 /****************************************************************
1069 ****************************************************************/
1071 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1072 struct libnet_JoinCtx *r)
1076 if (!W_ERROR_IS_OK(r->out.result)) {
1077 return r->out.result;
1080 werr = do_JoinConfig(r);
1081 if (!W_ERROR_IS_OK(werr)) {
1085 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1086 saf_store(r->in.domain_name, r->in.dc_name);
1092 /****************************************************************
1093 ****************************************************************/
1095 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1098 ads_destroy(&r->in.ads);
1104 /****************************************************************
1105 ****************************************************************/
1107 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1110 ads_destroy(&r->in.ads);
1116 /****************************************************************
1117 ****************************************************************/
1119 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1120 struct libnet_JoinCtx **r)
1122 struct libnet_JoinCtx *ctx;
1124 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1129 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1131 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1132 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1139 /****************************************************************
1140 ****************************************************************/
1142 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1143 struct libnet_UnjoinCtx **r)
1145 struct libnet_UnjoinCtx *ctx;
1147 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1152 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1154 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1155 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1162 /****************************************************************
1163 ****************************************************************/
1165 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1166 struct libnet_JoinCtx *r)
1170 ADS_STATUS ads_status;
1171 #endif /* WITH_ADS */
1173 if (!r->in.dc_name) {
1174 struct DS_DOMAIN_CONTROLLER_INFO *info;
1175 status = dsgetdcname(mem_ctx,
1179 DS_DIRECTORY_SERVICE_REQUIRED |
1180 DS_WRITABLE_REQUIRED |
1183 if (!NT_STATUS_IS_OK(status)) {
1184 libnet_join_set_error_string(mem_ctx, r,
1185 "failed to find DC for domain %s",
1187 get_friendly_nt_error_msg(status));
1188 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1191 r->in.dc_name = talloc_strdup(mem_ctx,
1192 info->domain_controller_name);
1193 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1197 if (r->in.account_ou) {
1199 ads_status = libnet_join_connect_ads(mem_ctx, r);
1200 if (!ADS_ERR_OK(ads_status)) {
1201 return WERR_DEFAULT_JOIN_REQUIRED;
1204 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1205 if (!ADS_ERR_OK(ads_status)) {
1206 libnet_join_set_error_string(mem_ctx, r,
1207 "failed to precreate account in ou %s: %s",
1209 ads_errstr(ads_status));
1210 return WERR_DEFAULT_JOIN_REQUIRED;
1213 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1215 #endif /* WITH_ADS */
1217 status = libnet_join_joindomain_rpc(mem_ctx, r);
1218 if (!NT_STATUS_IS_OK(status)) {
1219 libnet_join_set_error_string(mem_ctx, r,
1220 "failed to join domain over rpc: %s",
1221 get_friendly_nt_error_msg(status));
1222 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1223 return WERR_SETUP_ALREADY_JOINED;
1225 return ntstatus_to_werror(status);
1228 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1229 return WERR_SETUP_NOT_JOINED;
1233 if (r->out.domain_is_ad) {
1234 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1235 if (!ADS_ERR_OK(ads_status)) {
1236 return WERR_GENERAL_FAILURE;
1239 #endif /* WITH_ADS */
1244 /****************************************************************
1245 ****************************************************************/
1247 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1248 struct libnet_JoinCtx *r)
1253 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1256 werr = libnet_join_pre_processing(mem_ctx, r);
1257 if (!W_ERROR_IS_OK(werr)) {
1261 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1262 werr = libnet_DomainJoin(mem_ctx, r);
1263 if (!W_ERROR_IS_OK(werr)) {
1268 werr = libnet_join_post_processing(mem_ctx, r);
1269 if (!W_ERROR_IS_OK(werr)) {
1273 r->out.result = werr;
1276 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1281 /****************************************************************
1282 ****************************************************************/
1284 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1285 struct libnet_UnjoinCtx *r)
1289 if (!r->in.domain_sid) {
1291 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1292 libnet_unjoin_set_error_string(mem_ctx, r,
1293 "Unable to fetch domain sid: are we joined?");
1294 return WERR_SETUP_NOT_JOINED;
1296 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
1297 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
1300 if (!r->in.dc_name) {
1301 struct DS_DOMAIN_CONTROLLER_INFO *info;
1302 status = dsgetdcname(mem_ctx,
1306 DS_DIRECTORY_SERVICE_REQUIRED |
1307 DS_WRITABLE_REQUIRED |
1310 if (!NT_STATUS_IS_OK(status)) {
1311 libnet_unjoin_set_error_string(mem_ctx, r,
1312 "failed to find DC for domain %s",
1314 get_friendly_nt_error_msg(status));
1315 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1318 r->in.dc_name = talloc_strdup(mem_ctx,
1319 info->domain_controller_name);
1320 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1323 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
1324 if (!NT_STATUS_IS_OK(status)) {
1325 libnet_unjoin_set_error_string(mem_ctx, r,
1326 "failed to disable machine account via rpc: %s",
1327 get_friendly_nt_error_msg(status));
1328 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1329 return WERR_SETUP_NOT_JOINED;
1331 return ntstatus_to_werror(status);
1335 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
1336 ADS_STATUS ads_status;
1337 libnet_unjoin_connect_ads(mem_ctx, r);
1338 ads_status = libnet_unjoin_remove_machine_acct(mem_ctx, r);
1339 if (!ADS_ERR_OK(ads_status)) {
1340 libnet_unjoin_set_error_string(mem_ctx, r,
1341 "failed to remove machine account from AD: %s",
1342 ads_errstr(ads_status));
1345 #endif /* WITH_ADS */
1347 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
1352 /****************************************************************
1353 ****************************************************************/
1355 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
1356 struct libnet_UnjoinCtx *r)
1358 if (r->in.modify_config && !lp_config_backend_is_registry()) {
1359 return WERR_NOT_SUPPORTED;
1362 if (!secrets_init()) {
1363 libnet_unjoin_set_error_string(mem_ctx, r,
1364 "Unable to open secrets database");
1365 return WERR_CAN_NOT_COMPLETE;
1372 /****************************************************************
1373 ****************************************************************/
1375 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
1376 struct libnet_UnjoinCtx *r)
1381 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
1384 werr = libnet_unjoin_pre_processing(mem_ctx, r);
1385 if (!W_ERROR_IS_OK(werr)) {
1389 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1390 werr = libnet_DomainUnjoin(mem_ctx, r);
1391 if (!W_ERROR_IS_OK(werr)) {
1396 werr = do_UnjoinConfig(r);
1397 if (!W_ERROR_IS_OK(werr)) {
1402 r->out.result = werr;
1405 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);