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/>.
23 #include "librpc/gen_ndr/ndr_libnet_join.h"
24 #include "libnet/libnet_join.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_lsa_c.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/ndr_netlogon.h"
31 #include "rpc_client/cli_netlogon.h"
32 #include "lib/smbconf/smbconf.h"
33 #include "lib/smbconf/smbconf_reg.h"
34 #include "../libds/common/flags.h"
36 #include "rpc_client/init_lsa.h"
37 #include "rpc_client/cli_pipe.h"
38 #include "../libcli/security/security.h"
40 #include "libsmb/libsmb.h"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/param/loadparm.h"
43 #include "libcli/auth/netlogon_creds_cli.h"
44 #include "auth/credentials/credentials.h"
47 /****************************************************************
48 ****************************************************************/
50 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
53 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
54 DEBUG(1,("libnet_Join:\n%s", str)); \
58 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
59 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
60 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
61 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
63 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
66 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
67 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
71 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
72 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
73 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
74 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
76 /****************************************************************
77 ****************************************************************/
79 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
80 struct libnet_JoinCtx *r,
81 const char *format, ...)
82 PRINTF_ATTRIBUTE(3,4);
84 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
85 struct libnet_JoinCtx *r,
86 const char *format, ...)
90 if (r->out.error_string) {
94 va_start(args, format);
95 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
99 /****************************************************************
100 ****************************************************************/
102 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
103 struct libnet_UnjoinCtx *r,
104 const char *format, ...)
105 PRINTF_ATTRIBUTE(3,4);
107 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
108 struct libnet_UnjoinCtx *r,
109 const char *format, ...)
113 if (r->out.error_string) {
117 va_start(args, format);
118 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
124 /****************************************************************
125 ****************************************************************/
127 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
128 const char *netbios_domain_name,
130 const char *user_name,
131 const char *password,
136 ADS_STRUCT *my_ads = NULL;
139 my_ads = ads_init(dns_domain_name,
143 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
147 SAFE_FREE(my_ads->auth.user_name);
148 my_ads->auth.user_name = SMB_STRDUP(user_name);
149 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
151 SAFE_FREE(my_ads->auth.realm);
152 my_ads->auth.realm = smb_xstrdup(cp);
153 if (!strupper_m(my_ads->auth.realm)) {
154 ads_destroy(&my_ads);
155 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
161 SAFE_FREE(my_ads->auth.password);
162 my_ads->auth.password = SMB_STRDUP(password);
165 if (ccname != NULL) {
166 SAFE_FREE(my_ads->auth.ccache_name);
167 my_ads->auth.ccache_name = SMB_STRDUP(ccname);
168 setenv(KRB5_ENV_CCNAME, my_ads->auth.ccache_name, 1);
171 status = ads_connect_user_creds(my_ads);
172 if (!ADS_ERR_OK(status)) {
173 ads_destroy(&my_ads);
181 /****************************************************************
182 ****************************************************************/
184 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
185 struct libnet_JoinCtx *r,
186 bool use_machine_creds)
189 const char *username;
190 const char *password;
191 const char *ccname = NULL;
193 if (use_machine_creds) {
194 if (r->in.machine_name == NULL ||
195 r->in.machine_password == NULL) {
196 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
198 username = talloc_strdup(mem_ctx, r->in.machine_name);
199 if (username == NULL) {
200 return ADS_ERROR(LDAP_NO_MEMORY);
202 if (username[strlen(username)] != '$') {
203 username = talloc_asprintf(username, "%s$", username);
204 if (username == NULL) {
205 return ADS_ERROR(LDAP_NO_MEMORY);
208 password = r->in.machine_password;
209 ccname = "MEMORY:libnet_join_machine_creds";
211 username = r->in.admin_account;
212 password = r->in.admin_password;
215 * when r->in.use_kerberos is set to allow "net ads join -k" we
216 * may not override the provided credential cache - gd
219 if (!r->in.use_kerberos) {
220 ccname = "MEMORY:libnet_join_user_creds";
224 status = libnet_connect_ads(r->out.dns_domain_name,
225 r->out.netbios_domain_name,
231 if (!ADS_ERR_OK(status)) {
232 libnet_join_set_error_string(mem_ctx, r,
233 "failed to connect to AD: %s",
238 if (!r->out.netbios_domain_name) {
239 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
240 r->in.ads->server.workgroup);
241 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
244 if (!r->out.dns_domain_name) {
245 r->out.dns_domain_name = talloc_strdup(mem_ctx,
246 r->in.ads->config.realm);
247 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
250 r->out.domain_is_ad = true;
255 /****************************************************************
256 ****************************************************************/
258 static ADS_STATUS libnet_join_connect_ads_user(TALLOC_CTX *mem_ctx,
259 struct libnet_JoinCtx *r)
261 return libnet_join_connect_ads(mem_ctx, r, false);
264 /****************************************************************
265 ****************************************************************/
267 static ADS_STATUS libnet_join_connect_ads_machine(TALLOC_CTX *mem_ctx,
268 struct libnet_JoinCtx *r)
270 return libnet_join_connect_ads(mem_ctx, r, true);
273 /****************************************************************
274 ****************************************************************/
276 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
277 struct libnet_UnjoinCtx *r)
281 status = libnet_connect_ads(r->in.domain_name,
285 r->in.admin_password,
288 if (!ADS_ERR_OK(status)) {
289 libnet_unjoin_set_error_string(mem_ctx, r,
290 "failed to connect to AD: %s",
297 /****************************************************************
298 join a domain using ADS (LDAP mods)
299 ****************************************************************/
301 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
302 struct libnet_JoinCtx *r)
305 LDAPMessage *res = NULL;
306 const char *attrs[] = { "dn", NULL };
309 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
310 if (!ADS_ERR_OK(status)) {
314 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
315 if (!ADS_ERR_OK(status)) {
319 if (ads_count_replies(r->in.ads, res) != 1) {
320 ads_msgfree(r->in.ads, res);
321 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
324 ads_msgfree(r->in.ads, res);
326 /* Attempt to create the machine account and bail if this fails.
327 Assume that the admin wants exactly what they requested */
329 status = ads_create_machine_acct(r->in.ads,
332 r->in.desired_encryption_types);
334 if (ADS_ERR_OK(status)) {
335 DEBUG(1,("machine account creation created\n"));
337 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
338 (status.err.rc == LDAP_ALREADY_EXISTS)) {
339 status = ADS_SUCCESS;
342 if (!ADS_ERR_OK(status)) {
343 DEBUG(1,("machine account creation failed\n"));
347 status = ads_move_machine_acct(r->in.ads,
351 if (!ADS_ERR_OK(status)) {
352 DEBUG(1,("failure to locate/move pre-existing "
353 "machine account\n"));
357 DEBUG(1,("The machine account %s the specified OU.\n",
358 moved ? "was moved into" : "already exists in"));
363 /****************************************************************
364 ****************************************************************/
366 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
367 struct libnet_UnjoinCtx *r)
372 status = libnet_unjoin_connect_ads(mem_ctx, r);
373 if (!ADS_ERR_OK(status)) {
374 libnet_unjoin_set_error_string(mem_ctx, r,
375 "failed to connect to AD: %s",
381 status = ads_leave_realm(r->in.ads, r->in.machine_name);
382 if (!ADS_ERR_OK(status)) {
383 libnet_unjoin_set_error_string(mem_ctx, r,
384 "failed to leave realm: %s",
392 /****************************************************************
393 ****************************************************************/
395 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
396 struct libnet_JoinCtx *r)
399 LDAPMessage *res = NULL;
402 if (!r->in.machine_name) {
403 return ADS_ERROR(LDAP_NO_MEMORY);
406 status = ads_find_machine_acct(r->in.ads,
409 if (!ADS_ERR_OK(status)) {
413 if (ads_count_replies(r->in.ads, res) != 1) {
414 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
418 dn = ads_get_dn(r->in.ads, mem_ctx, res);
420 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
424 r->out.dn = talloc_strdup(mem_ctx, dn);
426 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
430 if (!ads_pull_uint32(r->in.ads, res, "msDS-SupportedEncryptionTypes",
431 &r->out.set_encryption_types)) {
432 r->out.set_encryption_types = 0;
436 ads_msgfree(r->in.ads, res);
442 static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
443 struct libnet_JoinCtx *r,
449 if (r->in.machine_name == NULL) {
450 return ADS_ERROR_SYSTEM(EINVAL);
453 status = ads_get_service_principal_names(mem_ctx,
462 /****************************************************************
463 Set a machines dNSHostName and servicePrincipalName attributes
464 ****************************************************************/
466 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
467 struct libnet_JoinCtx *r)
472 const char **spn_array = NULL;
476 const char **netbios_aliases = NULL;
480 status = libnet_join_find_machine_acct(mem_ctx, r);
481 if (!ADS_ERR_OK(status)) {
485 status = libnet_join_get_machine_spns(mem_ctx,
487 discard_const_p(char **, &spn_array),
489 if (!ADS_ERR_OK(status)) {
490 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
493 /* Windows only creates HOST/shortname & HOST/fqdn. */
495 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
497 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
499 if (!strupper_m(spn)) {
500 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
503 ok = ads_element_in_array(spn_array, num_spns, spn);
505 ok = add_string_to_array(spn_array, spn,
506 &spn_array, &num_spns);
508 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
512 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
513 || (strchr(my_fqdn, '.') == NULL)) {
514 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
515 r->out.dns_domain_name);
518 if (!strlower_m(my_fqdn)) {
519 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
522 if (!strequal(my_fqdn, r->in.machine_name)) {
523 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
525 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
528 ok = ads_element_in_array(spn_array, num_spns, spn);
530 ok = add_string_to_array(spn_array, spn,
531 &spn_array, &num_spns);
533 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
538 netbios_aliases = lp_netbios_aliases();
539 if (netbios_aliases != NULL) {
540 for (; *netbios_aliases != NULL; netbios_aliases++) {
542 * Add HOST/NETBIOSNAME
544 spn = talloc_asprintf(mem_ctx, "HOST/%s", *netbios_aliases);
547 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
549 if (!strupper_m(spn)) {
551 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
554 ok = ads_element_in_array(spn_array, num_spns, spn);
559 ok = add_string_to_array(spn_array, spn,
560 &spn_array, &num_spns);
563 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
568 * Add HOST/netbiosname.domainname
570 if (r->out.dns_domain_name == NULL) {
573 fstr_sprintf(my_fqdn, "%s.%s",
575 r->out.dns_domain_name);
577 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
579 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
582 ok = ads_element_in_array(spn_array, num_spns, spn);
587 ok = add_string_to_array(spn_array, spn,
588 &spn_array, &num_spns);
591 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
597 /* make sure to NULL terminate the array */
598 spn_array = talloc_realloc(mem_ctx, spn_array, const char *, num_spns + 1);
599 if (spn_array == NULL) {
600 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
602 spn_array[num_spns] = NULL;
604 mods = ads_init_mods(mem_ctx);
606 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
609 /* fields of primary importance */
611 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
612 if (!ADS_ERR_OK(status)) {
613 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
616 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
618 if (!ADS_ERR_OK(status)) {
619 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
622 return ads_gen_mod(r->in.ads, r->out.dn, mods);
625 /****************************************************************
626 ****************************************************************/
628 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
629 struct libnet_JoinCtx *r)
634 if (!r->in.create_upn) {
640 status = libnet_join_find_machine_acct(mem_ctx, r);
641 if (!ADS_ERR_OK(status)) {
646 const char *realm = r->out.dns_domain_name;
648 /* in case we are about to generate a keytab during the join
649 * make sure the default upn we create is usable with kinit -k.
652 if (USE_KERBEROS_KEYTAB) {
653 realm = talloc_strdup_upper(mem_ctx,
654 r->out.dns_domain_name);
658 return ADS_ERROR(LDAP_NO_MEMORY);
661 r->in.upn = talloc_asprintf(mem_ctx,
666 return ADS_ERROR(LDAP_NO_MEMORY);
670 /* now do the mods */
672 mods = ads_init_mods(mem_ctx);
674 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
677 /* fields of primary importance */
679 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
680 if (!ADS_ERR_OK(status)) {
681 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
684 return ads_gen_mod(r->in.ads, r->out.dn, mods);
688 /****************************************************************
689 ****************************************************************/
691 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
692 struct libnet_JoinCtx *r)
698 if (!r->in.os_name || !r->in.os_version ) {
704 status = libnet_join_find_machine_acct(mem_ctx, r);
705 if (!ADS_ERR_OK(status)) {
709 /* now do the mods */
711 mods = ads_init_mods(mem_ctx);
713 return ADS_ERROR(LDAP_NO_MEMORY);
716 if (r->in.os_servicepack) {
718 * if blank string then leave os_sp equal to NULL to force
719 * attribute delete (LDAP_MOD_DELETE)
721 if (!strequal(r->in.os_servicepack,"")) {
722 os_sp = talloc_strdup(mem_ctx, r->in.os_servicepack);
725 os_sp = talloc_asprintf(mem_ctx, "Samba %s",
726 samba_version_string());
728 if (!os_sp && !strequal(r->in.os_servicepack,"")) {
729 return ADS_ERROR(LDAP_NO_MEMORY);
732 /* fields of primary importance */
734 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
736 if (!ADS_ERR_OK(status)) {
740 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
742 if (!ADS_ERR_OK(status)) {
746 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
748 if (!ADS_ERR_OK(status)) {
752 return ads_gen_mod(r->in.ads, r->out.dn, mods);
755 /****************************************************************
756 ****************************************************************/
758 static ADS_STATUS libnet_join_set_etypes(TALLOC_CTX *mem_ctx,
759 struct libnet_JoinCtx *r)
763 const char *etype_list_str;
765 etype_list_str = talloc_asprintf(mem_ctx, "%d",
766 r->in.desired_encryption_types);
767 if (!etype_list_str) {
768 return ADS_ERROR(LDAP_NO_MEMORY);
773 status = libnet_join_find_machine_acct(mem_ctx, r);
774 if (!ADS_ERR_OK(status)) {
778 if (r->in.desired_encryption_types == r->out.set_encryption_types) {
782 /* now do the mods */
784 mods = ads_init_mods(mem_ctx);
786 return ADS_ERROR(LDAP_NO_MEMORY);
789 status = ads_mod_str(mem_ctx, &mods, "msDS-SupportedEncryptionTypes",
791 if (!ADS_ERR_OK(status)) {
795 status = ads_gen_mod(r->in.ads, r->out.dn, mods);
796 if (!ADS_ERR_OK(status)) {
800 r->out.set_encryption_types = r->in.desired_encryption_types;
805 /****************************************************************
806 ****************************************************************/
808 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
809 struct libnet_JoinCtx *r)
811 if (!USE_SYSTEM_KEYTAB) {
815 if (ads_keytab_create_default(r->in.ads) != 0) {
822 /****************************************************************
823 ****************************************************************/
825 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
826 struct libnet_JoinCtx *r)
828 uint32_t domain_func;
830 const char *salt = NULL;
831 char *std_salt = NULL;
833 status = ads_domain_func_level(r->in.ads, &domain_func);
834 if (!ADS_ERR_OK(status)) {
835 libnet_join_set_error_string(mem_ctx, r,
836 "failed to determine domain functional level: %s",
841 /* go ahead and setup the default salt */
843 std_salt = kerberos_standard_des_salt();
845 libnet_join_set_error_string(mem_ctx, r,
846 "failed to obtain standard DES salt");
850 salt = talloc_strdup(mem_ctx, std_salt);
857 /* if it's a Windows functional domain, we have to look for the UPN */
859 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
862 upn = ads_get_upn(r->in.ads, mem_ctx,
865 salt = talloc_strdup(mem_ctx, upn);
872 return kerberos_secrets_store_des_salt(salt);
875 /****************************************************************
876 ****************************************************************/
878 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
879 struct libnet_JoinCtx *r)
882 bool need_etype_update = false;
885 status = libnet_join_connect_ads_user(mem_ctx, r);
886 if (!ADS_ERR_OK(status)) {
891 status = libnet_join_set_machine_spn(mem_ctx, r);
892 if (!ADS_ERR_OK(status)) {
893 libnet_join_set_error_string(mem_ctx, r,
894 "Failed to set machine spn: %s\n"
895 "Do you have sufficient permissions to create machine "
901 status = libnet_join_set_os_attributes(mem_ctx, r);
902 if (!ADS_ERR_OK(status)) {
903 libnet_join_set_error_string(mem_ctx, r,
904 "failed to set machine os attributes: %s",
909 status = libnet_join_set_machine_upn(mem_ctx, r);
910 if (!ADS_ERR_OK(status)) {
911 libnet_join_set_error_string(mem_ctx, r,
912 "failed to set machine upn: %s",
917 status = libnet_join_find_machine_acct(mem_ctx, r);
918 if (!ADS_ERR_OK(status)) {
922 if (r->in.desired_encryption_types != r->out.set_encryption_types) {
923 uint32_t func_level = 0;
925 status = ads_domain_func_level(r->in.ads, &func_level);
926 if (!ADS_ERR_OK(status)) {
927 libnet_join_set_error_string(mem_ctx, r,
928 "failed to query domain controller functional level: %s",
933 if (func_level >= DS_DOMAIN_FUNCTION_2008) {
934 need_etype_update = true;
938 if (need_etype_update) {
940 * We need to reconnect as machine account in order
941 * to update msDS-SupportedEncryptionTypes reliable
944 if (r->in.ads->auth.ccache_name != NULL) {
945 ads_kdestroy(r->in.ads->auth.ccache_name);
948 ads_destroy(&r->in.ads);
950 status = libnet_join_connect_ads_machine(mem_ctx, r);
951 if (!ADS_ERR_OK(status)) {
952 libnet_join_set_error_string(mem_ctx, r,
953 "Failed to connect as machine account: %s",
958 status = libnet_join_set_etypes(mem_ctx, r);
959 if (!ADS_ERR_OK(status)) {
960 libnet_join_set_error_string(mem_ctx, r,
961 "failed to set machine kerberos encryption types: %s",
967 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
968 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
971 if (!libnet_join_create_keytab(mem_ctx, r)) {
972 libnet_join_set_error_string(mem_ctx, r,
973 "failed to create kerberos keytab");
974 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
979 #endif /* HAVE_ADS */
981 /****************************************************************
982 Store the machine password and domain SID
983 ****************************************************************/
985 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
986 struct libnet_JoinCtx *r)
988 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
991 DEBUG(1,("Failed to save domain sid\n"));
995 if (!secrets_store_machine_password(r->in.machine_password,
996 r->out.netbios_domain_name,
997 r->in.secure_channel_type))
999 DEBUG(1,("Failed to save machine password\n"));
1006 /****************************************************************
1007 Connect dc's IPC$ share
1008 ****************************************************************/
1010 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
1015 struct cli_state **cli)
1020 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1023 if (use_kerberos && pass) {
1024 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1027 return cli_full_connection(cli, NULL,
1035 SMB_SIGNING_IPC_DEFAULT);
1038 /****************************************************************
1039 Lookup domain dc's info
1040 ****************************************************************/
1042 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
1043 struct libnet_JoinCtx *r,
1044 struct cli_state **cli)
1046 struct rpc_pipe_client *pipe_hnd = NULL;
1047 struct policy_handle lsa_pol;
1048 NTSTATUS status, result;
1049 union lsa_PolicyInformation *info = NULL;
1050 struct dcerpc_binding_handle *b;
1052 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1053 r->in.admin_account,
1055 r->in.admin_password,
1058 if (!NT_STATUS_IS_OK(status)) {
1062 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
1064 if (!NT_STATUS_IS_OK(status)) {
1065 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
1066 nt_errstr(status)));
1070 b = pipe_hnd->binding_handle;
1072 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
1073 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1074 if (!NT_STATUS_IS_OK(status)) {
1078 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
1080 LSA_POLICY_INFO_DNS,
1083 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
1084 r->out.domain_is_ad = true;
1085 r->out.netbios_domain_name = info->dns.name.string;
1086 r->out.dns_domain_name = info->dns.dns_domain.string;
1087 r->out.forest_name = info->dns.dns_forest.string;
1088 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
1089 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1092 if (!NT_STATUS_IS_OK(status)) {
1093 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
1095 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
1098 if (!NT_STATUS_IS_OK(status)) {
1101 if (!NT_STATUS_IS_OK(result)) {
1106 r->out.netbios_domain_name = info->account_domain.name.string;
1107 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
1108 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1111 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1112 TALLOC_FREE(pipe_hnd);
1118 /****************************************************************
1119 Do the domain join unsecure
1120 ****************************************************************/
1122 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
1123 struct libnet_JoinCtx *r,
1124 struct cli_state *cli)
1126 TALLOC_CTX *frame = talloc_stackframe();
1127 struct rpc_pipe_client *netlogon_pipe = NULL;
1128 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1129 struct samr_Password current_nt_hash;
1130 const char *account_name = NULL;
1133 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
1135 if (!NT_STATUS_IS_OK(status)) {
1140 if (!r->in.machine_password) {
1141 r->in.machine_password = generate_random_password(mem_ctx,
1142 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
1143 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
1144 if (r->in.machine_password == NULL) {
1146 return NT_STATUS_NO_MEMORY;
1150 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
1151 E_md4hash(r->in.admin_password, current_nt_hash.hash);
1153 account_name = talloc_asprintf(frame, "%s$",
1154 r->in.machine_name);
1155 if (account_name == NULL) {
1157 return NT_STATUS_NO_MEMORY;
1160 status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
1163 r->in.secure_channel_type,
1167 if (!NT_STATUS_IS_OK(status)) {
1172 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
1174 true, /* force_reauth */
1176 NULL); /* previous_nt_hash */
1177 if (!NT_STATUS_IS_OK(status)) {
1182 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
1183 netlogon_pipe->binding_handle,
1184 r->in.machine_password,
1185 NULL); /* new_version */
1186 if (!NT_STATUS_IS_OK(status)) {
1192 return NT_STATUS_OK;
1195 /****************************************************************
1197 ****************************************************************/
1199 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
1200 struct libnet_JoinCtx *r,
1201 struct cli_state *cli)
1203 struct rpc_pipe_client *pipe_hnd = NULL;
1204 struct policy_handle sam_pol, domain_pol, user_pol;
1205 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1207 struct lsa_String lsa_acct_name;
1209 uint32_t acct_flags = ACB_WSTRUST;
1210 struct samr_Ids user_rids;
1211 struct samr_Ids name_types;
1212 union samr_UserInfo user_info;
1213 struct dcerpc_binding_handle *b = NULL;
1214 unsigned int old_timeout = 0;
1216 DATA_BLOB session_key = data_blob_null;
1217 struct samr_CryptPassword crypt_pwd;
1218 struct samr_CryptPasswordEx crypt_pwd_ex;
1220 ZERO_STRUCT(sam_pol);
1221 ZERO_STRUCT(domain_pol);
1222 ZERO_STRUCT(user_pol);
1224 switch (r->in.secure_channel_type) {
1225 case SEC_CHAN_WKSTA:
1226 acct_flags = ACB_WSTRUST;
1229 acct_flags = ACB_SVRTRUST;
1232 return NT_STATUS_INVALID_PARAMETER;
1235 if (!r->in.machine_password) {
1236 r->in.machine_password = generate_random_password(mem_ctx,
1237 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
1238 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
1239 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
1242 /* Open the domain */
1244 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1246 if (!NT_STATUS_IS_OK(status)) {
1247 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1248 nt_errstr(status)));
1252 b = pipe_hnd->binding_handle;
1254 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1255 if (!NT_STATUS_IS_OK(status)) {
1256 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1257 nt_errstr(status)));
1261 status = dcerpc_samr_Connect2(b, mem_ctx,
1263 SAMR_ACCESS_ENUM_DOMAINS
1264 | SAMR_ACCESS_LOOKUP_DOMAIN,
1267 if (!NT_STATUS_IS_OK(status)) {
1270 if (!NT_STATUS_IS_OK(result)) {
1275 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1277 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1278 | SAMR_DOMAIN_ACCESS_CREATE_USER
1279 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1283 if (!NT_STATUS_IS_OK(status)) {
1286 if (!NT_STATUS_IS_OK(result)) {
1291 /* Create domain user */
1293 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1294 if (!strlower_m(acct_name)) {
1295 status = NT_STATUS_INVALID_PARAMETER;
1299 init_lsa_String(&lsa_acct_name, acct_name);
1301 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1302 uint32_t access_desired =
1303 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1304 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1305 SAMR_USER_ACCESS_SET_PASSWORD |
1306 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1307 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1308 uint32_t access_granted = 0;
1310 DEBUG(10,("Creating account with desired access mask: %d\n",
1313 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1322 if (!NT_STATUS_IS_OK(status)) {
1327 if (!NT_STATUS_IS_OK(status) &&
1328 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1330 DEBUG(10,("Creation of workstation account failed: %s\n",
1331 nt_errstr(status)));
1333 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1334 username/password combo but the user does not have
1335 administrator access. */
1337 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1338 libnet_join_set_error_string(mem_ctx, r,
1339 "User specified does not have "
1340 "administrator privileges");
1346 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1347 if (!(r->in.join_flags &
1348 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1353 /* We *must* do this.... don't ask... */
1355 if (NT_STATUS_IS_OK(status)) {
1356 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1360 status = dcerpc_samr_LookupNames(b, mem_ctx,
1367 if (!NT_STATUS_IS_OK(status)) {
1370 if (!NT_STATUS_IS_OK(result)) {
1374 if (user_rids.count != 1) {
1375 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1378 if (name_types.count != 1) {
1379 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1383 if (name_types.ids[0] != SID_NAME_USER) {
1384 DEBUG(0,("%s is not a user account (type=%d)\n",
1385 acct_name, name_types.ids[0]));
1386 status = NT_STATUS_INVALID_WORKSTATION;
1390 user_rid = user_rids.ids[0];
1392 /* Open handle on user */
1394 status = dcerpc_samr_OpenUser(b, mem_ctx,
1396 SEC_FLAG_MAXIMUM_ALLOWED,
1400 if (!NT_STATUS_IS_OK(status)) {
1403 if (!NT_STATUS_IS_OK(result)) {
1408 /* Fill in the additional account flags now */
1410 acct_flags |= ACB_PWNOEXP;
1412 /* Set account flags on machine account */
1413 ZERO_STRUCT(user_info.info16);
1414 user_info.info16.acct_flags = acct_flags;
1416 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1421 if (!NT_STATUS_IS_OK(status)) {
1422 dcerpc_samr_DeleteUser(b, mem_ctx,
1426 libnet_join_set_error_string(mem_ctx, r,
1427 "Failed to set account flags for machine account (%s)\n",
1432 if (!NT_STATUS_IS_OK(result)) {
1435 dcerpc_samr_DeleteUser(b, mem_ctx,
1439 libnet_join_set_error_string(mem_ctx, r,
1440 "Failed to set account flags for machine account (%s)\n",
1445 /* Set password on machine account - first try level 26 */
1448 * increase the timeout as password filter modules on the DC
1449 * might delay the operation for a significant amount of time
1451 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1453 init_samr_CryptPasswordEx(r->in.machine_password,
1457 user_info.info26.password = crypt_pwd_ex;
1458 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1460 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1466 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1468 /* retry with level 24 */
1470 init_samr_CryptPassword(r->in.machine_password,
1474 user_info.info24.password = crypt_pwd;
1475 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1477 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1484 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1486 if (!NT_STATUS_IS_OK(status)) {
1488 dcerpc_samr_DeleteUser(b, mem_ctx,
1492 libnet_join_set_error_string(mem_ctx, r,
1493 "Failed to set password for machine account (%s)\n",
1497 if (!NT_STATUS_IS_OK(result)) {
1500 dcerpc_samr_DeleteUser(b, mem_ctx,
1504 libnet_join_set_error_string(mem_ctx, r,
1505 "Failed to set password for machine account (%s)\n",
1510 status = NT_STATUS_OK;
1517 data_blob_clear_free(&session_key);
1519 if (is_valid_policy_hnd(&sam_pol)) {
1520 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1522 if (is_valid_policy_hnd(&domain_pol)) {
1523 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1525 if (is_valid_policy_hnd(&user_pol)) {
1526 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1528 TALLOC_FREE(pipe_hnd);
1533 /****************************************************************
1534 ****************************************************************/
1536 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1537 const char *netbios_domain_name,
1538 const char *dc_name,
1539 const bool use_kerberos)
1541 TALLOC_CTX *frame = talloc_stackframe();
1542 struct cli_state *cli = NULL;
1543 struct rpc_pipe_client *netlogon_pipe = NULL;
1544 struct cli_credentials *cli_creds = NULL;
1545 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1546 struct netlogon_creds_CredentialState *creds = NULL;
1547 uint32_t netlogon_flags = 0;
1549 const char *machine_account = NULL;
1550 const char *machine_domain = NULL;
1551 const char *machine_password = NULL;
1556 return NT_STATUS_INVALID_PARAMETER;
1559 if (!secrets_init()) {
1561 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1564 status = pdb_get_trust_credentials(netbios_domain_name, NULL,
1566 if (!NT_STATUS_IS_OK(status)) {
1571 /* we don't want any old password */
1572 cli_credentials_set_old_password(cli_creds, NULL, CRED_SPECIFIED);
1575 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1578 machine_account = cli_credentials_get_username(cli_creds);
1579 machine_domain = cli_credentials_get_domain(cli_creds);
1580 machine_password = cli_credentials_get_password(cli_creds);
1582 status = cli_full_connection(&cli, NULL,
1590 SMB_SIGNING_IPC_DEFAULT);
1592 if (!NT_STATUS_IS_OK(status)) {
1593 status = cli_full_connection(&cli, NULL,
1601 SMB_SIGNING_IPC_DEFAULT);
1604 if (!NT_STATUS_IS_OK(status)) {
1609 status = rpccli_create_netlogon_creds_with_creds(cli_creds,
1614 if (!NT_STATUS_IS_OK(status)) {
1620 status = rpccli_setup_netlogon_creds_with_creds(cli, NCACN_NP,
1622 true, /* force_reauth */
1624 if (!NT_STATUS_IS_OK(status)) {
1625 DEBUG(0,("connect_to_domain_password_server: "
1626 "unable to open the domain client session to "
1627 "machine %s. Flags[0x%08X] Error was : %s.\n",
1628 dc_name, (unsigned)netlogon_flags,
1629 nt_errstr(status)));
1635 status = netlogon_creds_cli_get(netlogon_creds,
1638 if (!NT_STATUS_IS_OK(status)) {
1643 netlogon_flags = creds->negotiate_flags;
1646 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1649 return NT_STATUS_OK;
1652 status = cli_rpc_pipe_open_schannel_with_creds(
1653 cli, &ndr_table_netlogon, NCACN_NP,
1655 netlogon_creds, &netlogon_pipe);
1657 TALLOC_FREE(netlogon_pipe);
1659 if (!NT_STATUS_IS_OK(status)) {
1660 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1661 "on netlogon pipe to server %s for domain %s. "
1663 smbXcli_conn_remote_name(cli->conn),
1664 netbios_domain_name, nt_errstr(status)));
1672 return NT_STATUS_OK;
1675 /****************************************************************
1676 ****************************************************************/
1678 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1679 struct libnet_JoinCtx *r)
1683 status = libnet_join_ok(r->in.msg_ctx,
1684 r->out.netbios_domain_name,
1686 r->in.use_kerberos);
1687 if (!NT_STATUS_IS_OK(status)) {
1688 libnet_join_set_error_string(mem_ctx, r,
1689 "failed to verify domain membership after joining: %s",
1690 get_friendly_nt_error_msg(status));
1691 return WERR_SETUP_NOT_JOINED;
1697 /****************************************************************
1698 ****************************************************************/
1700 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1701 struct libnet_UnjoinCtx *r)
1703 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1707 if (!secrets_delete_domain_sid(lp_workgroup())) {
1714 /****************************************************************
1715 ****************************************************************/
1717 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1718 struct libnet_UnjoinCtx *r)
1720 struct cli_state *cli = NULL;
1721 struct rpc_pipe_client *pipe_hnd = NULL;
1722 struct policy_handle sam_pol, domain_pol, user_pol;
1723 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1726 struct lsa_String lsa_acct_name;
1727 struct samr_Ids user_rids;
1728 struct samr_Ids name_types;
1729 union samr_UserInfo *info = NULL;
1730 struct dcerpc_binding_handle *b = NULL;
1732 ZERO_STRUCT(sam_pol);
1733 ZERO_STRUCT(domain_pol);
1734 ZERO_STRUCT(user_pol);
1736 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1737 r->in.admin_account,
1739 r->in.admin_password,
1742 if (!NT_STATUS_IS_OK(status)) {
1746 /* Open the domain */
1748 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1750 if (!NT_STATUS_IS_OK(status)) {
1751 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1752 nt_errstr(status)));
1756 b = pipe_hnd->binding_handle;
1758 status = dcerpc_samr_Connect2(b, mem_ctx,
1760 SEC_FLAG_MAXIMUM_ALLOWED,
1763 if (!NT_STATUS_IS_OK(status)) {
1766 if (!NT_STATUS_IS_OK(result)) {
1771 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1773 SEC_FLAG_MAXIMUM_ALLOWED,
1777 if (!NT_STATUS_IS_OK(status)) {
1780 if (!NT_STATUS_IS_OK(result)) {
1785 /* Create domain user */
1787 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1788 if (!strlower_m(acct_name)) {
1789 status = NT_STATUS_INVALID_PARAMETER;
1793 init_lsa_String(&lsa_acct_name, acct_name);
1795 status = dcerpc_samr_LookupNames(b, mem_ctx,
1803 if (!NT_STATUS_IS_OK(status)) {
1806 if (!NT_STATUS_IS_OK(result)) {
1810 if (user_rids.count != 1) {
1811 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1814 if (name_types.count != 1) {
1815 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1819 if (name_types.ids[0] != SID_NAME_USER) {
1820 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1821 name_types.ids[0]));
1822 status = NT_STATUS_INVALID_WORKSTATION;
1826 user_rid = user_rids.ids[0];
1828 /* Open handle on user */
1830 status = dcerpc_samr_OpenUser(b, mem_ctx,
1832 SEC_FLAG_MAXIMUM_ALLOWED,
1836 if (!NT_STATUS_IS_OK(status)) {
1839 if (!NT_STATUS_IS_OK(result)) {
1846 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1851 if (!NT_STATUS_IS_OK(status)) {
1852 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1855 if (!NT_STATUS_IS_OK(result)) {
1857 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1861 /* now disable and setuser info */
1863 info->info16.acct_flags |= ACB_DISABLED;
1865 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1870 if (!NT_STATUS_IS_OK(status)) {
1871 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1874 if (!NT_STATUS_IS_OK(result)) {
1876 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1880 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1883 if (pipe_hnd && b) {
1884 if (is_valid_policy_hnd(&domain_pol)) {
1885 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1887 if (is_valid_policy_hnd(&sam_pol)) {
1888 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1890 TALLOC_FREE(pipe_hnd);
1900 /****************************************************************
1901 ****************************************************************/
1903 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1905 WERROR werr = WERR_OK;
1907 struct smbconf_ctx *ctx;
1909 err = smbconf_init_reg(r, &ctx, NULL);
1910 if (!SBC_ERROR_IS_OK(err)) {
1911 werr = WERR_NO_SUCH_SERVICE;
1915 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1917 err = smbconf_set_global_parameter(ctx, "security", "user");
1918 if (!SBC_ERROR_IS_OK(err)) {
1919 werr = WERR_NO_SUCH_SERVICE;
1923 err = smbconf_set_global_parameter(ctx, "workgroup",
1925 if (!SBC_ERROR_IS_OK(err)) {
1926 werr = WERR_NO_SUCH_SERVICE;
1930 smbconf_delete_global_parameter(ctx, "realm");
1934 err = smbconf_set_global_parameter(ctx, "security", "domain");
1935 if (!SBC_ERROR_IS_OK(err)) {
1936 werr = WERR_NO_SUCH_SERVICE;
1940 err = smbconf_set_global_parameter(ctx, "workgroup",
1941 r->out.netbios_domain_name);
1942 if (!SBC_ERROR_IS_OK(err)) {
1943 werr = WERR_NO_SUCH_SERVICE;
1947 if (r->out.domain_is_ad) {
1948 err = smbconf_set_global_parameter(ctx, "security", "ads");
1949 if (!SBC_ERROR_IS_OK(err)) {
1950 werr = WERR_NO_SUCH_SERVICE;
1954 err = smbconf_set_global_parameter(ctx, "realm",
1955 r->out.dns_domain_name);
1956 if (!SBC_ERROR_IS_OK(err)) {
1957 werr = WERR_NO_SUCH_SERVICE;
1963 smbconf_shutdown(ctx);
1967 /****************************************************************
1968 ****************************************************************/
1970 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1972 WERROR werr = WERR_OK;
1974 struct smbconf_ctx *ctx;
1976 err = smbconf_init_reg(r, &ctx, NULL);
1977 if (!SBC_ERROR_IS_OK(err)) {
1978 werr = WERR_NO_SUCH_SERVICE;
1982 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1984 err = smbconf_set_global_parameter(ctx, "security", "user");
1985 if (!SBC_ERROR_IS_OK(err)) {
1986 werr = WERR_NO_SUCH_SERVICE;
1990 err = smbconf_delete_global_parameter(ctx, "workgroup");
1991 if (!SBC_ERROR_IS_OK(err)) {
1992 werr = WERR_NO_SUCH_SERVICE;
1996 smbconf_delete_global_parameter(ctx, "realm");
2000 smbconf_shutdown(ctx);
2004 /****************************************************************
2005 ****************************************************************/
2007 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
2011 if (!W_ERROR_IS_OK(r->out.result)) {
2012 return r->out.result;
2015 if (!r->in.modify_config) {
2019 werr = do_join_modify_vals_config(r);
2020 if (!W_ERROR_IS_OK(werr)) {
2024 lp_load_global(get_dyn_CONFIGFILE());
2026 r->out.modified_config = true;
2027 r->out.result = werr;
2032 /****************************************************************
2033 ****************************************************************/
2035 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
2039 if (!W_ERROR_IS_OK(r->out.result)) {
2040 return r->out.result;
2043 if (!r->in.modify_config) {
2047 werr = do_unjoin_modify_vals_config(r);
2048 if (!W_ERROR_IS_OK(werr)) {
2052 lp_load_global(get_dyn_CONFIGFILE());
2054 r->out.modified_config = true;
2055 r->out.result = werr;
2060 /****************************************************************
2061 ****************************************************************/
2063 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
2064 const char *domain_str,
2065 const char **domain_p,
2068 char *domain = NULL;
2070 const char *p = NULL;
2072 if (!domain_str || !domain_p || !dc_p) {
2076 p = strchr_m(domain_str, '\\');
2079 domain = talloc_strndup(mem_ctx, domain_str,
2080 PTR_DIFF(p, domain_str));
2081 dc = talloc_strdup(mem_ctx, p+1);
2086 domain = talloc_strdup(mem_ctx, domain_str);
2102 /****************************************************************
2103 ****************************************************************/
2105 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
2106 struct libnet_JoinCtx *r)
2108 if (!r->in.domain_name) {
2109 libnet_join_set_error_string(mem_ctx, r,
2110 "No domain name defined");
2111 return WERR_INVALID_PARAM;
2114 if (strlen(r->in.machine_name) > 15) {
2115 libnet_join_set_error_string(mem_ctx, r,
2116 "Our netbios name can be at most 15 chars long, "
2117 "\"%s\" is %u chars long\n",
2119 (unsigned int)strlen(r->in.machine_name));
2120 return WERR_INVALID_PARAM;
2123 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2126 libnet_join_set_error_string(mem_ctx, r,
2127 "Failed to parse domain name");
2128 return WERR_INVALID_PARAM;
2131 if (!r->in.admin_domain) {
2132 char *admin_domain = NULL;
2133 char *admin_account = NULL;
2134 split_domain_user(mem_ctx,
2135 r->in.admin_account,
2138 r->in.admin_domain = admin_domain;
2139 r->in.admin_account = admin_account;
2142 if (!secrets_init()) {
2143 libnet_join_set_error_string(mem_ctx, r,
2144 "Unable to open secrets database");
2145 return WERR_CAN_NOT_COMPLETE;
2151 /****************************************************************
2152 ****************************************************************/
2154 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
2158 /* Try adding dom admins to builtin\admins. Only log failures. */
2159 status = create_builtin_administrators(domain_sid);
2160 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2161 DEBUG(10,("Unable to auto-add domain administrators to "
2162 "BUILTIN\\Administrators during join because "
2163 "winbindd must be running.\n"));
2164 } else if (!NT_STATUS_IS_OK(status)) {
2165 DEBUG(5, ("Failed to auto-add domain administrators to "
2166 "BUILTIN\\Administrators during join: %s\n",
2167 nt_errstr(status)));
2170 /* Try adding dom users to builtin\users. Only log failures. */
2171 status = create_builtin_users(domain_sid);
2172 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2173 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2174 "during join because winbindd must be running.\n"));
2175 } else if (!NT_STATUS_IS_OK(status)) {
2176 DEBUG(5, ("Failed to auto-add domain administrators to "
2177 "BUILTIN\\Administrators during join: %s\n",
2178 nt_errstr(status)));
2182 /****************************************************************
2183 ****************************************************************/
2185 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
2186 struct libnet_JoinCtx *r)
2190 if (!W_ERROR_IS_OK(r->out.result)) {
2191 return r->out.result;
2194 werr = do_JoinConfig(r);
2195 if (!W_ERROR_IS_OK(werr)) {
2199 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2203 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
2204 if (r->out.dns_domain_name) {
2205 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
2209 if (r->out.domain_is_ad &&
2210 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2211 ADS_STATUS ads_status;
2213 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
2214 if (!ADS_ERR_OK(ads_status)) {
2215 return WERR_GENERAL_FAILURE;
2218 #endif /* HAVE_ADS */
2220 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
2225 /****************************************************************
2226 ****************************************************************/
2228 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
2231 ads_destroy(&r->in.ads);
2237 /****************************************************************
2238 ****************************************************************/
2240 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2243 ads_destroy(&r->in.ads);
2249 /****************************************************************
2250 ****************************************************************/
2252 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2253 struct libnet_JoinCtx **r)
2255 struct libnet_JoinCtx *ctx;
2257 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2262 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2264 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2265 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2267 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2269 ctx->in.desired_encryption_types = ENC_CRC32 |
2272 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2273 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES128;
2275 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2276 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES256;
2284 /****************************************************************
2285 ****************************************************************/
2287 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2288 struct libnet_UnjoinCtx **r)
2290 struct libnet_UnjoinCtx *ctx;
2292 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2297 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2299 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2300 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2307 /****************************************************************
2308 ****************************************************************/
2310 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2311 struct libnet_JoinCtx *r)
2313 bool valid_security = false;
2314 bool valid_workgroup = false;
2315 bool valid_realm = false;
2316 bool ignored_realm = false;
2318 /* check if configuration is already set correctly */
2320 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2322 switch (r->out.domain_is_ad) {
2324 valid_security = (lp_security() == SEC_DOMAIN)
2325 || (lp_server_role() == ROLE_DOMAIN_PDC)
2326 || (lp_server_role() == ROLE_DOMAIN_BDC);
2327 if (valid_workgroup && valid_security) {
2328 /* nothing to be done */
2333 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2334 switch (lp_security()) {
2336 if (!valid_realm && lp_winbind_rpc_only()) {
2338 ignored_realm = true;
2342 valid_security = true;
2345 if (valid_workgroup && valid_realm && valid_security) {
2346 if (ignored_realm && !r->in.modify_config)
2348 libnet_join_set_error_string(mem_ctx, r,
2349 "Warning: ignoring realm when "
2350 "joining AD domain with "
2351 "'security=domain' and "
2352 "'winbind rpc only = yes'. "
2353 "(realm set to '%s', "
2354 "should be '%s').", lp_realm(),
2355 r->out.dns_domain_name);
2357 /* nothing to be done */
2363 /* check if we are supposed to manipulate configuration */
2365 if (!r->in.modify_config) {
2367 char *wrong_conf = talloc_strdup(mem_ctx, "");
2369 if (!valid_workgroup) {
2370 wrong_conf = talloc_asprintf_append(wrong_conf,
2371 "\"workgroup\" set to '%s', should be '%s'",
2372 lp_workgroup(), r->out.netbios_domain_name);
2373 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2377 wrong_conf = talloc_asprintf_append(wrong_conf,
2378 "\"realm\" set to '%s', should be '%s'",
2379 lp_realm(), r->out.dns_domain_name);
2380 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2383 if (!valid_security) {
2384 const char *sec = NULL;
2385 switch (lp_security()) {
2386 case SEC_USER: sec = "user"; break;
2387 case SEC_DOMAIN: sec = "domain"; break;
2388 case SEC_ADS: sec = "ads"; break;
2390 wrong_conf = talloc_asprintf_append(wrong_conf,
2391 "\"security\" set to '%s', should be %s",
2392 sec, r->out.domain_is_ad ?
2393 "either 'domain' or 'ads'" : "'domain'");
2394 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2397 libnet_join_set_error_string(mem_ctx, r,
2398 "Invalid configuration (%s) and configuration modification "
2399 "was not requested", wrong_conf);
2400 return WERR_CAN_NOT_COMPLETE;
2403 /* check if we are able to manipulate configuration */
2405 if (!lp_config_backend_is_registry()) {
2406 libnet_join_set_error_string(mem_ctx, r,
2407 "Configuration manipulation requested but not "
2408 "supported by backend");
2409 return WERR_NOT_SUPPORTED;
2415 /****************************************************************
2416 ****************************************************************/
2418 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2419 struct libnet_JoinCtx *r)
2423 struct cli_state *cli = NULL;
2425 ADS_STATUS ads_status;
2426 #endif /* HAVE_ADS */
2427 const char *pre_connect_realm = NULL;
2428 const char *numeric_dcip = NULL;
2429 const char *sitename = NULL;
2431 /* Before contacting a DC, we can securely know
2432 * the realm only if the user specifies it.
2434 if (r->in.use_kerberos &&
2435 r->in.domain_name_type == JoinDomNameTypeDNS) {
2436 pre_connect_realm = r->in.domain_name;
2439 if (!r->in.dc_name) {
2440 struct netr_DsRGetDCNameInfo *info;
2442 uint32_t name_type_flags = 0;
2443 if (r->in.domain_name_type == JoinDomNameTypeDNS) {
2444 name_type_flags = DS_IS_DNS_NAME;
2445 } else if (r->in.domain_name_type == JoinDomNameTypeNBT) {
2446 name_type_flags = DS_IS_FLAT_NAME;
2448 status = dsgetdcname(mem_ctx,
2453 DS_FORCE_REDISCOVERY |
2454 DS_DIRECTORY_SERVICE_REQUIRED |
2455 DS_WRITABLE_REQUIRED |
2456 DS_RETURN_DNS_NAME |
2459 if (!NT_STATUS_IS_OK(status)) {
2460 libnet_join_set_error_string(mem_ctx, r,
2461 "failed to find DC for domain %s - %s",
2463 get_friendly_nt_error_msg(status));
2464 return WERR_DCNOTFOUND;
2467 dc = strip_hostname(info->dc_unc);
2468 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2469 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2471 if (info->dc_address == NULL || info->dc_address[0] != '\\' ||
2472 info->dc_address[1] != '\\') {
2473 DBG_ERR("ill-formed DC address '%s'\n",
2475 return WERR_DCNOTFOUND;
2478 numeric_dcip = info->dc_address + 2;
2479 sitename = info->dc_site_name;
2480 /* info goes out of scope but the memory stays
2481 allocated on the talloc context */
2484 if (pre_connect_realm != NULL) {
2485 struct sockaddr_storage ss = {0};
2487 if (numeric_dcip != NULL) {
2488 if (!interpret_string_addr(&ss, numeric_dcip,
2491 "cannot parse IP address '%s' of DC '%s'\n",
2492 numeric_dcip, r->in.dc_name);
2493 return WERR_DCNOTFOUND;
2496 if (!interpret_string_addr(&ss, r->in.dc_name, 0)) {
2498 "cannot resolve IP address of DC '%s'\n",
2500 return WERR_DCNOTFOUND;
2504 /* The domain parameter is only used as modifier
2505 * to krb5.conf file name. .JOIN is is not a valid
2506 * NetBIOS name so it cannot clash with another domain
2509 create_local_private_krb5_conf_for_domain(
2510 pre_connect_realm, ".JOIN", sitename, &ss);
2513 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2514 if (!NT_STATUS_IS_OK(status)) {
2515 libnet_join_set_error_string(mem_ctx, r,
2516 "failed to lookup DC info for domain '%s' over rpc: %s",
2517 r->in.domain_name, get_friendly_nt_error_msg(status));
2518 return ntstatus_to_werror(status);
2521 werr = libnet_join_check_config(mem_ctx, r);
2522 if (!W_ERROR_IS_OK(werr)) {
2528 if (r->out.domain_is_ad) {
2529 create_local_private_krb5_conf_for_domain(
2530 r->out.dns_domain_name, r->out.netbios_domain_name,
2531 sitename, smbXcli_conn_remote_sockaddr(cli->conn));
2534 if (r->out.domain_is_ad &&
2535 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2537 const char *initial_account_ou = r->in.account_ou;
2540 * we want to create the msDS-SupportedEncryptionTypes attribute
2541 * as early as possible so always try an LDAP create as the user
2542 * first. We copy r->in.account_ou because it may be changed
2543 * during the machine pre-creation.
2546 ads_status = libnet_join_connect_ads_user(mem_ctx, r);
2547 if (!ADS_ERR_OK(ads_status)) {
2548 return WERR_DEFAULT_JOIN_REQUIRED;
2551 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2552 if (ADS_ERR_OK(ads_status)) {
2555 * LDAP object create succeeded, now go to the rpc
2556 * password set routines
2559 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2563 if (initial_account_ou != NULL) {
2564 libnet_join_set_error_string(mem_ctx, r,
2565 "failed to precreate account in ou %s: %s",
2567 ads_errstr(ads_status));
2568 return WERR_DEFAULT_JOIN_REQUIRED;
2571 DEBUG(5, ("failed to precreate account in ou %s: %s",
2572 r->in.account_ou, ads_errstr(ads_status)));
2574 #endif /* HAVE_ADS */
2577 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2578 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2579 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2581 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2583 if (!NT_STATUS_IS_OK(status)) {
2584 libnet_join_set_error_string(mem_ctx, r,
2585 "failed to join domain '%s' over rpc: %s",
2586 r->in.domain_name, get_friendly_nt_error_msg(status));
2587 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2588 return WERR_SETUP_ALREADY_JOINED;
2590 werr = ntstatus_to_werror(status);
2594 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2595 werr = WERR_SETUP_NOT_JOINED;
2609 /****************************************************************
2610 ****************************************************************/
2612 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2613 struct libnet_JoinCtx *r)
2616 struct libnet_UnjoinCtx *u = NULL;
2618 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2619 if (!W_ERROR_IS_OK(werr)) {
2623 u->in.debug = r->in.debug;
2624 u->in.dc_name = r->in.dc_name;
2625 u->in.domain_name = r->in.domain_name;
2626 u->in.admin_account = r->in.admin_account;
2627 u->in.admin_password = r->in.admin_password;
2628 u->in.modify_config = r->in.modify_config;
2629 u->in.use_kerberos = r->in.use_kerberos;
2630 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2631 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2633 werr = libnet_Unjoin(mem_ctx, u);
2639 /****************************************************************
2640 ****************************************************************/
2642 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2643 struct libnet_JoinCtx *r)
2648 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2651 ZERO_STRUCT(r->out);
2653 werr = libnet_join_pre_processing(mem_ctx, r);
2654 if (!W_ERROR_IS_OK(werr)) {
2658 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2659 werr = libnet_DomainJoin(mem_ctx, r);
2660 if (!W_ERROR_IS_OK(werr)) {
2665 werr = libnet_join_post_processing(mem_ctx, r);
2666 if (!W_ERROR_IS_OK(werr)) {
2670 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2671 werr = libnet_join_post_verify(mem_ctx, r);
2672 if (!W_ERROR_IS_OK(werr)) {
2673 libnet_join_rollback(mem_ctx, r);
2678 r->out.result = werr;
2681 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2686 /****************************************************************
2687 ****************************************************************/
2689 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2690 struct libnet_UnjoinCtx *r)
2694 if (!r->in.domain_sid) {
2696 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2697 libnet_unjoin_set_error_string(mem_ctx, r,
2698 "Unable to fetch domain sid: are we joined?");
2699 return WERR_SETUP_NOT_JOINED;
2701 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2702 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2705 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2706 !r->in.delete_machine_account) {
2707 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2711 if (!r->in.dc_name) {
2712 struct netr_DsRGetDCNameInfo *info;
2714 status = dsgetdcname(mem_ctx,
2719 DS_DIRECTORY_SERVICE_REQUIRED |
2720 DS_WRITABLE_REQUIRED |
2723 if (!NT_STATUS_IS_OK(status)) {
2724 libnet_unjoin_set_error_string(mem_ctx, r,
2725 "failed to find DC for domain %s - %s",
2727 get_friendly_nt_error_msg(status));
2728 return WERR_DCNOTFOUND;
2731 dc = strip_hostname(info->dc_unc);
2732 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2733 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2737 /* for net ads leave, try to delete the account. If it works,
2738 no sense in disabling. If it fails, we can still try to
2741 if (r->in.delete_machine_account) {
2742 ADS_STATUS ads_status;
2743 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2744 if (ADS_ERR_OK(ads_status)) {
2746 r->out.dns_domain_name =
2747 talloc_strdup(mem_ctx,
2748 r->in.ads->server.realm);
2750 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2752 if (!ADS_ERR_OK(ads_status)) {
2753 libnet_unjoin_set_error_string(mem_ctx, r,
2754 "failed to remove machine account from AD: %s",
2755 ads_errstr(ads_status));
2757 r->out.deleted_machine_account = true;
2758 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2759 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2763 #endif /* HAVE_ADS */
2765 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2767 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2768 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2769 if (!NT_STATUS_IS_OK(status)) {
2770 libnet_unjoin_set_error_string(mem_ctx, r,
2771 "failed to disable machine account via rpc: %s",
2772 get_friendly_nt_error_msg(status));
2773 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2774 return WERR_SETUP_NOT_JOINED;
2776 return ntstatus_to_werror(status);
2779 r->out.disabled_machine_account = true;
2782 /* If disable succeeded or was not requested at all, we
2783 should be getting rid of our end of things */
2785 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2790 /****************************************************************
2791 ****************************************************************/
2793 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2794 struct libnet_UnjoinCtx *r)
2796 if (!r->in.domain_name) {
2797 libnet_unjoin_set_error_string(mem_ctx, r,
2798 "No domain name defined");
2799 return WERR_INVALID_PARAM;
2802 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2805 libnet_unjoin_set_error_string(mem_ctx, r,
2806 "Failed to parse domain name");
2807 return WERR_INVALID_PARAM;
2811 return WERR_SETUP_DOMAIN_CONTROLLER;
2814 if (!r->in.admin_domain) {
2815 char *admin_domain = NULL;
2816 char *admin_account = NULL;
2817 split_domain_user(mem_ctx,
2818 r->in.admin_account,
2821 r->in.admin_domain = admin_domain;
2822 r->in.admin_account = admin_account;
2825 if (!secrets_init()) {
2826 libnet_unjoin_set_error_string(mem_ctx, r,
2827 "Unable to open secrets database");
2828 return WERR_CAN_NOT_COMPLETE;
2834 /****************************************************************
2835 ****************************************************************/
2837 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2838 struct libnet_UnjoinCtx *r)
2840 saf_delete(r->out.netbios_domain_name);
2841 saf_delete(r->out.dns_domain_name);
2843 return libnet_unjoin_config(r);
2846 /****************************************************************
2847 ****************************************************************/
2849 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2850 struct libnet_UnjoinCtx *r)
2855 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2858 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2859 if (!W_ERROR_IS_OK(werr)) {
2863 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2864 werr = libnet_DomainUnjoin(mem_ctx, r);
2865 if (!W_ERROR_IS_OK(werr)) {
2866 libnet_unjoin_config(r);
2871 werr = libnet_unjoin_post_processing(mem_ctx, r);
2872 if (!W_ERROR_IS_OK(werr)) {
2877 r->out.result = werr;
2880 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);