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[3] = {NULL, NULL, NULL};
398 status = libnet_join_find_machine_acct(mem_ctx, r);
399 if (!ADS_ERR_OK(status)) {
403 /* Windows only creates HOST/shortname & HOST/fqdn. */
405 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
407 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
409 if (!strupper_m(spn)) {
410 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
414 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
415 || (strchr(my_fqdn, '.') == NULL)) {
416 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
417 r->out.dns_domain_name);
420 if (!strlower_m(my_fqdn)) {
421 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
424 if (!strequal(my_fqdn, r->in.machine_name)) {
425 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
427 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
432 mods = ads_init_mods(mem_ctx);
434 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
437 /* fields of primary importance */
439 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
440 if (!ADS_ERR_OK(status)) {
441 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
444 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
446 if (!ADS_ERR_OK(status)) {
447 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
450 return ads_gen_mod(r->in.ads, r->out.dn, mods);
453 /****************************************************************
454 ****************************************************************/
456 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
457 struct libnet_JoinCtx *r)
462 if (!r->in.create_upn) {
468 status = libnet_join_find_machine_acct(mem_ctx, r);
469 if (!ADS_ERR_OK(status)) {
474 const char *realm = r->out.dns_domain_name;
476 /* in case we are about to generate a keytab during the join
477 * make sure the default upn we create is usable with kinit -k.
480 if (USE_KERBEROS_KEYTAB) {
481 realm = talloc_strdup_upper(mem_ctx,
482 r->out.dns_domain_name);
486 return ADS_ERROR(LDAP_NO_MEMORY);
489 r->in.upn = talloc_asprintf(mem_ctx,
494 return ADS_ERROR(LDAP_NO_MEMORY);
498 /* now do the mods */
500 mods = ads_init_mods(mem_ctx);
502 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
505 /* fields of primary importance */
507 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
508 if (!ADS_ERR_OK(status)) {
509 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
512 return ads_gen_mod(r->in.ads, r->out.dn, mods);
516 /****************************************************************
517 ****************************************************************/
519 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
520 struct libnet_JoinCtx *r)
526 if (!r->in.os_name || !r->in.os_version ) {
532 status = libnet_join_find_machine_acct(mem_ctx, r);
533 if (!ADS_ERR_OK(status)) {
537 /* now do the mods */
539 mods = ads_init_mods(mem_ctx);
541 return ADS_ERROR(LDAP_NO_MEMORY);
544 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
546 return ADS_ERROR(LDAP_NO_MEMORY);
549 /* fields of primary importance */
551 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
553 if (!ADS_ERR_OK(status)) {
557 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
559 if (!ADS_ERR_OK(status)) {
563 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
565 if (!ADS_ERR_OK(status)) {
569 return ads_gen_mod(r->in.ads, r->out.dn, mods);
572 /****************************************************************
573 ****************************************************************/
575 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
576 struct libnet_JoinCtx *r)
578 if (!USE_SYSTEM_KEYTAB) {
582 if (ads_keytab_create_default(r->in.ads) != 0) {
589 /****************************************************************
590 ****************************************************************/
592 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
593 struct libnet_JoinCtx *r)
595 uint32_t domain_func;
597 const char *salt = NULL;
598 char *std_salt = NULL;
600 status = ads_domain_func_level(r->in.ads, &domain_func);
601 if (!ADS_ERR_OK(status)) {
602 libnet_join_set_error_string(mem_ctx, r,
603 "failed to determine domain functional level: %s",
608 /* go ahead and setup the default salt */
610 std_salt = kerberos_standard_des_salt();
612 libnet_join_set_error_string(mem_ctx, r,
613 "failed to obtain standard DES salt");
617 salt = talloc_strdup(mem_ctx, std_salt);
624 /* if it's a Windows functional domain, we have to look for the UPN */
626 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
629 upn = ads_get_upn(r->in.ads, mem_ctx,
632 salt = talloc_strdup(mem_ctx, upn);
639 return kerberos_secrets_store_des_salt(salt);
642 /****************************************************************
643 ****************************************************************/
645 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
646 struct libnet_JoinCtx *r)
651 status = libnet_join_connect_ads(mem_ctx, r);
652 if (!ADS_ERR_OK(status)) {
657 status = libnet_join_set_machine_spn(mem_ctx, r);
658 if (!ADS_ERR_OK(status)) {
659 libnet_join_set_error_string(mem_ctx, r,
660 "Failed to set machine spn: %s\n"
661 "Do you have sufficient permissions to create machine "
667 status = libnet_join_set_os_attributes(mem_ctx, r);
668 if (!ADS_ERR_OK(status)) {
669 libnet_join_set_error_string(mem_ctx, r,
670 "failed to set machine os attributes: %s",
675 status = libnet_join_set_machine_upn(mem_ctx, r);
676 if (!ADS_ERR_OK(status)) {
677 libnet_join_set_error_string(mem_ctx, r,
678 "failed to set machine upn: %s",
683 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
684 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
687 if (!libnet_join_create_keytab(mem_ctx, r)) {
688 libnet_join_set_error_string(mem_ctx, r,
689 "failed to create kerberos keytab");
690 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
695 #endif /* HAVE_ADS */
697 /****************************************************************
698 Store the machine password and domain SID
699 ****************************************************************/
701 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
702 struct libnet_JoinCtx *r)
704 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
707 DEBUG(1,("Failed to save domain sid\n"));
711 if (!secrets_store_machine_password(r->in.machine_password,
712 r->out.netbios_domain_name,
713 r->in.secure_channel_type))
715 DEBUG(1,("Failed to save machine password\n"));
722 /****************************************************************
723 Connect dc's IPC$ share
724 ****************************************************************/
726 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
731 struct cli_state **cli)
736 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
739 if (use_kerberos && pass) {
740 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
743 return cli_full_connection(cli, NULL,
751 SMB_SIGNING_DEFAULT);
754 /****************************************************************
755 Lookup domain dc's info
756 ****************************************************************/
758 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
759 struct libnet_JoinCtx *r,
760 struct cli_state **cli)
762 struct rpc_pipe_client *pipe_hnd = NULL;
763 struct policy_handle lsa_pol;
764 NTSTATUS status, result;
765 union lsa_PolicyInformation *info = NULL;
766 struct dcerpc_binding_handle *b;
768 status = libnet_join_connect_dc_ipc(r->in.dc_name,
771 r->in.admin_password,
774 if (!NT_STATUS_IS_OK(status)) {
778 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
780 if (!NT_STATUS_IS_OK(status)) {
781 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
786 b = pipe_hnd->binding_handle;
788 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
789 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
790 if (!NT_STATUS_IS_OK(status)) {
794 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
799 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
800 r->out.domain_is_ad = true;
801 r->out.netbios_domain_name = info->dns.name.string;
802 r->out.dns_domain_name = info->dns.dns_domain.string;
803 r->out.forest_name = info->dns.dns_forest.string;
804 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
805 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
808 if (!NT_STATUS_IS_OK(status)) {
809 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
811 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
814 if (!NT_STATUS_IS_OK(status)) {
817 if (!NT_STATUS_IS_OK(result)) {
822 r->out.netbios_domain_name = info->account_domain.name.string;
823 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
824 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
827 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
828 TALLOC_FREE(pipe_hnd);
834 /****************************************************************
835 Do the domain join unsecure
836 ****************************************************************/
838 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
839 struct libnet_JoinCtx *r,
840 struct cli_state *cli)
842 TALLOC_CTX *frame = talloc_stackframe();
843 struct rpc_pipe_client *netlogon_pipe = NULL;
844 struct netlogon_creds_cli_context *netlogon_creds = NULL;
845 struct samr_Password current_nt_hash;
846 const char *account_name = NULL;
849 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
851 if (!NT_STATUS_IS_OK(status)) {
856 if (!r->in.machine_password) {
857 r->in.machine_password = generate_random_password(mem_ctx,
858 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
859 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
860 if (r->in.machine_password == NULL) {
862 return NT_STATUS_NO_MEMORY;
866 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
867 E_md4hash(r->in.admin_password, current_nt_hash.hash);
869 account_name = talloc_asprintf(frame, "%s$",
871 if (account_name == NULL) {
873 return NT_STATUS_NO_MEMORY;
876 status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
879 r->in.secure_channel_type,
883 if (!NT_STATUS_IS_OK(status)) {
888 status = rpccli_setup_netlogon_creds(cli,
890 true, /* force_reauth */
892 NULL); /* previous_nt_hash */
893 if (!NT_STATUS_IS_OK(status)) {
898 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
899 netlogon_pipe->binding_handle,
900 r->in.machine_password,
901 NULL); /* new_version */
902 if (!NT_STATUS_IS_OK(status)) {
911 /****************************************************************
913 ****************************************************************/
915 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
916 struct libnet_JoinCtx *r,
917 struct cli_state *cli)
919 struct rpc_pipe_client *pipe_hnd = NULL;
920 struct policy_handle sam_pol, domain_pol, user_pol;
921 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
923 struct lsa_String lsa_acct_name;
925 uint32_t acct_flags = ACB_WSTRUST;
926 struct samr_Ids user_rids;
927 struct samr_Ids name_types;
928 union samr_UserInfo user_info;
929 struct dcerpc_binding_handle *b = NULL;
930 unsigned int old_timeout = 0;
932 DATA_BLOB session_key = data_blob_null;
933 struct samr_CryptPassword crypt_pwd;
934 struct samr_CryptPasswordEx crypt_pwd_ex;
936 ZERO_STRUCT(sam_pol);
937 ZERO_STRUCT(domain_pol);
938 ZERO_STRUCT(user_pol);
940 switch (r->in.secure_channel_type) {
942 acct_flags = ACB_WSTRUST;
945 acct_flags = ACB_SVRTRUST;
948 return NT_STATUS_INVALID_PARAMETER;
951 if (!r->in.machine_password) {
952 r->in.machine_password = generate_random_password(mem_ctx,
953 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
954 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
955 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
958 /* Open the domain */
960 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
962 if (!NT_STATUS_IS_OK(status)) {
963 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
968 b = pipe_hnd->binding_handle;
970 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
971 if (!NT_STATUS_IS_OK(status)) {
972 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
977 status = dcerpc_samr_Connect2(b, mem_ctx,
979 SAMR_ACCESS_ENUM_DOMAINS
980 | SAMR_ACCESS_LOOKUP_DOMAIN,
983 if (!NT_STATUS_IS_OK(status)) {
986 if (!NT_STATUS_IS_OK(result)) {
991 status = dcerpc_samr_OpenDomain(b, mem_ctx,
993 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
994 | SAMR_DOMAIN_ACCESS_CREATE_USER
995 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
999 if (!NT_STATUS_IS_OK(status)) {
1002 if (!NT_STATUS_IS_OK(result)) {
1007 /* Create domain user */
1009 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1010 if (!strlower_m(acct_name)) {
1011 status = NT_STATUS_INVALID_PARAMETER;
1015 init_lsa_String(&lsa_acct_name, acct_name);
1017 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1018 uint32_t access_desired =
1019 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1020 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1021 SAMR_USER_ACCESS_SET_PASSWORD |
1022 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1023 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1024 uint32_t access_granted = 0;
1026 DEBUG(10,("Creating account with desired access mask: %d\n",
1029 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1038 if (!NT_STATUS_IS_OK(status)) {
1043 if (!NT_STATUS_IS_OK(status) &&
1044 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1046 DEBUG(10,("Creation of workstation account failed: %s\n",
1047 nt_errstr(status)));
1049 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1050 username/password combo but the user does not have
1051 administrator access. */
1053 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1054 libnet_join_set_error_string(mem_ctx, r,
1055 "User specified does not have "
1056 "administrator privileges");
1062 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1063 if (!(r->in.join_flags &
1064 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1069 /* We *must* do this.... don't ask... */
1071 if (NT_STATUS_IS_OK(status)) {
1072 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1076 status = dcerpc_samr_LookupNames(b, mem_ctx,
1083 if (!NT_STATUS_IS_OK(status)) {
1086 if (!NT_STATUS_IS_OK(result)) {
1090 if (user_rids.count != 1) {
1091 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1094 if (name_types.count != 1) {
1095 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1099 if (name_types.ids[0] != SID_NAME_USER) {
1100 DEBUG(0,("%s is not a user account (type=%d)\n",
1101 acct_name, name_types.ids[0]));
1102 status = NT_STATUS_INVALID_WORKSTATION;
1106 user_rid = user_rids.ids[0];
1108 /* Open handle on user */
1110 status = dcerpc_samr_OpenUser(b, mem_ctx,
1112 SEC_FLAG_MAXIMUM_ALLOWED,
1116 if (!NT_STATUS_IS_OK(status)) {
1119 if (!NT_STATUS_IS_OK(result)) {
1124 /* Fill in the additional account flags now */
1126 acct_flags |= ACB_PWNOEXP;
1128 /* Set account flags on machine account */
1129 ZERO_STRUCT(user_info.info16);
1130 user_info.info16.acct_flags = acct_flags;
1132 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1137 if (!NT_STATUS_IS_OK(status)) {
1138 dcerpc_samr_DeleteUser(b, mem_ctx,
1142 libnet_join_set_error_string(mem_ctx, r,
1143 "Failed to set account flags for machine account (%s)\n",
1148 if (!NT_STATUS_IS_OK(result)) {
1151 dcerpc_samr_DeleteUser(b, mem_ctx,
1155 libnet_join_set_error_string(mem_ctx, r,
1156 "Failed to set account flags for machine account (%s)\n",
1161 /* Set password on machine account - first try level 26 */
1164 * increase the timeout as password filter modules on the DC
1165 * might delay the operation for a significant amount of time
1167 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1169 init_samr_CryptPasswordEx(r->in.machine_password,
1173 user_info.info26.password = crypt_pwd_ex;
1174 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1176 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1182 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1184 /* retry with level 24 */
1186 init_samr_CryptPassword(r->in.machine_password,
1190 user_info.info24.password = crypt_pwd;
1191 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1193 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1200 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1202 if (!NT_STATUS_IS_OK(status)) {
1204 dcerpc_samr_DeleteUser(b, mem_ctx,
1208 libnet_join_set_error_string(mem_ctx, r,
1209 "Failed to set password for machine account (%s)\n",
1213 if (!NT_STATUS_IS_OK(result)) {
1216 dcerpc_samr_DeleteUser(b, mem_ctx,
1220 libnet_join_set_error_string(mem_ctx, r,
1221 "Failed to set password for machine account (%s)\n",
1226 status = NT_STATUS_OK;
1233 data_blob_clear_free(&session_key);
1235 if (is_valid_policy_hnd(&sam_pol)) {
1236 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1238 if (is_valid_policy_hnd(&domain_pol)) {
1239 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1241 if (is_valid_policy_hnd(&user_pol)) {
1242 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1244 TALLOC_FREE(pipe_hnd);
1249 /****************************************************************
1250 ****************************************************************/
1252 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1253 const char *netbios_domain_name,
1254 const char *dc_name,
1255 const bool use_kerberos)
1257 TALLOC_CTX *frame = talloc_stackframe();
1258 struct cli_state *cli = NULL;
1259 struct rpc_pipe_client *netlogon_pipe = NULL;
1260 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1261 struct netlogon_creds_CredentialState *creds = NULL;
1262 uint32_t netlogon_flags = 0;
1263 enum netr_SchannelType sec_chan_type = 0;
1265 char *machine_password = NULL;
1266 const char *machine_name = NULL;
1267 const char *machine_account = NULL;
1269 struct samr_Password current_nt_hash;
1270 struct samr_Password *previous_nt_hash = NULL;
1275 return NT_STATUS_INVALID_PARAMETER;
1278 if (!secrets_init()) {
1280 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1283 ok = get_trust_pw_clear(netbios_domain_name,
1289 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1292 machine_account = talloc_asprintf(frame, "%s$", machine_name);
1293 if (machine_account == NULL) {
1294 SAFE_FREE(machine_password);
1295 SAFE_FREE(previous_nt_hash);
1297 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1301 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1304 status = cli_full_connection(&cli, NULL,
1309 netbios_domain_name,
1312 SMB_SIGNING_DEFAULT);
1314 E_md4hash(machine_password, current_nt_hash.hash);
1315 SAFE_FREE(machine_password);
1317 if (!NT_STATUS_IS_OK(status)) {
1318 status = cli_full_connection(&cli, NULL,
1326 SMB_SIGNING_DEFAULT);
1329 if (!NT_STATUS_IS_OK(status)) {
1330 SAFE_FREE(previous_nt_hash);
1335 status = rpccli_create_netlogon_creds(dc_name,
1336 netbios_domain_name,
1342 if (!NT_STATUS_IS_OK(status)) {
1343 SAFE_FREE(previous_nt_hash);
1349 status = rpccli_setup_netlogon_creds(cli,
1351 true, /* force_reauth */
1354 SAFE_FREE(previous_nt_hash);
1355 if (!NT_STATUS_IS_OK(status)) {
1356 DEBUG(0,("connect_to_domain_password_server: "
1357 "unable to open the domain client session to "
1358 "machine %s. Flags[0x%08X] Error was : %s.\n",
1359 dc_name, (unsigned)netlogon_flags,
1360 nt_errstr(status)));
1366 status = netlogon_creds_cli_get(netlogon_creds,
1369 if (!NT_STATUS_IS_OK(status)) {
1374 netlogon_flags = creds->negotiate_flags;
1377 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1380 return NT_STATUS_OK;
1383 status = cli_rpc_pipe_open_schannel_with_key(
1384 cli, &ndr_table_netlogon, NCACN_NP,
1385 netbios_domain_name,
1386 netlogon_creds, &netlogon_pipe);
1388 TALLOC_FREE(netlogon_pipe);
1390 if (!NT_STATUS_IS_OK(status)) {
1391 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1392 "on netlogon pipe to server %s for domain %s. "
1394 smbXcli_conn_remote_name(cli->conn),
1395 netbios_domain_name, nt_errstr(status)));
1403 return NT_STATUS_OK;
1406 /****************************************************************
1407 ****************************************************************/
1409 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1410 struct libnet_JoinCtx *r)
1414 status = libnet_join_ok(r->in.msg_ctx,
1415 r->out.netbios_domain_name,
1417 r->in.use_kerberos);
1418 if (!NT_STATUS_IS_OK(status)) {
1419 libnet_join_set_error_string(mem_ctx, r,
1420 "failed to verify domain membership after joining: %s",
1421 get_friendly_nt_error_msg(status));
1422 return WERR_SETUP_NOT_JOINED;
1428 /****************************************************************
1429 ****************************************************************/
1431 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1432 struct libnet_UnjoinCtx *r)
1434 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1438 if (!secrets_delete_domain_sid(lp_workgroup())) {
1445 /****************************************************************
1446 ****************************************************************/
1448 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1449 struct libnet_UnjoinCtx *r)
1451 struct cli_state *cli = NULL;
1452 struct rpc_pipe_client *pipe_hnd = NULL;
1453 struct policy_handle sam_pol, domain_pol, user_pol;
1454 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1457 struct lsa_String lsa_acct_name;
1458 struct samr_Ids user_rids;
1459 struct samr_Ids name_types;
1460 union samr_UserInfo *info = NULL;
1461 struct dcerpc_binding_handle *b = NULL;
1463 ZERO_STRUCT(sam_pol);
1464 ZERO_STRUCT(domain_pol);
1465 ZERO_STRUCT(user_pol);
1467 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1468 r->in.admin_account,
1470 r->in.admin_password,
1473 if (!NT_STATUS_IS_OK(status)) {
1477 /* Open the domain */
1479 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1481 if (!NT_STATUS_IS_OK(status)) {
1482 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1483 nt_errstr(status)));
1487 b = pipe_hnd->binding_handle;
1489 status = dcerpc_samr_Connect2(b, mem_ctx,
1491 SEC_FLAG_MAXIMUM_ALLOWED,
1494 if (!NT_STATUS_IS_OK(status)) {
1497 if (!NT_STATUS_IS_OK(result)) {
1502 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1504 SEC_FLAG_MAXIMUM_ALLOWED,
1508 if (!NT_STATUS_IS_OK(status)) {
1511 if (!NT_STATUS_IS_OK(result)) {
1516 /* Create domain user */
1518 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1519 if (!strlower_m(acct_name)) {
1520 status = NT_STATUS_INVALID_PARAMETER;
1524 init_lsa_String(&lsa_acct_name, acct_name);
1526 status = dcerpc_samr_LookupNames(b, mem_ctx,
1534 if (!NT_STATUS_IS_OK(status)) {
1537 if (!NT_STATUS_IS_OK(result)) {
1541 if (user_rids.count != 1) {
1542 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1545 if (name_types.count != 1) {
1546 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1550 if (name_types.ids[0] != SID_NAME_USER) {
1551 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1552 name_types.ids[0]));
1553 status = NT_STATUS_INVALID_WORKSTATION;
1557 user_rid = user_rids.ids[0];
1559 /* Open handle on user */
1561 status = dcerpc_samr_OpenUser(b, mem_ctx,
1563 SEC_FLAG_MAXIMUM_ALLOWED,
1567 if (!NT_STATUS_IS_OK(status)) {
1570 if (!NT_STATUS_IS_OK(result)) {
1577 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1582 if (!NT_STATUS_IS_OK(status)) {
1583 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1586 if (!NT_STATUS_IS_OK(result)) {
1588 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1592 /* now disable and setuser info */
1594 info->info16.acct_flags |= ACB_DISABLED;
1596 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1601 if (!NT_STATUS_IS_OK(status)) {
1602 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1605 if (!NT_STATUS_IS_OK(result)) {
1607 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1611 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1614 if (pipe_hnd && b) {
1615 if (is_valid_policy_hnd(&domain_pol)) {
1616 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1618 if (is_valid_policy_hnd(&sam_pol)) {
1619 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1621 TALLOC_FREE(pipe_hnd);
1631 /****************************************************************
1632 ****************************************************************/
1634 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1636 WERROR werr = WERR_OK;
1638 struct smbconf_ctx *ctx;
1640 err = smbconf_init_reg(r, &ctx, NULL);
1641 if (!SBC_ERROR_IS_OK(err)) {
1642 werr = WERR_NO_SUCH_SERVICE;
1646 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1648 err = smbconf_set_global_parameter(ctx, "security", "user");
1649 if (!SBC_ERROR_IS_OK(err)) {
1650 werr = WERR_NO_SUCH_SERVICE;
1654 err = smbconf_set_global_parameter(ctx, "workgroup",
1656 if (!SBC_ERROR_IS_OK(err)) {
1657 werr = WERR_NO_SUCH_SERVICE;
1661 smbconf_delete_global_parameter(ctx, "realm");
1665 err = smbconf_set_global_parameter(ctx, "security", "domain");
1666 if (!SBC_ERROR_IS_OK(err)) {
1667 werr = WERR_NO_SUCH_SERVICE;
1671 err = smbconf_set_global_parameter(ctx, "workgroup",
1672 r->out.netbios_domain_name);
1673 if (!SBC_ERROR_IS_OK(err)) {
1674 werr = WERR_NO_SUCH_SERVICE;
1678 if (r->out.domain_is_ad) {
1679 err = smbconf_set_global_parameter(ctx, "security", "ads");
1680 if (!SBC_ERROR_IS_OK(err)) {
1681 werr = WERR_NO_SUCH_SERVICE;
1685 err = smbconf_set_global_parameter(ctx, "realm",
1686 r->out.dns_domain_name);
1687 if (!SBC_ERROR_IS_OK(err)) {
1688 werr = WERR_NO_SUCH_SERVICE;
1694 smbconf_shutdown(ctx);
1698 /****************************************************************
1699 ****************************************************************/
1701 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1703 WERROR werr = WERR_OK;
1705 struct smbconf_ctx *ctx;
1707 err = smbconf_init_reg(r, &ctx, NULL);
1708 if (!SBC_ERROR_IS_OK(err)) {
1709 werr = WERR_NO_SUCH_SERVICE;
1713 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1715 err = smbconf_set_global_parameter(ctx, "security", "user");
1716 if (!SBC_ERROR_IS_OK(err)) {
1717 werr = WERR_NO_SUCH_SERVICE;
1721 err = smbconf_delete_global_parameter(ctx, "workgroup");
1722 if (!SBC_ERROR_IS_OK(err)) {
1723 werr = WERR_NO_SUCH_SERVICE;
1727 smbconf_delete_global_parameter(ctx, "realm");
1731 smbconf_shutdown(ctx);
1735 /****************************************************************
1736 ****************************************************************/
1738 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1742 if (!W_ERROR_IS_OK(r->out.result)) {
1743 return r->out.result;
1746 if (!r->in.modify_config) {
1750 werr = do_join_modify_vals_config(r);
1751 if (!W_ERROR_IS_OK(werr)) {
1755 lp_load_global(get_dyn_CONFIGFILE());
1757 r->out.modified_config = true;
1758 r->out.result = werr;
1763 /****************************************************************
1764 ****************************************************************/
1766 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1770 if (!W_ERROR_IS_OK(r->out.result)) {
1771 return r->out.result;
1774 if (!r->in.modify_config) {
1778 werr = do_unjoin_modify_vals_config(r);
1779 if (!W_ERROR_IS_OK(werr)) {
1783 lp_load_global(get_dyn_CONFIGFILE());
1785 r->out.modified_config = true;
1786 r->out.result = werr;
1791 /****************************************************************
1792 ****************************************************************/
1794 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1795 const char *domain_str,
1796 const char **domain_p,
1799 char *domain = NULL;
1801 const char *p = NULL;
1803 if (!domain_str || !domain_p || !dc_p) {
1807 p = strchr_m(domain_str, '\\');
1810 domain = talloc_strndup(mem_ctx, domain_str,
1811 PTR_DIFF(p, domain_str));
1812 dc = talloc_strdup(mem_ctx, p+1);
1817 domain = talloc_strdup(mem_ctx, domain_str);
1833 /****************************************************************
1834 ****************************************************************/
1836 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1837 struct libnet_JoinCtx *r)
1839 if (!r->in.domain_name) {
1840 libnet_join_set_error_string(mem_ctx, r,
1841 "No domain name defined");
1842 return WERR_INVALID_PARAM;
1845 if (strlen(r->in.machine_name) > 15) {
1846 libnet_join_set_error_string(mem_ctx, r,
1847 "Our netbios name can be at most 15 chars long, "
1848 "\"%s\" is %u chars long\n",
1850 (unsigned int)strlen(r->in.machine_name));
1851 return WERR_INVALID_PARAM;
1854 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1857 libnet_join_set_error_string(mem_ctx, r,
1858 "Failed to parse domain name");
1859 return WERR_INVALID_PARAM;
1863 return WERR_SETUP_DOMAIN_CONTROLLER;
1866 if (!r->in.admin_domain) {
1867 char *admin_domain = NULL;
1868 char *admin_account = NULL;
1869 split_domain_user(mem_ctx,
1870 r->in.admin_account,
1873 r->in.admin_domain = admin_domain;
1874 r->in.admin_account = admin_account;
1877 if (!secrets_init()) {
1878 libnet_join_set_error_string(mem_ctx, r,
1879 "Unable to open secrets database");
1880 return WERR_CAN_NOT_COMPLETE;
1886 /****************************************************************
1887 ****************************************************************/
1889 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1893 /* Try adding dom admins to builtin\admins. Only log failures. */
1894 status = create_builtin_administrators(domain_sid);
1895 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1896 DEBUG(10,("Unable to auto-add domain administrators to "
1897 "BUILTIN\\Administrators during join because "
1898 "winbindd must be running.\n"));
1899 } else if (!NT_STATUS_IS_OK(status)) {
1900 DEBUG(5, ("Failed to auto-add domain administrators to "
1901 "BUILTIN\\Administrators during join: %s\n",
1902 nt_errstr(status)));
1905 /* Try adding dom users to builtin\users. Only log failures. */
1906 status = create_builtin_users(domain_sid);
1907 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1908 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1909 "during join because winbindd must be running.\n"));
1910 } else if (!NT_STATUS_IS_OK(status)) {
1911 DEBUG(5, ("Failed to auto-add domain administrators to "
1912 "BUILTIN\\Administrators during join: %s\n",
1913 nt_errstr(status)));
1917 /****************************************************************
1918 ****************************************************************/
1920 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1921 struct libnet_JoinCtx *r)
1925 if (!W_ERROR_IS_OK(r->out.result)) {
1926 return r->out.result;
1929 werr = do_JoinConfig(r);
1930 if (!W_ERROR_IS_OK(werr)) {
1934 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1938 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1939 if (r->out.dns_domain_name) {
1940 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1944 if (r->out.domain_is_ad &&
1945 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1946 ADS_STATUS ads_status;
1948 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1949 if (!ADS_ERR_OK(ads_status)) {
1950 return WERR_GENERAL_FAILURE;
1953 #endif /* HAVE_ADS */
1955 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1960 /****************************************************************
1961 ****************************************************************/
1963 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1966 ads_destroy(&r->in.ads);
1972 /****************************************************************
1973 ****************************************************************/
1975 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1978 ads_destroy(&r->in.ads);
1984 /****************************************************************
1985 ****************************************************************/
1987 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1988 struct libnet_JoinCtx **r)
1990 struct libnet_JoinCtx *ctx;
1992 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1997 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1999 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2000 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2002 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2009 /****************************************************************
2010 ****************************************************************/
2012 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2013 struct libnet_UnjoinCtx **r)
2015 struct libnet_UnjoinCtx *ctx;
2017 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2022 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2024 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2025 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2032 /****************************************************************
2033 ****************************************************************/
2035 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2036 struct libnet_JoinCtx *r)
2038 bool valid_security = false;
2039 bool valid_workgroup = false;
2040 bool valid_realm = false;
2042 /* check if configuration is already set correctly */
2044 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2046 switch (r->out.domain_is_ad) {
2048 valid_security = (lp_security() == SEC_DOMAIN);
2049 if (valid_workgroup && valid_security) {
2050 /* nothing to be done */
2055 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2056 switch (lp_security()) {
2059 valid_security = true;
2062 if (valid_workgroup && valid_realm && valid_security) {
2063 /* nothing to be done */
2069 /* check if we are supposed to manipulate configuration */
2071 if (!r->in.modify_config) {
2073 char *wrong_conf = talloc_strdup(mem_ctx, "");
2075 if (!valid_workgroup) {
2076 wrong_conf = talloc_asprintf_append(wrong_conf,
2077 "\"workgroup\" set to '%s', should be '%s'",
2078 lp_workgroup(), r->out.netbios_domain_name);
2079 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2083 wrong_conf = talloc_asprintf_append(wrong_conf,
2084 "\"realm\" set to '%s', should be '%s'",
2085 lp_realm(), r->out.dns_domain_name);
2086 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2089 if (!valid_security) {
2090 const char *sec = NULL;
2091 switch (lp_security()) {
2092 case SEC_USER: sec = "user"; break;
2093 case SEC_DOMAIN: sec = "domain"; break;
2094 case SEC_ADS: sec = "ads"; break;
2096 wrong_conf = talloc_asprintf_append(wrong_conf,
2097 "\"security\" set to '%s', should be %s",
2098 sec, r->out.domain_is_ad ?
2099 "either 'domain' or 'ads'" : "'domain'");
2100 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2103 libnet_join_set_error_string(mem_ctx, r,
2104 "Invalid configuration (%s) and configuration modification "
2105 "was not requested", wrong_conf);
2106 return WERR_CAN_NOT_COMPLETE;
2109 /* check if we are able to manipulate configuration */
2111 if (!lp_config_backend_is_registry()) {
2112 libnet_join_set_error_string(mem_ctx, r,
2113 "Configuration manipulation requested but not "
2114 "supported by backend");
2115 return WERR_NOT_SUPPORTED;
2121 /****************************************************************
2122 ****************************************************************/
2124 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2125 struct libnet_JoinCtx *r)
2129 struct cli_state *cli = NULL;
2131 ADS_STATUS ads_status;
2132 #endif /* HAVE_ADS */
2134 if (!r->in.dc_name) {
2135 struct netr_DsRGetDCNameInfo *info;
2137 status = dsgetdcname(mem_ctx,
2142 DS_FORCE_REDISCOVERY |
2143 DS_DIRECTORY_SERVICE_REQUIRED |
2144 DS_WRITABLE_REQUIRED |
2147 if (!NT_STATUS_IS_OK(status)) {
2148 libnet_join_set_error_string(mem_ctx, r,
2149 "failed to find DC for domain %s",
2151 get_friendly_nt_error_msg(status));
2152 return WERR_DCNOTFOUND;
2155 dc = strip_hostname(info->dc_unc);
2156 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2157 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2160 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2161 if (!NT_STATUS_IS_OK(status)) {
2162 libnet_join_set_error_string(mem_ctx, r,
2163 "failed to lookup DC info for domain '%s' over rpc: %s",
2164 r->in.domain_name, get_friendly_nt_error_msg(status));
2165 return ntstatus_to_werror(status);
2168 werr = libnet_join_check_config(mem_ctx, r);
2169 if (!W_ERROR_IS_OK(werr)) {
2175 create_local_private_krb5_conf_for_domain(
2176 r->out.dns_domain_name, r->out.netbios_domain_name,
2177 NULL, smbXcli_conn_remote_sockaddr(cli->conn));
2179 if (r->out.domain_is_ad && r->in.account_ou &&
2180 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2182 ads_status = libnet_join_connect_ads(mem_ctx, r);
2183 if (!ADS_ERR_OK(ads_status)) {
2184 return WERR_DEFAULT_JOIN_REQUIRED;
2187 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2188 if (!ADS_ERR_OK(ads_status)) {
2189 libnet_join_set_error_string(mem_ctx, r,
2190 "failed to precreate account in ou %s: %s",
2192 ads_errstr(ads_status));
2193 return WERR_DEFAULT_JOIN_REQUIRED;
2196 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2198 #endif /* HAVE_ADS */
2200 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2201 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2202 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2204 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2206 if (!NT_STATUS_IS_OK(status)) {
2207 libnet_join_set_error_string(mem_ctx, r,
2208 "failed to join domain '%s' over rpc: %s",
2209 r->in.domain_name, get_friendly_nt_error_msg(status));
2210 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2211 return WERR_SETUP_ALREADY_JOINED;
2213 werr = ntstatus_to_werror(status);
2217 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2218 werr = WERR_SETUP_NOT_JOINED;
2232 /****************************************************************
2233 ****************************************************************/
2235 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2236 struct libnet_JoinCtx *r)
2239 struct libnet_UnjoinCtx *u = NULL;
2241 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2242 if (!W_ERROR_IS_OK(werr)) {
2246 u->in.debug = r->in.debug;
2247 u->in.dc_name = r->in.dc_name;
2248 u->in.domain_name = r->in.domain_name;
2249 u->in.admin_account = r->in.admin_account;
2250 u->in.admin_password = r->in.admin_password;
2251 u->in.modify_config = r->in.modify_config;
2252 u->in.use_kerberos = r->in.use_kerberos;
2253 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2254 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2256 werr = libnet_Unjoin(mem_ctx, u);
2262 /****************************************************************
2263 ****************************************************************/
2265 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2266 struct libnet_JoinCtx *r)
2271 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2274 ZERO_STRUCT(r->out);
2276 werr = libnet_join_pre_processing(mem_ctx, r);
2277 if (!W_ERROR_IS_OK(werr)) {
2281 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2282 werr = libnet_DomainJoin(mem_ctx, r);
2283 if (!W_ERROR_IS_OK(werr)) {
2288 werr = libnet_join_post_processing(mem_ctx, r);
2289 if (!W_ERROR_IS_OK(werr)) {
2293 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2294 werr = libnet_join_post_verify(mem_ctx, r);
2295 if (!W_ERROR_IS_OK(werr)) {
2296 libnet_join_rollback(mem_ctx, r);
2301 r->out.result = werr;
2304 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2309 /****************************************************************
2310 ****************************************************************/
2312 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2313 struct libnet_UnjoinCtx *r)
2317 if (!r->in.domain_sid) {
2319 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2320 libnet_unjoin_set_error_string(mem_ctx, r,
2321 "Unable to fetch domain sid: are we joined?");
2322 return WERR_SETUP_NOT_JOINED;
2324 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2325 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2328 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2329 !r->in.delete_machine_account) {
2330 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2334 if (!r->in.dc_name) {
2335 struct netr_DsRGetDCNameInfo *info;
2337 status = dsgetdcname(mem_ctx,
2342 DS_DIRECTORY_SERVICE_REQUIRED |
2343 DS_WRITABLE_REQUIRED |
2346 if (!NT_STATUS_IS_OK(status)) {
2347 libnet_unjoin_set_error_string(mem_ctx, r,
2348 "failed to find DC for domain %s",
2350 get_friendly_nt_error_msg(status));
2351 return WERR_DCNOTFOUND;
2354 dc = strip_hostname(info->dc_unc);
2355 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2356 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2360 /* for net ads leave, try to delete the account. If it works,
2361 no sense in disabling. If it fails, we can still try to
2364 if (r->in.delete_machine_account) {
2365 ADS_STATUS ads_status;
2366 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2367 if (ADS_ERR_OK(ads_status)) {
2369 r->out.dns_domain_name =
2370 talloc_strdup(mem_ctx,
2371 r->in.ads->server.realm);
2373 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2375 if (!ADS_ERR_OK(ads_status)) {
2376 libnet_unjoin_set_error_string(mem_ctx, r,
2377 "failed to remove machine account from AD: %s",
2378 ads_errstr(ads_status));
2380 r->out.deleted_machine_account = true;
2381 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2382 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2386 #endif /* HAVE_ADS */
2388 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2390 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2391 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2392 if (!NT_STATUS_IS_OK(status)) {
2393 libnet_unjoin_set_error_string(mem_ctx, r,
2394 "failed to disable machine account via rpc: %s",
2395 get_friendly_nt_error_msg(status));
2396 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2397 return WERR_SETUP_NOT_JOINED;
2399 return ntstatus_to_werror(status);
2402 r->out.disabled_machine_account = true;
2405 /* If disable succeeded or was not requested at all, we
2406 should be getting rid of our end of things */
2408 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2413 /****************************************************************
2414 ****************************************************************/
2416 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2417 struct libnet_UnjoinCtx *r)
2419 if (!r->in.domain_name) {
2420 libnet_unjoin_set_error_string(mem_ctx, r,
2421 "No domain name defined");
2422 return WERR_INVALID_PARAM;
2425 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2428 libnet_unjoin_set_error_string(mem_ctx, r,
2429 "Failed to parse domain name");
2430 return WERR_INVALID_PARAM;
2434 return WERR_SETUP_DOMAIN_CONTROLLER;
2437 if (!r->in.admin_domain) {
2438 char *admin_domain = NULL;
2439 char *admin_account = NULL;
2440 split_domain_user(mem_ctx,
2441 r->in.admin_account,
2444 r->in.admin_domain = admin_domain;
2445 r->in.admin_account = admin_account;
2448 if (!secrets_init()) {
2449 libnet_unjoin_set_error_string(mem_ctx, r,
2450 "Unable to open secrets database");
2451 return WERR_CAN_NOT_COMPLETE;
2457 /****************************************************************
2458 ****************************************************************/
2460 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2461 struct libnet_UnjoinCtx *r)
2463 saf_delete(r->out.netbios_domain_name);
2464 saf_delete(r->out.dns_domain_name);
2466 return libnet_unjoin_config(r);
2469 /****************************************************************
2470 ****************************************************************/
2472 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2473 struct libnet_UnjoinCtx *r)
2478 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2481 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2482 if (!W_ERROR_IS_OK(werr)) {
2486 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2487 werr = libnet_DomainUnjoin(mem_ctx, r);
2488 if (!W_ERROR_IS_OK(werr)) {
2489 libnet_unjoin_config(r);
2494 werr = libnet_unjoin_post_processing(mem_ctx, r);
2495 if (!W_ERROR_IS_OK(werr)) {
2500 r->out.result = werr;
2503 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);