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 "libsmb/namequery.h"
24 #include "librpc/gen_ndr/ndr_libnet_join.h"
25 #include "libnet/libnet_join.h"
26 #include "libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/init_samr.h"
29 #include "../librpc/gen_ndr/ndr_lsa_c.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../librpc/gen_ndr/ndr_netlogon.h"
32 #include "rpc_client/cli_netlogon.h"
33 #include "lib/smbconf/smbconf.h"
34 #include "lib/smbconf/smbconf_reg.h"
35 #include "../libds/common/flags.h"
37 #include "rpc_client/init_lsa.h"
38 #include "rpc_client/cli_pipe.h"
39 #include "../libcli/security/security.h"
41 #include "libsmb/libsmb.h"
42 #include "../libcli/smb/smbXcli_base.h"
43 #include "lib/param/loadparm.h"
44 #include "libcli/auth/netlogon_creds_cli.h"
45 #include "auth/credentials/credentials.h"
47 #include "libsmb/dsgetdcname.h"
49 /****************************************************************
50 ****************************************************************/
52 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
55 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
56 DEBUG(1,("libnet_Join:\n%s", str)); \
60 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
61 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
62 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
63 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
65 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
68 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
69 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
73 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
74 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
75 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
76 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
78 /****************************************************************
79 ****************************************************************/
81 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
82 struct libnet_JoinCtx *r,
83 const char *format, ...)
84 PRINTF_ATTRIBUTE(3,4);
86 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
87 struct libnet_JoinCtx *r,
88 const char *format, ...)
92 if (r->out.error_string) {
96 va_start(args, format);
97 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
101 /****************************************************************
102 ****************************************************************/
104 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
105 struct libnet_UnjoinCtx *r,
106 const char *format, ...)
107 PRINTF_ATTRIBUTE(3,4);
109 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
110 struct libnet_UnjoinCtx *r,
111 const char *format, ...)
115 if (r->out.error_string) {
119 va_start(args, format);
120 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
126 /****************************************************************
127 ****************************************************************/
129 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
130 const char *netbios_domain_name,
132 const char *user_name,
133 const char *password,
138 ADS_STRUCT *my_ads = NULL;
141 my_ads = ads_init(dns_domain_name,
146 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
149 my_ads->auth.flags |= ADS_AUTH_ALLOW_NTLMSSP;
152 SAFE_FREE(my_ads->auth.user_name);
153 my_ads->auth.user_name = SMB_STRDUP(user_name);
154 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
156 SAFE_FREE(my_ads->auth.realm);
157 my_ads->auth.realm = smb_xstrdup(cp);
158 if (!strupper_m(my_ads->auth.realm)) {
159 ads_destroy(&my_ads);
160 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
166 SAFE_FREE(my_ads->auth.password);
167 my_ads->auth.password = SMB_STRDUP(password);
170 if (ccname != NULL) {
171 SAFE_FREE(my_ads->auth.ccache_name);
172 my_ads->auth.ccache_name = SMB_STRDUP(ccname);
173 setenv(KRB5_ENV_CCNAME, my_ads->auth.ccache_name, 1);
176 status = ads_connect_user_creds(my_ads);
177 if (!ADS_ERR_OK(status)) {
178 ads_destroy(&my_ads);
186 /****************************************************************
187 ****************************************************************/
189 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
190 struct libnet_JoinCtx *r,
191 bool use_machine_creds)
194 const char *username;
195 const char *password;
196 const char *ccname = NULL;
198 if (use_machine_creds) {
199 if (r->in.machine_name == NULL ||
200 r->in.machine_password == NULL) {
201 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
203 username = talloc_asprintf(mem_ctx, "%s$",
205 if (username == NULL) {
206 return ADS_ERROR(LDAP_NO_MEMORY);
208 password = r->in.machine_password;
209 ccname = "MEMORY:libnet_join_machine_creds";
213 username = r->in.admin_account;
215 p = strchr(r->in.admin_account, '@');
217 username = talloc_asprintf(mem_ctx, "%s@%s",
221 if (username == NULL) {
222 return ADS_ERROR(LDAP_NO_MEMORY);
224 password = r->in.admin_password;
227 * when r->in.use_kerberos is set to allow "net ads join -k" we
228 * may not override the provided credential cache - gd
231 if (!r->in.use_kerberos) {
232 ccname = "MEMORY:libnet_join_user_creds";
236 status = libnet_connect_ads(r->out.dns_domain_name,
237 r->out.netbios_domain_name,
243 if (!ADS_ERR_OK(status)) {
244 libnet_join_set_error_string(mem_ctx, r,
245 "failed to connect to AD: %s",
250 if (!r->out.netbios_domain_name) {
251 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
252 r->in.ads->server.workgroup);
253 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
256 if (!r->out.dns_domain_name) {
257 r->out.dns_domain_name = talloc_strdup(mem_ctx,
258 r->in.ads->config.realm);
259 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
262 r->out.domain_is_ad = true;
267 /****************************************************************
268 ****************************************************************/
270 static ADS_STATUS libnet_join_connect_ads_user(TALLOC_CTX *mem_ctx,
271 struct libnet_JoinCtx *r)
273 return libnet_join_connect_ads(mem_ctx, r, false);
276 /****************************************************************
277 ****************************************************************/
279 static ADS_STATUS libnet_join_connect_ads_machine(TALLOC_CTX *mem_ctx,
280 struct libnet_JoinCtx *r)
282 return libnet_join_connect_ads(mem_ctx, r, true);
285 /****************************************************************
286 ****************************************************************/
288 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
289 struct libnet_UnjoinCtx *r)
293 status = libnet_connect_ads(r->in.domain_name,
297 r->in.admin_password,
300 if (!ADS_ERR_OK(status)) {
301 libnet_unjoin_set_error_string(mem_ctx, r,
302 "failed to connect to AD: %s",
309 /****************************************************************
310 join a domain using ADS (LDAP mods)
311 ****************************************************************/
313 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
314 struct libnet_JoinCtx *r)
317 LDAPMessage *res = NULL;
318 const char *attrs[] = { "dn", NULL };
321 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
322 if (!ADS_ERR_OK(status)) {
326 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
327 if (!ADS_ERR_OK(status)) {
331 if (ads_count_replies(r->in.ads, res) != 1) {
332 ads_msgfree(r->in.ads, res);
333 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
336 ads_msgfree(r->in.ads, res);
338 /* Attempt to create the machine account and bail if this fails.
339 Assume that the admin wants exactly what they requested */
341 if (r->in.machine_password == NULL) {
342 r->in.machine_password =
343 trust_pw_new_value(mem_ctx,
344 r->in.secure_channel_type,
346 if (r->in.machine_password == NULL) {
347 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
351 status = ads_create_machine_acct(r->in.ads,
353 r->in.machine_password,
355 r->in.desired_encryption_types,
356 r->out.dns_domain_name);
358 if (ADS_ERR_OK(status)) {
359 DBG_WARNING("Machine account successfully created\n");
361 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
362 (status.err.rc == LDAP_ALREADY_EXISTS)) {
363 status = ADS_SUCCESS;
366 if (!ADS_ERR_OK(status)) {
367 DBG_WARNING("Failed to create machine account\n");
371 status = ads_move_machine_acct(r->in.ads,
375 if (!ADS_ERR_OK(status)) {
376 DEBUG(1,("failure to locate/move pre-existing "
377 "machine account\n"));
381 DEBUG(1,("The machine account %s the specified OU.\n",
382 moved ? "was moved into" : "already exists in"));
387 /****************************************************************
388 ****************************************************************/
390 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
391 struct libnet_UnjoinCtx *r)
396 status = libnet_unjoin_connect_ads(mem_ctx, r);
397 if (!ADS_ERR_OK(status)) {
398 libnet_unjoin_set_error_string(mem_ctx, r,
399 "failed to connect to AD: %s",
405 status = ads_leave_realm(r->in.ads, r->in.machine_name);
406 if (!ADS_ERR_OK(status)) {
407 libnet_unjoin_set_error_string(mem_ctx, r,
408 "failed to leave realm: %s",
416 /****************************************************************
417 ****************************************************************/
419 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
420 struct libnet_JoinCtx *r)
423 LDAPMessage *res = NULL;
426 if (!r->in.machine_name) {
427 return ADS_ERROR(LDAP_NO_MEMORY);
430 status = ads_find_machine_acct(r->in.ads,
433 if (!ADS_ERR_OK(status)) {
437 if (ads_count_replies(r->in.ads, res) != 1) {
438 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
442 dn = ads_get_dn(r->in.ads, mem_ctx, res);
444 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
448 r->out.dn = talloc_strdup(mem_ctx, dn);
450 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
454 if (!ads_pull_uint32(r->in.ads, res, "msDS-SupportedEncryptionTypes",
455 &r->out.set_encryption_types)) {
456 r->out.set_encryption_types = 0;
460 ads_msgfree(r->in.ads, res);
466 static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
467 struct libnet_JoinCtx *r,
473 if (r->in.machine_name == NULL) {
474 return ADS_ERROR_SYSTEM(EINVAL);
477 status = ads_get_service_principal_names(mem_ctx,
486 static ADS_STATUS add_uniq_spn(TALLOC_CTX *mem_ctx, const char *spn,
487 const char ***array, size_t *num)
489 bool ok = ads_element_in_array(*array, *num, spn);
491 ok = add_string_to_array(mem_ctx, spn, array, num);
493 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
499 /****************************************************************
500 Set a machines dNSHostName and servicePrincipalName attributes
501 ****************************************************************/
503 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
504 struct libnet_JoinCtx *r)
506 TALLOC_CTX *frame = talloc_stackframe();
510 const char **spn_array = NULL;
513 const char **netbios_aliases = NULL;
514 const char **addl_hostnames = NULL;
518 status = libnet_join_find_machine_acct(mem_ctx, r);
519 if (!ADS_ERR_OK(status)) {
523 status = libnet_join_get_machine_spns(frame,
525 discard_const_p(char **, &spn_array),
527 if (!ADS_ERR_OK(status)) {
528 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
531 /* Windows only creates HOST/shortname & HOST/fqdn. */
533 spn = talloc_asprintf(frame, "HOST/%s", r->in.machine_name);
535 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
538 if (!strupper_m(spn)) {
539 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
543 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
544 if (!ADS_ERR_OK(status)) {
548 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name, lp_dnsdomain());
550 if (!strlower_m(my_fqdn)) {
551 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
555 spn = talloc_asprintf(frame, "HOST/%s", my_fqdn);
557 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
561 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
562 if (!ADS_ERR_OK(status)) {
566 for (netbios_aliases = lp_netbios_aliases();
567 netbios_aliases != NULL && *netbios_aliases != NULL;
570 * Add HOST/NETBIOSNAME
572 spn = talloc_asprintf(frame, "HOST/%s", *netbios_aliases);
574 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
577 if (!strupper_m(spn)) {
578 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
582 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
583 if (!ADS_ERR_OK(status)) {
588 * Add HOST/netbiosname.domainname
590 fstr_sprintf(my_fqdn, "%s.%s",
594 spn = talloc_asprintf(frame, "HOST/%s", my_fqdn);
596 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
600 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
601 if (!ADS_ERR_OK(status)) {
606 for (addl_hostnames = lp_additional_dns_hostnames();
607 addl_hostnames != NULL && *addl_hostnames != NULL;
610 spn = talloc_asprintf(frame, "HOST/%s", *addl_hostnames);
612 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
616 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
617 if (!ADS_ERR_OK(status)) {
622 /* make sure to NULL terminate the array */
623 spn_array = talloc_realloc(frame, spn_array, const char *, num_spns + 1);
624 if (spn_array == NULL) {
625 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
628 spn_array[num_spns] = NULL;
630 mods = ads_init_mods(mem_ctx);
632 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
636 /* fields of primary importance */
638 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
639 if (!ADS_ERR_OK(status)) {
643 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
645 if (!ADS_ERR_OK(status)) {
649 addl_hostnames = lp_additional_dns_hostnames();
650 if (addl_hostnames != NULL && *addl_hostnames != NULL) {
651 status = ads_mod_strlist(mem_ctx, &mods,
652 "msDS-AdditionalDnsHostName",
654 if (!ADS_ERR_OK(status)) {
659 status = ads_gen_mod(r->in.ads, r->out.dn, mods);
666 /****************************************************************
667 ****************************************************************/
669 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
670 struct libnet_JoinCtx *r)
675 if (!r->in.create_upn) {
681 status = libnet_join_find_machine_acct(mem_ctx, r);
682 if (!ADS_ERR_OK(status)) {
687 const char *realm = r->out.dns_domain_name;
689 /* in case we are about to generate a keytab during the join
690 * make sure the default upn we create is usable with kinit -k.
693 if (USE_KERBEROS_KEYTAB) {
694 realm = talloc_strdup_upper(mem_ctx,
695 r->out.dns_domain_name);
699 return ADS_ERROR(LDAP_NO_MEMORY);
702 r->in.upn = talloc_asprintf(mem_ctx,
707 return ADS_ERROR(LDAP_NO_MEMORY);
711 /* now do the mods */
713 mods = ads_init_mods(mem_ctx);
715 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
718 /* fields of primary importance */
720 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
721 if (!ADS_ERR_OK(status)) {
722 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
725 return ads_gen_mod(r->in.ads, r->out.dn, mods);
729 /****************************************************************
730 ****************************************************************/
732 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
733 struct libnet_JoinCtx *r)
739 if (!r->in.os_name || !r->in.os_version ) {
745 status = libnet_join_find_machine_acct(mem_ctx, r);
746 if (!ADS_ERR_OK(status)) {
750 /* now do the mods */
752 mods = ads_init_mods(mem_ctx);
754 return ADS_ERROR(LDAP_NO_MEMORY);
757 if (r->in.os_servicepack) {
759 * if blank string then leave os_sp equal to NULL to force
760 * attribute delete (LDAP_MOD_DELETE)
762 if (!strequal(r->in.os_servicepack,"")) {
763 os_sp = talloc_strdup(mem_ctx, r->in.os_servicepack);
766 os_sp = talloc_asprintf(mem_ctx, "Samba %s",
767 samba_version_string());
769 if (!os_sp && !strequal(r->in.os_servicepack,"")) {
770 return ADS_ERROR(LDAP_NO_MEMORY);
773 /* fields of primary importance */
775 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
777 if (!ADS_ERR_OK(status)) {
781 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
783 if (!ADS_ERR_OK(status)) {
787 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
789 if (!ADS_ERR_OK(status)) {
793 return ads_gen_mod(r->in.ads, r->out.dn, mods);
796 /****************************************************************
797 ****************************************************************/
799 static ADS_STATUS libnet_join_set_etypes(TALLOC_CTX *mem_ctx,
800 struct libnet_JoinCtx *r)
804 const char *etype_list_str;
806 etype_list_str = talloc_asprintf(mem_ctx, "%d",
807 r->in.desired_encryption_types);
808 if (!etype_list_str) {
809 return ADS_ERROR(LDAP_NO_MEMORY);
814 status = libnet_join_find_machine_acct(mem_ctx, r);
815 if (!ADS_ERR_OK(status)) {
819 if (r->in.desired_encryption_types == r->out.set_encryption_types) {
823 /* now do the mods */
825 mods = ads_init_mods(mem_ctx);
827 return ADS_ERROR(LDAP_NO_MEMORY);
830 status = ads_mod_str(mem_ctx, &mods, "msDS-SupportedEncryptionTypes",
832 if (!ADS_ERR_OK(status)) {
836 status = ads_gen_mod(r->in.ads, r->out.dn, mods);
837 if (!ADS_ERR_OK(status)) {
841 r->out.set_encryption_types = r->in.desired_encryption_types;
846 /****************************************************************
847 ****************************************************************/
849 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
850 struct libnet_JoinCtx *r)
852 if (!USE_SYSTEM_KEYTAB) {
856 if (ads_keytab_create_default(r->in.ads) != 0) {
863 /****************************************************************
864 ****************************************************************/
866 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
867 struct libnet_JoinCtx *r)
869 uint32_t domain_func;
871 const char *salt = NULL;
872 char *std_salt = NULL;
874 status = ads_domain_func_level(r->in.ads, &domain_func);
875 if (!ADS_ERR_OK(status)) {
876 libnet_join_set_error_string(mem_ctx, r,
877 "failed to determine domain functional level: %s",
882 /* go ahead and setup the default salt */
884 std_salt = kerberos_standard_des_salt();
886 libnet_join_set_error_string(mem_ctx, r,
887 "failed to obtain standard DES salt");
891 salt = talloc_strdup(mem_ctx, std_salt);
898 /* if it's a Windows functional domain, we have to look for the UPN */
900 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
903 upn = ads_get_upn(r->in.ads, mem_ctx,
906 salt = talloc_strdup(mem_ctx, upn);
913 r->out.krb5_salt = salt;
917 /****************************************************************
918 ****************************************************************/
920 static ADS_STATUS libnet_join_post_processing_ads_modify(TALLOC_CTX *mem_ctx,
921 struct libnet_JoinCtx *r)
924 bool need_etype_update = false;
927 status = libnet_join_connect_ads_user(mem_ctx, r);
928 if (!ADS_ERR_OK(status)) {
933 status = libnet_join_set_machine_spn(mem_ctx, r);
934 if (!ADS_ERR_OK(status)) {
935 libnet_join_set_error_string(mem_ctx, r,
936 "Failed to set machine spn: %s\n"
937 "Do you have sufficient permissions to create machine "
943 status = libnet_join_set_os_attributes(mem_ctx, r);
944 if (!ADS_ERR_OK(status)) {
945 libnet_join_set_error_string(mem_ctx, r,
946 "failed to set machine os attributes: %s",
951 status = libnet_join_set_machine_upn(mem_ctx, r);
952 if (!ADS_ERR_OK(status)) {
953 libnet_join_set_error_string(mem_ctx, r,
954 "failed to set machine upn: %s",
959 status = libnet_join_find_machine_acct(mem_ctx, r);
960 if (!ADS_ERR_OK(status)) {
964 if (r->in.desired_encryption_types != r->out.set_encryption_types) {
965 uint32_t func_level = 0;
967 status = ads_domain_func_level(r->in.ads, &func_level);
968 if (!ADS_ERR_OK(status)) {
969 libnet_join_set_error_string(mem_ctx, r,
970 "failed to query domain controller functional level: %s",
975 if (func_level >= DS_DOMAIN_FUNCTION_2008) {
976 need_etype_update = true;
980 if (need_etype_update) {
982 * We need to reconnect as machine account in order
983 * to update msDS-SupportedEncryptionTypes reliable
986 if (r->in.ads->auth.ccache_name != NULL) {
987 ads_kdestroy(r->in.ads->auth.ccache_name);
988 r->in.ads->auth.ccache_name = NULL;
991 ads_destroy(&r->in.ads);
993 status = libnet_join_connect_ads_machine(mem_ctx, r);
994 if (!ADS_ERR_OK(status)) {
995 libnet_join_set_error_string(mem_ctx, r,
996 "Failed to connect as machine account: %s",
1001 status = libnet_join_set_etypes(mem_ctx, r);
1002 if (!ADS_ERR_OK(status)) {
1003 libnet_join_set_error_string(mem_ctx, r,
1004 "failed to set machine kerberos encryption types: %s",
1005 ads_errstr(status));
1010 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
1011 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1017 static ADS_STATUS libnet_join_post_processing_ads_sync(TALLOC_CTX *mem_ctx,
1018 struct libnet_JoinCtx *r)
1020 if (!libnet_join_create_keytab(mem_ctx, r)) {
1021 libnet_join_set_error_string(mem_ctx, r,
1022 "failed to create kerberos keytab");
1023 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1028 #endif /* HAVE_ADS */
1030 /****************************************************************
1031 Store the machine password and domain SID
1032 ****************************************************************/
1034 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
1035 struct libnet_JoinCtx *r)
1039 status = secrets_store_JoinCtx(r);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 DBG_ERR("secrets_store_JoinCtx() failed %s\n",
1049 /****************************************************************
1050 Connect dc's IPC$ share
1051 ****************************************************************/
1053 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
1058 struct cli_state **cli)
1063 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1066 if (use_kerberos && pass) {
1067 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1070 return cli_full_connection(cli, NULL,
1078 SMB_SIGNING_IPC_DEFAULT);
1081 /****************************************************************
1082 Lookup domain dc's info
1083 ****************************************************************/
1085 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
1086 struct libnet_JoinCtx *r,
1087 struct cli_state **cli)
1089 struct rpc_pipe_client *pipe_hnd = NULL;
1090 struct policy_handle lsa_pol;
1091 NTSTATUS status, result;
1092 union lsa_PolicyInformation *info = NULL;
1093 struct dcerpc_binding_handle *b;
1094 const char *account = r->in.admin_account;
1095 const char *domain = r->in.admin_domain;
1096 const char *password = r->in.admin_password;
1097 bool use_kerberos = r->in.use_kerberos;
1099 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) {
1103 use_kerberos = false;
1106 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1112 if (!NT_STATUS_IS_OK(status)) {
1116 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
1118 if (!NT_STATUS_IS_OK(status)) {
1119 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
1120 nt_errstr(status)));
1124 b = pipe_hnd->binding_handle;
1126 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
1127 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1128 if (!NT_STATUS_IS_OK(status)) {
1132 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
1134 LSA_POLICY_INFO_DNS,
1137 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
1138 r->out.domain_is_ad = true;
1139 r->out.netbios_domain_name = info->dns.name.string;
1140 r->out.dns_domain_name = info->dns.dns_domain.string;
1141 r->out.forest_name = info->dns.dns_forest.string;
1142 r->out.domain_guid = info->dns.domain_guid;
1143 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
1144 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1147 if (!NT_STATUS_IS_OK(status)) {
1148 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
1150 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
1153 if (!NT_STATUS_IS_OK(status)) {
1156 if (!NT_STATUS_IS_OK(result)) {
1161 r->out.netbios_domain_name = info->account_domain.name.string;
1162 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
1163 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1166 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1167 TALLOC_FREE(pipe_hnd);
1173 /****************************************************************
1174 Do the domain join unsecure
1175 ****************************************************************/
1177 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
1178 struct libnet_JoinCtx *r,
1179 struct cli_state *cli)
1181 TALLOC_CTX *frame = talloc_stackframe();
1182 struct rpc_pipe_client *authenticate_pipe = NULL;
1183 struct rpc_pipe_client *passwordset_pipe = NULL;
1184 struct cli_credentials *cli_creds;
1185 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1186 struct netlogon_creds_CredentialState *creds = NULL;
1187 uint32_t netlogon_flags = 0;
1190 DATA_BLOB new_trust_blob = data_blob_null;
1193 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
1194 &authenticate_pipe);
1195 if (!NT_STATUS_IS_OK(status)) {
1200 if (!r->in.machine_password) {
1201 int security = r->in.ads ? SEC_ADS : SEC_DOMAIN;
1203 r->in.machine_password = trust_pw_new_value(mem_ctx,
1204 r->in.secure_channel_type,
1206 if (r->in.machine_password == NULL) {
1208 return NT_STATUS_NO_MEMORY;
1212 cli_creds = cli_credentials_init(talloc_tos());
1213 if (cli_creds == NULL) {
1215 return NT_STATUS_NO_MEMORY;
1218 cli_credentials_set_username(cli_creds, r->out.account_name,
1220 cli_credentials_set_domain(cli_creds, r->in.domain_name,
1222 cli_credentials_set_realm(cli_creds, "", CRED_SPECIFIED);
1223 cli_credentials_set_secure_channel_type(cli_creds,
1224 r->in.secure_channel_type);
1226 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
1227 cli_credentials_set_password(cli_creds, r->in.admin_password,
1230 status = rpccli_create_netlogon_creds_ctx(
1231 cli_creds, authenticate_pipe->desthost, r->in.msg_ctx,
1232 frame, &netlogon_creds);
1233 if (!NT_STATUS_IS_OK(status)) {
1238 status = rpccli_setup_netlogon_creds(
1239 cli, NCACN_NP, netlogon_creds, true /* force_reauth */,
1241 if (!NT_STATUS_IS_OK(status)) {
1246 status = netlogon_creds_cli_get(netlogon_creds, frame, &creds);
1247 if (!NT_STATUS_IS_OK(status)) {
1252 netlogon_flags = creds->negotiate_flags;
1255 if (netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC) {
1256 status = cli_rpc_pipe_open_schannel_with_creds(cli,
1257 &ndr_table_netlogon,
1261 if (!NT_STATUS_IS_OK(status)) {
1266 passwordset_pipe = authenticate_pipe;
1269 len = strlen(r->in.machine_password);
1270 ok = convert_string_talloc(frame, CH_UNIX, CH_UTF16,
1271 r->in.machine_password, len,
1272 (void **)&new_trust_blob.data,
1273 &new_trust_blob.length);
1275 status = NT_STATUS_UNMAPPABLE_CHARACTER;
1276 if (errno == ENOMEM) {
1277 status = NT_STATUS_NO_MEMORY;
1283 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
1284 passwordset_pipe->binding_handle,
1286 NULL); /* new_version */
1287 if (!NT_STATUS_IS_OK(status)) {
1293 return NT_STATUS_OK;
1296 /****************************************************************
1298 ****************************************************************/
1300 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
1301 struct libnet_JoinCtx *r,
1302 struct cli_state *cli)
1304 struct rpc_pipe_client *pipe_hnd = NULL;
1305 struct policy_handle sam_pol, domain_pol, user_pol;
1306 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1308 struct lsa_String lsa_acct_name;
1310 uint32_t acct_flags = ACB_WSTRUST;
1311 struct samr_Ids user_rids;
1312 struct samr_Ids name_types;
1313 union samr_UserInfo user_info;
1314 struct dcerpc_binding_handle *b = NULL;
1315 unsigned int old_timeout = 0;
1317 DATA_BLOB session_key = data_blob_null;
1318 struct samr_CryptPassword crypt_pwd;
1319 struct samr_CryptPasswordEx crypt_pwd_ex;
1321 ZERO_STRUCT(sam_pol);
1322 ZERO_STRUCT(domain_pol);
1323 ZERO_STRUCT(user_pol);
1325 switch (r->in.secure_channel_type) {
1326 case SEC_CHAN_WKSTA:
1327 acct_flags = ACB_WSTRUST;
1330 acct_flags = ACB_SVRTRUST;
1333 return NT_STATUS_INVALID_PARAMETER;
1336 if (!r->in.machine_password) {
1337 int security = r->in.ads ? SEC_ADS : SEC_DOMAIN;
1339 r->in.machine_password = trust_pw_new_value(mem_ctx,
1340 r->in.secure_channel_type,
1342 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
1345 /* Open the domain */
1347 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1349 if (!NT_STATUS_IS_OK(status)) {
1350 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1351 nt_errstr(status)));
1355 b = pipe_hnd->binding_handle;
1357 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1358 if (!NT_STATUS_IS_OK(status)) {
1359 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1360 nt_errstr(status)));
1364 status = dcerpc_samr_Connect2(b, mem_ctx,
1366 SAMR_ACCESS_ENUM_DOMAINS
1367 | SAMR_ACCESS_LOOKUP_DOMAIN,
1370 if (!NT_STATUS_IS_OK(status)) {
1373 if (!NT_STATUS_IS_OK(result)) {
1378 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1380 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1381 | SAMR_DOMAIN_ACCESS_CREATE_USER
1382 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1386 if (!NT_STATUS_IS_OK(status)) {
1389 if (!NT_STATUS_IS_OK(result)) {
1394 /* Create domain user */
1396 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1397 if (!strlower_m(acct_name)) {
1398 status = NT_STATUS_INVALID_PARAMETER;
1402 init_lsa_String(&lsa_acct_name, acct_name);
1404 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1405 uint32_t access_desired =
1406 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1407 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1408 SAMR_USER_ACCESS_SET_PASSWORD |
1409 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1410 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1411 uint32_t access_granted = 0;
1413 DEBUG(10,("Creating account with desired access mask: %d\n",
1416 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1425 if (!NT_STATUS_IS_OK(status)) {
1430 if (!NT_STATUS_IS_OK(status) &&
1431 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1433 DEBUG(10,("Creation of workstation account failed: %s\n",
1434 nt_errstr(status)));
1436 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1437 username/password combo but the user does not have
1438 administrator access. */
1440 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1441 libnet_join_set_error_string(mem_ctx, r,
1442 "User specified does not have "
1443 "administrator privileges");
1449 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1450 if (!(r->in.join_flags &
1451 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1456 /* We *must* do this.... don't ask... */
1458 if (NT_STATUS_IS_OK(status)) {
1459 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1463 status = dcerpc_samr_LookupNames(b, mem_ctx,
1470 if (!NT_STATUS_IS_OK(status)) {
1473 if (!NT_STATUS_IS_OK(result)) {
1477 if (user_rids.count != 1) {
1478 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1481 if (name_types.count != 1) {
1482 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1486 if (name_types.ids[0] != SID_NAME_USER) {
1487 DEBUG(0,("%s is not a user account (type=%d)\n",
1488 acct_name, name_types.ids[0]));
1489 status = NT_STATUS_INVALID_WORKSTATION;
1493 user_rid = user_rids.ids[0];
1495 /* Open handle on user */
1497 status = dcerpc_samr_OpenUser(b, mem_ctx,
1499 SEC_FLAG_MAXIMUM_ALLOWED,
1503 if (!NT_STATUS_IS_OK(status)) {
1506 if (!NT_STATUS_IS_OK(result)) {
1511 /* Fill in the additional account flags now */
1513 acct_flags |= ACB_PWNOEXP;
1515 /* Set account flags on machine account */
1516 ZERO_STRUCT(user_info.info16);
1517 user_info.info16.acct_flags = acct_flags;
1519 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1521 UserControlInformation,
1524 if (!NT_STATUS_IS_OK(status)) {
1525 dcerpc_samr_DeleteUser(b, mem_ctx,
1529 libnet_join_set_error_string(mem_ctx, r,
1530 "Failed to set account flags for machine account (%s)\n",
1535 if (!NT_STATUS_IS_OK(result)) {
1538 dcerpc_samr_DeleteUser(b, mem_ctx,
1542 libnet_join_set_error_string(mem_ctx, r,
1543 "Failed to set account flags for machine account (%s)\n",
1548 /* Set password on machine account - first try level 26 */
1551 * increase the timeout as password filter modules on the DC
1552 * might delay the operation for a significant amount of time
1554 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1556 status = init_samr_CryptPasswordEx(r->in.machine_password,
1559 if (!NT_STATUS_IS_OK(status)) {
1563 user_info.info26.password = crypt_pwd_ex;
1564 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1566 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1568 UserInternal5InformationNew,
1572 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1574 /* retry with level 24 */
1576 status = init_samr_CryptPassword(r->in.machine_password,
1579 if (!NT_STATUS_IS_OK(status)) {
1583 user_info.info24.password = crypt_pwd;
1584 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1586 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1588 UserInternal5Information,
1594 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1596 if (!NT_STATUS_IS_OK(status)) {
1598 dcerpc_samr_DeleteUser(b, mem_ctx,
1602 libnet_join_set_error_string(mem_ctx, r,
1603 "Failed to set password for machine account (%s)\n",
1607 if (!NT_STATUS_IS_OK(result)) {
1610 dcerpc_samr_DeleteUser(b, mem_ctx,
1614 libnet_join_set_error_string(mem_ctx, r,
1615 "Failed to set password for machine account (%s)\n",
1620 status = NT_STATUS_OK;
1627 data_blob_clear_free(&session_key);
1629 if (is_valid_policy_hnd(&sam_pol)) {
1630 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1632 if (is_valid_policy_hnd(&domain_pol)) {
1633 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1635 if (is_valid_policy_hnd(&user_pol)) {
1636 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1638 TALLOC_FREE(pipe_hnd);
1643 /****************************************************************
1644 ****************************************************************/
1646 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1647 const char *netbios_domain_name,
1648 const char *dc_name,
1649 const bool use_kerberos)
1651 TALLOC_CTX *frame = talloc_stackframe();
1652 struct cli_state *cli = NULL;
1653 struct rpc_pipe_client *netlogon_pipe = NULL;
1654 struct cli_credentials *cli_creds = NULL;
1655 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1656 struct netlogon_creds_CredentialState *creds = NULL;
1657 uint32_t netlogon_flags = 0;
1663 return NT_STATUS_INVALID_PARAMETER;
1666 if (!secrets_init()) {
1668 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1671 status = pdb_get_trust_credentials(netbios_domain_name, NULL,
1673 if (!NT_STATUS_IS_OK(status)) {
1678 /* we don't want any old password */
1679 cli_credentials_set_old_password(cli_creds, NULL, CRED_SPECIFIED);
1682 cli_credentials_set_kerberos_state(cli_creds,
1683 CRED_MUST_USE_KERBEROS);
1686 status = cli_full_connection_creds(&cli, NULL,
1692 SMB_SIGNING_IPC_DEFAULT);
1694 if (!NT_STATUS_IS_OK(status)) {
1695 status = cli_full_connection(&cli, NULL,
1703 SMB_SIGNING_IPC_DEFAULT);
1706 if (!NT_STATUS_IS_OK(status)) {
1711 status = rpccli_create_netlogon_creds_ctx(cli_creds,
1716 if (!NT_STATUS_IS_OK(status)) {
1722 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
1724 true, /* force_reauth */
1726 if (!NT_STATUS_IS_OK(status)) {
1727 DEBUG(0,("connect_to_domain_password_server: "
1728 "unable to open the domain client session to "
1729 "machine %s. Flags[0x%08X] Error was : %s.\n",
1730 dc_name, (unsigned)netlogon_flags,
1731 nt_errstr(status)));
1737 status = netlogon_creds_cli_get(netlogon_creds,
1740 if (!NT_STATUS_IS_OK(status)) {
1745 netlogon_flags = creds->negotiate_flags;
1748 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1751 return NT_STATUS_OK;
1754 status = cli_rpc_pipe_open_schannel_with_creds(
1755 cli, &ndr_table_netlogon, NCACN_NP,
1756 netlogon_creds, &netlogon_pipe);
1758 TALLOC_FREE(netlogon_pipe);
1760 if (!NT_STATUS_IS_OK(status)) {
1761 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1762 "on netlogon pipe to server %s for domain %s. "
1764 smbXcli_conn_remote_name(cli->conn),
1765 netbios_domain_name, nt_errstr(status)));
1773 return NT_STATUS_OK;
1776 /****************************************************************
1777 ****************************************************************/
1779 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1780 struct libnet_JoinCtx *r)
1784 status = libnet_join_ok(r->in.msg_ctx,
1785 r->out.netbios_domain_name,
1787 r->in.use_kerberos);
1788 if (!NT_STATUS_IS_OK(status)) {
1789 libnet_join_set_error_string(mem_ctx, r,
1790 "failed to verify domain membership after joining: %s",
1791 get_friendly_nt_error_msg(status));
1792 return WERR_NERR_SETUPNOTJOINED;
1798 /****************************************************************
1799 ****************************************************************/
1801 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1802 struct libnet_UnjoinCtx *r)
1805 * TODO: use values from 'struct libnet_UnjoinCtx' ?
1807 return secrets_delete_machine_password_ex(lp_workgroup(), lp_realm());
1810 /****************************************************************
1811 ****************************************************************/
1813 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1814 struct libnet_UnjoinCtx *r)
1816 struct cli_state *cli = NULL;
1817 struct rpc_pipe_client *pipe_hnd = NULL;
1818 struct policy_handle sam_pol, domain_pol, user_pol;
1819 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1822 struct lsa_String lsa_acct_name;
1823 struct samr_Ids user_rids;
1824 struct samr_Ids name_types;
1825 union samr_UserInfo *info = NULL;
1826 struct dcerpc_binding_handle *b = NULL;
1828 ZERO_STRUCT(sam_pol);
1829 ZERO_STRUCT(domain_pol);
1830 ZERO_STRUCT(user_pol);
1832 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1833 r->in.admin_account,
1835 r->in.admin_password,
1838 if (!NT_STATUS_IS_OK(status)) {
1842 /* Open the domain */
1844 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1846 if (!NT_STATUS_IS_OK(status)) {
1847 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1848 nt_errstr(status)));
1852 b = pipe_hnd->binding_handle;
1854 status = dcerpc_samr_Connect2(b, mem_ctx,
1856 SEC_FLAG_MAXIMUM_ALLOWED,
1859 if (!NT_STATUS_IS_OK(status)) {
1862 if (!NT_STATUS_IS_OK(result)) {
1867 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1869 SEC_FLAG_MAXIMUM_ALLOWED,
1873 if (!NT_STATUS_IS_OK(status)) {
1876 if (!NT_STATUS_IS_OK(result)) {
1881 /* Create domain user */
1883 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1884 if (!strlower_m(acct_name)) {
1885 status = NT_STATUS_INVALID_PARAMETER;
1889 init_lsa_String(&lsa_acct_name, acct_name);
1891 status = dcerpc_samr_LookupNames(b, mem_ctx,
1899 if (!NT_STATUS_IS_OK(status)) {
1902 if (!NT_STATUS_IS_OK(result)) {
1906 if (user_rids.count != 1) {
1907 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1910 if (name_types.count != 1) {
1911 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1915 if (name_types.ids[0] != SID_NAME_USER) {
1916 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1917 name_types.ids[0]));
1918 status = NT_STATUS_INVALID_WORKSTATION;
1922 user_rid = user_rids.ids[0];
1924 /* Open handle on user */
1926 status = dcerpc_samr_OpenUser(b, mem_ctx,
1928 SEC_FLAG_MAXIMUM_ALLOWED,
1932 if (!NT_STATUS_IS_OK(status)) {
1935 if (!NT_STATUS_IS_OK(result)) {
1942 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1947 if (!NT_STATUS_IS_OK(status)) {
1948 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1951 if (!NT_STATUS_IS_OK(result)) {
1953 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1957 /* now disable and setuser info */
1959 info->info16.acct_flags |= ACB_DISABLED;
1961 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1966 if (!NT_STATUS_IS_OK(status)) {
1967 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1970 if (!NT_STATUS_IS_OK(result)) {
1972 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1976 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1979 if (pipe_hnd && b) {
1980 if (is_valid_policy_hnd(&domain_pol)) {
1981 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1983 if (is_valid_policy_hnd(&sam_pol)) {
1984 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1986 TALLOC_FREE(pipe_hnd);
1996 /****************************************************************
1997 ****************************************************************/
1999 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
2001 WERROR werr = WERR_OK;
2003 struct smbconf_ctx *ctx;
2005 err = smbconf_init_reg(r, &ctx, NULL);
2006 if (!SBC_ERROR_IS_OK(err)) {
2007 werr = WERR_SERVICE_DOES_NOT_EXIST;
2011 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2013 err = smbconf_set_global_parameter(ctx, "security", "user");
2014 if (!SBC_ERROR_IS_OK(err)) {
2015 werr = WERR_SERVICE_DOES_NOT_EXIST;
2019 err = smbconf_set_global_parameter(ctx, "workgroup",
2021 if (!SBC_ERROR_IS_OK(err)) {
2022 werr = WERR_SERVICE_DOES_NOT_EXIST;
2026 smbconf_delete_global_parameter(ctx, "realm");
2030 err = smbconf_set_global_parameter(ctx, "security", "domain");
2031 if (!SBC_ERROR_IS_OK(err)) {
2032 werr = WERR_SERVICE_DOES_NOT_EXIST;
2036 err = smbconf_set_global_parameter(ctx, "workgroup",
2037 r->out.netbios_domain_name);
2038 if (!SBC_ERROR_IS_OK(err)) {
2039 werr = WERR_SERVICE_DOES_NOT_EXIST;
2043 if (r->out.domain_is_ad) {
2044 err = smbconf_set_global_parameter(ctx, "security", "ads");
2045 if (!SBC_ERROR_IS_OK(err)) {
2046 werr = WERR_SERVICE_DOES_NOT_EXIST;
2050 err = smbconf_set_global_parameter(ctx, "realm",
2051 r->out.dns_domain_name);
2052 if (!SBC_ERROR_IS_OK(err)) {
2053 werr = WERR_SERVICE_DOES_NOT_EXIST;
2059 smbconf_shutdown(ctx);
2063 /****************************************************************
2064 ****************************************************************/
2066 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
2068 WERROR werr = WERR_OK;
2070 struct smbconf_ctx *ctx;
2072 err = smbconf_init_reg(r, &ctx, NULL);
2073 if (!SBC_ERROR_IS_OK(err)) {
2074 werr = WERR_SERVICE_DOES_NOT_EXIST;
2078 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2080 err = smbconf_set_global_parameter(ctx, "security", "user");
2081 if (!SBC_ERROR_IS_OK(err)) {
2082 werr = WERR_SERVICE_DOES_NOT_EXIST;
2086 err = smbconf_delete_global_parameter(ctx, "workgroup");
2087 if (!SBC_ERROR_IS_OK(err)) {
2088 werr = WERR_SERVICE_DOES_NOT_EXIST;
2092 smbconf_delete_global_parameter(ctx, "realm");
2096 smbconf_shutdown(ctx);
2100 /****************************************************************
2101 ****************************************************************/
2103 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
2107 if (!W_ERROR_IS_OK(r->out.result)) {
2108 return r->out.result;
2111 if (!r->in.modify_config) {
2115 werr = do_join_modify_vals_config(r);
2116 if (!W_ERROR_IS_OK(werr)) {
2120 lp_load_global(get_dyn_CONFIGFILE());
2122 r->out.modified_config = true;
2123 r->out.result = werr;
2128 /****************************************************************
2129 ****************************************************************/
2131 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
2135 if (!W_ERROR_IS_OK(r->out.result)) {
2136 return r->out.result;
2139 if (!r->in.modify_config) {
2143 werr = do_unjoin_modify_vals_config(r);
2144 if (!W_ERROR_IS_OK(werr)) {
2148 lp_load_global(get_dyn_CONFIGFILE());
2150 r->out.modified_config = true;
2151 r->out.result = werr;
2156 /****************************************************************
2157 ****************************************************************/
2159 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
2160 const char *domain_str,
2161 const char **domain_p,
2164 char *domain = NULL;
2166 const char *p = NULL;
2168 if (!domain_str || !domain_p || !dc_p) {
2172 p = strchr_m(domain_str, '\\');
2175 domain = talloc_strndup(mem_ctx, domain_str,
2176 PTR_DIFF(p, domain_str));
2177 dc = talloc_strdup(mem_ctx, p+1);
2182 domain = talloc_strdup(mem_ctx, domain_str);
2198 /****************************************************************
2199 ****************************************************************/
2201 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
2202 struct libnet_JoinCtx *r)
2204 if (!r->in.domain_name) {
2205 libnet_join_set_error_string(mem_ctx, r,
2206 "No domain name defined");
2207 return WERR_INVALID_PARAMETER;
2210 if (strlen(r->in.machine_name) > 15) {
2211 libnet_join_set_error_string(mem_ctx, r,
2212 "Our netbios name can be at most 15 chars long, "
2213 "\"%s\" is %u chars long\n",
2215 (unsigned int)strlen(r->in.machine_name));
2216 return WERR_INVALID_PARAMETER;
2219 r->out.account_name = talloc_asprintf(mem_ctx, "%s$",
2220 r->in.machine_name);
2221 if (r->out.account_name == NULL) {
2222 libnet_join_set_error_string(mem_ctx, r,
2223 "Unable to construct r->out.account_name");
2224 return WERR_NOT_ENOUGH_MEMORY;
2227 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2230 libnet_join_set_error_string(mem_ctx, r,
2231 "Failed to parse domain name");
2232 return WERR_INVALID_PARAMETER;
2235 if (!r->in.admin_domain) {
2236 char *admin_domain = NULL;
2237 char *admin_account = NULL;
2240 ok = split_domain_user(mem_ctx,
2241 r->in.admin_account,
2245 return WERR_NOT_ENOUGH_MEMORY;
2248 if (admin_domain != NULL) {
2249 r->in.admin_domain = admin_domain;
2251 r->in.admin_domain = r->in.domain_name;
2253 r->in.admin_account = admin_account;
2256 if (!secrets_init()) {
2257 libnet_join_set_error_string(mem_ctx, r,
2258 "Unable to open secrets database");
2259 return WERR_CAN_NOT_COMPLETE;
2265 /****************************************************************
2266 ****************************************************************/
2268 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
2272 /* Try adding dom admins to builtin\admins. Only log failures. */
2273 status = create_builtin_administrators(domain_sid);
2274 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2275 DEBUG(10,("Unable to auto-add domain administrators to "
2276 "BUILTIN\\Administrators during join because "
2277 "winbindd must be running.\n"));
2278 } else if (!NT_STATUS_IS_OK(status)) {
2279 DEBUG(5, ("Failed to auto-add domain administrators to "
2280 "BUILTIN\\Administrators during join: %s\n",
2281 nt_errstr(status)));
2284 /* Try adding dom users to builtin\users. Only log failures. */
2285 status = create_builtin_users(domain_sid);
2286 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2287 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2288 "during join because winbindd must be running.\n"));
2289 } else if (!NT_STATUS_IS_OK(status)) {
2290 DEBUG(5, ("Failed to auto-add domain administrators to "
2291 "BUILTIN\\Administrators during join: %s\n",
2292 nt_errstr(status)));
2295 /* Try adding dom guests to builtin\guests. Only log failures. */
2296 status = create_builtin_guests(domain_sid);
2297 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2298 DEBUG(10,("Unable to auto-add domain guests to "
2299 "BUILTIN\\Guests during join because "
2300 "winbindd must be running.\n"));
2301 } else if (!NT_STATUS_IS_OK(status)) {
2302 DEBUG(5, ("Failed to auto-add domain guests to "
2303 "BUILTIN\\Guests during join: %s\n",
2304 nt_errstr(status)));
2308 /****************************************************************
2309 ****************************************************************/
2311 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
2312 struct libnet_JoinCtx *r)
2316 if (!W_ERROR_IS_OK(r->out.result)) {
2317 return r->out.result;
2320 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2321 werr = do_JoinConfig(r);
2322 if (!W_ERROR_IS_OK(werr)) {
2330 if (r->out.domain_is_ad &&
2331 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2332 ADS_STATUS ads_status;
2334 ads_status = libnet_join_post_processing_ads_modify(mem_ctx, r);
2335 if (!ADS_ERR_OK(ads_status)) {
2336 return WERR_GEN_FAILURE;
2339 #endif /* HAVE_ADS */
2341 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
2342 if (r->out.dns_domain_name) {
2343 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
2346 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2347 return WERR_NERR_SETUPNOTJOINED;
2350 werr = do_JoinConfig(r);
2351 if (!W_ERROR_IS_OK(werr)) {
2356 if (r->out.domain_is_ad &&
2357 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2358 ADS_STATUS ads_status;
2360 ads_status = libnet_join_post_processing_ads_sync(mem_ctx, r);
2361 if (!ADS_ERR_OK(ads_status)) {
2362 return WERR_GEN_FAILURE;
2365 #endif /* HAVE_ADS */
2367 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
2372 /****************************************************************
2373 ****************************************************************/
2375 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
2378 ads_destroy(&r->in.ads);
2384 /****************************************************************
2385 ****************************************************************/
2387 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2390 ads_destroy(&r->in.ads);
2396 /****************************************************************
2397 ****************************************************************/
2399 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2400 struct libnet_JoinCtx **r)
2402 struct libnet_JoinCtx *ctx;
2404 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2406 return WERR_NOT_ENOUGH_MEMORY;
2409 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2411 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2412 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2414 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2416 ctx->in.desired_encryption_types = ENC_CRC32 |
2419 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2420 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES128;
2422 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2423 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES256;
2431 /****************************************************************
2432 ****************************************************************/
2434 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2435 struct libnet_UnjoinCtx **r)
2437 struct libnet_UnjoinCtx *ctx;
2439 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2441 return WERR_NOT_ENOUGH_MEMORY;
2444 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2446 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2447 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2454 /****************************************************************
2455 ****************************************************************/
2457 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2458 struct libnet_JoinCtx *r)
2460 bool valid_security = false;
2461 bool valid_workgroup = false;
2462 bool valid_realm = false;
2463 bool ignored_realm = false;
2465 /* check if configuration is already set correctly */
2467 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2469 switch (r->out.domain_is_ad) {
2471 valid_security = (lp_security() == SEC_DOMAIN)
2472 || (lp_server_role() == ROLE_DOMAIN_PDC)
2473 || (lp_server_role() == ROLE_DOMAIN_BDC);
2474 if (valid_workgroup && valid_security) {
2475 /* nothing to be done */
2480 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2481 switch (lp_security()) {
2483 if (!valid_realm && lp_winbind_rpc_only()) {
2485 ignored_realm = true;
2490 valid_security = true;
2493 if (valid_workgroup && valid_realm && valid_security) {
2494 if (ignored_realm && !r->in.modify_config)
2496 libnet_join_set_error_string(mem_ctx, r,
2497 "Warning: ignoring realm when "
2498 "joining AD domain with "
2499 "'security=domain' and "
2500 "'winbind rpc only = yes'. "
2501 "(realm set to '%s', "
2502 "should be '%s').", lp_realm(),
2503 r->out.dns_domain_name);
2505 /* nothing to be done */
2511 /* check if we are supposed to manipulate configuration */
2513 if (!r->in.modify_config) {
2515 char *wrong_conf = talloc_strdup(mem_ctx, "");
2517 if (!valid_workgroup) {
2518 wrong_conf = talloc_asprintf_append(wrong_conf,
2519 "\"workgroup\" set to '%s', should be '%s'",
2520 lp_workgroup(), r->out.netbios_domain_name);
2521 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2525 wrong_conf = talloc_asprintf_append(wrong_conf,
2526 "\"realm\" set to '%s', should be '%s'",
2527 lp_realm(), r->out.dns_domain_name);
2528 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2531 if (!valid_security) {
2532 const char *sec = NULL;
2533 switch (lp_security()) {
2534 case SEC_USER: sec = "user"; break;
2535 case SEC_DOMAIN: sec = "domain"; break;
2536 case SEC_ADS: sec = "ads"; break;
2538 wrong_conf = talloc_asprintf_append(wrong_conf,
2539 "\"security\" set to '%s', should be %s",
2540 sec, r->out.domain_is_ad ?
2541 "either 'domain' or 'ads'" : "'domain'");
2542 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2545 libnet_join_set_error_string(mem_ctx, r,
2546 "Invalid configuration (%s) and configuration modification "
2547 "was not requested", wrong_conf);
2548 return WERR_CAN_NOT_COMPLETE;
2551 /* check if we are able to manipulate configuration */
2553 if (!lp_config_backend_is_registry()) {
2554 libnet_join_set_error_string(mem_ctx, r,
2555 "Configuration manipulation requested but not "
2556 "supported by backend");
2557 return WERR_NOT_SUPPORTED;
2563 /****************************************************************
2564 ****************************************************************/
2566 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2567 struct libnet_JoinCtx *r)
2571 struct cli_state *cli = NULL;
2573 ADS_STATUS ads_status;
2574 #endif /* HAVE_ADS */
2575 const char *pre_connect_realm = NULL;
2576 const char *numeric_dcip = NULL;
2577 const char *sitename = NULL;
2579 /* Before contacting a DC, we can securely know
2580 * the realm only if the user specifies it.
2582 if (r->in.use_kerberos &&
2583 r->in.domain_name_type == JoinDomNameTypeDNS) {
2584 pre_connect_realm = r->in.domain_name;
2587 if (!r->in.dc_name) {
2588 struct netr_DsRGetDCNameInfo *info;
2590 uint32_t name_type_flags = 0;
2591 if (r->in.domain_name_type == JoinDomNameTypeDNS) {
2592 name_type_flags = DS_IS_DNS_NAME;
2593 } else if (r->in.domain_name_type == JoinDomNameTypeNBT) {
2594 name_type_flags = DS_IS_FLAT_NAME;
2596 status = dsgetdcname(mem_ctx,
2601 DS_FORCE_REDISCOVERY |
2602 DS_DIRECTORY_SERVICE_REQUIRED |
2603 DS_WRITABLE_REQUIRED |
2604 DS_RETURN_DNS_NAME |
2607 if (!NT_STATUS_IS_OK(status)) {
2608 libnet_join_set_error_string(mem_ctx, r,
2609 "failed to find DC for domain %s - %s",
2611 get_friendly_nt_error_msg(status));
2612 return WERR_NERR_DCNOTFOUND;
2615 dc = strip_hostname(info->dc_unc);
2616 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2617 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2619 if (info->dc_address == NULL || info->dc_address[0] != '\\' ||
2620 info->dc_address[1] != '\\') {
2621 DBG_ERR("ill-formed DC address '%s'\n",
2623 return WERR_NERR_DCNOTFOUND;
2626 numeric_dcip = info->dc_address + 2;
2627 sitename = info->dc_site_name;
2628 /* info goes out of scope but the memory stays
2629 allocated on the talloc context */
2632 if (pre_connect_realm != NULL) {
2633 struct sockaddr_storage ss = {0};
2635 if (numeric_dcip != NULL) {
2636 if (!interpret_string_addr(&ss, numeric_dcip,
2639 "cannot parse IP address '%s' of DC '%s'\n",
2640 numeric_dcip, r->in.dc_name);
2641 return WERR_NERR_DCNOTFOUND;
2644 if (!interpret_string_addr(&ss, r->in.dc_name, 0)) {
2646 "cannot resolve IP address of DC '%s'\n",
2648 return WERR_NERR_DCNOTFOUND;
2652 /* The domain parameter is only used as modifier
2653 * to krb5.conf file name. _JOIN_ is is not a valid
2654 * NetBIOS name so it cannot clash with another domain
2657 create_local_private_krb5_conf_for_domain(pre_connect_realm,
2663 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2664 if (!NT_STATUS_IS_OK(status)) {
2665 libnet_join_set_error_string(mem_ctx, r,
2666 "failed to lookup DC info for domain '%s' over rpc: %s",
2667 r->in.domain_name, get_friendly_nt_error_msg(status));
2668 return ntstatus_to_werror(status);
2671 werr = libnet_join_check_config(mem_ctx, r);
2672 if (!W_ERROR_IS_OK(werr)) {
2678 if (r->out.domain_is_ad) {
2679 create_local_private_krb5_conf_for_domain(
2680 r->out.dns_domain_name, r->out.netbios_domain_name,
2681 sitename, smbXcli_conn_remote_sockaddr(cli->conn));
2684 if (r->out.domain_is_ad &&
2685 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2687 const char *initial_account_ou = r->in.account_ou;
2690 * we want to create the msDS-SupportedEncryptionTypes attribute
2691 * as early as possible so always try an LDAP create as the user
2692 * first. We copy r->in.account_ou because it may be changed
2693 * during the machine pre-creation.
2696 ads_status = libnet_join_connect_ads_user(mem_ctx, r);
2697 if (!ADS_ERR_OK(ads_status)) {
2698 libnet_join_set_error_string(mem_ctx, r,
2699 "failed to connect to AD: %s",
2700 ads_errstr(ads_status));
2701 return WERR_NERR_DEFAULTJOINREQUIRED;
2704 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2705 if (ADS_ERR_OK(ads_status)) {
2708 * LDAP object creation succeeded.
2710 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2715 if (initial_account_ou != NULL) {
2716 libnet_join_set_error_string(mem_ctx, r,
2717 "failed to precreate account in ou %s: %s",
2719 ads_errstr(ads_status));
2720 return WERR_NERR_DEFAULTJOINREQUIRED;
2723 DBG_INFO("Failed to pre-create account in OU %s: %s\n",
2724 r->in.account_ou, ads_errstr(ads_status));
2726 #endif /* HAVE_ADS */
2728 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2729 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2730 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2732 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2734 if (!NT_STATUS_IS_OK(status)) {
2735 libnet_join_set_error_string(mem_ctx, r,
2736 "failed to join domain '%s' over rpc: %s",
2737 r->in.domain_name, get_friendly_nt_error_msg(status));
2738 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2739 return WERR_NERR_SETUPALREADYJOINED;
2741 werr = ntstatus_to_werror(status);
2755 /****************************************************************
2756 ****************************************************************/
2758 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2759 struct libnet_JoinCtx *r)
2762 struct libnet_UnjoinCtx *u = NULL;
2764 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2765 if (!W_ERROR_IS_OK(werr)) {
2769 u->in.debug = r->in.debug;
2770 u->in.dc_name = r->in.dc_name;
2771 u->in.domain_name = r->in.domain_name;
2772 u->in.admin_account = r->in.admin_account;
2773 u->in.admin_password = r->in.admin_password;
2774 u->in.modify_config = r->in.modify_config;
2775 u->in.use_kerberos = r->in.use_kerberos;
2776 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2777 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2779 werr = libnet_Unjoin(mem_ctx, u);
2785 /****************************************************************
2786 ****************************************************************/
2788 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2789 struct libnet_JoinCtx *r)
2794 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2797 ZERO_STRUCT(r->out);
2799 werr = libnet_join_pre_processing(mem_ctx, r);
2800 if (!W_ERROR_IS_OK(werr)) {
2804 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2805 werr = libnet_DomainJoin(mem_ctx, r);
2806 if (!W_ERROR_IS_OK(werr)) {
2811 werr = libnet_join_post_processing(mem_ctx, r);
2812 if (!W_ERROR_IS_OK(werr)) {
2816 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2817 werr = libnet_join_post_verify(mem_ctx, r);
2818 if (!W_ERROR_IS_OK(werr)) {
2819 libnet_join_rollback(mem_ctx, r);
2824 r->out.result = werr;
2827 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2832 /****************************************************************
2833 ****************************************************************/
2835 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2836 struct libnet_UnjoinCtx *r)
2840 if (!r->in.domain_sid) {
2842 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2843 libnet_unjoin_set_error_string(mem_ctx, r,
2844 "Unable to fetch domain sid: are we joined?");
2845 return WERR_NERR_SETUPNOTJOINED;
2847 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2848 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2851 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2852 !r->in.delete_machine_account) {
2853 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2857 if (!r->in.dc_name) {
2858 struct netr_DsRGetDCNameInfo *info;
2860 status = dsgetdcname(mem_ctx,
2865 DS_DIRECTORY_SERVICE_REQUIRED |
2866 DS_WRITABLE_REQUIRED |
2869 if (!NT_STATUS_IS_OK(status)) {
2870 libnet_unjoin_set_error_string(mem_ctx, r,
2871 "failed to find DC for domain %s - %s",
2873 get_friendly_nt_error_msg(status));
2874 return WERR_NERR_DCNOTFOUND;
2877 dc = strip_hostname(info->dc_unc);
2878 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2879 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2883 /* for net ads leave, try to delete the account. If it works,
2884 no sense in disabling. If it fails, we can still try to
2887 if (r->in.delete_machine_account) {
2888 ADS_STATUS ads_status;
2889 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2890 if (ADS_ERR_OK(ads_status)) {
2892 r->out.dns_domain_name =
2893 talloc_strdup(mem_ctx,
2894 r->in.ads->server.realm);
2896 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2898 if (!ADS_ERR_OK(ads_status)) {
2899 libnet_unjoin_set_error_string(mem_ctx, r,
2900 "failed to remove machine account from AD: %s",
2901 ads_errstr(ads_status));
2903 r->out.deleted_machine_account = true;
2904 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2905 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2909 #endif /* HAVE_ADS */
2911 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2913 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2914 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2915 if (!NT_STATUS_IS_OK(status)) {
2916 libnet_unjoin_set_error_string(mem_ctx, r,
2917 "failed to disable machine account via rpc: %s",
2918 get_friendly_nt_error_msg(status));
2919 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2920 return WERR_NERR_SETUPNOTJOINED;
2922 return ntstatus_to_werror(status);
2925 r->out.dns_domain_name = talloc_strdup(mem_ctx,
2927 r->out.disabled_machine_account = true;
2930 /* If disable succeeded or was not requested at all, we
2931 should be getting rid of our end of things */
2933 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2938 /****************************************************************
2939 ****************************************************************/
2941 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2942 struct libnet_UnjoinCtx *r)
2944 if (!r->in.domain_name) {
2945 libnet_unjoin_set_error_string(mem_ctx, r,
2946 "No domain name defined");
2947 return WERR_INVALID_PARAMETER;
2950 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2953 libnet_unjoin_set_error_string(mem_ctx, r,
2954 "Failed to parse domain name");
2955 return WERR_INVALID_PARAMETER;
2959 return WERR_NERR_SETUPDOMAINCONTROLLER;
2962 if (!r->in.admin_domain) {
2963 char *admin_domain = NULL;
2964 char *admin_account = NULL;
2967 ok = split_domain_user(mem_ctx,
2968 r->in.admin_account,
2972 return WERR_NOT_ENOUGH_MEMORY;
2975 if (admin_domain != NULL) {
2976 r->in.admin_domain = admin_domain;
2978 r->in.admin_domain = r->in.domain_name;
2980 r->in.admin_account = admin_account;
2983 if (!secrets_init()) {
2984 libnet_unjoin_set_error_string(mem_ctx, r,
2985 "Unable to open secrets database");
2986 return WERR_CAN_NOT_COMPLETE;
2992 /****************************************************************
2993 ****************************************************************/
2995 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2996 struct libnet_UnjoinCtx *r)
2998 saf_delete(r->out.netbios_domain_name);
2999 saf_delete(r->out.dns_domain_name);
3001 return libnet_unjoin_config(r);
3004 /****************************************************************
3005 ****************************************************************/
3007 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
3008 struct libnet_UnjoinCtx *r)
3013 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
3016 werr = libnet_unjoin_pre_processing(mem_ctx, r);
3017 if (!W_ERROR_IS_OK(werr)) {
3021 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
3022 werr = libnet_DomainUnjoin(mem_ctx, r);
3023 if (!W_ERROR_IS_OK(werr)) {
3024 libnet_unjoin_config(r);
3029 werr = libnet_unjoin_post_processing(mem_ctx, r);
3030 if (!W_ERROR_IS_OK(werr)) {
3035 r->out.result = werr;
3038 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);