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"
46 /****************************************************************
47 ****************************************************************/
49 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
52 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
53 DEBUG(1,("libnet_Join:\n%s", str)); \
57 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
58 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
59 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
60 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
62 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
65 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
66 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
70 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
71 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
72 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
73 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
75 /****************************************************************
76 ****************************************************************/
78 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
79 struct libnet_JoinCtx *r,
80 const char *format, ...)
84 if (r->out.error_string) {
88 va_start(args, format);
89 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
93 /****************************************************************
94 ****************************************************************/
96 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
97 struct libnet_UnjoinCtx *r,
98 const char *format, ...)
102 if (r->out.error_string) {
106 va_start(args, format);
107 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
113 /****************************************************************
114 ****************************************************************/
116 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
117 const char *netbios_domain_name,
119 const char *user_name,
120 const char *password,
124 ADS_STRUCT *my_ads = NULL;
127 my_ads = ads_init(dns_domain_name,
131 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
135 SAFE_FREE(my_ads->auth.user_name);
136 my_ads->auth.user_name = SMB_STRDUP(user_name);
137 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
139 SAFE_FREE(my_ads->auth.realm);
140 my_ads->auth.realm = smb_xstrdup(cp);
141 if (!strupper_m(my_ads->auth.realm)) {
142 ads_destroy(&my_ads);
143 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
149 SAFE_FREE(my_ads->auth.password);
150 my_ads->auth.password = SMB_STRDUP(password);
153 status = ads_connect_user_creds(my_ads);
154 if (!ADS_ERR_OK(status)) {
155 ads_destroy(&my_ads);
163 /****************************************************************
164 ****************************************************************/
166 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
167 struct libnet_JoinCtx *r)
171 status = libnet_connect_ads(r->out.dns_domain_name,
172 r->out.netbios_domain_name,
175 r->in.admin_password,
177 if (!ADS_ERR_OK(status)) {
178 libnet_join_set_error_string(mem_ctx, r,
179 "failed to connect to AD: %s",
184 if (!r->out.netbios_domain_name) {
185 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
186 r->in.ads->server.workgroup);
187 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
190 if (!r->out.dns_domain_name) {
191 r->out.dns_domain_name = talloc_strdup(mem_ctx,
192 r->in.ads->config.realm);
193 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
196 r->out.domain_is_ad = true;
201 /****************************************************************
202 ****************************************************************/
204 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
205 struct libnet_UnjoinCtx *r)
209 status = libnet_connect_ads(r->in.domain_name,
213 r->in.admin_password,
215 if (!ADS_ERR_OK(status)) {
216 libnet_unjoin_set_error_string(mem_ctx, r,
217 "failed to connect to AD: %s",
224 /****************************************************************
225 join a domain using ADS (LDAP mods)
226 ****************************************************************/
228 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
229 struct libnet_JoinCtx *r)
232 LDAPMessage *res = NULL;
233 const char *attrs[] = { "dn", NULL };
236 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
237 if (!ADS_ERR_OK(status)) {
241 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
242 if (!ADS_ERR_OK(status)) {
246 if (ads_count_replies(r->in.ads, res) != 1) {
247 ads_msgfree(r->in.ads, res);
248 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
251 ads_msgfree(r->in.ads, res);
253 /* Attempt to create the machine account and bail if this fails.
254 Assume that the admin wants exactly what they requested */
256 status = ads_create_machine_acct(r->in.ads,
260 if (ADS_ERR_OK(status)) {
261 DEBUG(1,("machine account creation created\n"));
263 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
264 (status.err.rc == LDAP_ALREADY_EXISTS)) {
265 status = ADS_SUCCESS;
268 if (!ADS_ERR_OK(status)) {
269 DEBUG(1,("machine account creation failed\n"));
273 status = ads_move_machine_acct(r->in.ads,
277 if (!ADS_ERR_OK(status)) {
278 DEBUG(1,("failure to locate/move pre-existing "
279 "machine account\n"));
283 DEBUG(1,("The machine account %s the specified OU.\n",
284 moved ? "was moved into" : "already exists in"));
289 /****************************************************************
290 ****************************************************************/
292 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
293 struct libnet_UnjoinCtx *r)
298 status = libnet_unjoin_connect_ads(mem_ctx, r);
299 if (!ADS_ERR_OK(status)) {
300 libnet_unjoin_set_error_string(mem_ctx, r,
301 "failed to connect to AD: %s",
307 status = ads_leave_realm(r->in.ads, r->in.machine_name);
308 if (!ADS_ERR_OK(status)) {
309 libnet_unjoin_set_error_string(mem_ctx, r,
310 "failed to leave realm: %s",
318 /****************************************************************
319 ****************************************************************/
321 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
322 struct libnet_JoinCtx *r)
325 LDAPMessage *res = NULL;
328 if (!r->in.machine_name) {
329 return ADS_ERROR(LDAP_NO_MEMORY);
332 status = ads_find_machine_acct(r->in.ads,
335 if (!ADS_ERR_OK(status)) {
339 if (ads_count_replies(r->in.ads, res) != 1) {
340 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
344 dn = ads_get_dn(r->in.ads, mem_ctx, res);
346 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
350 r->out.dn = talloc_strdup(mem_ctx, dn);
352 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
357 ads_msgfree(r->in.ads, res);
363 static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
364 struct libnet_JoinCtx *r,
370 if (r->in.machine_name == NULL) {
371 return ADS_ERROR_SYSTEM(EINVAL);
374 status = ads_get_service_principal_names(mem_ctx,
383 /****************************************************************
384 Set a machines dNSHostName and servicePrincipalName attributes
385 ****************************************************************/
387 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
388 struct libnet_JoinCtx *r)
393 const char **spn_array = NULL;
400 status = libnet_join_find_machine_acct(mem_ctx, r);
401 if (!ADS_ERR_OK(status)) {
405 status = libnet_join_get_machine_spns(mem_ctx,
407 discard_const_p(char **, &spn_array),
409 if (!ADS_ERR_OK(status)) {
410 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
413 /* Windows only creates HOST/shortname & HOST/fqdn. */
415 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
417 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
419 if (!strupper_m(spn)) {
420 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
423 ok = ads_element_in_array(spn_array, num_spns, spn);
425 ok = add_string_to_array(spn_array, spn,
426 &spn_array, &num_spns);
428 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
432 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
433 || (strchr(my_fqdn, '.') == NULL)) {
434 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
435 r->out.dns_domain_name);
438 if (!strlower_m(my_fqdn)) {
439 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
442 if (!strequal(my_fqdn, r->in.machine_name)) {
443 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
445 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
448 ok = ads_element_in_array(spn_array, num_spns, spn);
450 ok = add_string_to_array(spn_array, spn,
451 &spn_array, &num_spns);
453 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
458 /* make sure to NULL terminate the array */
459 spn_array = talloc_realloc(mem_ctx, spn_array, const char *, num_spns + 1);
460 if (spn_array == NULL) {
461 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
463 spn_array[num_spns] = NULL;
465 mods = ads_init_mods(mem_ctx);
467 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
470 /* fields of primary importance */
472 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
473 if (!ADS_ERR_OK(status)) {
474 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
477 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
479 if (!ADS_ERR_OK(status)) {
480 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
483 return ads_gen_mod(r->in.ads, r->out.dn, mods);
486 /****************************************************************
487 ****************************************************************/
489 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
490 struct libnet_JoinCtx *r)
495 if (!r->in.create_upn) {
501 status = libnet_join_find_machine_acct(mem_ctx, r);
502 if (!ADS_ERR_OK(status)) {
507 const char *realm = r->out.dns_domain_name;
509 /* in case we are about to generate a keytab during the join
510 * make sure the default upn we create is usable with kinit -k.
513 if (USE_KERBEROS_KEYTAB) {
514 realm = talloc_strdup_upper(mem_ctx,
515 r->out.dns_domain_name);
519 return ADS_ERROR(LDAP_NO_MEMORY);
522 r->in.upn = talloc_asprintf(mem_ctx,
527 return ADS_ERROR(LDAP_NO_MEMORY);
531 /* now do the mods */
533 mods = ads_init_mods(mem_ctx);
535 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
538 /* fields of primary importance */
540 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
541 if (!ADS_ERR_OK(status)) {
542 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
545 return ads_gen_mod(r->in.ads, r->out.dn, mods);
549 /****************************************************************
550 ****************************************************************/
552 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
553 struct libnet_JoinCtx *r)
559 if (!r->in.os_name || !r->in.os_version ) {
565 status = libnet_join_find_machine_acct(mem_ctx, r);
566 if (!ADS_ERR_OK(status)) {
570 /* now do the mods */
572 mods = ads_init_mods(mem_ctx);
574 return ADS_ERROR(LDAP_NO_MEMORY);
577 if (r->in.os_servicepack) {
579 * if blank string then leave os_sp equal to NULL to force
580 * attribute delete (LDAP_MOD_DELETE)
582 if (!strequal(r->in.os_servicepack,"")) {
583 os_sp = talloc_strdup(mem_ctx, r->in.os_servicepack);
586 os_sp = talloc_asprintf(mem_ctx, "Samba %s",
587 samba_version_string());
589 if (!os_sp && !strequal(r->in.os_servicepack,"")) {
590 return ADS_ERROR(LDAP_NO_MEMORY);
593 /* fields of primary importance */
595 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
597 if (!ADS_ERR_OK(status)) {
601 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
603 if (!ADS_ERR_OK(status)) {
607 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
609 if (!ADS_ERR_OK(status)) {
613 return ads_gen_mod(r->in.ads, r->out.dn, mods);
616 /****************************************************************
617 ****************************************************************/
619 static ADS_STATUS libnet_join_set_etypes(TALLOC_CTX *mem_ctx,
620 struct libnet_JoinCtx *r)
624 uint32_t etype_list = ENC_CRC32 | ENC_RSA_MD5 | ENC_RC4_HMAC_MD5;
625 const char *etype_list_str;
627 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
628 etype_list |= ENC_HMAC_SHA1_96_AES128;
630 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
631 etype_list |= ENC_HMAC_SHA1_96_AES256;
634 etype_list_str = talloc_asprintf(mem_ctx, "%d", etype_list);
635 if (!etype_list_str) {
636 return ADS_ERROR(LDAP_NO_MEMORY);
641 status = libnet_join_find_machine_acct(mem_ctx, r);
642 if (!ADS_ERR_OK(status)) {
646 /* now do the mods */
648 mods = ads_init_mods(mem_ctx);
650 return ADS_ERROR(LDAP_NO_MEMORY);
653 status = ads_mod_str(mem_ctx, &mods, "msDS-SupportedEncryptionTypes",
655 if (!ADS_ERR_OK(status)) {
659 return ads_gen_mod(r->in.ads, r->out.dn, mods);
662 /****************************************************************
663 ****************************************************************/
665 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
666 struct libnet_JoinCtx *r)
668 if (!USE_SYSTEM_KEYTAB) {
672 if (ads_keytab_create_default(r->in.ads) != 0) {
679 /****************************************************************
680 ****************************************************************/
682 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
683 struct libnet_JoinCtx *r)
685 uint32_t domain_func;
687 const char *salt = NULL;
688 char *std_salt = NULL;
690 status = ads_domain_func_level(r->in.ads, &domain_func);
691 if (!ADS_ERR_OK(status)) {
692 libnet_join_set_error_string(mem_ctx, r,
693 "failed to determine domain functional level: %s",
698 /* go ahead and setup the default salt */
700 std_salt = kerberos_standard_des_salt();
702 libnet_join_set_error_string(mem_ctx, r,
703 "failed to obtain standard DES salt");
707 salt = talloc_strdup(mem_ctx, std_salt);
714 /* if it's a Windows functional domain, we have to look for the UPN */
716 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
719 upn = ads_get_upn(r->in.ads, mem_ctx,
722 salt = talloc_strdup(mem_ctx, upn);
729 return kerberos_secrets_store_des_salt(salt);
732 /****************************************************************
733 ****************************************************************/
735 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
736 struct libnet_JoinCtx *r)
739 uint32_t func_level = 0;
742 status = libnet_join_connect_ads(mem_ctx, r);
743 if (!ADS_ERR_OK(status)) {
748 status = libnet_join_set_machine_spn(mem_ctx, r);
749 if (!ADS_ERR_OK(status)) {
750 libnet_join_set_error_string(mem_ctx, r,
751 "Failed to set machine spn: %s\n"
752 "Do you have sufficient permissions to create machine "
758 status = libnet_join_set_os_attributes(mem_ctx, r);
759 if (!ADS_ERR_OK(status)) {
760 libnet_join_set_error_string(mem_ctx, r,
761 "failed to set machine os attributes: %s",
766 status = libnet_join_set_machine_upn(mem_ctx, r);
767 if (!ADS_ERR_OK(status)) {
768 libnet_join_set_error_string(mem_ctx, r,
769 "failed to set machine upn: %s",
774 status = ads_domain_func_level(r->in.ads, &func_level);
775 if (!ADS_ERR_OK(status)) {
776 libnet_join_set_error_string(mem_ctx, r,
777 "failed to query domain controller functional level: %s",
782 if (func_level >= DS_DOMAIN_FUNCTION_2008) {
783 status = libnet_join_set_etypes(mem_ctx, r);
784 if (!ADS_ERR_OK(status)) {
785 libnet_join_set_error_string(mem_ctx, r,
786 "failed to set machine kerberos encryption types: %s",
792 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
793 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
796 if (!libnet_join_create_keytab(mem_ctx, r)) {
797 libnet_join_set_error_string(mem_ctx, r,
798 "failed to create kerberos keytab");
799 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
804 #endif /* HAVE_ADS */
806 /****************************************************************
807 Store the machine password and domain SID
808 ****************************************************************/
810 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
811 struct libnet_JoinCtx *r)
813 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
816 DEBUG(1,("Failed to save domain sid\n"));
820 if (!secrets_store_machine_password(r->in.machine_password,
821 r->out.netbios_domain_name,
822 r->in.secure_channel_type))
824 DEBUG(1,("Failed to save machine password\n"));
831 /****************************************************************
832 Connect dc's IPC$ share
833 ****************************************************************/
835 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
840 struct cli_state **cli)
845 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
848 if (use_kerberos && pass) {
849 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
852 return cli_full_connection(cli, NULL,
860 SMB_SIGNING_DEFAULT);
863 /****************************************************************
864 Lookup domain dc's info
865 ****************************************************************/
867 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
868 struct libnet_JoinCtx *r,
869 struct cli_state **cli)
871 struct rpc_pipe_client *pipe_hnd = NULL;
872 struct policy_handle lsa_pol;
873 NTSTATUS status, result;
874 union lsa_PolicyInformation *info = NULL;
875 struct dcerpc_binding_handle *b;
877 status = libnet_join_connect_dc_ipc(r->in.dc_name,
880 r->in.admin_password,
883 if (!NT_STATUS_IS_OK(status)) {
887 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
889 if (!NT_STATUS_IS_OK(status)) {
890 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
895 b = pipe_hnd->binding_handle;
897 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
898 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
899 if (!NT_STATUS_IS_OK(status)) {
903 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
908 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
909 r->out.domain_is_ad = true;
910 r->out.netbios_domain_name = info->dns.name.string;
911 r->out.dns_domain_name = info->dns.dns_domain.string;
912 r->out.forest_name = info->dns.dns_forest.string;
913 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
914 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
917 if (!NT_STATUS_IS_OK(status)) {
918 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
920 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
923 if (!NT_STATUS_IS_OK(status)) {
926 if (!NT_STATUS_IS_OK(result)) {
931 r->out.netbios_domain_name = info->account_domain.name.string;
932 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
933 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
936 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
937 TALLOC_FREE(pipe_hnd);
943 /****************************************************************
944 Do the domain join unsecure
945 ****************************************************************/
947 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
948 struct libnet_JoinCtx *r,
949 struct cli_state *cli)
951 TALLOC_CTX *frame = talloc_stackframe();
952 struct rpc_pipe_client *netlogon_pipe = NULL;
953 struct netlogon_creds_cli_context *netlogon_creds = NULL;
954 struct samr_Password current_nt_hash;
955 const char *account_name = NULL;
958 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
960 if (!NT_STATUS_IS_OK(status)) {
965 if (!r->in.machine_password) {
966 r->in.machine_password = generate_random_password(mem_ctx,
967 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
968 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
969 if (r->in.machine_password == NULL) {
971 return NT_STATUS_NO_MEMORY;
975 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
976 E_md4hash(r->in.admin_password, current_nt_hash.hash);
978 account_name = talloc_asprintf(frame, "%s$",
980 if (account_name == NULL) {
982 return NT_STATUS_NO_MEMORY;
985 status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
988 r->in.secure_channel_type,
992 if (!NT_STATUS_IS_OK(status)) {
997 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
999 true, /* force_reauth */
1001 NULL); /* previous_nt_hash */
1002 if (!NT_STATUS_IS_OK(status)) {
1007 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
1008 netlogon_pipe->binding_handle,
1009 r->in.machine_password,
1010 NULL); /* new_version */
1011 if (!NT_STATUS_IS_OK(status)) {
1017 return NT_STATUS_OK;
1020 /****************************************************************
1022 ****************************************************************/
1024 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
1025 struct libnet_JoinCtx *r,
1026 struct cli_state *cli)
1028 struct rpc_pipe_client *pipe_hnd = NULL;
1029 struct policy_handle sam_pol, domain_pol, user_pol;
1030 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1032 struct lsa_String lsa_acct_name;
1034 uint32_t acct_flags = ACB_WSTRUST;
1035 struct samr_Ids user_rids;
1036 struct samr_Ids name_types;
1037 union samr_UserInfo user_info;
1038 struct dcerpc_binding_handle *b = NULL;
1039 unsigned int old_timeout = 0;
1041 DATA_BLOB session_key = data_blob_null;
1042 struct samr_CryptPassword crypt_pwd;
1043 struct samr_CryptPasswordEx crypt_pwd_ex;
1045 ZERO_STRUCT(sam_pol);
1046 ZERO_STRUCT(domain_pol);
1047 ZERO_STRUCT(user_pol);
1049 switch (r->in.secure_channel_type) {
1050 case SEC_CHAN_WKSTA:
1051 acct_flags = ACB_WSTRUST;
1054 acct_flags = ACB_SVRTRUST;
1057 return NT_STATUS_INVALID_PARAMETER;
1060 if (!r->in.machine_password) {
1061 r->in.machine_password = generate_random_password(mem_ctx,
1062 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
1063 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
1064 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
1067 /* Open the domain */
1069 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1071 if (!NT_STATUS_IS_OK(status)) {
1072 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1073 nt_errstr(status)));
1077 b = pipe_hnd->binding_handle;
1079 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1080 if (!NT_STATUS_IS_OK(status)) {
1081 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1082 nt_errstr(status)));
1086 status = dcerpc_samr_Connect2(b, mem_ctx,
1088 SAMR_ACCESS_ENUM_DOMAINS
1089 | SAMR_ACCESS_LOOKUP_DOMAIN,
1092 if (!NT_STATUS_IS_OK(status)) {
1095 if (!NT_STATUS_IS_OK(result)) {
1100 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1102 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1103 | SAMR_DOMAIN_ACCESS_CREATE_USER
1104 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1108 if (!NT_STATUS_IS_OK(status)) {
1111 if (!NT_STATUS_IS_OK(result)) {
1116 /* Create domain user */
1118 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1119 if (!strlower_m(acct_name)) {
1120 status = NT_STATUS_INVALID_PARAMETER;
1124 init_lsa_String(&lsa_acct_name, acct_name);
1126 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1127 uint32_t access_desired =
1128 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1129 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1130 SAMR_USER_ACCESS_SET_PASSWORD |
1131 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1132 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1133 uint32_t access_granted = 0;
1135 DEBUG(10,("Creating account with desired access mask: %d\n",
1138 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1147 if (!NT_STATUS_IS_OK(status)) {
1152 if (!NT_STATUS_IS_OK(status) &&
1153 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1155 DEBUG(10,("Creation of workstation account failed: %s\n",
1156 nt_errstr(status)));
1158 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1159 username/password combo but the user does not have
1160 administrator access. */
1162 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1163 libnet_join_set_error_string(mem_ctx, r,
1164 "User specified does not have "
1165 "administrator privileges");
1171 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1172 if (!(r->in.join_flags &
1173 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1178 /* We *must* do this.... don't ask... */
1180 if (NT_STATUS_IS_OK(status)) {
1181 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1185 status = dcerpc_samr_LookupNames(b, mem_ctx,
1192 if (!NT_STATUS_IS_OK(status)) {
1195 if (!NT_STATUS_IS_OK(result)) {
1199 if (user_rids.count != 1) {
1200 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1203 if (name_types.count != 1) {
1204 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1208 if (name_types.ids[0] != SID_NAME_USER) {
1209 DEBUG(0,("%s is not a user account (type=%d)\n",
1210 acct_name, name_types.ids[0]));
1211 status = NT_STATUS_INVALID_WORKSTATION;
1215 user_rid = user_rids.ids[0];
1217 /* Open handle on user */
1219 status = dcerpc_samr_OpenUser(b, mem_ctx,
1221 SEC_FLAG_MAXIMUM_ALLOWED,
1225 if (!NT_STATUS_IS_OK(status)) {
1228 if (!NT_STATUS_IS_OK(result)) {
1233 /* Fill in the additional account flags now */
1235 acct_flags |= ACB_PWNOEXP;
1237 /* Set account flags on machine account */
1238 ZERO_STRUCT(user_info.info16);
1239 user_info.info16.acct_flags = acct_flags;
1241 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1246 if (!NT_STATUS_IS_OK(status)) {
1247 dcerpc_samr_DeleteUser(b, mem_ctx,
1251 libnet_join_set_error_string(mem_ctx, r,
1252 "Failed to set account flags for machine account (%s)\n",
1257 if (!NT_STATUS_IS_OK(result)) {
1260 dcerpc_samr_DeleteUser(b, mem_ctx,
1264 libnet_join_set_error_string(mem_ctx, r,
1265 "Failed to set account flags for machine account (%s)\n",
1270 /* Set password on machine account - first try level 26 */
1273 * increase the timeout as password filter modules on the DC
1274 * might delay the operation for a significant amount of time
1276 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1278 init_samr_CryptPasswordEx(r->in.machine_password,
1282 user_info.info26.password = crypt_pwd_ex;
1283 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1285 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1291 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1293 /* retry with level 24 */
1295 init_samr_CryptPassword(r->in.machine_password,
1299 user_info.info24.password = crypt_pwd;
1300 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1302 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1309 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1311 if (!NT_STATUS_IS_OK(status)) {
1313 dcerpc_samr_DeleteUser(b, mem_ctx,
1317 libnet_join_set_error_string(mem_ctx, r,
1318 "Failed to set password for machine account (%s)\n",
1322 if (!NT_STATUS_IS_OK(result)) {
1325 dcerpc_samr_DeleteUser(b, mem_ctx,
1329 libnet_join_set_error_string(mem_ctx, r,
1330 "Failed to set password for machine account (%s)\n",
1335 status = NT_STATUS_OK;
1342 data_blob_clear_free(&session_key);
1344 if (is_valid_policy_hnd(&sam_pol)) {
1345 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1347 if (is_valid_policy_hnd(&domain_pol)) {
1348 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1350 if (is_valid_policy_hnd(&user_pol)) {
1351 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1353 TALLOC_FREE(pipe_hnd);
1358 /****************************************************************
1359 ****************************************************************/
1361 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1362 const char *netbios_domain_name,
1363 const char *dc_name,
1364 const bool use_kerberos)
1366 TALLOC_CTX *frame = talloc_stackframe();
1367 struct cli_state *cli = NULL;
1368 struct rpc_pipe_client *netlogon_pipe = NULL;
1369 struct cli_credentials *cli_creds = NULL;
1370 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1371 struct netlogon_creds_CredentialState *creds = NULL;
1372 uint32_t netlogon_flags = 0;
1374 const char *machine_account = NULL;
1375 const char *machine_domain = NULL;
1376 const char *machine_password = NULL;
1381 return NT_STATUS_INVALID_PARAMETER;
1384 if (!secrets_init()) {
1386 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1389 status = pdb_get_trust_credentials(netbios_domain_name, NULL,
1391 if (!NT_STATUS_IS_OK(status)) {
1396 /* we don't want any old password */
1397 cli_credentials_set_old_password(cli_creds, NULL, CRED_SPECIFIED);
1400 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1403 machine_account = cli_credentials_get_username(cli_creds);
1404 machine_domain = cli_credentials_get_domain(cli_creds);
1405 machine_password = cli_credentials_get_password(cli_creds);
1407 status = cli_full_connection(&cli, NULL,
1415 SMB_SIGNING_DEFAULT);
1417 if (!NT_STATUS_IS_OK(status)) {
1418 status = cli_full_connection(&cli, NULL,
1426 SMB_SIGNING_DEFAULT);
1429 if (!NT_STATUS_IS_OK(status)) {
1434 status = rpccli_create_netlogon_creds_with_creds(cli_creds,
1439 if (!NT_STATUS_IS_OK(status)) {
1445 status = rpccli_setup_netlogon_creds_with_creds(cli, NCACN_NP,
1447 true, /* force_reauth */
1449 if (!NT_STATUS_IS_OK(status)) {
1450 DEBUG(0,("connect_to_domain_password_server: "
1451 "unable to open the domain client session to "
1452 "machine %s. Flags[0x%08X] Error was : %s.\n",
1453 dc_name, (unsigned)netlogon_flags,
1454 nt_errstr(status)));
1460 status = netlogon_creds_cli_get(netlogon_creds,
1463 if (!NT_STATUS_IS_OK(status)) {
1468 netlogon_flags = creds->negotiate_flags;
1471 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1474 return NT_STATUS_OK;
1477 status = cli_rpc_pipe_open_schannel_with_creds(
1478 cli, &ndr_table_netlogon, NCACN_NP,
1480 netlogon_creds, &netlogon_pipe);
1482 TALLOC_FREE(netlogon_pipe);
1484 if (!NT_STATUS_IS_OK(status)) {
1485 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1486 "on netlogon pipe to server %s for domain %s. "
1488 smbXcli_conn_remote_name(cli->conn),
1489 netbios_domain_name, nt_errstr(status)));
1497 return NT_STATUS_OK;
1500 /****************************************************************
1501 ****************************************************************/
1503 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1504 struct libnet_JoinCtx *r)
1508 status = libnet_join_ok(r->in.msg_ctx,
1509 r->out.netbios_domain_name,
1511 r->in.use_kerberos);
1512 if (!NT_STATUS_IS_OK(status)) {
1513 libnet_join_set_error_string(mem_ctx, r,
1514 "failed to verify domain membership after joining: %s",
1515 get_friendly_nt_error_msg(status));
1516 return WERR_SETUP_NOT_JOINED;
1522 /****************************************************************
1523 ****************************************************************/
1525 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1526 struct libnet_UnjoinCtx *r)
1528 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1532 if (!secrets_delete_domain_sid(lp_workgroup())) {
1539 /****************************************************************
1540 ****************************************************************/
1542 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1543 struct libnet_UnjoinCtx *r)
1545 struct cli_state *cli = NULL;
1546 struct rpc_pipe_client *pipe_hnd = NULL;
1547 struct policy_handle sam_pol, domain_pol, user_pol;
1548 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1551 struct lsa_String lsa_acct_name;
1552 struct samr_Ids user_rids;
1553 struct samr_Ids name_types;
1554 union samr_UserInfo *info = NULL;
1555 struct dcerpc_binding_handle *b = NULL;
1557 ZERO_STRUCT(sam_pol);
1558 ZERO_STRUCT(domain_pol);
1559 ZERO_STRUCT(user_pol);
1561 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1562 r->in.admin_account,
1564 r->in.admin_password,
1567 if (!NT_STATUS_IS_OK(status)) {
1571 /* Open the domain */
1573 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1575 if (!NT_STATUS_IS_OK(status)) {
1576 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1577 nt_errstr(status)));
1581 b = pipe_hnd->binding_handle;
1583 status = dcerpc_samr_Connect2(b, mem_ctx,
1585 SEC_FLAG_MAXIMUM_ALLOWED,
1588 if (!NT_STATUS_IS_OK(status)) {
1591 if (!NT_STATUS_IS_OK(result)) {
1596 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1598 SEC_FLAG_MAXIMUM_ALLOWED,
1602 if (!NT_STATUS_IS_OK(status)) {
1605 if (!NT_STATUS_IS_OK(result)) {
1610 /* Create domain user */
1612 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1613 if (!strlower_m(acct_name)) {
1614 status = NT_STATUS_INVALID_PARAMETER;
1618 init_lsa_String(&lsa_acct_name, acct_name);
1620 status = dcerpc_samr_LookupNames(b, mem_ctx,
1628 if (!NT_STATUS_IS_OK(status)) {
1631 if (!NT_STATUS_IS_OK(result)) {
1635 if (user_rids.count != 1) {
1636 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1639 if (name_types.count != 1) {
1640 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1644 if (name_types.ids[0] != SID_NAME_USER) {
1645 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1646 name_types.ids[0]));
1647 status = NT_STATUS_INVALID_WORKSTATION;
1651 user_rid = user_rids.ids[0];
1653 /* Open handle on user */
1655 status = dcerpc_samr_OpenUser(b, mem_ctx,
1657 SEC_FLAG_MAXIMUM_ALLOWED,
1661 if (!NT_STATUS_IS_OK(status)) {
1664 if (!NT_STATUS_IS_OK(result)) {
1671 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1676 if (!NT_STATUS_IS_OK(status)) {
1677 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1680 if (!NT_STATUS_IS_OK(result)) {
1682 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1686 /* now disable and setuser info */
1688 info->info16.acct_flags |= ACB_DISABLED;
1690 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1695 if (!NT_STATUS_IS_OK(status)) {
1696 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1699 if (!NT_STATUS_IS_OK(result)) {
1701 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1705 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1708 if (pipe_hnd && b) {
1709 if (is_valid_policy_hnd(&domain_pol)) {
1710 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1712 if (is_valid_policy_hnd(&sam_pol)) {
1713 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1715 TALLOC_FREE(pipe_hnd);
1725 /****************************************************************
1726 ****************************************************************/
1728 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1730 WERROR werr = WERR_OK;
1732 struct smbconf_ctx *ctx;
1734 err = smbconf_init_reg(r, &ctx, NULL);
1735 if (!SBC_ERROR_IS_OK(err)) {
1736 werr = WERR_NO_SUCH_SERVICE;
1740 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1742 err = smbconf_set_global_parameter(ctx, "security", "user");
1743 if (!SBC_ERROR_IS_OK(err)) {
1744 werr = WERR_NO_SUCH_SERVICE;
1748 err = smbconf_set_global_parameter(ctx, "workgroup",
1750 if (!SBC_ERROR_IS_OK(err)) {
1751 werr = WERR_NO_SUCH_SERVICE;
1755 smbconf_delete_global_parameter(ctx, "realm");
1759 err = smbconf_set_global_parameter(ctx, "security", "domain");
1760 if (!SBC_ERROR_IS_OK(err)) {
1761 werr = WERR_NO_SUCH_SERVICE;
1765 err = smbconf_set_global_parameter(ctx, "workgroup",
1766 r->out.netbios_domain_name);
1767 if (!SBC_ERROR_IS_OK(err)) {
1768 werr = WERR_NO_SUCH_SERVICE;
1772 if (r->out.domain_is_ad) {
1773 err = smbconf_set_global_parameter(ctx, "security", "ads");
1774 if (!SBC_ERROR_IS_OK(err)) {
1775 werr = WERR_NO_SUCH_SERVICE;
1779 err = smbconf_set_global_parameter(ctx, "realm",
1780 r->out.dns_domain_name);
1781 if (!SBC_ERROR_IS_OK(err)) {
1782 werr = WERR_NO_SUCH_SERVICE;
1788 smbconf_shutdown(ctx);
1792 /****************************************************************
1793 ****************************************************************/
1795 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1797 WERROR werr = WERR_OK;
1799 struct smbconf_ctx *ctx;
1801 err = smbconf_init_reg(r, &ctx, NULL);
1802 if (!SBC_ERROR_IS_OK(err)) {
1803 werr = WERR_NO_SUCH_SERVICE;
1807 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1809 err = smbconf_set_global_parameter(ctx, "security", "user");
1810 if (!SBC_ERROR_IS_OK(err)) {
1811 werr = WERR_NO_SUCH_SERVICE;
1815 err = smbconf_delete_global_parameter(ctx, "workgroup");
1816 if (!SBC_ERROR_IS_OK(err)) {
1817 werr = WERR_NO_SUCH_SERVICE;
1821 smbconf_delete_global_parameter(ctx, "realm");
1825 smbconf_shutdown(ctx);
1829 /****************************************************************
1830 ****************************************************************/
1832 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1836 if (!W_ERROR_IS_OK(r->out.result)) {
1837 return r->out.result;
1840 if (!r->in.modify_config) {
1844 werr = do_join_modify_vals_config(r);
1845 if (!W_ERROR_IS_OK(werr)) {
1849 lp_load_global(get_dyn_CONFIGFILE());
1851 r->out.modified_config = true;
1852 r->out.result = werr;
1857 /****************************************************************
1858 ****************************************************************/
1860 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1864 if (!W_ERROR_IS_OK(r->out.result)) {
1865 return r->out.result;
1868 if (!r->in.modify_config) {
1872 werr = do_unjoin_modify_vals_config(r);
1873 if (!W_ERROR_IS_OK(werr)) {
1877 lp_load_global(get_dyn_CONFIGFILE());
1879 r->out.modified_config = true;
1880 r->out.result = werr;
1885 /****************************************************************
1886 ****************************************************************/
1888 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1889 const char *domain_str,
1890 const char **domain_p,
1893 char *domain = NULL;
1895 const char *p = NULL;
1897 if (!domain_str || !domain_p || !dc_p) {
1901 p = strchr_m(domain_str, '\\');
1904 domain = talloc_strndup(mem_ctx, domain_str,
1905 PTR_DIFF(p, domain_str));
1906 dc = talloc_strdup(mem_ctx, p+1);
1911 domain = talloc_strdup(mem_ctx, domain_str);
1927 /****************************************************************
1928 ****************************************************************/
1930 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1931 struct libnet_JoinCtx *r)
1933 if (!r->in.domain_name) {
1934 libnet_join_set_error_string(mem_ctx, r,
1935 "No domain name defined");
1936 return WERR_INVALID_PARAM;
1939 if (strlen(r->in.machine_name) > 15) {
1940 libnet_join_set_error_string(mem_ctx, r,
1941 "Our netbios name can be at most 15 chars long, "
1942 "\"%s\" is %u chars long\n",
1944 (unsigned int)strlen(r->in.machine_name));
1945 return WERR_INVALID_PARAM;
1948 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1951 libnet_join_set_error_string(mem_ctx, r,
1952 "Failed to parse domain name");
1953 return WERR_INVALID_PARAM;
1956 if (!r->in.admin_domain) {
1957 char *admin_domain = NULL;
1958 char *admin_account = NULL;
1959 split_domain_user(mem_ctx,
1960 r->in.admin_account,
1963 r->in.admin_domain = admin_domain;
1964 r->in.admin_account = admin_account;
1967 if (!secrets_init()) {
1968 libnet_join_set_error_string(mem_ctx, r,
1969 "Unable to open secrets database");
1970 return WERR_CAN_NOT_COMPLETE;
1976 /****************************************************************
1977 ****************************************************************/
1979 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1983 /* Try adding dom admins to builtin\admins. Only log failures. */
1984 status = create_builtin_administrators(domain_sid);
1985 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1986 DEBUG(10,("Unable to auto-add domain administrators to "
1987 "BUILTIN\\Administrators during join because "
1988 "winbindd must be running.\n"));
1989 } else if (!NT_STATUS_IS_OK(status)) {
1990 DEBUG(5, ("Failed to auto-add domain administrators to "
1991 "BUILTIN\\Administrators during join: %s\n",
1992 nt_errstr(status)));
1995 /* Try adding dom users to builtin\users. Only log failures. */
1996 status = create_builtin_users(domain_sid);
1997 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1998 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1999 "during join because winbindd must be running.\n"));
2000 } else if (!NT_STATUS_IS_OK(status)) {
2001 DEBUG(5, ("Failed to auto-add domain administrators to "
2002 "BUILTIN\\Administrators during join: %s\n",
2003 nt_errstr(status)));
2007 /****************************************************************
2008 ****************************************************************/
2010 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
2011 struct libnet_JoinCtx *r)
2015 if (!W_ERROR_IS_OK(r->out.result)) {
2016 return r->out.result;
2019 werr = do_JoinConfig(r);
2020 if (!W_ERROR_IS_OK(werr)) {
2024 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2028 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
2029 if (r->out.dns_domain_name) {
2030 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
2034 if (r->out.domain_is_ad &&
2035 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2036 ADS_STATUS ads_status;
2038 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
2039 if (!ADS_ERR_OK(ads_status)) {
2040 return WERR_GENERAL_FAILURE;
2043 #endif /* HAVE_ADS */
2045 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
2050 /****************************************************************
2051 ****************************************************************/
2053 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
2056 ads_destroy(&r->in.ads);
2062 /****************************************************************
2063 ****************************************************************/
2065 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2068 ads_destroy(&r->in.ads);
2074 /****************************************************************
2075 ****************************************************************/
2077 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2078 struct libnet_JoinCtx **r)
2080 struct libnet_JoinCtx *ctx;
2082 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2087 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2089 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2090 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2092 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2099 /****************************************************************
2100 ****************************************************************/
2102 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2103 struct libnet_UnjoinCtx **r)
2105 struct libnet_UnjoinCtx *ctx;
2107 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2112 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2114 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2115 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2122 /****************************************************************
2123 ****************************************************************/
2125 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2126 struct libnet_JoinCtx *r)
2128 bool valid_security = false;
2129 bool valid_workgroup = false;
2130 bool valid_realm = false;
2132 /* check if configuration is already set correctly */
2134 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2136 switch (r->out.domain_is_ad) {
2138 valid_security = (lp_security() == SEC_DOMAIN)
2139 || (lp_server_role() == ROLE_DOMAIN_PDC)
2140 || (lp_server_role() == ROLE_DOMAIN_BDC);
2141 if (valid_workgroup && valid_security) {
2142 /* nothing to be done */
2147 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2148 switch (lp_security()) {
2151 valid_security = true;
2154 if (valid_workgroup && valid_realm && valid_security) {
2155 /* nothing to be done */
2161 /* check if we are supposed to manipulate configuration */
2163 if (!r->in.modify_config) {
2165 char *wrong_conf = talloc_strdup(mem_ctx, "");
2167 if (!valid_workgroup) {
2168 wrong_conf = talloc_asprintf_append(wrong_conf,
2169 "\"workgroup\" set to '%s', should be '%s'",
2170 lp_workgroup(), r->out.netbios_domain_name);
2171 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2175 wrong_conf = talloc_asprintf_append(wrong_conf,
2176 "\"realm\" set to '%s', should be '%s'",
2177 lp_realm(), r->out.dns_domain_name);
2178 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2181 if (!valid_security) {
2182 const char *sec = NULL;
2183 switch (lp_security()) {
2184 case SEC_USER: sec = "user"; break;
2185 case SEC_DOMAIN: sec = "domain"; break;
2186 case SEC_ADS: sec = "ads"; break;
2188 wrong_conf = talloc_asprintf_append(wrong_conf,
2189 "\"security\" set to '%s', should be %s",
2190 sec, r->out.domain_is_ad ?
2191 "either 'domain' or 'ads'" : "'domain'");
2192 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2195 libnet_join_set_error_string(mem_ctx, r,
2196 "Invalid configuration (%s) and configuration modification "
2197 "was not requested", wrong_conf);
2198 return WERR_CAN_NOT_COMPLETE;
2201 /* check if we are able to manipulate configuration */
2203 if (!lp_config_backend_is_registry()) {
2204 libnet_join_set_error_string(mem_ctx, r,
2205 "Configuration manipulation requested but not "
2206 "supported by backend");
2207 return WERR_NOT_SUPPORTED;
2213 /****************************************************************
2214 ****************************************************************/
2216 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2217 struct libnet_JoinCtx *r)
2221 struct cli_state *cli = NULL;
2223 ADS_STATUS ads_status;
2224 #endif /* HAVE_ADS */
2226 if (!r->in.dc_name) {
2227 struct netr_DsRGetDCNameInfo *info;
2229 uint32_t name_type_flags = 0;
2230 if (r->in.domain_name_type == JoinDomNameTypeDNS) {
2231 name_type_flags = DS_IS_DNS_NAME;
2232 } else if (r->in.domain_name_type == JoinDomNameTypeNBT) {
2233 name_type_flags = DS_IS_FLAT_NAME;
2235 status = dsgetdcname(mem_ctx,
2240 DS_FORCE_REDISCOVERY |
2241 DS_DIRECTORY_SERVICE_REQUIRED |
2242 DS_WRITABLE_REQUIRED |
2243 DS_RETURN_DNS_NAME |
2246 if (!NT_STATUS_IS_OK(status)) {
2247 libnet_join_set_error_string(mem_ctx, r,
2248 "failed to find DC for domain %s",
2250 get_friendly_nt_error_msg(status));
2251 return WERR_DCNOTFOUND;
2254 dc = strip_hostname(info->dc_unc);
2255 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2256 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2259 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2260 if (!NT_STATUS_IS_OK(status)) {
2261 libnet_join_set_error_string(mem_ctx, r,
2262 "failed to lookup DC info for domain '%s' over rpc: %s",
2263 r->in.domain_name, get_friendly_nt_error_msg(status));
2264 return ntstatus_to_werror(status);
2267 werr = libnet_join_check_config(mem_ctx, r);
2268 if (!W_ERROR_IS_OK(werr)) {
2274 create_local_private_krb5_conf_for_domain(
2275 r->out.dns_domain_name, r->out.netbios_domain_name,
2276 NULL, smbXcli_conn_remote_sockaddr(cli->conn));
2278 if (r->out.domain_is_ad && r->in.account_ou &&
2279 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2281 ads_status = libnet_join_connect_ads(mem_ctx, r);
2282 if (!ADS_ERR_OK(ads_status)) {
2283 return WERR_DEFAULT_JOIN_REQUIRED;
2286 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2287 if (!ADS_ERR_OK(ads_status)) {
2288 libnet_join_set_error_string(mem_ctx, r,
2289 "failed to precreate account in ou %s: %s",
2291 ads_errstr(ads_status));
2292 return WERR_DEFAULT_JOIN_REQUIRED;
2295 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2297 #endif /* HAVE_ADS */
2299 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2300 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2301 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2303 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2305 if (!NT_STATUS_IS_OK(status)) {
2306 libnet_join_set_error_string(mem_ctx, r,
2307 "failed to join domain '%s' over rpc: %s",
2308 r->in.domain_name, get_friendly_nt_error_msg(status));
2309 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2310 return WERR_SETUP_ALREADY_JOINED;
2312 werr = ntstatus_to_werror(status);
2316 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2317 werr = WERR_SETUP_NOT_JOINED;
2331 /****************************************************************
2332 ****************************************************************/
2334 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2335 struct libnet_JoinCtx *r)
2338 struct libnet_UnjoinCtx *u = NULL;
2340 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2341 if (!W_ERROR_IS_OK(werr)) {
2345 u->in.debug = r->in.debug;
2346 u->in.dc_name = r->in.dc_name;
2347 u->in.domain_name = r->in.domain_name;
2348 u->in.admin_account = r->in.admin_account;
2349 u->in.admin_password = r->in.admin_password;
2350 u->in.modify_config = r->in.modify_config;
2351 u->in.use_kerberos = r->in.use_kerberos;
2352 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2353 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2355 werr = libnet_Unjoin(mem_ctx, u);
2361 /****************************************************************
2362 ****************************************************************/
2364 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2365 struct libnet_JoinCtx *r)
2370 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2373 ZERO_STRUCT(r->out);
2375 werr = libnet_join_pre_processing(mem_ctx, r);
2376 if (!W_ERROR_IS_OK(werr)) {
2380 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2381 werr = libnet_DomainJoin(mem_ctx, r);
2382 if (!W_ERROR_IS_OK(werr)) {
2387 werr = libnet_join_post_processing(mem_ctx, r);
2388 if (!W_ERROR_IS_OK(werr)) {
2392 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2393 werr = libnet_join_post_verify(mem_ctx, r);
2394 if (!W_ERROR_IS_OK(werr)) {
2395 libnet_join_rollback(mem_ctx, r);
2400 r->out.result = werr;
2403 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2408 /****************************************************************
2409 ****************************************************************/
2411 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2412 struct libnet_UnjoinCtx *r)
2416 if (!r->in.domain_sid) {
2418 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2419 libnet_unjoin_set_error_string(mem_ctx, r,
2420 "Unable to fetch domain sid: are we joined?");
2421 return WERR_SETUP_NOT_JOINED;
2423 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2424 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2427 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2428 !r->in.delete_machine_account) {
2429 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2433 if (!r->in.dc_name) {
2434 struct netr_DsRGetDCNameInfo *info;
2436 status = dsgetdcname(mem_ctx,
2441 DS_DIRECTORY_SERVICE_REQUIRED |
2442 DS_WRITABLE_REQUIRED |
2445 if (!NT_STATUS_IS_OK(status)) {
2446 libnet_unjoin_set_error_string(mem_ctx, r,
2447 "failed to find DC for domain %s",
2449 get_friendly_nt_error_msg(status));
2450 return WERR_DCNOTFOUND;
2453 dc = strip_hostname(info->dc_unc);
2454 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2455 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2459 /* for net ads leave, try to delete the account. If it works,
2460 no sense in disabling. If it fails, we can still try to
2463 if (r->in.delete_machine_account) {
2464 ADS_STATUS ads_status;
2465 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2466 if (ADS_ERR_OK(ads_status)) {
2468 r->out.dns_domain_name =
2469 talloc_strdup(mem_ctx,
2470 r->in.ads->server.realm);
2472 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2474 if (!ADS_ERR_OK(ads_status)) {
2475 libnet_unjoin_set_error_string(mem_ctx, r,
2476 "failed to remove machine account from AD: %s",
2477 ads_errstr(ads_status));
2479 r->out.deleted_machine_account = true;
2480 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2481 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2485 #endif /* HAVE_ADS */
2487 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2489 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2490 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2491 if (!NT_STATUS_IS_OK(status)) {
2492 libnet_unjoin_set_error_string(mem_ctx, r,
2493 "failed to disable machine account via rpc: %s",
2494 get_friendly_nt_error_msg(status));
2495 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2496 return WERR_SETUP_NOT_JOINED;
2498 return ntstatus_to_werror(status);
2501 r->out.disabled_machine_account = true;
2504 /* If disable succeeded or was not requested at all, we
2505 should be getting rid of our end of things */
2507 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2512 /****************************************************************
2513 ****************************************************************/
2515 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2516 struct libnet_UnjoinCtx *r)
2518 if (!r->in.domain_name) {
2519 libnet_unjoin_set_error_string(mem_ctx, r,
2520 "No domain name defined");
2521 return WERR_INVALID_PARAM;
2524 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2527 libnet_unjoin_set_error_string(mem_ctx, r,
2528 "Failed to parse domain name");
2529 return WERR_INVALID_PARAM;
2533 return WERR_SETUP_DOMAIN_CONTROLLER;
2536 if (!r->in.admin_domain) {
2537 char *admin_domain = NULL;
2538 char *admin_account = NULL;
2539 split_domain_user(mem_ctx,
2540 r->in.admin_account,
2543 r->in.admin_domain = admin_domain;
2544 r->in.admin_account = admin_account;
2547 if (!secrets_init()) {
2548 libnet_unjoin_set_error_string(mem_ctx, r,
2549 "Unable to open secrets database");
2550 return WERR_CAN_NOT_COMPLETE;
2556 /****************************************************************
2557 ****************************************************************/
2559 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2560 struct libnet_UnjoinCtx *r)
2562 saf_delete(r->out.netbios_domain_name);
2563 saf_delete(r->out.dns_domain_name);
2565 return libnet_unjoin_config(r);
2568 /****************************************************************
2569 ****************************************************************/
2571 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2572 struct libnet_UnjoinCtx *r)
2577 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2580 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2581 if (!W_ERROR_IS_OK(werr)) {
2585 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2586 werr = libnet_DomainUnjoin(mem_ctx, r);
2587 if (!W_ERROR_IS_OK(werr)) {
2588 libnet_unjoin_config(r);
2593 werr = libnet_unjoin_post_processing(mem_ctx, r);
2594 if (!W_ERROR_IS_OK(werr)) {
2599 r->out.result = werr;
2602 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);