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"
44 /****************************************************************
45 ****************************************************************/
47 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
50 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
51 DEBUG(1,("libnet_Join:\n%s", str)); \
55 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
56 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
57 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
58 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
60 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
63 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
64 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
68 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
69 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
70 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
71 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
73 /****************************************************************
74 ****************************************************************/
76 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
77 struct libnet_JoinCtx *r,
78 const char *format, ...)
82 if (r->out.error_string) {
86 va_start(args, format);
87 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
91 /****************************************************************
92 ****************************************************************/
94 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
95 struct libnet_UnjoinCtx *r,
96 const char *format, ...)
100 if (r->out.error_string) {
104 va_start(args, format);
105 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
111 /****************************************************************
112 ****************************************************************/
114 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
115 const char *netbios_domain_name,
117 const char *user_name,
118 const char *password,
122 ADS_STRUCT *my_ads = NULL;
125 my_ads = ads_init(dns_domain_name,
129 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
133 SAFE_FREE(my_ads->auth.user_name);
134 my_ads->auth.user_name = SMB_STRDUP(user_name);
135 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
137 SAFE_FREE(my_ads->auth.realm);
138 my_ads->auth.realm = smb_xstrdup(cp);
139 if (!strupper_m(my_ads->auth.realm)) {
140 ads_destroy(&my_ads);
141 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
147 SAFE_FREE(my_ads->auth.password);
148 my_ads->auth.password = SMB_STRDUP(password);
151 status = ads_connect_user_creds(my_ads);
152 if (!ADS_ERR_OK(status)) {
153 ads_destroy(&my_ads);
161 /****************************************************************
162 ****************************************************************/
164 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
165 struct libnet_JoinCtx *r)
169 status = libnet_connect_ads(r->out.dns_domain_name,
170 r->out.netbios_domain_name,
173 r->in.admin_password,
175 if (!ADS_ERR_OK(status)) {
176 libnet_join_set_error_string(mem_ctx, r,
177 "failed to connect to AD: %s",
182 if (!r->out.netbios_domain_name) {
183 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
184 r->in.ads->server.workgroup);
185 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
188 if (!r->out.dns_domain_name) {
189 r->out.dns_domain_name = talloc_strdup(mem_ctx,
190 r->in.ads->config.realm);
191 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
194 r->out.domain_is_ad = true;
199 /****************************************************************
200 ****************************************************************/
202 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
203 struct libnet_UnjoinCtx *r)
207 status = libnet_connect_ads(r->in.domain_name,
211 r->in.admin_password,
213 if (!ADS_ERR_OK(status)) {
214 libnet_unjoin_set_error_string(mem_ctx, r,
215 "failed to connect to AD: %s",
222 /****************************************************************
223 join a domain using ADS (LDAP mods)
224 ****************************************************************/
226 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
227 struct libnet_JoinCtx *r)
230 LDAPMessage *res = NULL;
231 const char *attrs[] = { "dn", NULL };
234 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
235 if (!ADS_ERR_OK(status)) {
239 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
240 if (!ADS_ERR_OK(status)) {
244 if (ads_count_replies(r->in.ads, res) != 1) {
245 ads_msgfree(r->in.ads, res);
246 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
249 ads_msgfree(r->in.ads, res);
251 /* Attempt to create the machine account and bail if this fails.
252 Assume that the admin wants exactly what they requested */
254 status = ads_create_machine_acct(r->in.ads,
258 if (ADS_ERR_OK(status)) {
259 DEBUG(1,("machine account creation created\n"));
261 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
262 (status.err.rc == LDAP_ALREADY_EXISTS)) {
263 status = ADS_SUCCESS;
266 if (!ADS_ERR_OK(status)) {
267 DEBUG(1,("machine account creation failed\n"));
271 status = ads_move_machine_acct(r->in.ads,
275 if (!ADS_ERR_OK(status)) {
276 DEBUG(1,("failure to locate/move pre-existing "
277 "machine account\n"));
281 DEBUG(1,("The machine account %s the specified OU.\n",
282 moved ? "was moved into" : "already exists in"));
287 /****************************************************************
288 ****************************************************************/
290 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
291 struct libnet_UnjoinCtx *r)
296 status = libnet_unjoin_connect_ads(mem_ctx, r);
297 if (!ADS_ERR_OK(status)) {
298 libnet_unjoin_set_error_string(mem_ctx, r,
299 "failed to connect to AD: %s",
305 status = ads_leave_realm(r->in.ads, r->in.machine_name);
306 if (!ADS_ERR_OK(status)) {
307 libnet_unjoin_set_error_string(mem_ctx, r,
308 "failed to leave realm: %s",
316 /****************************************************************
317 ****************************************************************/
319 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
320 struct libnet_JoinCtx *r)
323 LDAPMessage *res = NULL;
326 if (!r->in.machine_name) {
327 return ADS_ERROR(LDAP_NO_MEMORY);
330 status = ads_find_machine_acct(r->in.ads,
333 if (!ADS_ERR_OK(status)) {
337 if (ads_count_replies(r->in.ads, res) != 1) {
338 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
342 dn = ads_get_dn(r->in.ads, mem_ctx, res);
344 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
348 r->out.dn = talloc_strdup(mem_ctx, dn);
350 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
355 ads_msgfree(r->in.ads, res);
361 /****************************************************************
362 Set a machines dNSHostName and servicePrincipalName attributes
363 ****************************************************************/
365 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
366 struct libnet_JoinCtx *r)
371 const char *spn_array[3] = {NULL, NULL, NULL};
376 status = libnet_join_find_machine_acct(mem_ctx, r);
377 if (!ADS_ERR_OK(status)) {
381 /* Windows only creates HOST/shortname & HOST/fqdn. */
383 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
385 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
387 if (!strupper_m(spn)) {
388 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
392 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
393 || (strchr(my_fqdn, '.') == NULL)) {
394 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
395 r->out.dns_domain_name);
400 if (!strequal(my_fqdn, r->in.machine_name)) {
401 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
403 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
408 mods = ads_init_mods(mem_ctx);
410 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
413 /* fields of primary importance */
415 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
416 if (!ADS_ERR_OK(status)) {
417 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
420 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
422 if (!ADS_ERR_OK(status)) {
423 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
426 return ads_gen_mod(r->in.ads, r->out.dn, mods);
429 /****************************************************************
430 ****************************************************************/
432 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
433 struct libnet_JoinCtx *r)
438 if (!r->in.create_upn) {
444 status = libnet_join_find_machine_acct(mem_ctx, r);
445 if (!ADS_ERR_OK(status)) {
450 r->in.upn = talloc_asprintf(mem_ctx,
453 r->out.dns_domain_name);
455 return ADS_ERROR(LDAP_NO_MEMORY);
459 /* now do the mods */
461 mods = ads_init_mods(mem_ctx);
463 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
466 /* fields of primary importance */
468 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
469 if (!ADS_ERR_OK(status)) {
470 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
473 return ads_gen_mod(r->in.ads, r->out.dn, mods);
477 /****************************************************************
478 ****************************************************************/
480 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
481 struct libnet_JoinCtx *r)
487 if (!r->in.os_name || !r->in.os_version ) {
493 status = libnet_join_find_machine_acct(mem_ctx, r);
494 if (!ADS_ERR_OK(status)) {
498 /* now do the mods */
500 mods = ads_init_mods(mem_ctx);
502 return ADS_ERROR(LDAP_NO_MEMORY);
505 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
507 return ADS_ERROR(LDAP_NO_MEMORY);
510 /* fields of primary importance */
512 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
514 if (!ADS_ERR_OK(status)) {
518 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
520 if (!ADS_ERR_OK(status)) {
524 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
526 if (!ADS_ERR_OK(status)) {
530 return ads_gen_mod(r->in.ads, r->out.dn, mods);
533 /****************************************************************
534 ****************************************************************/
536 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
537 struct libnet_JoinCtx *r)
539 if (!USE_SYSTEM_KEYTAB) {
543 if (ads_keytab_create_default(r->in.ads) != 0) {
550 /****************************************************************
551 ****************************************************************/
553 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
554 struct libnet_JoinCtx *r)
556 uint32_t domain_func;
558 const char *salt = NULL;
559 char *std_salt = NULL;
561 status = ads_domain_func_level(r->in.ads, &domain_func);
562 if (!ADS_ERR_OK(status)) {
563 libnet_join_set_error_string(mem_ctx, r,
564 "failed to determine domain functional level: %s",
569 /* go ahead and setup the default salt */
571 std_salt = kerberos_standard_des_salt();
573 libnet_join_set_error_string(mem_ctx, r,
574 "failed to obtain standard DES salt");
578 salt = talloc_strdup(mem_ctx, std_salt);
585 /* if it's a Windows functional domain, we have to look for the UPN */
587 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
590 upn = ads_get_upn(r->in.ads, mem_ctx,
593 salt = talloc_strdup(mem_ctx, upn);
600 return kerberos_secrets_store_des_salt(salt);
603 /****************************************************************
604 ****************************************************************/
606 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
607 struct libnet_JoinCtx *r)
612 status = libnet_join_connect_ads(mem_ctx, r);
613 if (!ADS_ERR_OK(status)) {
618 status = libnet_join_set_machine_spn(mem_ctx, r);
619 if (!ADS_ERR_OK(status)) {
620 libnet_join_set_error_string(mem_ctx, r,
621 "failed to set machine spn: %s",
626 status = libnet_join_set_os_attributes(mem_ctx, r);
627 if (!ADS_ERR_OK(status)) {
628 libnet_join_set_error_string(mem_ctx, r,
629 "failed to set machine os attributes: %s",
634 status = libnet_join_set_machine_upn(mem_ctx, r);
635 if (!ADS_ERR_OK(status)) {
636 libnet_join_set_error_string(mem_ctx, r,
637 "failed to set machine upn: %s",
642 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
643 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
646 if (!libnet_join_create_keytab(mem_ctx, r)) {
647 libnet_join_set_error_string(mem_ctx, r,
648 "failed to create kerberos keytab");
649 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
654 #endif /* HAVE_ADS */
656 /****************************************************************
657 Store the machine password and domain SID
658 ****************************************************************/
660 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
661 struct libnet_JoinCtx *r)
663 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
666 DEBUG(1,("Failed to save domain sid\n"));
670 if (!secrets_store_machine_password(r->in.machine_password,
671 r->out.netbios_domain_name,
672 r->in.secure_channel_type))
674 DEBUG(1,("Failed to save machine password\n"));
681 /****************************************************************
682 Connect dc's IPC$ share
683 ****************************************************************/
685 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
689 struct cli_state **cli)
694 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
697 if (use_kerberos && pass) {
698 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
701 return cli_full_connection(cli, NULL,
709 SMB_SIGNING_DEFAULT);
712 /****************************************************************
713 Lookup domain dc's info
714 ****************************************************************/
716 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
717 struct libnet_JoinCtx *r,
718 struct cli_state **cli)
720 struct rpc_pipe_client *pipe_hnd = NULL;
721 struct policy_handle lsa_pol;
722 NTSTATUS status, result;
723 union lsa_PolicyInformation *info = NULL;
724 struct dcerpc_binding_handle *b;
726 status = libnet_join_connect_dc_ipc(r->in.dc_name,
728 r->in.admin_password,
731 if (!NT_STATUS_IS_OK(status)) {
735 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
737 if (!NT_STATUS_IS_OK(status)) {
738 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
743 b = pipe_hnd->binding_handle;
745 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
746 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
747 if (!NT_STATUS_IS_OK(status)) {
751 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
756 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
757 r->out.domain_is_ad = true;
758 r->out.netbios_domain_name = info->dns.name.string;
759 r->out.dns_domain_name = info->dns.dns_domain.string;
760 r->out.forest_name = info->dns.dns_forest.string;
761 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
762 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
765 if (!NT_STATUS_IS_OK(status)) {
766 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
768 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
771 if (!NT_STATUS_IS_OK(status)) {
774 if (!NT_STATUS_IS_OK(result)) {
779 r->out.netbios_domain_name = info->account_domain.name.string;
780 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
781 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
784 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
785 TALLOC_FREE(pipe_hnd);
791 /****************************************************************
792 Do the domain join unsecure
793 ****************************************************************/
795 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
796 struct libnet_JoinCtx *r,
797 struct cli_state *cli)
799 struct rpc_pipe_client *pipe_hnd = NULL;
800 unsigned char orig_trust_passwd_hash[16];
801 unsigned char new_trust_passwd_hash[16];
802 fstring trust_passwd;
805 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
807 if (!NT_STATUS_IS_OK(status)) {
811 if (!r->in.machine_password) {
812 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
813 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
816 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
818 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
819 fstrcpy(trust_passwd, r->in.admin_password);
820 strlower_m(trust_passwd);
823 * Machine names can be 15 characters, but the max length on
824 * a password is 14. --jerry
827 trust_passwd[14] = '\0';
829 E_md4hash(trust_passwd, orig_trust_passwd_hash);
831 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
833 orig_trust_passwd_hash,
834 r->in.machine_password,
835 new_trust_passwd_hash,
836 r->in.secure_channel_type);
841 /****************************************************************
843 ****************************************************************/
845 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
846 struct libnet_JoinCtx *r,
847 struct cli_state *cli)
849 struct rpc_pipe_client *pipe_hnd = NULL;
850 struct policy_handle sam_pol, domain_pol, user_pol;
851 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
853 struct lsa_String lsa_acct_name;
855 uint32_t acct_flags = ACB_WSTRUST;
856 struct samr_Ids user_rids;
857 struct samr_Ids name_types;
858 union samr_UserInfo user_info;
859 struct dcerpc_binding_handle *b = NULL;
861 DATA_BLOB session_key = data_blob_null;
862 struct samr_CryptPassword crypt_pwd;
863 struct samr_CryptPasswordEx crypt_pwd_ex;
865 ZERO_STRUCT(sam_pol);
866 ZERO_STRUCT(domain_pol);
867 ZERO_STRUCT(user_pol);
869 switch (r->in.secure_channel_type) {
871 acct_flags = ACB_WSTRUST;
874 acct_flags = ACB_SVRTRUST;
877 return NT_STATUS_INVALID_PARAMETER;
880 if (!r->in.machine_password) {
881 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
882 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
885 /* Open the domain */
887 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
889 if (!NT_STATUS_IS_OK(status)) {
890 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
895 b = pipe_hnd->binding_handle;
897 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
898 if (!NT_STATUS_IS_OK(status)) {
899 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
904 status = dcerpc_samr_Connect2(b, mem_ctx,
906 SAMR_ACCESS_ENUM_DOMAINS
907 | SAMR_ACCESS_LOOKUP_DOMAIN,
910 if (!NT_STATUS_IS_OK(status)) {
913 if (!NT_STATUS_IS_OK(result)) {
918 status = dcerpc_samr_OpenDomain(b, mem_ctx,
920 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
921 | SAMR_DOMAIN_ACCESS_CREATE_USER
922 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
926 if (!NT_STATUS_IS_OK(status)) {
929 if (!NT_STATUS_IS_OK(result)) {
934 /* Create domain user */
936 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
937 strlower_m(acct_name);
939 init_lsa_String(&lsa_acct_name, acct_name);
941 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
942 uint32_t access_desired =
943 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
944 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
945 SAMR_USER_ACCESS_SET_PASSWORD |
946 SAMR_USER_ACCESS_GET_ATTRIBUTES |
947 SAMR_USER_ACCESS_SET_ATTRIBUTES;
948 uint32_t access_granted = 0;
950 DEBUG(10,("Creating account with desired access mask: %d\n",
953 status = dcerpc_samr_CreateUser2(b, mem_ctx,
962 if (!NT_STATUS_IS_OK(status)) {
967 if (!NT_STATUS_IS_OK(status) &&
968 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
970 DEBUG(10,("Creation of workstation account failed: %s\n",
973 /* If NT_STATUS_ACCESS_DENIED then we have a valid
974 username/password combo but the user does not have
975 administrator access. */
977 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
978 libnet_join_set_error_string(mem_ctx, r,
979 "User specified does not have "
980 "administrator privileges");
986 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
987 if (!(r->in.join_flags &
988 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
993 /* We *must* do this.... don't ask... */
995 if (NT_STATUS_IS_OK(status)) {
996 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1000 status = dcerpc_samr_LookupNames(b, mem_ctx,
1007 if (!NT_STATUS_IS_OK(status)) {
1010 if (!NT_STATUS_IS_OK(result)) {
1015 if (name_types.ids[0] != SID_NAME_USER) {
1016 DEBUG(0,("%s is not a user account (type=%d)\n",
1017 acct_name, name_types.ids[0]));
1018 status = NT_STATUS_INVALID_WORKSTATION;
1022 user_rid = user_rids.ids[0];
1024 /* Open handle on user */
1026 status = dcerpc_samr_OpenUser(b, mem_ctx,
1028 SEC_FLAG_MAXIMUM_ALLOWED,
1032 if (!NT_STATUS_IS_OK(status)) {
1035 if (!NT_STATUS_IS_OK(result)) {
1040 /* Fill in the additional account flags now */
1042 acct_flags |= ACB_PWNOEXP;
1044 /* Set account flags on machine account */
1045 ZERO_STRUCT(user_info.info16);
1046 user_info.info16.acct_flags = acct_flags;
1048 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1053 if (!NT_STATUS_IS_OK(status)) {
1054 dcerpc_samr_DeleteUser(b, mem_ctx,
1058 libnet_join_set_error_string(mem_ctx, r,
1059 "Failed to set account flags for machine account (%s)\n",
1064 if (!NT_STATUS_IS_OK(result)) {
1067 dcerpc_samr_DeleteUser(b, mem_ctx,
1071 libnet_join_set_error_string(mem_ctx, r,
1072 "Failed to set account flags for machine account (%s)\n",
1077 /* Set password on machine account - first try level 26 */
1079 init_samr_CryptPasswordEx(r->in.machine_password,
1083 user_info.info26.password = crypt_pwd_ex;
1084 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1086 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1092 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1094 /* retry with level 24 */
1096 init_samr_CryptPassword(r->in.machine_password,
1100 user_info.info24.password = crypt_pwd;
1101 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1103 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1110 if (!NT_STATUS_IS_OK(status)) {
1112 dcerpc_samr_DeleteUser(b, mem_ctx,
1116 libnet_join_set_error_string(mem_ctx, r,
1117 "Failed to set password for machine account (%s)\n",
1121 if (!NT_STATUS_IS_OK(result)) {
1124 dcerpc_samr_DeleteUser(b, mem_ctx,
1128 libnet_join_set_error_string(mem_ctx, r,
1129 "Failed to set password for machine account (%s)\n",
1134 status = NT_STATUS_OK;
1141 data_blob_clear_free(&session_key);
1143 if (is_valid_policy_hnd(&sam_pol)) {
1144 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1146 if (is_valid_policy_hnd(&domain_pol)) {
1147 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1149 if (is_valid_policy_hnd(&user_pol)) {
1150 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1152 TALLOC_FREE(pipe_hnd);
1157 /****************************************************************
1158 ****************************************************************/
1160 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1161 const char *machine_name,
1162 const char *dc_name)
1164 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1165 struct cli_state *cli = NULL;
1166 struct rpc_pipe_client *pipe_hnd = NULL;
1167 struct rpc_pipe_client *netlogon_pipe = NULL;
1169 char *machine_password = NULL;
1170 char *machine_account = NULL;
1173 return NT_STATUS_INVALID_PARAMETER;
1176 if (!secrets_init()) {
1177 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1180 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1182 if (!machine_password) {
1183 return NT_STATUS_NO_TRUST_LSA_SECRET;
1186 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1187 SAFE_FREE(machine_password);
1188 return NT_STATUS_NO_MEMORY;
1191 status = cli_full_connection(&cli, NULL,
1199 SMB_SIGNING_DEFAULT);
1200 free(machine_account);
1201 free(machine_password);
1203 if (!NT_STATUS_IS_OK(status)) {
1204 status = cli_full_connection(&cli, NULL,
1212 SMB_SIGNING_DEFAULT);
1215 if (!NT_STATUS_IS_OK(status)) {
1219 status = get_schannel_session_key(cli, netbios_domain_name,
1220 &neg_flags, &netlogon_pipe);
1221 if (!NT_STATUS_IS_OK(status)) {
1222 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1224 return NT_STATUS_OK;
1227 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1228 "key from server %s for domain %s. Error was %s\n",
1229 smbXcli_conn_remote_name(cli->conn),
1230 netbios_domain_name, nt_errstr(status)));
1235 if (!lp_client_schannel()) {
1237 return NT_STATUS_OK;
1240 status = cli_rpc_pipe_open_schannel_with_key(
1241 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
1242 DCERPC_AUTH_LEVEL_PRIVACY,
1243 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1247 if (!NT_STATUS_IS_OK(status)) {
1248 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1249 "on netlogon pipe to server %s for domain %s. "
1251 smbXcli_conn_remote_name(cli->conn),
1252 netbios_domain_name, nt_errstr(status)));
1256 return NT_STATUS_OK;
1259 /****************************************************************
1260 ****************************************************************/
1262 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1263 struct libnet_JoinCtx *r)
1267 status = libnet_join_ok(r->out.netbios_domain_name,
1270 if (!NT_STATUS_IS_OK(status)) {
1271 libnet_join_set_error_string(mem_ctx, r,
1272 "failed to verify domain membership after joining: %s",
1273 get_friendly_nt_error_msg(status));
1274 return WERR_SETUP_NOT_JOINED;
1280 /****************************************************************
1281 ****************************************************************/
1283 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1284 struct libnet_UnjoinCtx *r)
1286 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1290 if (!secrets_delete_domain_sid(lp_workgroup())) {
1297 /****************************************************************
1298 ****************************************************************/
1300 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1301 struct libnet_UnjoinCtx *r)
1303 struct cli_state *cli = NULL;
1304 struct rpc_pipe_client *pipe_hnd = NULL;
1305 struct policy_handle sam_pol, domain_pol, user_pol;
1306 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1309 struct lsa_String lsa_acct_name;
1310 struct samr_Ids user_rids;
1311 struct samr_Ids name_types;
1312 union samr_UserInfo *info = NULL;
1313 struct dcerpc_binding_handle *b = NULL;
1315 ZERO_STRUCT(sam_pol);
1316 ZERO_STRUCT(domain_pol);
1317 ZERO_STRUCT(user_pol);
1319 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1320 r->in.admin_account,
1321 r->in.admin_password,
1324 if (!NT_STATUS_IS_OK(status)) {
1328 /* Open the domain */
1330 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1332 if (!NT_STATUS_IS_OK(status)) {
1333 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1334 nt_errstr(status)));
1338 b = pipe_hnd->binding_handle;
1340 status = dcerpc_samr_Connect2(b, mem_ctx,
1342 SEC_FLAG_MAXIMUM_ALLOWED,
1345 if (!NT_STATUS_IS_OK(status)) {
1348 if (!NT_STATUS_IS_OK(result)) {
1353 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1355 SEC_FLAG_MAXIMUM_ALLOWED,
1359 if (!NT_STATUS_IS_OK(status)) {
1362 if (!NT_STATUS_IS_OK(result)) {
1367 /* Create domain user */
1369 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1370 strlower_m(acct_name);
1372 init_lsa_String(&lsa_acct_name, acct_name);
1374 status = dcerpc_samr_LookupNames(b, mem_ctx,
1382 if (!NT_STATUS_IS_OK(status)) {
1385 if (!NT_STATUS_IS_OK(result)) {
1390 if (name_types.ids[0] != SID_NAME_USER) {
1391 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1392 name_types.ids[0]));
1393 status = NT_STATUS_INVALID_WORKSTATION;
1397 user_rid = user_rids.ids[0];
1399 /* Open handle on user */
1401 status = dcerpc_samr_OpenUser(b, mem_ctx,
1403 SEC_FLAG_MAXIMUM_ALLOWED,
1407 if (!NT_STATUS_IS_OK(status)) {
1410 if (!NT_STATUS_IS_OK(result)) {
1417 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1422 if (!NT_STATUS_IS_OK(status)) {
1423 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1426 if (!NT_STATUS_IS_OK(result)) {
1428 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1432 /* now disable and setuser info */
1434 info->info16.acct_flags |= ACB_DISABLED;
1436 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1441 if (!NT_STATUS_IS_OK(status)) {
1442 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1445 if (!NT_STATUS_IS_OK(result)) {
1447 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1451 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1454 if (pipe_hnd && b) {
1455 if (is_valid_policy_hnd(&domain_pol)) {
1456 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1458 if (is_valid_policy_hnd(&sam_pol)) {
1459 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1461 TALLOC_FREE(pipe_hnd);
1471 /****************************************************************
1472 ****************************************************************/
1474 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1476 WERROR werr = WERR_OK;
1478 struct smbconf_ctx *ctx;
1480 err = smbconf_init_reg(r, &ctx, NULL);
1481 if (!SBC_ERROR_IS_OK(err)) {
1482 werr = WERR_NO_SUCH_SERVICE;
1486 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1488 err = smbconf_set_global_parameter(ctx, "security", "user");
1489 if (!SBC_ERROR_IS_OK(err)) {
1490 werr = WERR_NO_SUCH_SERVICE;
1494 err = smbconf_set_global_parameter(ctx, "workgroup",
1496 if (!SBC_ERROR_IS_OK(err)) {
1497 werr = WERR_NO_SUCH_SERVICE;
1501 smbconf_delete_global_parameter(ctx, "realm");
1505 err = smbconf_set_global_parameter(ctx, "security", "domain");
1506 if (!SBC_ERROR_IS_OK(err)) {
1507 werr = WERR_NO_SUCH_SERVICE;
1511 err = smbconf_set_global_parameter(ctx, "workgroup",
1512 r->out.netbios_domain_name);
1513 if (!SBC_ERROR_IS_OK(err)) {
1514 werr = WERR_NO_SUCH_SERVICE;
1518 if (r->out.domain_is_ad) {
1519 err = smbconf_set_global_parameter(ctx, "security", "ads");
1520 if (!SBC_ERROR_IS_OK(err)) {
1521 werr = WERR_NO_SUCH_SERVICE;
1525 err = smbconf_set_global_parameter(ctx, "realm",
1526 r->out.dns_domain_name);
1527 if (!SBC_ERROR_IS_OK(err)) {
1528 werr = WERR_NO_SUCH_SERVICE;
1534 smbconf_shutdown(ctx);
1538 /****************************************************************
1539 ****************************************************************/
1541 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1543 WERROR werr = WERR_OK;
1545 struct smbconf_ctx *ctx;
1547 err = smbconf_init_reg(r, &ctx, NULL);
1548 if (!SBC_ERROR_IS_OK(err)) {
1549 werr = WERR_NO_SUCH_SERVICE;
1553 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1555 err = smbconf_set_global_parameter(ctx, "security", "user");
1556 if (!SBC_ERROR_IS_OK(err)) {
1557 werr = WERR_NO_SUCH_SERVICE;
1561 err = smbconf_delete_global_parameter(ctx, "workgroup");
1562 if (!SBC_ERROR_IS_OK(err)) {
1563 werr = WERR_NO_SUCH_SERVICE;
1567 smbconf_delete_global_parameter(ctx, "realm");
1571 smbconf_shutdown(ctx);
1575 /****************************************************************
1576 ****************************************************************/
1578 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1582 if (!W_ERROR_IS_OK(r->out.result)) {
1583 return r->out.result;
1586 if (!r->in.modify_config) {
1590 werr = do_join_modify_vals_config(r);
1591 if (!W_ERROR_IS_OK(werr)) {
1595 lp_load_global(get_dyn_CONFIGFILE());
1597 r->out.modified_config = true;
1598 r->out.result = werr;
1603 /****************************************************************
1604 ****************************************************************/
1606 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1610 if (!W_ERROR_IS_OK(r->out.result)) {
1611 return r->out.result;
1614 if (!r->in.modify_config) {
1618 werr = do_unjoin_modify_vals_config(r);
1619 if (!W_ERROR_IS_OK(werr)) {
1623 lp_load_global(get_dyn_CONFIGFILE());
1625 r->out.modified_config = true;
1626 r->out.result = werr;
1631 /****************************************************************
1632 ****************************************************************/
1634 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1635 const char *domain_str,
1636 const char **domain_p,
1639 char *domain = NULL;
1641 const char *p = NULL;
1643 if (!domain_str || !domain_p || !dc_p) {
1647 p = strchr_m(domain_str, '\\');
1650 domain = talloc_strndup(mem_ctx, domain_str,
1651 PTR_DIFF(p, domain_str));
1652 dc = talloc_strdup(mem_ctx, p+1);
1657 domain = talloc_strdup(mem_ctx, domain_str);
1673 /****************************************************************
1674 ****************************************************************/
1676 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1677 struct libnet_JoinCtx *r)
1679 if (!r->in.domain_name) {
1680 libnet_join_set_error_string(mem_ctx, r,
1681 "No domain name defined");
1682 return WERR_INVALID_PARAM;
1685 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1688 libnet_join_set_error_string(mem_ctx, r,
1689 "Failed to parse domain name");
1690 return WERR_INVALID_PARAM;
1694 return WERR_SETUP_DOMAIN_CONTROLLER;
1697 if (!secrets_init()) {
1698 libnet_join_set_error_string(mem_ctx, r,
1699 "Unable to open secrets database");
1700 return WERR_CAN_NOT_COMPLETE;
1706 /****************************************************************
1707 ****************************************************************/
1709 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1713 /* Try adding dom admins to builtin\admins. Only log failures. */
1714 status = create_builtin_administrators(domain_sid);
1715 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1716 DEBUG(10,("Unable to auto-add domain administrators to "
1717 "BUILTIN\\Administrators during join because "
1718 "winbindd must be running.\n"));
1719 } else if (!NT_STATUS_IS_OK(status)) {
1720 DEBUG(5, ("Failed to auto-add domain administrators to "
1721 "BUILTIN\\Administrators during join: %s\n",
1722 nt_errstr(status)));
1725 /* Try adding dom users to builtin\users. Only log failures. */
1726 status = create_builtin_users(domain_sid);
1727 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1728 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1729 "during join because winbindd must be running.\n"));
1730 } else if (!NT_STATUS_IS_OK(status)) {
1731 DEBUG(5, ("Failed to auto-add domain administrators to "
1732 "BUILTIN\\Administrators during join: %s\n",
1733 nt_errstr(status)));
1737 /****************************************************************
1738 ****************************************************************/
1740 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1741 struct libnet_JoinCtx *r)
1745 if (!W_ERROR_IS_OK(r->out.result)) {
1746 return r->out.result;
1749 werr = do_JoinConfig(r);
1750 if (!W_ERROR_IS_OK(werr)) {
1754 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1758 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1759 if (r->out.dns_domain_name) {
1760 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1764 if (r->out.domain_is_ad &&
1765 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1766 ADS_STATUS ads_status;
1768 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1769 if (!ADS_ERR_OK(ads_status)) {
1770 return WERR_GENERAL_FAILURE;
1773 #endif /* HAVE_ADS */
1775 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1780 /****************************************************************
1781 ****************************************************************/
1783 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1786 ads_destroy(&r->in.ads);
1792 /****************************************************************
1793 ****************************************************************/
1795 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1798 ads_destroy(&r->in.ads);
1804 /****************************************************************
1805 ****************************************************************/
1807 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1808 struct libnet_JoinCtx **r)
1810 struct libnet_JoinCtx *ctx;
1812 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1817 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1819 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1820 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1822 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1829 /****************************************************************
1830 ****************************************************************/
1832 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1833 struct libnet_UnjoinCtx **r)
1835 struct libnet_UnjoinCtx *ctx;
1837 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1842 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1844 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1845 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1852 /****************************************************************
1853 ****************************************************************/
1855 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1856 struct libnet_JoinCtx *r)
1858 bool valid_security = false;
1859 bool valid_workgroup = false;
1860 bool valid_realm = false;
1862 /* check if configuration is already set correctly */
1864 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1866 switch (r->out.domain_is_ad) {
1868 valid_security = (lp_security() == SEC_DOMAIN);
1869 if (valid_workgroup && valid_security) {
1870 /* nothing to be done */
1875 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1876 switch (lp_security()) {
1879 valid_security = true;
1882 if (valid_workgroup && valid_realm && valid_security) {
1883 /* nothing to be done */
1889 /* check if we are supposed to manipulate configuration */
1891 if (!r->in.modify_config) {
1893 char *wrong_conf = talloc_strdup(mem_ctx, "");
1895 if (!valid_workgroup) {
1896 wrong_conf = talloc_asprintf_append(wrong_conf,
1897 "\"workgroup\" set to '%s', should be '%s'",
1898 lp_workgroup(), r->out.netbios_domain_name);
1899 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1903 wrong_conf = talloc_asprintf_append(wrong_conf,
1904 "\"realm\" set to '%s', should be '%s'",
1905 lp_realm(), r->out.dns_domain_name);
1906 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1909 if (!valid_security) {
1910 const char *sec = NULL;
1911 switch (lp_security()) {
1912 case SEC_USER: sec = "user"; break;
1913 case SEC_DOMAIN: sec = "domain"; break;
1914 case SEC_ADS: sec = "ads"; break;
1916 wrong_conf = talloc_asprintf_append(wrong_conf,
1917 "\"security\" set to '%s', should be %s",
1918 sec, r->out.domain_is_ad ?
1919 "either 'domain' or 'ads'" : "'domain'");
1920 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1923 libnet_join_set_error_string(mem_ctx, r,
1924 "Invalid configuration (%s) and configuration modification "
1925 "was not requested", wrong_conf);
1926 return WERR_CAN_NOT_COMPLETE;
1929 /* check if we are able to manipulate configuration */
1931 if (!lp_config_backend_is_registry()) {
1932 libnet_join_set_error_string(mem_ctx, r,
1933 "Configuration manipulation requested but not "
1934 "supported by backend");
1935 return WERR_NOT_SUPPORTED;
1941 /****************************************************************
1942 ****************************************************************/
1944 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1945 struct libnet_JoinCtx *r)
1949 struct cli_state *cli = NULL;
1951 ADS_STATUS ads_status;
1952 #endif /* HAVE_ADS */
1954 if (!r->in.dc_name) {
1955 struct netr_DsRGetDCNameInfo *info;
1957 status = dsgetdcname(mem_ctx,
1962 DS_FORCE_REDISCOVERY |
1963 DS_DIRECTORY_SERVICE_REQUIRED |
1964 DS_WRITABLE_REQUIRED |
1967 if (!NT_STATUS_IS_OK(status)) {
1968 libnet_join_set_error_string(mem_ctx, r,
1969 "failed to find DC for domain %s",
1971 get_friendly_nt_error_msg(status));
1972 return WERR_DCNOTFOUND;
1975 dc = strip_hostname(info->dc_unc);
1976 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1977 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1980 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1981 if (!NT_STATUS_IS_OK(status)) {
1982 libnet_join_set_error_string(mem_ctx, r,
1983 "failed to lookup DC info for domain '%s' over rpc: %s",
1984 r->in.domain_name, get_friendly_nt_error_msg(status));
1985 return ntstatus_to_werror(status);
1988 werr = libnet_join_check_config(mem_ctx, r);
1989 if (!W_ERROR_IS_OK(werr)) {
1995 create_local_private_krb5_conf_for_domain(
1996 r->out.dns_domain_name, r->out.netbios_domain_name,
1997 NULL, smbXcli_conn_remote_sockaddr(cli->conn),
1998 smbXcli_conn_remote_name(cli->conn));
2000 if (r->out.domain_is_ad && r->in.account_ou &&
2001 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2003 ads_status = libnet_join_connect_ads(mem_ctx, r);
2004 if (!ADS_ERR_OK(ads_status)) {
2005 return WERR_DEFAULT_JOIN_REQUIRED;
2008 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2009 if (!ADS_ERR_OK(ads_status)) {
2010 libnet_join_set_error_string(mem_ctx, r,
2011 "failed to precreate account in ou %s: %s",
2013 ads_errstr(ads_status));
2014 return WERR_DEFAULT_JOIN_REQUIRED;
2017 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2019 #endif /* HAVE_ADS */
2021 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2022 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2023 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2025 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2027 if (!NT_STATUS_IS_OK(status)) {
2028 libnet_join_set_error_string(mem_ctx, r,
2029 "failed to join domain '%s' over rpc: %s",
2030 r->in.domain_name, get_friendly_nt_error_msg(status));
2031 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2032 return WERR_SETUP_ALREADY_JOINED;
2034 werr = ntstatus_to_werror(status);
2038 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2039 werr = WERR_SETUP_NOT_JOINED;
2053 /****************************************************************
2054 ****************************************************************/
2056 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2057 struct libnet_JoinCtx *r)
2060 struct libnet_UnjoinCtx *u = NULL;
2062 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2063 if (!W_ERROR_IS_OK(werr)) {
2067 u->in.debug = r->in.debug;
2068 u->in.dc_name = r->in.dc_name;
2069 u->in.domain_name = r->in.domain_name;
2070 u->in.admin_account = r->in.admin_account;
2071 u->in.admin_password = r->in.admin_password;
2072 u->in.modify_config = r->in.modify_config;
2073 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2074 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2076 werr = libnet_Unjoin(mem_ctx, u);
2082 /****************************************************************
2083 ****************************************************************/
2085 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2086 struct libnet_JoinCtx *r)
2091 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2094 ZERO_STRUCT(r->out);
2096 werr = libnet_join_pre_processing(mem_ctx, r);
2097 if (!W_ERROR_IS_OK(werr)) {
2101 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2102 werr = libnet_DomainJoin(mem_ctx, r);
2103 if (!W_ERROR_IS_OK(werr)) {
2108 werr = libnet_join_post_processing(mem_ctx, r);
2109 if (!W_ERROR_IS_OK(werr)) {
2113 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2114 werr = libnet_join_post_verify(mem_ctx, r);
2115 if (!W_ERROR_IS_OK(werr)) {
2116 libnet_join_rollback(mem_ctx, r);
2121 r->out.result = werr;
2124 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2129 /****************************************************************
2130 ****************************************************************/
2132 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2133 struct libnet_UnjoinCtx *r)
2137 if (!r->in.domain_sid) {
2139 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2140 libnet_unjoin_set_error_string(mem_ctx, r,
2141 "Unable to fetch domain sid: are we joined?");
2142 return WERR_SETUP_NOT_JOINED;
2144 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2145 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2148 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2149 !r->in.delete_machine_account) {
2150 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2154 if (!r->in.dc_name) {
2155 struct netr_DsRGetDCNameInfo *info;
2157 status = dsgetdcname(mem_ctx,
2162 DS_DIRECTORY_SERVICE_REQUIRED |
2163 DS_WRITABLE_REQUIRED |
2166 if (!NT_STATUS_IS_OK(status)) {
2167 libnet_unjoin_set_error_string(mem_ctx, r,
2168 "failed to find DC for domain %s",
2170 get_friendly_nt_error_msg(status));
2171 return WERR_DCNOTFOUND;
2174 dc = strip_hostname(info->dc_unc);
2175 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2176 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2180 /* for net ads leave, try to delete the account. If it works,
2181 no sense in disabling. If it fails, we can still try to
2184 if (r->in.delete_machine_account) {
2185 ADS_STATUS ads_status;
2186 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2187 if (ADS_ERR_OK(ads_status)) {
2189 r->out.dns_domain_name =
2190 talloc_strdup(mem_ctx,
2191 r->in.ads->server.realm);
2193 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2195 if (!ADS_ERR_OK(ads_status)) {
2196 libnet_unjoin_set_error_string(mem_ctx, r,
2197 "failed to remove machine account from AD: %s",
2198 ads_errstr(ads_status));
2200 r->out.deleted_machine_account = true;
2201 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2202 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2206 #endif /* HAVE_ADS */
2208 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2210 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2211 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2212 if (!NT_STATUS_IS_OK(status)) {
2213 libnet_unjoin_set_error_string(mem_ctx, r,
2214 "failed to disable machine account via rpc: %s",
2215 get_friendly_nt_error_msg(status));
2216 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2217 return WERR_SETUP_NOT_JOINED;
2219 return ntstatus_to_werror(status);
2222 r->out.disabled_machine_account = true;
2225 /* If disable succeeded or was not requested at all, we
2226 should be getting rid of our end of things */
2228 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2233 /****************************************************************
2234 ****************************************************************/
2236 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2237 struct libnet_UnjoinCtx *r)
2239 if (!r->in.domain_name) {
2240 libnet_unjoin_set_error_string(mem_ctx, r,
2241 "No domain name defined");
2242 return WERR_INVALID_PARAM;
2245 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2248 libnet_unjoin_set_error_string(mem_ctx, r,
2249 "Failed to parse domain name");
2250 return WERR_INVALID_PARAM;
2254 return WERR_SETUP_DOMAIN_CONTROLLER;
2257 if (!secrets_init()) {
2258 libnet_unjoin_set_error_string(mem_ctx, r,
2259 "Unable to open secrets database");
2260 return WERR_CAN_NOT_COMPLETE;
2266 /****************************************************************
2267 ****************************************************************/
2269 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2270 struct libnet_UnjoinCtx *r)
2272 saf_delete(r->out.netbios_domain_name);
2273 saf_delete(r->out.dns_domain_name);
2275 return libnet_unjoin_config(r);
2278 /****************************************************************
2279 ****************************************************************/
2281 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2282 struct libnet_UnjoinCtx *r)
2287 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2290 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2291 if (!W_ERROR_IS_OK(werr)) {
2295 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2296 werr = libnet_DomainUnjoin(mem_ctx, r);
2297 if (!W_ERROR_IS_OK(werr)) {
2298 libnet_unjoin_config(r);
2303 werr = libnet_unjoin_post_processing(mem_ctx, r);
2304 if (!W_ERROR_IS_OK(werr)) {
2309 r->out.result = werr;
2312 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);