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, ...)
85 if (r->out.error_string) {
89 va_start(args, format);
90 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
94 /****************************************************************
95 ****************************************************************/
97 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
98 struct libnet_UnjoinCtx *r,
99 const char *format, ...)
103 if (r->out.error_string) {
107 va_start(args, format);
108 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
114 /****************************************************************
115 ****************************************************************/
117 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
118 const char *netbios_domain_name,
120 const char *user_name,
121 const char *password,
126 ADS_STRUCT *my_ads = NULL;
129 my_ads = ads_init(dns_domain_name,
133 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
137 SAFE_FREE(my_ads->auth.user_name);
138 my_ads->auth.user_name = SMB_STRDUP(user_name);
139 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
141 SAFE_FREE(my_ads->auth.realm);
142 my_ads->auth.realm = smb_xstrdup(cp);
143 if (!strupper_m(my_ads->auth.realm)) {
144 ads_destroy(&my_ads);
145 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
151 SAFE_FREE(my_ads->auth.password);
152 my_ads->auth.password = SMB_STRDUP(password);
155 if (ccname != NULL) {
156 SAFE_FREE(my_ads->auth.ccache_name);
157 my_ads->auth.ccache_name = SMB_STRDUP(ccname);
158 setenv(KRB5_ENV_CCNAME, my_ads->auth.ccache_name, 1);
161 status = ads_connect_user_creds(my_ads);
162 if (!ADS_ERR_OK(status)) {
163 ads_destroy(&my_ads);
171 /****************************************************************
172 ****************************************************************/
174 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
175 struct libnet_JoinCtx *r,
176 bool use_machine_creds)
179 const char *username;
180 const char *password;
181 const char *ccname = NULL;
183 if (use_machine_creds) {
184 if (r->in.machine_name == NULL ||
185 r->in.machine_password == NULL) {
186 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
188 username = talloc_strdup(mem_ctx, r->in.machine_name);
189 if (username == NULL) {
190 return ADS_ERROR(LDAP_NO_MEMORY);
192 if (username[strlen(username)] != '$') {
193 username = talloc_asprintf(username, "%s$", username);
194 if (username == NULL) {
195 return ADS_ERROR(LDAP_NO_MEMORY);
198 password = r->in.machine_password;
199 ccname = "MEMORY:libnet_join_machine_creds";
201 username = r->in.admin_account;
202 password = r->in.admin_password;
205 * when r->in.use_kerberos is set to allow "net ads join -k" we
206 * may not override the provided credential cache - gd
209 if (!r->in.use_kerberos) {
210 ccname = "MEMORY:libnet_join_user_creds";
214 status = libnet_connect_ads(r->out.dns_domain_name,
215 r->out.netbios_domain_name,
221 if (!ADS_ERR_OK(status)) {
222 libnet_join_set_error_string(mem_ctx, r,
223 "failed to connect to AD: %s",
228 if (!r->out.netbios_domain_name) {
229 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
230 r->in.ads->server.workgroup);
231 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
234 if (!r->out.dns_domain_name) {
235 r->out.dns_domain_name = talloc_strdup(mem_ctx,
236 r->in.ads->config.realm);
237 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
240 r->out.domain_is_ad = true;
245 /****************************************************************
246 ****************************************************************/
248 static ADS_STATUS libnet_join_connect_ads_user(TALLOC_CTX *mem_ctx,
249 struct libnet_JoinCtx *r)
251 return libnet_join_connect_ads(mem_ctx, r, false);
254 /****************************************************************
255 ****************************************************************/
257 static ADS_STATUS libnet_join_connect_ads_machine(TALLOC_CTX *mem_ctx,
258 struct libnet_JoinCtx *r)
260 return libnet_join_connect_ads(mem_ctx, r, true);
263 /****************************************************************
264 ****************************************************************/
266 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
267 struct libnet_UnjoinCtx *r)
271 status = libnet_connect_ads(r->in.domain_name,
275 r->in.admin_password,
278 if (!ADS_ERR_OK(status)) {
279 libnet_unjoin_set_error_string(mem_ctx, r,
280 "failed to connect to AD: %s",
287 /****************************************************************
288 join a domain using ADS (LDAP mods)
289 ****************************************************************/
291 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
292 struct libnet_JoinCtx *r)
295 LDAPMessage *res = NULL;
296 const char *attrs[] = { "dn", NULL };
299 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
300 if (!ADS_ERR_OK(status)) {
304 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
305 if (!ADS_ERR_OK(status)) {
309 if (ads_count_replies(r->in.ads, res) != 1) {
310 ads_msgfree(r->in.ads, res);
311 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
314 ads_msgfree(r->in.ads, res);
316 /* Attempt to create the machine account and bail if this fails.
317 Assume that the admin wants exactly what they requested */
319 status = ads_create_machine_acct(r->in.ads,
322 r->in.desired_encryption_types);
324 if (ADS_ERR_OK(status)) {
325 DEBUG(1,("machine account creation created\n"));
327 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
328 (status.err.rc == LDAP_ALREADY_EXISTS)) {
329 status = ADS_SUCCESS;
332 if (!ADS_ERR_OK(status)) {
333 DEBUG(1,("machine account creation failed\n"));
337 status = ads_move_machine_acct(r->in.ads,
341 if (!ADS_ERR_OK(status)) {
342 DEBUG(1,("failure to locate/move pre-existing "
343 "machine account\n"));
347 DEBUG(1,("The machine account %s the specified OU.\n",
348 moved ? "was moved into" : "already exists in"));
353 /****************************************************************
354 ****************************************************************/
356 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
357 struct libnet_UnjoinCtx *r)
362 status = libnet_unjoin_connect_ads(mem_ctx, r);
363 if (!ADS_ERR_OK(status)) {
364 libnet_unjoin_set_error_string(mem_ctx, r,
365 "failed to connect to AD: %s",
371 status = ads_leave_realm(r->in.ads, r->in.machine_name);
372 if (!ADS_ERR_OK(status)) {
373 libnet_unjoin_set_error_string(mem_ctx, r,
374 "failed to leave realm: %s",
382 /****************************************************************
383 ****************************************************************/
385 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
386 struct libnet_JoinCtx *r)
389 LDAPMessage *res = NULL;
392 if (!r->in.machine_name) {
393 return ADS_ERROR(LDAP_NO_MEMORY);
396 status = ads_find_machine_acct(r->in.ads,
399 if (!ADS_ERR_OK(status)) {
403 if (ads_count_replies(r->in.ads, res) != 1) {
404 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
408 dn = ads_get_dn(r->in.ads, mem_ctx, res);
410 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
414 r->out.dn = talloc_strdup(mem_ctx, dn);
416 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
420 if (!ads_pull_uint32(r->in.ads, res, "msDS-SupportedEncryptionTypes",
421 &r->out.set_encryption_types)) {
422 r->out.set_encryption_types = 0;
426 ads_msgfree(r->in.ads, res);
432 static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
433 struct libnet_JoinCtx *r,
439 if (r->in.machine_name == NULL) {
440 return ADS_ERROR_SYSTEM(EINVAL);
443 status = ads_get_service_principal_names(mem_ctx,
452 /****************************************************************
453 Set a machines dNSHostName and servicePrincipalName attributes
454 ****************************************************************/
456 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
457 struct libnet_JoinCtx *r)
462 const char **spn_array = NULL;
466 const char **netbios_aliases = NULL;
470 status = libnet_join_find_machine_acct(mem_ctx, r);
471 if (!ADS_ERR_OK(status)) {
475 status = libnet_join_get_machine_spns(mem_ctx,
477 discard_const_p(char **, &spn_array),
479 if (!ADS_ERR_OK(status)) {
480 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
483 /* Windows only creates HOST/shortname & HOST/fqdn. */
485 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
487 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
489 if (!strupper_m(spn)) {
490 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
493 ok = ads_element_in_array(spn_array, num_spns, spn);
495 ok = add_string_to_array(spn_array, spn,
496 &spn_array, &num_spns);
498 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
502 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
503 || (strchr(my_fqdn, '.') == NULL)) {
504 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
505 r->out.dns_domain_name);
508 if (!strlower_m(my_fqdn)) {
509 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
512 if (!strequal(my_fqdn, r->in.machine_name)) {
513 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
515 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
518 ok = ads_element_in_array(spn_array, num_spns, spn);
520 ok = add_string_to_array(spn_array, spn,
521 &spn_array, &num_spns);
523 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
528 netbios_aliases = lp_netbios_aliases();
529 if (netbios_aliases != NULL) {
530 for (; *netbios_aliases != NULL; netbios_aliases++) {
532 * Add HOST/NETBIOSNAME
534 spn = talloc_asprintf(mem_ctx, "HOST/%s", *netbios_aliases);
537 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
539 if (!strupper_m(spn)) {
541 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
544 ok = ads_element_in_array(spn_array, num_spns, spn);
549 ok = add_string_to_array(spn_array, spn,
550 &spn_array, &num_spns);
553 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
558 * Add HOST/netbiosname.domainname
560 if (r->out.dns_domain_name == NULL) {
563 fstr_sprintf(my_fqdn, "%s.%s",
565 r->out.dns_domain_name);
567 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
569 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
572 ok = ads_element_in_array(spn_array, num_spns, spn);
577 ok = add_string_to_array(spn_array, spn,
578 &spn_array, &num_spns);
581 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
587 /* make sure to NULL terminate the array */
588 spn_array = talloc_realloc(mem_ctx, spn_array, const char *, num_spns + 1);
589 if (spn_array == NULL) {
590 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
592 spn_array[num_spns] = NULL;
594 mods = ads_init_mods(mem_ctx);
596 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
599 /* fields of primary importance */
601 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
602 if (!ADS_ERR_OK(status)) {
603 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
606 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
608 if (!ADS_ERR_OK(status)) {
609 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
612 return ads_gen_mod(r->in.ads, r->out.dn, mods);
615 /****************************************************************
616 ****************************************************************/
618 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
619 struct libnet_JoinCtx *r)
624 if (!r->in.create_upn) {
630 status = libnet_join_find_machine_acct(mem_ctx, r);
631 if (!ADS_ERR_OK(status)) {
636 const char *realm = r->out.dns_domain_name;
638 /* in case we are about to generate a keytab during the join
639 * make sure the default upn we create is usable with kinit -k.
642 if (USE_KERBEROS_KEYTAB) {
643 realm = talloc_strdup_upper(mem_ctx,
644 r->out.dns_domain_name);
648 return ADS_ERROR(LDAP_NO_MEMORY);
651 r->in.upn = talloc_asprintf(mem_ctx,
656 return ADS_ERROR(LDAP_NO_MEMORY);
660 /* now do the mods */
662 mods = ads_init_mods(mem_ctx);
664 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
667 /* fields of primary importance */
669 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
670 if (!ADS_ERR_OK(status)) {
671 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
674 return ads_gen_mod(r->in.ads, r->out.dn, mods);
678 /****************************************************************
679 ****************************************************************/
681 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
682 struct libnet_JoinCtx *r)
688 if (!r->in.os_name || !r->in.os_version ) {
694 status = libnet_join_find_machine_acct(mem_ctx, r);
695 if (!ADS_ERR_OK(status)) {
699 /* now do the mods */
701 mods = ads_init_mods(mem_ctx);
703 return ADS_ERROR(LDAP_NO_MEMORY);
706 if (r->in.os_servicepack) {
708 * if blank string then leave os_sp equal to NULL to force
709 * attribute delete (LDAP_MOD_DELETE)
711 if (!strequal(r->in.os_servicepack,"")) {
712 os_sp = talloc_strdup(mem_ctx, r->in.os_servicepack);
715 os_sp = talloc_asprintf(mem_ctx, "Samba %s",
716 samba_version_string());
718 if (!os_sp && !strequal(r->in.os_servicepack,"")) {
719 return ADS_ERROR(LDAP_NO_MEMORY);
722 /* fields of primary importance */
724 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
726 if (!ADS_ERR_OK(status)) {
730 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
732 if (!ADS_ERR_OK(status)) {
736 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
738 if (!ADS_ERR_OK(status)) {
742 return ads_gen_mod(r->in.ads, r->out.dn, mods);
745 /****************************************************************
746 ****************************************************************/
748 static ADS_STATUS libnet_join_set_etypes(TALLOC_CTX *mem_ctx,
749 struct libnet_JoinCtx *r)
753 const char *etype_list_str;
755 etype_list_str = talloc_asprintf(mem_ctx, "%d",
756 r->in.desired_encryption_types);
757 if (!etype_list_str) {
758 return ADS_ERROR(LDAP_NO_MEMORY);
763 status = libnet_join_find_machine_acct(mem_ctx, r);
764 if (!ADS_ERR_OK(status)) {
768 if (r->in.desired_encryption_types == r->out.set_encryption_types) {
772 /* now do the mods */
774 mods = ads_init_mods(mem_ctx);
776 return ADS_ERROR(LDAP_NO_MEMORY);
779 status = ads_mod_str(mem_ctx, &mods, "msDS-SupportedEncryptionTypes",
781 if (!ADS_ERR_OK(status)) {
785 status = ads_gen_mod(r->in.ads, r->out.dn, mods);
786 if (!ADS_ERR_OK(status)) {
790 r->out.set_encryption_types = r->in.desired_encryption_types;
795 /****************************************************************
796 ****************************************************************/
798 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
799 struct libnet_JoinCtx *r)
801 if (!USE_SYSTEM_KEYTAB) {
805 if (ads_keytab_create_default(r->in.ads) != 0) {
812 /****************************************************************
813 ****************************************************************/
815 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
816 struct libnet_JoinCtx *r)
818 uint32_t domain_func;
820 const char *salt = NULL;
821 char *std_salt = NULL;
823 status = ads_domain_func_level(r->in.ads, &domain_func);
824 if (!ADS_ERR_OK(status)) {
825 libnet_join_set_error_string(mem_ctx, r,
826 "failed to determine domain functional level: %s",
831 /* go ahead and setup the default salt */
833 std_salt = kerberos_standard_des_salt();
835 libnet_join_set_error_string(mem_ctx, r,
836 "failed to obtain standard DES salt");
840 salt = talloc_strdup(mem_ctx, std_salt);
847 /* if it's a Windows functional domain, we have to look for the UPN */
849 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
852 upn = ads_get_upn(r->in.ads, mem_ctx,
855 salt = talloc_strdup(mem_ctx, upn);
862 return kerberos_secrets_store_des_salt(salt);
865 /****************************************************************
866 ****************************************************************/
868 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
869 struct libnet_JoinCtx *r)
872 bool need_etype_update = false;
875 status = libnet_join_connect_ads_user(mem_ctx, r);
876 if (!ADS_ERR_OK(status)) {
881 status = libnet_join_set_machine_spn(mem_ctx, r);
882 if (!ADS_ERR_OK(status)) {
883 libnet_join_set_error_string(mem_ctx, r,
884 "Failed to set machine spn: %s\n"
885 "Do you have sufficient permissions to create machine "
891 status = libnet_join_set_os_attributes(mem_ctx, r);
892 if (!ADS_ERR_OK(status)) {
893 libnet_join_set_error_string(mem_ctx, r,
894 "failed to set machine os attributes: %s",
899 status = libnet_join_set_machine_upn(mem_ctx, r);
900 if (!ADS_ERR_OK(status)) {
901 libnet_join_set_error_string(mem_ctx, r,
902 "failed to set machine upn: %s",
907 status = libnet_join_find_machine_acct(mem_ctx, r);
908 if (!ADS_ERR_OK(status)) {
912 if (r->in.desired_encryption_types != r->out.set_encryption_types) {
913 uint32_t func_level = 0;
915 status = ads_domain_func_level(r->in.ads, &func_level);
916 if (!ADS_ERR_OK(status)) {
917 libnet_join_set_error_string(mem_ctx, r,
918 "failed to query domain controller functional level: %s",
923 if (func_level >= DS_DOMAIN_FUNCTION_2008) {
924 need_etype_update = true;
928 if (need_etype_update) {
930 * We need to reconnect as machine account in order
931 * to update msDS-SupportedEncryptionTypes reliable
934 if (r->in.ads->auth.ccache_name != NULL) {
935 ads_kdestroy(r->in.ads->auth.ccache_name);
938 ads_destroy(&r->in.ads);
940 status = libnet_join_connect_ads_machine(mem_ctx, r);
941 if (!ADS_ERR_OK(status)) {
942 libnet_join_set_error_string(mem_ctx, r,
943 "Failed to connect as machine account: %s",
948 status = libnet_join_set_etypes(mem_ctx, r);
949 if (!ADS_ERR_OK(status)) {
950 libnet_join_set_error_string(mem_ctx, r,
951 "failed to set machine kerberos encryption types: %s",
957 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
958 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
961 if (!libnet_join_create_keytab(mem_ctx, r)) {
962 libnet_join_set_error_string(mem_ctx, r,
963 "failed to create kerberos keytab");
964 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
969 #endif /* HAVE_ADS */
971 /****************************************************************
972 Store the machine password and domain SID
973 ****************************************************************/
975 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
976 struct libnet_JoinCtx *r)
978 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
981 DEBUG(1,("Failed to save domain sid\n"));
985 if (!secrets_store_machine_password(r->in.machine_password,
986 r->out.netbios_domain_name,
987 r->in.secure_channel_type))
989 DEBUG(1,("Failed to save machine password\n"));
996 /****************************************************************
997 Connect dc's IPC$ share
998 ****************************************************************/
1000 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
1005 struct cli_state **cli)
1010 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1013 if (use_kerberos && pass) {
1014 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1017 return cli_full_connection(cli, NULL,
1025 SMB_SIGNING_IPC_DEFAULT);
1028 /****************************************************************
1029 Lookup domain dc's info
1030 ****************************************************************/
1032 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
1033 struct libnet_JoinCtx *r,
1034 struct cli_state **cli)
1036 struct rpc_pipe_client *pipe_hnd = NULL;
1037 struct policy_handle lsa_pol;
1038 NTSTATUS status, result;
1039 union lsa_PolicyInformation *info = NULL;
1040 struct dcerpc_binding_handle *b;
1042 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1043 r->in.admin_account,
1045 r->in.admin_password,
1048 if (!NT_STATUS_IS_OK(status)) {
1052 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
1054 if (!NT_STATUS_IS_OK(status)) {
1055 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
1056 nt_errstr(status)));
1060 b = pipe_hnd->binding_handle;
1062 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
1063 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1064 if (!NT_STATUS_IS_OK(status)) {
1068 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
1070 LSA_POLICY_INFO_DNS,
1073 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
1074 r->out.domain_is_ad = true;
1075 r->out.netbios_domain_name = info->dns.name.string;
1076 r->out.dns_domain_name = info->dns.dns_domain.string;
1077 r->out.forest_name = info->dns.dns_forest.string;
1078 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
1079 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1082 if (!NT_STATUS_IS_OK(status)) {
1083 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
1085 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
1088 if (!NT_STATUS_IS_OK(status)) {
1091 if (!NT_STATUS_IS_OK(result)) {
1096 r->out.netbios_domain_name = info->account_domain.name.string;
1097 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
1098 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1101 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1102 TALLOC_FREE(pipe_hnd);
1108 /****************************************************************
1109 Do the domain join unsecure
1110 ****************************************************************/
1112 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
1113 struct libnet_JoinCtx *r,
1114 struct cli_state *cli)
1116 TALLOC_CTX *frame = talloc_stackframe();
1117 struct rpc_pipe_client *netlogon_pipe = NULL;
1118 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1119 struct samr_Password current_nt_hash;
1120 const char *account_name = NULL;
1123 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
1125 if (!NT_STATUS_IS_OK(status)) {
1130 if (!r->in.machine_password) {
1131 r->in.machine_password = generate_random_password(mem_ctx,
1132 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
1133 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
1134 if (r->in.machine_password == NULL) {
1136 return NT_STATUS_NO_MEMORY;
1140 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
1141 E_md4hash(r->in.admin_password, current_nt_hash.hash);
1143 account_name = talloc_asprintf(frame, "%s$",
1144 r->in.machine_name);
1145 if (account_name == NULL) {
1147 return NT_STATUS_NO_MEMORY;
1150 status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
1153 r->in.secure_channel_type,
1157 if (!NT_STATUS_IS_OK(status)) {
1162 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
1164 true, /* force_reauth */
1166 NULL); /* previous_nt_hash */
1167 if (!NT_STATUS_IS_OK(status)) {
1172 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
1173 netlogon_pipe->binding_handle,
1174 r->in.machine_password,
1175 NULL); /* new_version */
1176 if (!NT_STATUS_IS_OK(status)) {
1182 return NT_STATUS_OK;
1185 /****************************************************************
1187 ****************************************************************/
1189 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
1190 struct libnet_JoinCtx *r,
1191 struct cli_state *cli)
1193 struct rpc_pipe_client *pipe_hnd = NULL;
1194 struct policy_handle sam_pol, domain_pol, user_pol;
1195 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1197 struct lsa_String lsa_acct_name;
1199 uint32_t acct_flags = ACB_WSTRUST;
1200 struct samr_Ids user_rids;
1201 struct samr_Ids name_types;
1202 union samr_UserInfo user_info;
1203 struct dcerpc_binding_handle *b = NULL;
1204 unsigned int old_timeout = 0;
1206 DATA_BLOB session_key = data_blob_null;
1207 struct samr_CryptPassword crypt_pwd;
1208 struct samr_CryptPasswordEx crypt_pwd_ex;
1210 ZERO_STRUCT(sam_pol);
1211 ZERO_STRUCT(domain_pol);
1212 ZERO_STRUCT(user_pol);
1214 switch (r->in.secure_channel_type) {
1215 case SEC_CHAN_WKSTA:
1216 acct_flags = ACB_WSTRUST;
1219 acct_flags = ACB_SVRTRUST;
1222 return NT_STATUS_INVALID_PARAMETER;
1225 if (!r->in.machine_password) {
1226 r->in.machine_password = generate_random_password(mem_ctx,
1227 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
1228 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
1229 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
1232 /* Open the domain */
1234 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1236 if (!NT_STATUS_IS_OK(status)) {
1237 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1238 nt_errstr(status)));
1242 b = pipe_hnd->binding_handle;
1244 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1245 if (!NT_STATUS_IS_OK(status)) {
1246 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1247 nt_errstr(status)));
1251 status = dcerpc_samr_Connect2(b, mem_ctx,
1253 SAMR_ACCESS_ENUM_DOMAINS
1254 | SAMR_ACCESS_LOOKUP_DOMAIN,
1257 if (!NT_STATUS_IS_OK(status)) {
1260 if (!NT_STATUS_IS_OK(result)) {
1265 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1267 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1268 | SAMR_DOMAIN_ACCESS_CREATE_USER
1269 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1273 if (!NT_STATUS_IS_OK(status)) {
1276 if (!NT_STATUS_IS_OK(result)) {
1281 /* Create domain user */
1283 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1284 if (!strlower_m(acct_name)) {
1285 status = NT_STATUS_INVALID_PARAMETER;
1289 init_lsa_String(&lsa_acct_name, acct_name);
1291 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1292 uint32_t access_desired =
1293 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1294 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1295 SAMR_USER_ACCESS_SET_PASSWORD |
1296 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1297 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1298 uint32_t access_granted = 0;
1300 DEBUG(10,("Creating account with desired access mask: %d\n",
1303 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1312 if (!NT_STATUS_IS_OK(status)) {
1317 if (!NT_STATUS_IS_OK(status) &&
1318 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1320 DEBUG(10,("Creation of workstation account failed: %s\n",
1321 nt_errstr(status)));
1323 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1324 username/password combo but the user does not have
1325 administrator access. */
1327 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1328 libnet_join_set_error_string(mem_ctx, r,
1329 "User specified does not have "
1330 "administrator privileges");
1336 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1337 if (!(r->in.join_flags &
1338 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1343 /* We *must* do this.... don't ask... */
1345 if (NT_STATUS_IS_OK(status)) {
1346 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1350 status = dcerpc_samr_LookupNames(b, mem_ctx,
1357 if (!NT_STATUS_IS_OK(status)) {
1360 if (!NT_STATUS_IS_OK(result)) {
1364 if (user_rids.count != 1) {
1365 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1368 if (name_types.count != 1) {
1369 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1373 if (name_types.ids[0] != SID_NAME_USER) {
1374 DEBUG(0,("%s is not a user account (type=%d)\n",
1375 acct_name, name_types.ids[0]));
1376 status = NT_STATUS_INVALID_WORKSTATION;
1380 user_rid = user_rids.ids[0];
1382 /* Open handle on user */
1384 status = dcerpc_samr_OpenUser(b, mem_ctx,
1386 SEC_FLAG_MAXIMUM_ALLOWED,
1390 if (!NT_STATUS_IS_OK(status)) {
1393 if (!NT_STATUS_IS_OK(result)) {
1398 /* Fill in the additional account flags now */
1400 acct_flags |= ACB_PWNOEXP;
1402 /* Set account flags on machine account */
1403 ZERO_STRUCT(user_info.info16);
1404 user_info.info16.acct_flags = acct_flags;
1406 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1411 if (!NT_STATUS_IS_OK(status)) {
1412 dcerpc_samr_DeleteUser(b, mem_ctx,
1416 libnet_join_set_error_string(mem_ctx, r,
1417 "Failed to set account flags for machine account (%s)\n",
1422 if (!NT_STATUS_IS_OK(result)) {
1425 dcerpc_samr_DeleteUser(b, mem_ctx,
1429 libnet_join_set_error_string(mem_ctx, r,
1430 "Failed to set account flags for machine account (%s)\n",
1435 /* Set password on machine account - first try level 26 */
1438 * increase the timeout as password filter modules on the DC
1439 * might delay the operation for a significant amount of time
1441 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1443 init_samr_CryptPasswordEx(r->in.machine_password,
1447 user_info.info26.password = crypt_pwd_ex;
1448 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1450 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1456 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1458 /* retry with level 24 */
1460 init_samr_CryptPassword(r->in.machine_password,
1464 user_info.info24.password = crypt_pwd;
1465 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1467 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1474 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1476 if (!NT_STATUS_IS_OK(status)) {
1478 dcerpc_samr_DeleteUser(b, mem_ctx,
1482 libnet_join_set_error_string(mem_ctx, r,
1483 "Failed to set password for machine account (%s)\n",
1487 if (!NT_STATUS_IS_OK(result)) {
1490 dcerpc_samr_DeleteUser(b, mem_ctx,
1494 libnet_join_set_error_string(mem_ctx, r,
1495 "Failed to set password for machine account (%s)\n",
1500 status = NT_STATUS_OK;
1507 data_blob_clear_free(&session_key);
1509 if (is_valid_policy_hnd(&sam_pol)) {
1510 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1512 if (is_valid_policy_hnd(&domain_pol)) {
1513 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1515 if (is_valid_policy_hnd(&user_pol)) {
1516 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1518 TALLOC_FREE(pipe_hnd);
1523 /****************************************************************
1524 ****************************************************************/
1526 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1527 const char *netbios_domain_name,
1528 const char *dc_name,
1529 const bool use_kerberos)
1531 TALLOC_CTX *frame = talloc_stackframe();
1532 struct cli_state *cli = NULL;
1533 struct rpc_pipe_client *netlogon_pipe = NULL;
1534 struct cli_credentials *cli_creds = NULL;
1535 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1536 struct netlogon_creds_CredentialState *creds = NULL;
1537 uint32_t netlogon_flags = 0;
1539 const char *machine_account = NULL;
1540 const char *machine_domain = NULL;
1541 const char *machine_password = NULL;
1546 return NT_STATUS_INVALID_PARAMETER;
1549 if (!secrets_init()) {
1551 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1554 status = pdb_get_trust_credentials(netbios_domain_name, NULL,
1556 if (!NT_STATUS_IS_OK(status)) {
1561 /* we don't want any old password */
1562 cli_credentials_set_old_password(cli_creds, NULL, CRED_SPECIFIED);
1565 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1568 machine_account = cli_credentials_get_username(cli_creds);
1569 machine_domain = cli_credentials_get_domain(cli_creds);
1570 machine_password = cli_credentials_get_password(cli_creds);
1572 status = cli_full_connection(&cli, NULL,
1580 SMB_SIGNING_IPC_DEFAULT);
1582 if (!NT_STATUS_IS_OK(status)) {
1583 status = cli_full_connection(&cli, NULL,
1591 SMB_SIGNING_IPC_DEFAULT);
1594 if (!NT_STATUS_IS_OK(status)) {
1599 status = rpccli_create_netlogon_creds_with_creds(cli_creds,
1604 if (!NT_STATUS_IS_OK(status)) {
1610 status = rpccli_setup_netlogon_creds_with_creds(cli, NCACN_NP,
1612 true, /* force_reauth */
1614 if (!NT_STATUS_IS_OK(status)) {
1615 DEBUG(0,("connect_to_domain_password_server: "
1616 "unable to open the domain client session to "
1617 "machine %s. Flags[0x%08X] Error was : %s.\n",
1618 dc_name, (unsigned)netlogon_flags,
1619 nt_errstr(status)));
1625 status = netlogon_creds_cli_get(netlogon_creds,
1628 if (!NT_STATUS_IS_OK(status)) {
1633 netlogon_flags = creds->negotiate_flags;
1636 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1639 return NT_STATUS_OK;
1642 status = cli_rpc_pipe_open_schannel_with_creds(
1643 cli, &ndr_table_netlogon, NCACN_NP,
1645 netlogon_creds, &netlogon_pipe);
1647 TALLOC_FREE(netlogon_pipe);
1649 if (!NT_STATUS_IS_OK(status)) {
1650 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1651 "on netlogon pipe to server %s for domain %s. "
1653 smbXcli_conn_remote_name(cli->conn),
1654 netbios_domain_name, nt_errstr(status)));
1662 return NT_STATUS_OK;
1665 /****************************************************************
1666 ****************************************************************/
1668 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1669 struct libnet_JoinCtx *r)
1673 status = libnet_join_ok(r->in.msg_ctx,
1674 r->out.netbios_domain_name,
1676 r->in.use_kerberos);
1677 if (!NT_STATUS_IS_OK(status)) {
1678 libnet_join_set_error_string(mem_ctx, r,
1679 "failed to verify domain membership after joining: %s",
1680 get_friendly_nt_error_msg(status));
1681 return WERR_SETUP_NOT_JOINED;
1687 /****************************************************************
1688 ****************************************************************/
1690 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1691 struct libnet_UnjoinCtx *r)
1693 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1697 if (!secrets_delete_domain_sid(lp_workgroup())) {
1704 /****************************************************************
1705 ****************************************************************/
1707 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1708 struct libnet_UnjoinCtx *r)
1710 struct cli_state *cli = NULL;
1711 struct rpc_pipe_client *pipe_hnd = NULL;
1712 struct policy_handle sam_pol, domain_pol, user_pol;
1713 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1716 struct lsa_String lsa_acct_name;
1717 struct samr_Ids user_rids;
1718 struct samr_Ids name_types;
1719 union samr_UserInfo *info = NULL;
1720 struct dcerpc_binding_handle *b = NULL;
1722 ZERO_STRUCT(sam_pol);
1723 ZERO_STRUCT(domain_pol);
1724 ZERO_STRUCT(user_pol);
1726 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1727 r->in.admin_account,
1729 r->in.admin_password,
1732 if (!NT_STATUS_IS_OK(status)) {
1736 /* Open the domain */
1738 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1740 if (!NT_STATUS_IS_OK(status)) {
1741 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1742 nt_errstr(status)));
1746 b = pipe_hnd->binding_handle;
1748 status = dcerpc_samr_Connect2(b, mem_ctx,
1750 SEC_FLAG_MAXIMUM_ALLOWED,
1753 if (!NT_STATUS_IS_OK(status)) {
1756 if (!NT_STATUS_IS_OK(result)) {
1761 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1763 SEC_FLAG_MAXIMUM_ALLOWED,
1767 if (!NT_STATUS_IS_OK(status)) {
1770 if (!NT_STATUS_IS_OK(result)) {
1775 /* Create domain user */
1777 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1778 if (!strlower_m(acct_name)) {
1779 status = NT_STATUS_INVALID_PARAMETER;
1783 init_lsa_String(&lsa_acct_name, acct_name);
1785 status = dcerpc_samr_LookupNames(b, mem_ctx,
1793 if (!NT_STATUS_IS_OK(status)) {
1796 if (!NT_STATUS_IS_OK(result)) {
1800 if (user_rids.count != 1) {
1801 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1804 if (name_types.count != 1) {
1805 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1809 if (name_types.ids[0] != SID_NAME_USER) {
1810 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1811 name_types.ids[0]));
1812 status = NT_STATUS_INVALID_WORKSTATION;
1816 user_rid = user_rids.ids[0];
1818 /* Open handle on user */
1820 status = dcerpc_samr_OpenUser(b, mem_ctx,
1822 SEC_FLAG_MAXIMUM_ALLOWED,
1826 if (!NT_STATUS_IS_OK(status)) {
1829 if (!NT_STATUS_IS_OK(result)) {
1836 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1841 if (!NT_STATUS_IS_OK(status)) {
1842 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1845 if (!NT_STATUS_IS_OK(result)) {
1847 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1851 /* now disable and setuser info */
1853 info->info16.acct_flags |= ACB_DISABLED;
1855 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1860 if (!NT_STATUS_IS_OK(status)) {
1861 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1864 if (!NT_STATUS_IS_OK(result)) {
1866 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1870 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1873 if (pipe_hnd && b) {
1874 if (is_valid_policy_hnd(&domain_pol)) {
1875 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1877 if (is_valid_policy_hnd(&sam_pol)) {
1878 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1880 TALLOC_FREE(pipe_hnd);
1890 /****************************************************************
1891 ****************************************************************/
1893 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1895 WERROR werr = WERR_OK;
1897 struct smbconf_ctx *ctx;
1899 err = smbconf_init_reg(r, &ctx, NULL);
1900 if (!SBC_ERROR_IS_OK(err)) {
1901 werr = WERR_NO_SUCH_SERVICE;
1905 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1907 err = smbconf_set_global_parameter(ctx, "security", "user");
1908 if (!SBC_ERROR_IS_OK(err)) {
1909 werr = WERR_NO_SUCH_SERVICE;
1913 err = smbconf_set_global_parameter(ctx, "workgroup",
1915 if (!SBC_ERROR_IS_OK(err)) {
1916 werr = WERR_NO_SUCH_SERVICE;
1920 smbconf_delete_global_parameter(ctx, "realm");
1924 err = smbconf_set_global_parameter(ctx, "security", "domain");
1925 if (!SBC_ERROR_IS_OK(err)) {
1926 werr = WERR_NO_SUCH_SERVICE;
1930 err = smbconf_set_global_parameter(ctx, "workgroup",
1931 r->out.netbios_domain_name);
1932 if (!SBC_ERROR_IS_OK(err)) {
1933 werr = WERR_NO_SUCH_SERVICE;
1937 if (r->out.domain_is_ad) {
1938 err = smbconf_set_global_parameter(ctx, "security", "ads");
1939 if (!SBC_ERROR_IS_OK(err)) {
1940 werr = WERR_NO_SUCH_SERVICE;
1944 err = smbconf_set_global_parameter(ctx, "realm",
1945 r->out.dns_domain_name);
1946 if (!SBC_ERROR_IS_OK(err)) {
1947 werr = WERR_NO_SUCH_SERVICE;
1953 smbconf_shutdown(ctx);
1957 /****************************************************************
1958 ****************************************************************/
1960 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1962 WERROR werr = WERR_OK;
1964 struct smbconf_ctx *ctx;
1966 err = smbconf_init_reg(r, &ctx, NULL);
1967 if (!SBC_ERROR_IS_OK(err)) {
1968 werr = WERR_NO_SUCH_SERVICE;
1972 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1974 err = smbconf_set_global_parameter(ctx, "security", "user");
1975 if (!SBC_ERROR_IS_OK(err)) {
1976 werr = WERR_NO_SUCH_SERVICE;
1980 err = smbconf_delete_global_parameter(ctx, "workgroup");
1981 if (!SBC_ERROR_IS_OK(err)) {
1982 werr = WERR_NO_SUCH_SERVICE;
1986 smbconf_delete_global_parameter(ctx, "realm");
1990 smbconf_shutdown(ctx);
1994 /****************************************************************
1995 ****************************************************************/
1997 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
2001 if (!W_ERROR_IS_OK(r->out.result)) {
2002 return r->out.result;
2005 if (!r->in.modify_config) {
2009 werr = do_join_modify_vals_config(r);
2010 if (!W_ERROR_IS_OK(werr)) {
2014 lp_load_global(get_dyn_CONFIGFILE());
2016 r->out.modified_config = true;
2017 r->out.result = werr;
2022 /****************************************************************
2023 ****************************************************************/
2025 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
2029 if (!W_ERROR_IS_OK(r->out.result)) {
2030 return r->out.result;
2033 if (!r->in.modify_config) {
2037 werr = do_unjoin_modify_vals_config(r);
2038 if (!W_ERROR_IS_OK(werr)) {
2042 lp_load_global(get_dyn_CONFIGFILE());
2044 r->out.modified_config = true;
2045 r->out.result = werr;
2050 /****************************************************************
2051 ****************************************************************/
2053 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
2054 const char *domain_str,
2055 const char **domain_p,
2058 char *domain = NULL;
2060 const char *p = NULL;
2062 if (!domain_str || !domain_p || !dc_p) {
2066 p = strchr_m(domain_str, '\\');
2069 domain = talloc_strndup(mem_ctx, domain_str,
2070 PTR_DIFF(p, domain_str));
2071 dc = talloc_strdup(mem_ctx, p+1);
2076 domain = talloc_strdup(mem_ctx, domain_str);
2092 /****************************************************************
2093 ****************************************************************/
2095 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
2096 struct libnet_JoinCtx *r)
2098 if (!r->in.domain_name) {
2099 libnet_join_set_error_string(mem_ctx, r,
2100 "No domain name defined");
2101 return WERR_INVALID_PARAM;
2104 if (strlen(r->in.machine_name) > 15) {
2105 libnet_join_set_error_string(mem_ctx, r,
2106 "Our netbios name can be at most 15 chars long, "
2107 "\"%s\" is %u chars long\n",
2109 (unsigned int)strlen(r->in.machine_name));
2110 return WERR_INVALID_PARAM;
2113 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2116 libnet_join_set_error_string(mem_ctx, r,
2117 "Failed to parse domain name");
2118 return WERR_INVALID_PARAM;
2121 if (!r->in.admin_domain) {
2122 char *admin_domain = NULL;
2123 char *admin_account = NULL;
2124 split_domain_user(mem_ctx,
2125 r->in.admin_account,
2128 r->in.admin_domain = admin_domain;
2129 r->in.admin_account = admin_account;
2132 if (!secrets_init()) {
2133 libnet_join_set_error_string(mem_ctx, r,
2134 "Unable to open secrets database");
2135 return WERR_CAN_NOT_COMPLETE;
2141 /****************************************************************
2142 ****************************************************************/
2144 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
2148 /* Try adding dom admins to builtin\admins. Only log failures. */
2149 status = create_builtin_administrators(domain_sid);
2150 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2151 DEBUG(10,("Unable to auto-add domain administrators to "
2152 "BUILTIN\\Administrators during join because "
2153 "winbindd must be running.\n"));
2154 } else if (!NT_STATUS_IS_OK(status)) {
2155 DEBUG(5, ("Failed to auto-add domain administrators to "
2156 "BUILTIN\\Administrators during join: %s\n",
2157 nt_errstr(status)));
2160 /* Try adding dom users to builtin\users. Only log failures. */
2161 status = create_builtin_users(domain_sid);
2162 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2163 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2164 "during join because winbindd must be running.\n"));
2165 } else if (!NT_STATUS_IS_OK(status)) {
2166 DEBUG(5, ("Failed to auto-add domain administrators to "
2167 "BUILTIN\\Administrators during join: %s\n",
2168 nt_errstr(status)));
2172 /****************************************************************
2173 ****************************************************************/
2175 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
2176 struct libnet_JoinCtx *r)
2180 if (!W_ERROR_IS_OK(r->out.result)) {
2181 return r->out.result;
2184 werr = do_JoinConfig(r);
2185 if (!W_ERROR_IS_OK(werr)) {
2189 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2193 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
2194 if (r->out.dns_domain_name) {
2195 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
2199 if (r->out.domain_is_ad &&
2200 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2201 ADS_STATUS ads_status;
2203 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
2204 if (!ADS_ERR_OK(ads_status)) {
2205 return WERR_GENERAL_FAILURE;
2208 #endif /* HAVE_ADS */
2210 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
2215 /****************************************************************
2216 ****************************************************************/
2218 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
2221 ads_destroy(&r->in.ads);
2227 /****************************************************************
2228 ****************************************************************/
2230 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2233 ads_destroy(&r->in.ads);
2239 /****************************************************************
2240 ****************************************************************/
2242 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2243 struct libnet_JoinCtx **r)
2245 struct libnet_JoinCtx *ctx;
2247 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2252 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2254 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2255 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2257 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2259 ctx->in.desired_encryption_types = ENC_CRC32 |
2262 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2263 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES128;
2265 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2266 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES256;
2274 /****************************************************************
2275 ****************************************************************/
2277 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2278 struct libnet_UnjoinCtx **r)
2280 struct libnet_UnjoinCtx *ctx;
2282 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2287 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2289 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2290 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2297 /****************************************************************
2298 ****************************************************************/
2300 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2301 struct libnet_JoinCtx *r)
2303 bool valid_security = false;
2304 bool valid_workgroup = false;
2305 bool valid_realm = false;
2307 /* check if configuration is already set correctly */
2309 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2311 switch (r->out.domain_is_ad) {
2313 valid_security = (lp_security() == SEC_DOMAIN)
2314 || (lp_server_role() == ROLE_DOMAIN_PDC)
2315 || (lp_server_role() == ROLE_DOMAIN_BDC);
2316 if (valid_workgroup && valid_security) {
2317 /* nothing to be done */
2322 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2323 switch (lp_security()) {
2326 valid_security = true;
2329 if (valid_workgroup && valid_realm && valid_security) {
2330 /* nothing to be done */
2336 /* check if we are supposed to manipulate configuration */
2338 if (!r->in.modify_config) {
2340 char *wrong_conf = talloc_strdup(mem_ctx, "");
2342 if (!valid_workgroup) {
2343 wrong_conf = talloc_asprintf_append(wrong_conf,
2344 "\"workgroup\" set to '%s', should be '%s'",
2345 lp_workgroup(), r->out.netbios_domain_name);
2346 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2350 wrong_conf = talloc_asprintf_append(wrong_conf,
2351 "\"realm\" set to '%s', should be '%s'",
2352 lp_realm(), r->out.dns_domain_name);
2353 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2356 if (!valid_security) {
2357 const char *sec = NULL;
2358 switch (lp_security()) {
2359 case SEC_USER: sec = "user"; break;
2360 case SEC_DOMAIN: sec = "domain"; break;
2361 case SEC_ADS: sec = "ads"; break;
2363 wrong_conf = talloc_asprintf_append(wrong_conf,
2364 "\"security\" set to '%s', should be %s",
2365 sec, r->out.domain_is_ad ?
2366 "either 'domain' or 'ads'" : "'domain'");
2367 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2371 * We should generate the warning for the special case when
2372 * domain is AD, "security = domain" and the realm parameter is
2375 if (lp_security() == SEC_DOMAIN &&
2376 r->out.domain_is_ad &&
2378 libnet_join_set_error_string(mem_ctx, r,
2379 "Warning: when joining AD domains with security=domain, "
2380 "\"realm\" should be defined in the configuration (%s) "
2381 "and configuration modification was not requested",
2386 libnet_join_set_error_string(mem_ctx, r,
2387 "Invalid configuration (%s) and configuration modification "
2388 "was not requested", wrong_conf);
2390 return WERR_CAN_NOT_COMPLETE;
2393 /* check if we are able to manipulate configuration */
2395 if (!lp_config_backend_is_registry()) {
2396 libnet_join_set_error_string(mem_ctx, r,
2397 "Configuration manipulation requested but not "
2398 "supported by backend");
2399 return WERR_NOT_SUPPORTED;
2405 /****************************************************************
2406 ****************************************************************/
2408 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2409 struct libnet_JoinCtx *r)
2413 struct cli_state *cli = NULL;
2415 ADS_STATUS ads_status;
2416 #endif /* HAVE_ADS */
2417 const char *pre_connect_realm = NULL;
2418 const char *numeric_dcip = NULL;
2419 const char *sitename = NULL;
2421 /* Before contacting a DC, we can securely know
2422 * the realm only if the user specifies it.
2424 if (r->in.use_kerberos &&
2425 r->in.domain_name_type == JoinDomNameTypeDNS) {
2426 pre_connect_realm = r->in.domain_name;
2429 if (!r->in.dc_name) {
2430 struct netr_DsRGetDCNameInfo *info;
2432 uint32_t name_type_flags = 0;
2433 if (r->in.domain_name_type == JoinDomNameTypeDNS) {
2434 name_type_flags = DS_IS_DNS_NAME;
2435 } else if (r->in.domain_name_type == JoinDomNameTypeNBT) {
2436 name_type_flags = DS_IS_FLAT_NAME;
2438 status = dsgetdcname(mem_ctx,
2443 DS_FORCE_REDISCOVERY |
2444 DS_DIRECTORY_SERVICE_REQUIRED |
2445 DS_WRITABLE_REQUIRED |
2446 DS_RETURN_DNS_NAME |
2449 if (!NT_STATUS_IS_OK(status)) {
2450 libnet_join_set_error_string(mem_ctx, r,
2451 "failed to find DC for domain %s",
2453 get_friendly_nt_error_msg(status));
2454 return WERR_DCNOTFOUND;
2457 dc = strip_hostname(info->dc_unc);
2458 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2459 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2461 if (info->dc_address == NULL || info->dc_address[0] != '\\' ||
2462 info->dc_address[1] != '\\') {
2463 DBG_ERR("ill-formed DC address '%s'\n",
2465 return WERR_DCNOTFOUND;
2468 numeric_dcip = info->dc_address + 2;
2469 sitename = info->dc_site_name;
2470 /* info goes out of scope but the memory stays
2471 allocated on the talloc context */
2474 if (pre_connect_realm != NULL) {
2475 struct sockaddr_storage ss = {0};
2477 if (numeric_dcip != NULL) {
2478 if (!interpret_string_addr(&ss, numeric_dcip,
2481 "cannot parse IP address '%s' of DC '%s'\n",
2482 numeric_dcip, r->in.dc_name);
2483 return WERR_DCNOTFOUND;
2486 if (!interpret_string_addr(&ss, r->in.dc_name, 0)) {
2488 "cannot resolve IP address of DC '%s'\n",
2490 return WERR_DCNOTFOUND;
2494 /* The domain parameter is only used as modifier
2495 * to krb5.conf file name. .JOIN is is not a valid
2496 * NetBIOS name so it cannot clash with another domain
2499 create_local_private_krb5_conf_for_domain(
2500 pre_connect_realm, ".JOIN", sitename, &ss);
2503 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2504 if (!NT_STATUS_IS_OK(status)) {
2505 libnet_join_set_error_string(mem_ctx, r,
2506 "failed to lookup DC info for domain '%s' over rpc: %s",
2507 r->in.domain_name, get_friendly_nt_error_msg(status));
2508 return ntstatus_to_werror(status);
2511 werr = libnet_join_check_config(mem_ctx, r);
2512 if (!W_ERROR_IS_OK(werr)) {
2518 if (r->out.domain_is_ad) {
2519 create_local_private_krb5_conf_for_domain(
2520 r->out.dns_domain_name, r->out.netbios_domain_name,
2521 sitename, smbXcli_conn_remote_sockaddr(cli->conn));
2524 if (r->out.domain_is_ad &&
2525 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2527 const char *initial_account_ou = r->in.account_ou;
2530 * we want to create the msDS-SupportedEncryptionTypes attribute
2531 * as early as possible so always try an LDAP create as the user
2532 * first. We copy r->in.account_ou because it may be changed
2533 * during the machine pre-creation.
2536 ads_status = libnet_join_connect_ads_user(mem_ctx, r);
2537 if (!ADS_ERR_OK(ads_status)) {
2538 return WERR_DEFAULT_JOIN_REQUIRED;
2541 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2542 if (ADS_ERR_OK(ads_status)) {
2545 * LDAP object create succeeded, now go to the rpc
2546 * password set routines
2549 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2553 if (initial_account_ou != NULL) {
2554 libnet_join_set_error_string(mem_ctx, r,
2555 "failed to precreate account in ou %s: %s",
2557 ads_errstr(ads_status));
2558 return WERR_DEFAULT_JOIN_REQUIRED;
2561 DEBUG(5, ("failed to precreate account in ou %s: %s",
2562 r->in.account_ou, ads_errstr(ads_status)));
2564 #endif /* HAVE_ADS */
2567 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2568 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2569 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2571 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2573 if (!NT_STATUS_IS_OK(status)) {
2574 libnet_join_set_error_string(mem_ctx, r,
2575 "failed to join domain '%s' over rpc: %s",
2576 r->in.domain_name, get_friendly_nt_error_msg(status));
2577 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2578 return WERR_SETUP_ALREADY_JOINED;
2580 werr = ntstatus_to_werror(status);
2584 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2585 werr = WERR_SETUP_NOT_JOINED;
2599 /****************************************************************
2600 ****************************************************************/
2602 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2603 struct libnet_JoinCtx *r)
2606 struct libnet_UnjoinCtx *u = NULL;
2608 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2609 if (!W_ERROR_IS_OK(werr)) {
2613 u->in.debug = r->in.debug;
2614 u->in.dc_name = r->in.dc_name;
2615 u->in.domain_name = r->in.domain_name;
2616 u->in.admin_account = r->in.admin_account;
2617 u->in.admin_password = r->in.admin_password;
2618 u->in.modify_config = r->in.modify_config;
2619 u->in.use_kerberos = r->in.use_kerberos;
2620 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2621 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2623 werr = libnet_Unjoin(mem_ctx, u);
2629 /****************************************************************
2630 ****************************************************************/
2632 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2633 struct libnet_JoinCtx *r)
2638 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2641 ZERO_STRUCT(r->out);
2643 werr = libnet_join_pre_processing(mem_ctx, r);
2644 if (!W_ERROR_IS_OK(werr)) {
2648 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2649 werr = libnet_DomainJoin(mem_ctx, r);
2650 if (!W_ERROR_IS_OK(werr)) {
2655 werr = libnet_join_post_processing(mem_ctx, r);
2656 if (!W_ERROR_IS_OK(werr)) {
2660 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2661 werr = libnet_join_post_verify(mem_ctx, r);
2662 if (!W_ERROR_IS_OK(werr)) {
2663 libnet_join_rollback(mem_ctx, r);
2668 r->out.result = werr;
2671 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2676 /****************************************************************
2677 ****************************************************************/
2679 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2680 struct libnet_UnjoinCtx *r)
2684 if (!r->in.domain_sid) {
2686 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2687 libnet_unjoin_set_error_string(mem_ctx, r,
2688 "Unable to fetch domain sid: are we joined?");
2689 return WERR_SETUP_NOT_JOINED;
2691 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2692 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2695 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2696 !r->in.delete_machine_account) {
2697 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2701 if (!r->in.dc_name) {
2702 struct netr_DsRGetDCNameInfo *info;
2704 status = dsgetdcname(mem_ctx,
2709 DS_DIRECTORY_SERVICE_REQUIRED |
2710 DS_WRITABLE_REQUIRED |
2713 if (!NT_STATUS_IS_OK(status)) {
2714 libnet_unjoin_set_error_string(mem_ctx, r,
2715 "failed to find DC for domain %s",
2717 get_friendly_nt_error_msg(status));
2718 return WERR_DCNOTFOUND;
2721 dc = strip_hostname(info->dc_unc);
2722 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2723 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2727 /* for net ads leave, try to delete the account. If it works,
2728 no sense in disabling. If it fails, we can still try to
2731 if (r->in.delete_machine_account) {
2732 ADS_STATUS ads_status;
2733 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2734 if (ADS_ERR_OK(ads_status)) {
2736 r->out.dns_domain_name =
2737 talloc_strdup(mem_ctx,
2738 r->in.ads->server.realm);
2740 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2742 if (!ADS_ERR_OK(ads_status)) {
2743 libnet_unjoin_set_error_string(mem_ctx, r,
2744 "failed to remove machine account from AD: %s",
2745 ads_errstr(ads_status));
2747 r->out.deleted_machine_account = true;
2748 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2749 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2753 #endif /* HAVE_ADS */
2755 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2757 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2758 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2759 if (!NT_STATUS_IS_OK(status)) {
2760 libnet_unjoin_set_error_string(mem_ctx, r,
2761 "failed to disable machine account via rpc: %s",
2762 get_friendly_nt_error_msg(status));
2763 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2764 return WERR_SETUP_NOT_JOINED;
2766 return ntstatus_to_werror(status);
2769 r->out.disabled_machine_account = true;
2772 /* If disable succeeded or was not requested at all, we
2773 should be getting rid of our end of things */
2775 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2780 /****************************************************************
2781 ****************************************************************/
2783 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2784 struct libnet_UnjoinCtx *r)
2786 if (!r->in.domain_name) {
2787 libnet_unjoin_set_error_string(mem_ctx, r,
2788 "No domain name defined");
2789 return WERR_INVALID_PARAM;
2792 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2795 libnet_unjoin_set_error_string(mem_ctx, r,
2796 "Failed to parse domain name");
2797 return WERR_INVALID_PARAM;
2801 return WERR_SETUP_DOMAIN_CONTROLLER;
2804 if (!r->in.admin_domain) {
2805 char *admin_domain = NULL;
2806 char *admin_account = NULL;
2807 split_domain_user(mem_ctx,
2808 r->in.admin_account,
2811 r->in.admin_domain = admin_domain;
2812 r->in.admin_account = admin_account;
2815 if (!secrets_init()) {
2816 libnet_unjoin_set_error_string(mem_ctx, r,
2817 "Unable to open secrets database");
2818 return WERR_CAN_NOT_COMPLETE;
2824 /****************************************************************
2825 ****************************************************************/
2827 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2828 struct libnet_UnjoinCtx *r)
2830 saf_delete(r->out.netbios_domain_name);
2831 saf_delete(r->out.dns_domain_name);
2833 return libnet_unjoin_config(r);
2836 /****************************************************************
2837 ****************************************************************/
2839 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2840 struct libnet_UnjoinCtx *r)
2845 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2848 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2849 if (!W_ERROR_IS_OK(werr)) {
2853 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2854 werr = libnet_DomainUnjoin(mem_ctx, r);
2855 if (!W_ERROR_IS_OK(werr)) {
2856 libnet_unjoin_config(r);
2861 werr = libnet_unjoin_post_processing(mem_ctx, r);
2862 if (!W_ERROR_IS_OK(werr)) {
2867 r->out.result = werr;
2870 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);