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 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
579 return ADS_ERROR(LDAP_NO_MEMORY);
582 /* fields of primary importance */
584 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
586 if (!ADS_ERR_OK(status)) {
590 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
592 if (!ADS_ERR_OK(status)) {
596 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
598 if (!ADS_ERR_OK(status)) {
602 return ads_gen_mod(r->in.ads, r->out.dn, mods);
605 /****************************************************************
606 ****************************************************************/
608 static ADS_STATUS libnet_join_set_etypes(TALLOC_CTX *mem_ctx,
609 struct libnet_JoinCtx *r)
613 uint32_t etype_list = ENC_CRC32 | ENC_RSA_MD5 | ENC_RC4_HMAC_MD5;
614 const char *etype_list_str;
616 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
617 etype_list |= ENC_HMAC_SHA1_96_AES128;
619 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
620 etype_list |= ENC_HMAC_SHA1_96_AES256;
623 etype_list_str = talloc_asprintf(mem_ctx, "%d", etype_list);
624 if (!etype_list_str) {
625 return ADS_ERROR(LDAP_NO_MEMORY);
630 status = libnet_join_find_machine_acct(mem_ctx, r);
631 if (!ADS_ERR_OK(status)) {
635 /* now do the mods */
637 mods = ads_init_mods(mem_ctx);
639 return ADS_ERROR(LDAP_NO_MEMORY);
642 status = ads_mod_str(mem_ctx, &mods, "msDS-SupportedEncryptionTypes",
644 if (!ADS_ERR_OK(status)) {
648 return ads_gen_mod(r->in.ads, r->out.dn, mods);
651 /****************************************************************
652 ****************************************************************/
654 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
655 struct libnet_JoinCtx *r)
657 if (!USE_SYSTEM_KEYTAB) {
661 if (ads_keytab_create_default(r->in.ads) != 0) {
668 /****************************************************************
669 ****************************************************************/
671 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
672 struct libnet_JoinCtx *r)
674 uint32_t domain_func;
676 const char *salt = NULL;
677 char *std_salt = NULL;
679 status = ads_domain_func_level(r->in.ads, &domain_func);
680 if (!ADS_ERR_OK(status)) {
681 libnet_join_set_error_string(mem_ctx, r,
682 "failed to determine domain functional level: %s",
687 /* go ahead and setup the default salt */
689 std_salt = kerberos_standard_des_salt();
691 libnet_join_set_error_string(mem_ctx, r,
692 "failed to obtain standard DES salt");
696 salt = talloc_strdup(mem_ctx, std_salt);
703 /* if it's a Windows functional domain, we have to look for the UPN */
705 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
708 upn = ads_get_upn(r->in.ads, mem_ctx,
711 salt = talloc_strdup(mem_ctx, upn);
718 return kerberos_secrets_store_des_salt(salt);
721 /****************************************************************
722 ****************************************************************/
724 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
725 struct libnet_JoinCtx *r)
728 uint32_t func_level = 0;
731 status = libnet_join_connect_ads(mem_ctx, r);
732 if (!ADS_ERR_OK(status)) {
737 status = libnet_join_set_machine_spn(mem_ctx, r);
738 if (!ADS_ERR_OK(status)) {
739 libnet_join_set_error_string(mem_ctx, r,
740 "Failed to set machine spn: %s\n"
741 "Do you have sufficient permissions to create machine "
747 status = libnet_join_set_os_attributes(mem_ctx, r);
748 if (!ADS_ERR_OK(status)) {
749 libnet_join_set_error_string(mem_ctx, r,
750 "failed to set machine os attributes: %s",
755 status = libnet_join_set_machine_upn(mem_ctx, r);
756 if (!ADS_ERR_OK(status)) {
757 libnet_join_set_error_string(mem_ctx, r,
758 "failed to set machine upn: %s",
763 status = ads_domain_func_level(r->in.ads, &func_level);
764 if (!ADS_ERR_OK(status)) {
765 libnet_join_set_error_string(mem_ctx, r,
766 "failed to query domain controller functional level: %s",
771 if (func_level >= DS_DOMAIN_FUNCTION_2008) {
772 status = libnet_join_set_etypes(mem_ctx, r);
773 if (!ADS_ERR_OK(status)) {
774 libnet_join_set_error_string(mem_ctx, r,
775 "failed to set machine kerberos encryption types: %s",
781 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
782 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
785 if (!libnet_join_create_keytab(mem_ctx, r)) {
786 libnet_join_set_error_string(mem_ctx, r,
787 "failed to create kerberos keytab");
788 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
793 #endif /* HAVE_ADS */
795 /****************************************************************
796 Store the machine password and domain SID
797 ****************************************************************/
799 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
800 struct libnet_JoinCtx *r)
802 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
805 DEBUG(1,("Failed to save domain sid\n"));
809 if (!secrets_store_machine_password(r->in.machine_password,
810 r->out.netbios_domain_name,
811 r->in.secure_channel_type))
813 DEBUG(1,("Failed to save machine password\n"));
820 /****************************************************************
821 Connect dc's IPC$ share
822 ****************************************************************/
824 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
829 struct cli_state **cli)
834 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
837 if (use_kerberos && pass) {
838 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
841 return cli_full_connection(cli, NULL,
849 SMB_SIGNING_DEFAULT);
852 /****************************************************************
853 Lookup domain dc's info
854 ****************************************************************/
856 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
857 struct libnet_JoinCtx *r,
858 struct cli_state **cli)
860 struct rpc_pipe_client *pipe_hnd = NULL;
861 struct policy_handle lsa_pol;
862 NTSTATUS status, result;
863 union lsa_PolicyInformation *info = NULL;
864 struct dcerpc_binding_handle *b;
866 status = libnet_join_connect_dc_ipc(r->in.dc_name,
869 r->in.admin_password,
872 if (!NT_STATUS_IS_OK(status)) {
876 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
878 if (!NT_STATUS_IS_OK(status)) {
879 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
884 b = pipe_hnd->binding_handle;
886 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
887 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
888 if (!NT_STATUS_IS_OK(status)) {
892 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
897 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
898 r->out.domain_is_ad = true;
899 r->out.netbios_domain_name = info->dns.name.string;
900 r->out.dns_domain_name = info->dns.dns_domain.string;
901 r->out.forest_name = info->dns.dns_forest.string;
902 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
903 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
906 if (!NT_STATUS_IS_OK(status)) {
907 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
909 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
912 if (!NT_STATUS_IS_OK(status)) {
915 if (!NT_STATUS_IS_OK(result)) {
920 r->out.netbios_domain_name = info->account_domain.name.string;
921 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
922 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
925 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
926 TALLOC_FREE(pipe_hnd);
932 /****************************************************************
933 Do the domain join unsecure
934 ****************************************************************/
936 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
937 struct libnet_JoinCtx *r,
938 struct cli_state *cli)
940 TALLOC_CTX *frame = talloc_stackframe();
941 struct rpc_pipe_client *netlogon_pipe = NULL;
942 struct netlogon_creds_cli_context *netlogon_creds = NULL;
943 struct samr_Password current_nt_hash;
944 const char *account_name = NULL;
947 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
949 if (!NT_STATUS_IS_OK(status)) {
954 if (!r->in.machine_password) {
955 r->in.machine_password = generate_random_password(mem_ctx,
956 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
957 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
958 if (r->in.machine_password == NULL) {
960 return NT_STATUS_NO_MEMORY;
964 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
965 E_md4hash(r->in.admin_password, current_nt_hash.hash);
967 account_name = talloc_asprintf(frame, "%s$",
969 if (account_name == NULL) {
971 return NT_STATUS_NO_MEMORY;
974 status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
977 r->in.secure_channel_type,
981 if (!NT_STATUS_IS_OK(status)) {
986 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
988 true, /* force_reauth */
990 NULL); /* previous_nt_hash */
991 if (!NT_STATUS_IS_OK(status)) {
996 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
997 netlogon_pipe->binding_handle,
998 r->in.machine_password,
999 NULL); /* new_version */
1000 if (!NT_STATUS_IS_OK(status)) {
1006 return NT_STATUS_OK;
1009 /****************************************************************
1011 ****************************************************************/
1013 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
1014 struct libnet_JoinCtx *r,
1015 struct cli_state *cli)
1017 struct rpc_pipe_client *pipe_hnd = NULL;
1018 struct policy_handle sam_pol, domain_pol, user_pol;
1019 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1021 struct lsa_String lsa_acct_name;
1023 uint32_t acct_flags = ACB_WSTRUST;
1024 struct samr_Ids user_rids;
1025 struct samr_Ids name_types;
1026 union samr_UserInfo user_info;
1027 struct dcerpc_binding_handle *b = NULL;
1028 unsigned int old_timeout = 0;
1030 DATA_BLOB session_key = data_blob_null;
1031 struct samr_CryptPassword crypt_pwd;
1032 struct samr_CryptPasswordEx crypt_pwd_ex;
1034 ZERO_STRUCT(sam_pol);
1035 ZERO_STRUCT(domain_pol);
1036 ZERO_STRUCT(user_pol);
1038 switch (r->in.secure_channel_type) {
1039 case SEC_CHAN_WKSTA:
1040 acct_flags = ACB_WSTRUST;
1043 acct_flags = ACB_SVRTRUST;
1046 return NT_STATUS_INVALID_PARAMETER;
1049 if (!r->in.machine_password) {
1050 r->in.machine_password = generate_random_password(mem_ctx,
1051 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
1052 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
1053 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
1056 /* Open the domain */
1058 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1060 if (!NT_STATUS_IS_OK(status)) {
1061 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1062 nt_errstr(status)));
1066 b = pipe_hnd->binding_handle;
1068 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1069 if (!NT_STATUS_IS_OK(status)) {
1070 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1071 nt_errstr(status)));
1075 status = dcerpc_samr_Connect2(b, mem_ctx,
1077 SAMR_ACCESS_ENUM_DOMAINS
1078 | SAMR_ACCESS_LOOKUP_DOMAIN,
1081 if (!NT_STATUS_IS_OK(status)) {
1084 if (!NT_STATUS_IS_OK(result)) {
1089 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1091 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1092 | SAMR_DOMAIN_ACCESS_CREATE_USER
1093 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1097 if (!NT_STATUS_IS_OK(status)) {
1100 if (!NT_STATUS_IS_OK(result)) {
1105 /* Create domain user */
1107 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1108 if (!strlower_m(acct_name)) {
1109 status = NT_STATUS_INVALID_PARAMETER;
1113 init_lsa_String(&lsa_acct_name, acct_name);
1115 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1116 uint32_t access_desired =
1117 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1118 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1119 SAMR_USER_ACCESS_SET_PASSWORD |
1120 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1121 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1122 uint32_t access_granted = 0;
1124 DEBUG(10,("Creating account with desired access mask: %d\n",
1127 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1136 if (!NT_STATUS_IS_OK(status)) {
1141 if (!NT_STATUS_IS_OK(status) &&
1142 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1144 DEBUG(10,("Creation of workstation account failed: %s\n",
1145 nt_errstr(status)));
1147 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1148 username/password combo but the user does not have
1149 administrator access. */
1151 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1152 libnet_join_set_error_string(mem_ctx, r,
1153 "User specified does not have "
1154 "administrator privileges");
1160 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1161 if (!(r->in.join_flags &
1162 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1167 /* We *must* do this.... don't ask... */
1169 if (NT_STATUS_IS_OK(status)) {
1170 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1174 status = dcerpc_samr_LookupNames(b, mem_ctx,
1181 if (!NT_STATUS_IS_OK(status)) {
1184 if (!NT_STATUS_IS_OK(result)) {
1188 if (user_rids.count != 1) {
1189 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1192 if (name_types.count != 1) {
1193 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1197 if (name_types.ids[0] != SID_NAME_USER) {
1198 DEBUG(0,("%s is not a user account (type=%d)\n",
1199 acct_name, name_types.ids[0]));
1200 status = NT_STATUS_INVALID_WORKSTATION;
1204 user_rid = user_rids.ids[0];
1206 /* Open handle on user */
1208 status = dcerpc_samr_OpenUser(b, mem_ctx,
1210 SEC_FLAG_MAXIMUM_ALLOWED,
1214 if (!NT_STATUS_IS_OK(status)) {
1217 if (!NT_STATUS_IS_OK(result)) {
1222 /* Fill in the additional account flags now */
1224 acct_flags |= ACB_PWNOEXP;
1226 /* Set account flags on machine account */
1227 ZERO_STRUCT(user_info.info16);
1228 user_info.info16.acct_flags = acct_flags;
1230 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1235 if (!NT_STATUS_IS_OK(status)) {
1236 dcerpc_samr_DeleteUser(b, mem_ctx,
1240 libnet_join_set_error_string(mem_ctx, r,
1241 "Failed to set account flags for machine account (%s)\n",
1246 if (!NT_STATUS_IS_OK(result)) {
1249 dcerpc_samr_DeleteUser(b, mem_ctx,
1253 libnet_join_set_error_string(mem_ctx, r,
1254 "Failed to set account flags for machine account (%s)\n",
1259 /* Set password on machine account - first try level 26 */
1262 * increase the timeout as password filter modules on the DC
1263 * might delay the operation for a significant amount of time
1265 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1267 init_samr_CryptPasswordEx(r->in.machine_password,
1271 user_info.info26.password = crypt_pwd_ex;
1272 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1274 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1280 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1282 /* retry with level 24 */
1284 init_samr_CryptPassword(r->in.machine_password,
1288 user_info.info24.password = crypt_pwd;
1289 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1291 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1298 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1300 if (!NT_STATUS_IS_OK(status)) {
1302 dcerpc_samr_DeleteUser(b, mem_ctx,
1306 libnet_join_set_error_string(mem_ctx, r,
1307 "Failed to set password for machine account (%s)\n",
1311 if (!NT_STATUS_IS_OK(result)) {
1314 dcerpc_samr_DeleteUser(b, mem_ctx,
1318 libnet_join_set_error_string(mem_ctx, r,
1319 "Failed to set password for machine account (%s)\n",
1324 status = NT_STATUS_OK;
1331 data_blob_clear_free(&session_key);
1333 if (is_valid_policy_hnd(&sam_pol)) {
1334 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1336 if (is_valid_policy_hnd(&domain_pol)) {
1337 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1339 if (is_valid_policy_hnd(&user_pol)) {
1340 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1342 TALLOC_FREE(pipe_hnd);
1347 /****************************************************************
1348 ****************************************************************/
1350 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1351 const char *netbios_domain_name,
1352 const char *dc_name,
1353 const bool use_kerberos)
1355 TALLOC_CTX *frame = talloc_stackframe();
1356 struct cli_state *cli = NULL;
1357 struct rpc_pipe_client *netlogon_pipe = NULL;
1358 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1359 struct netlogon_creds_CredentialState *creds = NULL;
1360 uint32_t netlogon_flags = 0;
1361 enum netr_SchannelType sec_chan_type = 0;
1363 char *machine_password = NULL;
1364 const char *machine_name = NULL;
1365 const char *machine_account = NULL;
1367 struct samr_Password current_nt_hash;
1368 struct samr_Password *previous_nt_hash = NULL;
1373 return NT_STATUS_INVALID_PARAMETER;
1376 if (!secrets_init()) {
1378 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1381 ok = get_trust_pw_clear(netbios_domain_name,
1387 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1390 machine_account = talloc_asprintf(frame, "%s$", machine_name);
1391 if (machine_account == NULL) {
1392 SAFE_FREE(machine_password);
1393 SAFE_FREE(previous_nt_hash);
1395 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1399 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1402 status = cli_full_connection(&cli, NULL,
1407 netbios_domain_name,
1410 SMB_SIGNING_DEFAULT);
1412 E_md4hash(machine_password, current_nt_hash.hash);
1413 SAFE_FREE(machine_password);
1415 if (!NT_STATUS_IS_OK(status)) {
1416 status = cli_full_connection(&cli, NULL,
1424 SMB_SIGNING_DEFAULT);
1427 if (!NT_STATUS_IS_OK(status)) {
1428 SAFE_FREE(previous_nt_hash);
1433 status = rpccli_create_netlogon_creds(dc_name,
1434 netbios_domain_name,
1440 if (!NT_STATUS_IS_OK(status)) {
1441 SAFE_FREE(previous_nt_hash);
1447 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
1449 true, /* force_reauth */
1452 SAFE_FREE(previous_nt_hash);
1453 if (!NT_STATUS_IS_OK(status)) {
1454 DEBUG(0,("connect_to_domain_password_server: "
1455 "unable to open the domain client session to "
1456 "machine %s. Flags[0x%08X] Error was : %s.\n",
1457 dc_name, (unsigned)netlogon_flags,
1458 nt_errstr(status)));
1464 status = netlogon_creds_cli_get(netlogon_creds,
1467 if (!NT_STATUS_IS_OK(status)) {
1472 netlogon_flags = creds->negotiate_flags;
1475 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1478 return NT_STATUS_OK;
1481 status = cli_rpc_pipe_open_schannel_with_key(
1482 cli, &ndr_table_netlogon, NCACN_NP,
1483 netbios_domain_name,
1484 netlogon_creds, &netlogon_pipe);
1486 TALLOC_FREE(netlogon_pipe);
1488 if (!NT_STATUS_IS_OK(status)) {
1489 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1490 "on netlogon pipe to server %s for domain %s. "
1492 smbXcli_conn_remote_name(cli->conn),
1493 netbios_domain_name, nt_errstr(status)));
1501 return NT_STATUS_OK;
1504 /****************************************************************
1505 ****************************************************************/
1507 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1508 struct libnet_JoinCtx *r)
1512 status = libnet_join_ok(r->in.msg_ctx,
1513 r->out.netbios_domain_name,
1515 r->in.use_kerberos);
1516 if (!NT_STATUS_IS_OK(status)) {
1517 libnet_join_set_error_string(mem_ctx, r,
1518 "failed to verify domain membership after joining: %s",
1519 get_friendly_nt_error_msg(status));
1520 return WERR_SETUP_NOT_JOINED;
1526 /****************************************************************
1527 ****************************************************************/
1529 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1530 struct libnet_UnjoinCtx *r)
1532 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1536 if (!secrets_delete_domain_sid(lp_workgroup())) {
1543 /****************************************************************
1544 ****************************************************************/
1546 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1547 struct libnet_UnjoinCtx *r)
1549 struct cli_state *cli = NULL;
1550 struct rpc_pipe_client *pipe_hnd = NULL;
1551 struct policy_handle sam_pol, domain_pol, user_pol;
1552 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1555 struct lsa_String lsa_acct_name;
1556 struct samr_Ids user_rids;
1557 struct samr_Ids name_types;
1558 union samr_UserInfo *info = NULL;
1559 struct dcerpc_binding_handle *b = NULL;
1561 ZERO_STRUCT(sam_pol);
1562 ZERO_STRUCT(domain_pol);
1563 ZERO_STRUCT(user_pol);
1565 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1566 r->in.admin_account,
1568 r->in.admin_password,
1571 if (!NT_STATUS_IS_OK(status)) {
1575 /* Open the domain */
1577 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1579 if (!NT_STATUS_IS_OK(status)) {
1580 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1581 nt_errstr(status)));
1585 b = pipe_hnd->binding_handle;
1587 status = dcerpc_samr_Connect2(b, mem_ctx,
1589 SEC_FLAG_MAXIMUM_ALLOWED,
1592 if (!NT_STATUS_IS_OK(status)) {
1595 if (!NT_STATUS_IS_OK(result)) {
1600 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1602 SEC_FLAG_MAXIMUM_ALLOWED,
1606 if (!NT_STATUS_IS_OK(status)) {
1609 if (!NT_STATUS_IS_OK(result)) {
1614 /* Create domain user */
1616 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1617 if (!strlower_m(acct_name)) {
1618 status = NT_STATUS_INVALID_PARAMETER;
1622 init_lsa_String(&lsa_acct_name, acct_name);
1624 status = dcerpc_samr_LookupNames(b, mem_ctx,
1632 if (!NT_STATUS_IS_OK(status)) {
1635 if (!NT_STATUS_IS_OK(result)) {
1639 if (user_rids.count != 1) {
1640 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1643 if (name_types.count != 1) {
1644 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1648 if (name_types.ids[0] != SID_NAME_USER) {
1649 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1650 name_types.ids[0]));
1651 status = NT_STATUS_INVALID_WORKSTATION;
1655 user_rid = user_rids.ids[0];
1657 /* Open handle on user */
1659 status = dcerpc_samr_OpenUser(b, mem_ctx,
1661 SEC_FLAG_MAXIMUM_ALLOWED,
1665 if (!NT_STATUS_IS_OK(status)) {
1668 if (!NT_STATUS_IS_OK(result)) {
1675 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1680 if (!NT_STATUS_IS_OK(status)) {
1681 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1684 if (!NT_STATUS_IS_OK(result)) {
1686 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1690 /* now disable and setuser info */
1692 info->info16.acct_flags |= ACB_DISABLED;
1694 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1699 if (!NT_STATUS_IS_OK(status)) {
1700 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1703 if (!NT_STATUS_IS_OK(result)) {
1705 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1709 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1712 if (pipe_hnd && b) {
1713 if (is_valid_policy_hnd(&domain_pol)) {
1714 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1716 if (is_valid_policy_hnd(&sam_pol)) {
1717 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1719 TALLOC_FREE(pipe_hnd);
1729 /****************************************************************
1730 ****************************************************************/
1732 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1734 WERROR werr = WERR_OK;
1736 struct smbconf_ctx *ctx;
1738 err = smbconf_init_reg(r, &ctx, NULL);
1739 if (!SBC_ERROR_IS_OK(err)) {
1740 werr = WERR_NO_SUCH_SERVICE;
1744 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1746 err = smbconf_set_global_parameter(ctx, "security", "user");
1747 if (!SBC_ERROR_IS_OK(err)) {
1748 werr = WERR_NO_SUCH_SERVICE;
1752 err = smbconf_set_global_parameter(ctx, "workgroup",
1754 if (!SBC_ERROR_IS_OK(err)) {
1755 werr = WERR_NO_SUCH_SERVICE;
1759 smbconf_delete_global_parameter(ctx, "realm");
1763 err = smbconf_set_global_parameter(ctx, "security", "domain");
1764 if (!SBC_ERROR_IS_OK(err)) {
1765 werr = WERR_NO_SUCH_SERVICE;
1769 err = smbconf_set_global_parameter(ctx, "workgroup",
1770 r->out.netbios_domain_name);
1771 if (!SBC_ERROR_IS_OK(err)) {
1772 werr = WERR_NO_SUCH_SERVICE;
1776 if (r->out.domain_is_ad) {
1777 err = smbconf_set_global_parameter(ctx, "security", "ads");
1778 if (!SBC_ERROR_IS_OK(err)) {
1779 werr = WERR_NO_SUCH_SERVICE;
1783 err = smbconf_set_global_parameter(ctx, "realm",
1784 r->out.dns_domain_name);
1785 if (!SBC_ERROR_IS_OK(err)) {
1786 werr = WERR_NO_SUCH_SERVICE;
1792 smbconf_shutdown(ctx);
1796 /****************************************************************
1797 ****************************************************************/
1799 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1801 WERROR werr = WERR_OK;
1803 struct smbconf_ctx *ctx;
1805 err = smbconf_init_reg(r, &ctx, NULL);
1806 if (!SBC_ERROR_IS_OK(err)) {
1807 werr = WERR_NO_SUCH_SERVICE;
1811 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1813 err = smbconf_set_global_parameter(ctx, "security", "user");
1814 if (!SBC_ERROR_IS_OK(err)) {
1815 werr = WERR_NO_SUCH_SERVICE;
1819 err = smbconf_delete_global_parameter(ctx, "workgroup");
1820 if (!SBC_ERROR_IS_OK(err)) {
1821 werr = WERR_NO_SUCH_SERVICE;
1825 smbconf_delete_global_parameter(ctx, "realm");
1829 smbconf_shutdown(ctx);
1833 /****************************************************************
1834 ****************************************************************/
1836 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1840 if (!W_ERROR_IS_OK(r->out.result)) {
1841 return r->out.result;
1844 if (!r->in.modify_config) {
1848 werr = do_join_modify_vals_config(r);
1849 if (!W_ERROR_IS_OK(werr)) {
1853 lp_load_global(get_dyn_CONFIGFILE());
1855 r->out.modified_config = true;
1856 r->out.result = werr;
1861 /****************************************************************
1862 ****************************************************************/
1864 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1868 if (!W_ERROR_IS_OK(r->out.result)) {
1869 return r->out.result;
1872 if (!r->in.modify_config) {
1876 werr = do_unjoin_modify_vals_config(r);
1877 if (!W_ERROR_IS_OK(werr)) {
1881 lp_load_global(get_dyn_CONFIGFILE());
1883 r->out.modified_config = true;
1884 r->out.result = werr;
1889 /****************************************************************
1890 ****************************************************************/
1892 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1893 const char *domain_str,
1894 const char **domain_p,
1897 char *domain = NULL;
1899 const char *p = NULL;
1901 if (!domain_str || !domain_p || !dc_p) {
1905 p = strchr_m(domain_str, '\\');
1908 domain = talloc_strndup(mem_ctx, domain_str,
1909 PTR_DIFF(p, domain_str));
1910 dc = talloc_strdup(mem_ctx, p+1);
1915 domain = talloc_strdup(mem_ctx, domain_str);
1931 /****************************************************************
1932 ****************************************************************/
1934 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1935 struct libnet_JoinCtx *r)
1937 if (!r->in.domain_name) {
1938 libnet_join_set_error_string(mem_ctx, r,
1939 "No domain name defined");
1940 return WERR_INVALID_PARAM;
1943 if (strlen(r->in.machine_name) > 15) {
1944 libnet_join_set_error_string(mem_ctx, r,
1945 "Our netbios name can be at most 15 chars long, "
1946 "\"%s\" is %u chars long\n",
1948 (unsigned int)strlen(r->in.machine_name));
1949 return WERR_INVALID_PARAM;
1952 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1955 libnet_join_set_error_string(mem_ctx, r,
1956 "Failed to parse domain name");
1957 return WERR_INVALID_PARAM;
1961 return WERR_SETUP_DOMAIN_CONTROLLER;
1964 if (!r->in.admin_domain) {
1965 char *admin_domain = NULL;
1966 char *admin_account = NULL;
1967 split_domain_user(mem_ctx,
1968 r->in.admin_account,
1971 r->in.admin_domain = admin_domain;
1972 r->in.admin_account = admin_account;
1975 if (!secrets_init()) {
1976 libnet_join_set_error_string(mem_ctx, r,
1977 "Unable to open secrets database");
1978 return WERR_CAN_NOT_COMPLETE;
1984 /****************************************************************
1985 ****************************************************************/
1987 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1991 /* Try adding dom admins to builtin\admins. Only log failures. */
1992 status = create_builtin_administrators(domain_sid);
1993 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1994 DEBUG(10,("Unable to auto-add domain administrators to "
1995 "BUILTIN\\Administrators during join because "
1996 "winbindd must be running.\n"));
1997 } else if (!NT_STATUS_IS_OK(status)) {
1998 DEBUG(5, ("Failed to auto-add domain administrators to "
1999 "BUILTIN\\Administrators during join: %s\n",
2000 nt_errstr(status)));
2003 /* Try adding dom users to builtin\users. Only log failures. */
2004 status = create_builtin_users(domain_sid);
2005 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2006 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2007 "during join because winbindd must be running.\n"));
2008 } else if (!NT_STATUS_IS_OK(status)) {
2009 DEBUG(5, ("Failed to auto-add domain administrators to "
2010 "BUILTIN\\Administrators during join: %s\n",
2011 nt_errstr(status)));
2015 /****************************************************************
2016 ****************************************************************/
2018 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
2019 struct libnet_JoinCtx *r)
2023 if (!W_ERROR_IS_OK(r->out.result)) {
2024 return r->out.result;
2027 werr = do_JoinConfig(r);
2028 if (!W_ERROR_IS_OK(werr)) {
2032 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2036 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
2037 if (r->out.dns_domain_name) {
2038 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
2042 if (r->out.domain_is_ad &&
2043 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2044 ADS_STATUS ads_status;
2046 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
2047 if (!ADS_ERR_OK(ads_status)) {
2048 return WERR_GENERAL_FAILURE;
2051 #endif /* HAVE_ADS */
2053 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
2058 /****************************************************************
2059 ****************************************************************/
2061 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
2064 ads_destroy(&r->in.ads);
2070 /****************************************************************
2071 ****************************************************************/
2073 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2076 ads_destroy(&r->in.ads);
2082 /****************************************************************
2083 ****************************************************************/
2085 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2086 struct libnet_JoinCtx **r)
2088 struct libnet_JoinCtx *ctx;
2090 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2095 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2097 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2098 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2100 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2107 /****************************************************************
2108 ****************************************************************/
2110 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2111 struct libnet_UnjoinCtx **r)
2113 struct libnet_UnjoinCtx *ctx;
2115 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2120 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2122 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2123 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2130 /****************************************************************
2131 ****************************************************************/
2133 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2134 struct libnet_JoinCtx *r)
2136 bool valid_security = false;
2137 bool valid_workgroup = false;
2138 bool valid_realm = false;
2140 /* check if configuration is already set correctly */
2142 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2144 switch (r->out.domain_is_ad) {
2146 valid_security = (lp_security() == SEC_DOMAIN);
2147 if (valid_workgroup && valid_security) {
2148 /* nothing to be done */
2153 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2154 switch (lp_security()) {
2157 valid_security = true;
2160 if (valid_workgroup && valid_realm && valid_security) {
2161 /* nothing to be done */
2167 /* check if we are supposed to manipulate configuration */
2169 if (!r->in.modify_config) {
2171 char *wrong_conf = talloc_strdup(mem_ctx, "");
2173 if (!valid_workgroup) {
2174 wrong_conf = talloc_asprintf_append(wrong_conf,
2175 "\"workgroup\" set to '%s', should be '%s'",
2176 lp_workgroup(), r->out.netbios_domain_name);
2177 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2181 wrong_conf = talloc_asprintf_append(wrong_conf,
2182 "\"realm\" set to '%s', should be '%s'",
2183 lp_realm(), r->out.dns_domain_name);
2184 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2187 if (!valid_security) {
2188 const char *sec = NULL;
2189 switch (lp_security()) {
2190 case SEC_USER: sec = "user"; break;
2191 case SEC_DOMAIN: sec = "domain"; break;
2192 case SEC_ADS: sec = "ads"; break;
2194 wrong_conf = talloc_asprintf_append(wrong_conf,
2195 "\"security\" set to '%s', should be %s",
2196 sec, r->out.domain_is_ad ?
2197 "either 'domain' or 'ads'" : "'domain'");
2198 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2201 libnet_join_set_error_string(mem_ctx, r,
2202 "Invalid configuration (%s) and configuration modification "
2203 "was not requested", wrong_conf);
2204 return WERR_CAN_NOT_COMPLETE;
2207 /* check if we are able to manipulate configuration */
2209 if (!lp_config_backend_is_registry()) {
2210 libnet_join_set_error_string(mem_ctx, r,
2211 "Configuration manipulation requested but not "
2212 "supported by backend");
2213 return WERR_NOT_SUPPORTED;
2219 /****************************************************************
2220 ****************************************************************/
2222 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2223 struct libnet_JoinCtx *r)
2227 struct cli_state *cli = NULL;
2229 ADS_STATUS ads_status;
2230 #endif /* HAVE_ADS */
2232 if (!r->in.dc_name) {
2233 struct netr_DsRGetDCNameInfo *info;
2235 status = dsgetdcname(mem_ctx,
2240 DS_FORCE_REDISCOVERY |
2241 DS_DIRECTORY_SERVICE_REQUIRED |
2242 DS_WRITABLE_REQUIRED |
2245 if (!NT_STATUS_IS_OK(status)) {
2246 libnet_join_set_error_string(mem_ctx, r,
2247 "failed to find DC for domain %s",
2249 get_friendly_nt_error_msg(status));
2250 return WERR_DCNOTFOUND;
2253 dc = strip_hostname(info->dc_unc);
2254 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2255 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2258 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2259 if (!NT_STATUS_IS_OK(status)) {
2260 libnet_join_set_error_string(mem_ctx, r,
2261 "failed to lookup DC info for domain '%s' over rpc: %s",
2262 r->in.domain_name, get_friendly_nt_error_msg(status));
2263 return ntstatus_to_werror(status);
2266 werr = libnet_join_check_config(mem_ctx, r);
2267 if (!W_ERROR_IS_OK(werr)) {
2273 create_local_private_krb5_conf_for_domain(
2274 r->out.dns_domain_name, r->out.netbios_domain_name,
2275 NULL, smbXcli_conn_remote_sockaddr(cli->conn));
2277 if (r->out.domain_is_ad && r->in.account_ou &&
2278 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2280 ads_status = libnet_join_connect_ads(mem_ctx, r);
2281 if (!ADS_ERR_OK(ads_status)) {
2282 return WERR_DEFAULT_JOIN_REQUIRED;
2285 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2286 if (!ADS_ERR_OK(ads_status)) {
2287 libnet_join_set_error_string(mem_ctx, r,
2288 "failed to precreate account in ou %s: %s",
2290 ads_errstr(ads_status));
2291 return WERR_DEFAULT_JOIN_REQUIRED;
2294 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2296 #endif /* HAVE_ADS */
2298 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2299 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2300 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2302 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2304 if (!NT_STATUS_IS_OK(status)) {
2305 libnet_join_set_error_string(mem_ctx, r,
2306 "failed to join domain '%s' over rpc: %s",
2307 r->in.domain_name, get_friendly_nt_error_msg(status));
2308 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2309 return WERR_SETUP_ALREADY_JOINED;
2311 werr = ntstatus_to_werror(status);
2315 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2316 werr = WERR_SETUP_NOT_JOINED;
2330 /****************************************************************
2331 ****************************************************************/
2333 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2334 struct libnet_JoinCtx *r)
2337 struct libnet_UnjoinCtx *u = NULL;
2339 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2340 if (!W_ERROR_IS_OK(werr)) {
2344 u->in.debug = r->in.debug;
2345 u->in.dc_name = r->in.dc_name;
2346 u->in.domain_name = r->in.domain_name;
2347 u->in.admin_account = r->in.admin_account;
2348 u->in.admin_password = r->in.admin_password;
2349 u->in.modify_config = r->in.modify_config;
2350 u->in.use_kerberos = r->in.use_kerberos;
2351 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2352 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2354 werr = libnet_Unjoin(mem_ctx, u);
2360 /****************************************************************
2361 ****************************************************************/
2363 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2364 struct libnet_JoinCtx *r)
2369 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2372 ZERO_STRUCT(r->out);
2374 werr = libnet_join_pre_processing(mem_ctx, r);
2375 if (!W_ERROR_IS_OK(werr)) {
2379 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2380 werr = libnet_DomainJoin(mem_ctx, r);
2381 if (!W_ERROR_IS_OK(werr)) {
2386 werr = libnet_join_post_processing(mem_ctx, r);
2387 if (!W_ERROR_IS_OK(werr)) {
2391 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2392 werr = libnet_join_post_verify(mem_ctx, r);
2393 if (!W_ERROR_IS_OK(werr)) {
2394 libnet_join_rollback(mem_ctx, r);
2399 r->out.result = werr;
2402 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2407 /****************************************************************
2408 ****************************************************************/
2410 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2411 struct libnet_UnjoinCtx *r)
2415 if (!r->in.domain_sid) {
2417 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2418 libnet_unjoin_set_error_string(mem_ctx, r,
2419 "Unable to fetch domain sid: are we joined?");
2420 return WERR_SETUP_NOT_JOINED;
2422 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2423 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2426 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2427 !r->in.delete_machine_account) {
2428 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2432 if (!r->in.dc_name) {
2433 struct netr_DsRGetDCNameInfo *info;
2435 status = dsgetdcname(mem_ctx,
2440 DS_DIRECTORY_SERVICE_REQUIRED |
2441 DS_WRITABLE_REQUIRED |
2444 if (!NT_STATUS_IS_OK(status)) {
2445 libnet_unjoin_set_error_string(mem_ctx, r,
2446 "failed to find DC for domain %s",
2448 get_friendly_nt_error_msg(status));
2449 return WERR_DCNOTFOUND;
2452 dc = strip_hostname(info->dc_unc);
2453 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2454 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2458 /* for net ads leave, try to delete the account. If it works,
2459 no sense in disabling. If it fails, we can still try to
2462 if (r->in.delete_machine_account) {
2463 ADS_STATUS ads_status;
2464 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2465 if (ADS_ERR_OK(ads_status)) {
2467 r->out.dns_domain_name =
2468 talloc_strdup(mem_ctx,
2469 r->in.ads->server.realm);
2471 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2473 if (!ADS_ERR_OK(ads_status)) {
2474 libnet_unjoin_set_error_string(mem_ctx, r,
2475 "failed to remove machine account from AD: %s",
2476 ads_errstr(ads_status));
2478 r->out.deleted_machine_account = true;
2479 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2480 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2484 #endif /* HAVE_ADS */
2486 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2488 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2489 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2490 if (!NT_STATUS_IS_OK(status)) {
2491 libnet_unjoin_set_error_string(mem_ctx, r,
2492 "failed to disable machine account via rpc: %s",
2493 get_friendly_nt_error_msg(status));
2494 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2495 return WERR_SETUP_NOT_JOINED;
2497 return ntstatus_to_werror(status);
2500 r->out.disabled_machine_account = true;
2503 /* If disable succeeded or was not requested at all, we
2504 should be getting rid of our end of things */
2506 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2511 /****************************************************************
2512 ****************************************************************/
2514 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2515 struct libnet_UnjoinCtx *r)
2517 if (!r->in.domain_name) {
2518 libnet_unjoin_set_error_string(mem_ctx, r,
2519 "No domain name defined");
2520 return WERR_INVALID_PARAM;
2523 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2526 libnet_unjoin_set_error_string(mem_ctx, r,
2527 "Failed to parse domain name");
2528 return WERR_INVALID_PARAM;
2532 return WERR_SETUP_DOMAIN_CONTROLLER;
2535 if (!r->in.admin_domain) {
2536 char *admin_domain = NULL;
2537 char *admin_account = NULL;
2538 split_domain_user(mem_ctx,
2539 r->in.admin_account,
2542 r->in.admin_domain = admin_domain;
2543 r->in.admin_account = admin_account;
2546 if (!secrets_init()) {
2547 libnet_unjoin_set_error_string(mem_ctx, r,
2548 "Unable to open secrets database");
2549 return WERR_CAN_NOT_COMPLETE;
2555 /****************************************************************
2556 ****************************************************************/
2558 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2559 struct libnet_UnjoinCtx *r)
2561 saf_delete(r->out.netbios_domain_name);
2562 saf_delete(r->out.dns_domain_name);
2564 return libnet_unjoin_config(r);
2567 /****************************************************************
2568 ****************************************************************/
2570 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2571 struct libnet_UnjoinCtx *r)
2576 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2579 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2580 if (!W_ERROR_IS_OK(werr)) {
2584 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2585 werr = libnet_DomainUnjoin(mem_ctx, r);
2586 if (!W_ERROR_IS_OK(werr)) {
2587 libnet_unjoin_config(r);
2592 werr = libnet_unjoin_post_processing(mem_ctx, r);
2593 if (!W_ERROR_IS_OK(werr)) {
2598 r->out.result = werr;
2601 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);