2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Gerald (Jerry) Carter 2006
5 * Copyright (C) Guenther Deschner 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "librpc/gen_ndr/ndr_libnet_join.h"
24 #include "libnet/libnet_join.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_lsa_c.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/ndr_netlogon.h"
31 #include "rpc_client/cli_netlogon.h"
32 #include "lib/smbconf/smbconf.h"
33 #include "lib/smbconf/smbconf_reg.h"
34 #include "../libds/common/flags.h"
36 #include "rpc_client/init_lsa.h"
37 #include "rpc_client/cli_pipe.h"
38 #include "../libcli/security/security.h"
40 #include "libsmb/libsmb.h"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/param/loadparm.h"
43 #include "libcli/auth/netlogon_creds_cli.h"
44 #include "auth/credentials/credentials.h"
46 /****************************************************************
47 ****************************************************************/
49 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
52 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
53 DEBUG(1,("libnet_Join:\n%s", str)); \
57 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
58 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
59 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
60 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
62 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
65 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
66 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
70 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
71 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
72 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
73 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
75 /****************************************************************
76 ****************************************************************/
78 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
79 struct libnet_JoinCtx *r,
80 const char *format, ...)
84 if (r->out.error_string) {
88 va_start(args, format);
89 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
93 /****************************************************************
94 ****************************************************************/
96 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
97 struct libnet_UnjoinCtx *r,
98 const char *format, ...)
102 if (r->out.error_string) {
106 va_start(args, format);
107 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
113 /****************************************************************
114 ****************************************************************/
116 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
117 const char *netbios_domain_name,
119 const char *user_name,
120 const char *password,
124 ADS_STRUCT *my_ads = NULL;
127 my_ads = ads_init(dns_domain_name,
131 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
135 SAFE_FREE(my_ads->auth.user_name);
136 my_ads->auth.user_name = SMB_STRDUP(user_name);
137 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
139 SAFE_FREE(my_ads->auth.realm);
140 my_ads->auth.realm = smb_xstrdup(cp);
141 if (!strupper_m(my_ads->auth.realm)) {
142 ads_destroy(&my_ads);
143 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
149 SAFE_FREE(my_ads->auth.password);
150 my_ads->auth.password = SMB_STRDUP(password);
153 status = ads_connect_user_creds(my_ads);
154 if (!ADS_ERR_OK(status)) {
155 ads_destroy(&my_ads);
163 /****************************************************************
164 ****************************************************************/
166 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
167 struct libnet_JoinCtx *r)
171 status = libnet_connect_ads(r->out.dns_domain_name,
172 r->out.netbios_domain_name,
175 r->in.admin_password,
177 if (!ADS_ERR_OK(status)) {
178 libnet_join_set_error_string(mem_ctx, r,
179 "failed to connect to AD: %s",
184 if (!r->out.netbios_domain_name) {
185 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
186 r->in.ads->server.workgroup);
187 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
190 if (!r->out.dns_domain_name) {
191 r->out.dns_domain_name = talloc_strdup(mem_ctx,
192 r->in.ads->config.realm);
193 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
196 r->out.domain_is_ad = true;
201 /****************************************************************
202 ****************************************************************/
204 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
205 struct libnet_UnjoinCtx *r)
209 status = libnet_connect_ads(r->in.domain_name,
213 r->in.admin_password,
215 if (!ADS_ERR_OK(status)) {
216 libnet_unjoin_set_error_string(mem_ctx, r,
217 "failed to connect to AD: %s",
224 /****************************************************************
225 join a domain using ADS (LDAP mods)
226 ****************************************************************/
228 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
229 struct libnet_JoinCtx *r)
232 LDAPMessage *res = NULL;
233 const char *attrs[] = { "dn", NULL };
236 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
237 if (!ADS_ERR_OK(status)) {
241 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
242 if (!ADS_ERR_OK(status)) {
246 if (ads_count_replies(r->in.ads, res) != 1) {
247 ads_msgfree(r->in.ads, res);
248 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
251 ads_msgfree(r->in.ads, res);
253 /* Attempt to create the machine account and bail if this fails.
254 Assume that the admin wants exactly what they requested */
256 status = ads_create_machine_acct(r->in.ads,
260 if (ADS_ERR_OK(status)) {
261 DEBUG(1,("machine account creation created\n"));
263 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
264 (status.err.rc == LDAP_ALREADY_EXISTS)) {
265 status = ADS_SUCCESS;
268 if (!ADS_ERR_OK(status)) {
269 DEBUG(1,("machine account creation failed\n"));
273 status = ads_move_machine_acct(r->in.ads,
277 if (!ADS_ERR_OK(status)) {
278 DEBUG(1,("failure to locate/move pre-existing "
279 "machine account\n"));
283 DEBUG(1,("The machine account %s the specified OU.\n",
284 moved ? "was moved into" : "already exists in"));
289 /****************************************************************
290 ****************************************************************/
292 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
293 struct libnet_UnjoinCtx *r)
298 status = libnet_unjoin_connect_ads(mem_ctx, r);
299 if (!ADS_ERR_OK(status)) {
300 libnet_unjoin_set_error_string(mem_ctx, r,
301 "failed to connect to AD: %s",
307 status = ads_leave_realm(r->in.ads, r->in.machine_name);
308 if (!ADS_ERR_OK(status)) {
309 libnet_unjoin_set_error_string(mem_ctx, r,
310 "failed to leave realm: %s",
318 /****************************************************************
319 ****************************************************************/
321 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
322 struct libnet_JoinCtx *r)
325 LDAPMessage *res = NULL;
328 if (!r->in.machine_name) {
329 return ADS_ERROR(LDAP_NO_MEMORY);
332 status = ads_find_machine_acct(r->in.ads,
335 if (!ADS_ERR_OK(status)) {
339 if (ads_count_replies(r->in.ads, res) != 1) {
340 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
344 dn = ads_get_dn(r->in.ads, mem_ctx, res);
346 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
350 r->out.dn = talloc_strdup(mem_ctx, dn);
352 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
357 ads_msgfree(r->in.ads, res);
363 static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
364 struct libnet_JoinCtx *r,
370 if (r->in.machine_name == NULL) {
371 return ADS_ERROR_SYSTEM(EINVAL);
374 status = ads_get_service_principal_names(mem_ctx,
383 /****************************************************************
384 Set a machines dNSHostName and servicePrincipalName attributes
385 ****************************************************************/
387 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
388 struct libnet_JoinCtx *r)
393 const char **spn_array = NULL;
400 status = libnet_join_find_machine_acct(mem_ctx, r);
401 if (!ADS_ERR_OK(status)) {
405 status = libnet_join_get_machine_spns(mem_ctx,
407 discard_const_p(char **, &spn_array),
409 if (!ADS_ERR_OK(status)) {
410 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
413 /* Windows only creates HOST/shortname & HOST/fqdn. */
415 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
417 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
419 if (!strupper_m(spn)) {
420 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
423 ok = ads_element_in_array(spn_array, num_spns, spn);
425 ok = add_string_to_array(spn_array, spn,
426 &spn_array, (int *)&num_spns);
428 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
432 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
433 || (strchr(my_fqdn, '.') == NULL)) {
434 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
435 r->out.dns_domain_name);
438 if (!strlower_m(my_fqdn)) {
439 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
442 if (!strequal(my_fqdn, r->in.machine_name)) {
443 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
445 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
448 ok = ads_element_in_array(spn_array, num_spns, spn);
450 ok = add_string_to_array(spn_array, spn,
451 &spn_array, (int *)&num_spns);
453 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
458 /* make sure to NULL terminate the array */
459 spn_array = talloc_realloc(mem_ctx, spn_array, const char *, num_spns + 1);
460 if (spn_array == NULL) {
461 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
463 spn_array[num_spns] = NULL;
465 mods = ads_init_mods(mem_ctx);
467 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
470 /* fields of primary importance */
472 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
473 if (!ADS_ERR_OK(status)) {
474 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
477 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
479 if (!ADS_ERR_OK(status)) {
480 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
483 return ads_gen_mod(r->in.ads, r->out.dn, mods);
486 /****************************************************************
487 ****************************************************************/
489 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
490 struct libnet_JoinCtx *r)
495 if (!r->in.create_upn) {
501 status = libnet_join_find_machine_acct(mem_ctx, r);
502 if (!ADS_ERR_OK(status)) {
507 const char *realm = r->out.dns_domain_name;
509 /* in case we are about to generate a keytab during the join
510 * make sure the default upn we create is usable with kinit -k.
513 if (USE_KERBEROS_KEYTAB) {
514 realm = talloc_strdup_upper(mem_ctx,
515 r->out.dns_domain_name);
519 return ADS_ERROR(LDAP_NO_MEMORY);
522 r->in.upn = talloc_asprintf(mem_ctx,
527 return ADS_ERROR(LDAP_NO_MEMORY);
531 /* now do the mods */
533 mods = ads_init_mods(mem_ctx);
535 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
538 /* fields of primary importance */
540 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
541 if (!ADS_ERR_OK(status)) {
542 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
545 return ads_gen_mod(r->in.ads, r->out.dn, mods);
549 /****************************************************************
550 ****************************************************************/
552 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
553 struct libnet_JoinCtx *r)
559 if (!r->in.os_name || !r->in.os_version ) {
565 status = libnet_join_find_machine_acct(mem_ctx, r);
566 if (!ADS_ERR_OK(status)) {
570 /* now do the mods */
572 mods = ads_init_mods(mem_ctx);
574 return ADS_ERROR(LDAP_NO_MEMORY);
577 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
579 return ADS_ERROR(LDAP_NO_MEMORY);
582 /* fields of primary importance */
584 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
586 if (!ADS_ERR_OK(status)) {
590 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
592 if (!ADS_ERR_OK(status)) {
596 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
598 if (!ADS_ERR_OK(status)) {
602 return ads_gen_mod(r->in.ads, r->out.dn, mods);
605 /****************************************************************
606 ****************************************************************/
608 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
609 struct libnet_JoinCtx *r)
611 if (!USE_SYSTEM_KEYTAB) {
615 if (ads_keytab_create_default(r->in.ads) != 0) {
622 /****************************************************************
623 ****************************************************************/
625 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
626 struct libnet_JoinCtx *r)
628 uint32_t domain_func;
630 const char *salt = NULL;
631 char *std_salt = NULL;
633 status = ads_domain_func_level(r->in.ads, &domain_func);
634 if (!ADS_ERR_OK(status)) {
635 libnet_join_set_error_string(mem_ctx, r,
636 "failed to determine domain functional level: %s",
641 /* go ahead and setup the default salt */
643 std_salt = kerberos_standard_des_salt();
645 libnet_join_set_error_string(mem_ctx, r,
646 "failed to obtain standard DES salt");
650 salt = talloc_strdup(mem_ctx, std_salt);
657 /* if it's a Windows functional domain, we have to look for the UPN */
659 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
662 upn = ads_get_upn(r->in.ads, mem_ctx,
665 salt = talloc_strdup(mem_ctx, upn);
672 return kerberos_secrets_store_des_salt(salt);
675 /****************************************************************
676 ****************************************************************/
678 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
679 struct libnet_JoinCtx *r)
684 status = libnet_join_connect_ads(mem_ctx, r);
685 if (!ADS_ERR_OK(status)) {
690 status = libnet_join_set_machine_spn(mem_ctx, r);
691 if (!ADS_ERR_OK(status)) {
692 libnet_join_set_error_string(mem_ctx, r,
693 "Failed to set machine spn: %s\n"
694 "Do you have sufficient permissions to create machine "
700 status = libnet_join_set_os_attributes(mem_ctx, r);
701 if (!ADS_ERR_OK(status)) {
702 libnet_join_set_error_string(mem_ctx, r,
703 "failed to set machine os attributes: %s",
708 status = libnet_join_set_machine_upn(mem_ctx, r);
709 if (!ADS_ERR_OK(status)) {
710 libnet_join_set_error_string(mem_ctx, r,
711 "failed to set machine upn: %s",
716 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
717 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
720 if (!libnet_join_create_keytab(mem_ctx, r)) {
721 libnet_join_set_error_string(mem_ctx, r,
722 "failed to create kerberos keytab");
723 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
728 #endif /* HAVE_ADS */
730 /****************************************************************
731 Store the machine password and domain SID
732 ****************************************************************/
734 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
735 struct libnet_JoinCtx *r)
737 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
740 DEBUG(1,("Failed to save domain sid\n"));
744 if (!secrets_store_machine_password(r->in.machine_password,
745 r->out.netbios_domain_name,
746 r->in.secure_channel_type))
748 DEBUG(1,("Failed to save machine password\n"));
755 /****************************************************************
756 Connect dc's IPC$ share
757 ****************************************************************/
759 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
764 struct cli_state **cli)
769 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
772 if (use_kerberos && pass) {
773 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
776 return cli_full_connection(cli, NULL,
784 SMB_SIGNING_DEFAULT);
787 /****************************************************************
788 Lookup domain dc's info
789 ****************************************************************/
791 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
792 struct libnet_JoinCtx *r,
793 struct cli_state **cli)
795 struct rpc_pipe_client *pipe_hnd = NULL;
796 struct policy_handle lsa_pol;
797 NTSTATUS status, result;
798 union lsa_PolicyInformation *info = NULL;
799 struct dcerpc_binding_handle *b;
801 status = libnet_join_connect_dc_ipc(r->in.dc_name,
804 r->in.admin_password,
807 if (!NT_STATUS_IS_OK(status)) {
811 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
813 if (!NT_STATUS_IS_OK(status)) {
814 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
819 b = pipe_hnd->binding_handle;
821 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
822 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
823 if (!NT_STATUS_IS_OK(status)) {
827 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
832 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
833 r->out.domain_is_ad = true;
834 r->out.netbios_domain_name = info->dns.name.string;
835 r->out.dns_domain_name = info->dns.dns_domain.string;
836 r->out.forest_name = info->dns.dns_forest.string;
837 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
838 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
841 if (!NT_STATUS_IS_OK(status)) {
842 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
844 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
847 if (!NT_STATUS_IS_OK(status)) {
850 if (!NT_STATUS_IS_OK(result)) {
855 r->out.netbios_domain_name = info->account_domain.name.string;
856 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
857 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
860 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
861 TALLOC_FREE(pipe_hnd);
867 /****************************************************************
868 Do the domain join unsecure
869 ****************************************************************/
871 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
872 struct libnet_JoinCtx *r,
873 struct cli_state *cli)
875 TALLOC_CTX *frame = talloc_stackframe();
876 struct rpc_pipe_client *netlogon_pipe = NULL;
877 struct netlogon_creds_cli_context *netlogon_creds = NULL;
878 struct samr_Password current_nt_hash;
879 const char *account_name = NULL;
882 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
884 if (!NT_STATUS_IS_OK(status)) {
889 if (!r->in.machine_password) {
890 r->in.machine_password = generate_random_password(mem_ctx,
891 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
892 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
893 if (r->in.machine_password == NULL) {
895 return NT_STATUS_NO_MEMORY;
899 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
900 E_md4hash(r->in.admin_password, current_nt_hash.hash);
902 account_name = talloc_asprintf(frame, "%s$",
904 if (account_name == NULL) {
906 return NT_STATUS_NO_MEMORY;
909 status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
912 r->in.secure_channel_type,
916 if (!NT_STATUS_IS_OK(status)) {
921 status = rpccli_setup_netlogon_creds(cli,
923 true, /* force_reauth */
925 NULL); /* previous_nt_hash */
926 if (!NT_STATUS_IS_OK(status)) {
931 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
932 netlogon_pipe->binding_handle,
933 r->in.machine_password,
934 NULL); /* new_version */
935 if (!NT_STATUS_IS_OK(status)) {
944 /****************************************************************
946 ****************************************************************/
948 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
949 struct libnet_JoinCtx *r,
950 struct cli_state *cli)
952 struct rpc_pipe_client *pipe_hnd = NULL;
953 struct policy_handle sam_pol, domain_pol, user_pol;
954 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
956 struct lsa_String lsa_acct_name;
958 uint32_t acct_flags = ACB_WSTRUST;
959 struct samr_Ids user_rids;
960 struct samr_Ids name_types;
961 union samr_UserInfo user_info;
962 struct dcerpc_binding_handle *b = NULL;
963 unsigned int old_timeout = 0;
965 DATA_BLOB session_key = data_blob_null;
966 struct samr_CryptPassword crypt_pwd;
967 struct samr_CryptPasswordEx crypt_pwd_ex;
969 ZERO_STRUCT(sam_pol);
970 ZERO_STRUCT(domain_pol);
971 ZERO_STRUCT(user_pol);
973 switch (r->in.secure_channel_type) {
975 acct_flags = ACB_WSTRUST;
978 acct_flags = ACB_SVRTRUST;
981 return NT_STATUS_INVALID_PARAMETER;
984 if (!r->in.machine_password) {
985 r->in.machine_password = generate_random_password(mem_ctx,
986 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
987 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
988 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
991 /* Open the domain */
993 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
995 if (!NT_STATUS_IS_OK(status)) {
996 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1001 b = pipe_hnd->binding_handle;
1003 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1004 if (!NT_STATUS_IS_OK(status)) {
1005 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1006 nt_errstr(status)));
1010 status = dcerpc_samr_Connect2(b, mem_ctx,
1012 SAMR_ACCESS_ENUM_DOMAINS
1013 | SAMR_ACCESS_LOOKUP_DOMAIN,
1016 if (!NT_STATUS_IS_OK(status)) {
1019 if (!NT_STATUS_IS_OK(result)) {
1024 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1026 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1027 | SAMR_DOMAIN_ACCESS_CREATE_USER
1028 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1032 if (!NT_STATUS_IS_OK(status)) {
1035 if (!NT_STATUS_IS_OK(result)) {
1040 /* Create domain user */
1042 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1043 if (!strlower_m(acct_name)) {
1044 status = NT_STATUS_INVALID_PARAMETER;
1048 init_lsa_String(&lsa_acct_name, acct_name);
1050 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1051 uint32_t access_desired =
1052 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1053 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1054 SAMR_USER_ACCESS_SET_PASSWORD |
1055 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1056 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1057 uint32_t access_granted = 0;
1059 DEBUG(10,("Creating account with desired access mask: %d\n",
1062 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1071 if (!NT_STATUS_IS_OK(status)) {
1076 if (!NT_STATUS_IS_OK(status) &&
1077 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1079 DEBUG(10,("Creation of workstation account failed: %s\n",
1080 nt_errstr(status)));
1082 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1083 username/password combo but the user does not have
1084 administrator access. */
1086 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1087 libnet_join_set_error_string(mem_ctx, r,
1088 "User specified does not have "
1089 "administrator privileges");
1095 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1096 if (!(r->in.join_flags &
1097 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1102 /* We *must* do this.... don't ask... */
1104 if (NT_STATUS_IS_OK(status)) {
1105 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1109 status = dcerpc_samr_LookupNames(b, mem_ctx,
1116 if (!NT_STATUS_IS_OK(status)) {
1119 if (!NT_STATUS_IS_OK(result)) {
1123 if (user_rids.count != 1) {
1124 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1127 if (name_types.count != 1) {
1128 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1132 if (name_types.ids[0] != SID_NAME_USER) {
1133 DEBUG(0,("%s is not a user account (type=%d)\n",
1134 acct_name, name_types.ids[0]));
1135 status = NT_STATUS_INVALID_WORKSTATION;
1139 user_rid = user_rids.ids[0];
1141 /* Open handle on user */
1143 status = dcerpc_samr_OpenUser(b, mem_ctx,
1145 SEC_FLAG_MAXIMUM_ALLOWED,
1149 if (!NT_STATUS_IS_OK(status)) {
1152 if (!NT_STATUS_IS_OK(result)) {
1157 /* Fill in the additional account flags now */
1159 acct_flags |= ACB_PWNOEXP;
1161 /* Set account flags on machine account */
1162 ZERO_STRUCT(user_info.info16);
1163 user_info.info16.acct_flags = acct_flags;
1165 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1170 if (!NT_STATUS_IS_OK(status)) {
1171 dcerpc_samr_DeleteUser(b, mem_ctx,
1175 libnet_join_set_error_string(mem_ctx, r,
1176 "Failed to set account flags for machine account (%s)\n",
1181 if (!NT_STATUS_IS_OK(result)) {
1184 dcerpc_samr_DeleteUser(b, mem_ctx,
1188 libnet_join_set_error_string(mem_ctx, r,
1189 "Failed to set account flags for machine account (%s)\n",
1194 /* Set password on machine account - first try level 26 */
1197 * increase the timeout as password filter modules on the DC
1198 * might delay the operation for a significant amount of time
1200 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1202 init_samr_CryptPasswordEx(r->in.machine_password,
1206 user_info.info26.password = crypt_pwd_ex;
1207 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1209 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1215 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1217 /* retry with level 24 */
1219 init_samr_CryptPassword(r->in.machine_password,
1223 user_info.info24.password = crypt_pwd;
1224 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1226 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1233 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1235 if (!NT_STATUS_IS_OK(status)) {
1237 dcerpc_samr_DeleteUser(b, mem_ctx,
1241 libnet_join_set_error_string(mem_ctx, r,
1242 "Failed to set password for machine account (%s)\n",
1246 if (!NT_STATUS_IS_OK(result)) {
1249 dcerpc_samr_DeleteUser(b, mem_ctx,
1253 libnet_join_set_error_string(mem_ctx, r,
1254 "Failed to set password for machine account (%s)\n",
1259 status = NT_STATUS_OK;
1266 data_blob_clear_free(&session_key);
1268 if (is_valid_policy_hnd(&sam_pol)) {
1269 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1271 if (is_valid_policy_hnd(&domain_pol)) {
1272 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1274 if (is_valid_policy_hnd(&user_pol)) {
1275 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1277 TALLOC_FREE(pipe_hnd);
1282 /****************************************************************
1283 ****************************************************************/
1285 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1286 const char *netbios_domain_name,
1287 const char *dc_name,
1288 const bool use_kerberos)
1290 TALLOC_CTX *frame = talloc_stackframe();
1291 struct cli_state *cli = NULL;
1292 struct rpc_pipe_client *netlogon_pipe = NULL;
1293 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1294 struct netlogon_creds_CredentialState *creds = NULL;
1295 uint32_t netlogon_flags = 0;
1296 enum netr_SchannelType sec_chan_type = 0;
1298 char *machine_password = NULL;
1299 const char *machine_name = NULL;
1300 const char *machine_account = NULL;
1302 struct samr_Password current_nt_hash;
1303 struct samr_Password *previous_nt_hash = NULL;
1308 return NT_STATUS_INVALID_PARAMETER;
1311 if (!secrets_init()) {
1313 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1316 ok = get_trust_pw_clear(netbios_domain_name,
1322 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1325 machine_account = talloc_asprintf(frame, "%s$", machine_name);
1326 if (machine_account == NULL) {
1327 SAFE_FREE(machine_password);
1328 SAFE_FREE(previous_nt_hash);
1330 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1334 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1337 status = cli_full_connection(&cli, NULL,
1342 netbios_domain_name,
1345 SMB_SIGNING_DEFAULT);
1347 E_md4hash(machine_password, current_nt_hash.hash);
1348 SAFE_FREE(machine_password);
1350 if (!NT_STATUS_IS_OK(status)) {
1351 status = cli_full_connection(&cli, NULL,
1359 SMB_SIGNING_DEFAULT);
1362 if (!NT_STATUS_IS_OK(status)) {
1363 SAFE_FREE(previous_nt_hash);
1368 status = rpccli_create_netlogon_creds(dc_name,
1369 netbios_domain_name,
1375 if (!NT_STATUS_IS_OK(status)) {
1376 SAFE_FREE(previous_nt_hash);
1382 status = rpccli_setup_netlogon_creds(cli,
1384 true, /* force_reauth */
1387 SAFE_FREE(previous_nt_hash);
1388 if (!NT_STATUS_IS_OK(status)) {
1389 DEBUG(0,("connect_to_domain_password_server: "
1390 "unable to open the domain client session to "
1391 "machine %s. Flags[0x%08X] Error was : %s.\n",
1392 dc_name, (unsigned)netlogon_flags,
1393 nt_errstr(status)));
1399 status = netlogon_creds_cli_get(netlogon_creds,
1402 if (!NT_STATUS_IS_OK(status)) {
1407 netlogon_flags = creds->negotiate_flags;
1410 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1413 return NT_STATUS_OK;
1416 status = cli_rpc_pipe_open_schannel_with_key(
1417 cli, &ndr_table_netlogon, NCACN_NP,
1418 netbios_domain_name,
1419 netlogon_creds, &netlogon_pipe);
1421 TALLOC_FREE(netlogon_pipe);
1423 if (!NT_STATUS_IS_OK(status)) {
1424 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1425 "on netlogon pipe to server %s for domain %s. "
1427 smbXcli_conn_remote_name(cli->conn),
1428 netbios_domain_name, nt_errstr(status)));
1436 return NT_STATUS_OK;
1439 /****************************************************************
1440 ****************************************************************/
1442 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1443 struct libnet_JoinCtx *r)
1447 status = libnet_join_ok(r->in.msg_ctx,
1448 r->out.netbios_domain_name,
1450 r->in.use_kerberos);
1451 if (!NT_STATUS_IS_OK(status)) {
1452 libnet_join_set_error_string(mem_ctx, r,
1453 "failed to verify domain membership after joining: %s",
1454 get_friendly_nt_error_msg(status));
1455 return WERR_SETUP_NOT_JOINED;
1461 /****************************************************************
1462 ****************************************************************/
1464 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1465 struct libnet_UnjoinCtx *r)
1467 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1471 if (!secrets_delete_domain_sid(lp_workgroup())) {
1478 /****************************************************************
1479 ****************************************************************/
1481 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1482 struct libnet_UnjoinCtx *r)
1484 struct cli_state *cli = NULL;
1485 struct rpc_pipe_client *pipe_hnd = NULL;
1486 struct policy_handle sam_pol, domain_pol, user_pol;
1487 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1490 struct lsa_String lsa_acct_name;
1491 struct samr_Ids user_rids;
1492 struct samr_Ids name_types;
1493 union samr_UserInfo *info = NULL;
1494 struct dcerpc_binding_handle *b = NULL;
1496 ZERO_STRUCT(sam_pol);
1497 ZERO_STRUCT(domain_pol);
1498 ZERO_STRUCT(user_pol);
1500 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1501 r->in.admin_account,
1503 r->in.admin_password,
1506 if (!NT_STATUS_IS_OK(status)) {
1510 /* Open the domain */
1512 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1514 if (!NT_STATUS_IS_OK(status)) {
1515 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1516 nt_errstr(status)));
1520 b = pipe_hnd->binding_handle;
1522 status = dcerpc_samr_Connect2(b, mem_ctx,
1524 SEC_FLAG_MAXIMUM_ALLOWED,
1527 if (!NT_STATUS_IS_OK(status)) {
1530 if (!NT_STATUS_IS_OK(result)) {
1535 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1537 SEC_FLAG_MAXIMUM_ALLOWED,
1541 if (!NT_STATUS_IS_OK(status)) {
1544 if (!NT_STATUS_IS_OK(result)) {
1549 /* Create domain user */
1551 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1552 if (!strlower_m(acct_name)) {
1553 status = NT_STATUS_INVALID_PARAMETER;
1557 init_lsa_String(&lsa_acct_name, acct_name);
1559 status = dcerpc_samr_LookupNames(b, mem_ctx,
1567 if (!NT_STATUS_IS_OK(status)) {
1570 if (!NT_STATUS_IS_OK(result)) {
1574 if (user_rids.count != 1) {
1575 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1578 if (name_types.count != 1) {
1579 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1583 if (name_types.ids[0] != SID_NAME_USER) {
1584 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1585 name_types.ids[0]));
1586 status = NT_STATUS_INVALID_WORKSTATION;
1590 user_rid = user_rids.ids[0];
1592 /* Open handle on user */
1594 status = dcerpc_samr_OpenUser(b, mem_ctx,
1596 SEC_FLAG_MAXIMUM_ALLOWED,
1600 if (!NT_STATUS_IS_OK(status)) {
1603 if (!NT_STATUS_IS_OK(result)) {
1610 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1615 if (!NT_STATUS_IS_OK(status)) {
1616 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1619 if (!NT_STATUS_IS_OK(result)) {
1621 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1625 /* now disable and setuser info */
1627 info->info16.acct_flags |= ACB_DISABLED;
1629 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1634 if (!NT_STATUS_IS_OK(status)) {
1635 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1638 if (!NT_STATUS_IS_OK(result)) {
1640 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1644 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1647 if (pipe_hnd && b) {
1648 if (is_valid_policy_hnd(&domain_pol)) {
1649 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1651 if (is_valid_policy_hnd(&sam_pol)) {
1652 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1654 TALLOC_FREE(pipe_hnd);
1664 /****************************************************************
1665 ****************************************************************/
1667 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1669 WERROR werr = WERR_OK;
1671 struct smbconf_ctx *ctx;
1673 err = smbconf_init_reg(r, &ctx, NULL);
1674 if (!SBC_ERROR_IS_OK(err)) {
1675 werr = WERR_NO_SUCH_SERVICE;
1679 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1681 err = smbconf_set_global_parameter(ctx, "security", "user");
1682 if (!SBC_ERROR_IS_OK(err)) {
1683 werr = WERR_NO_SUCH_SERVICE;
1687 err = smbconf_set_global_parameter(ctx, "workgroup",
1689 if (!SBC_ERROR_IS_OK(err)) {
1690 werr = WERR_NO_SUCH_SERVICE;
1694 smbconf_delete_global_parameter(ctx, "realm");
1698 err = smbconf_set_global_parameter(ctx, "security", "domain");
1699 if (!SBC_ERROR_IS_OK(err)) {
1700 werr = WERR_NO_SUCH_SERVICE;
1704 err = smbconf_set_global_parameter(ctx, "workgroup",
1705 r->out.netbios_domain_name);
1706 if (!SBC_ERROR_IS_OK(err)) {
1707 werr = WERR_NO_SUCH_SERVICE;
1711 if (r->out.domain_is_ad) {
1712 err = smbconf_set_global_parameter(ctx, "security", "ads");
1713 if (!SBC_ERROR_IS_OK(err)) {
1714 werr = WERR_NO_SUCH_SERVICE;
1718 err = smbconf_set_global_parameter(ctx, "realm",
1719 r->out.dns_domain_name);
1720 if (!SBC_ERROR_IS_OK(err)) {
1721 werr = WERR_NO_SUCH_SERVICE;
1727 smbconf_shutdown(ctx);
1731 /****************************************************************
1732 ****************************************************************/
1734 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1736 WERROR werr = WERR_OK;
1738 struct smbconf_ctx *ctx;
1740 err = smbconf_init_reg(r, &ctx, NULL);
1741 if (!SBC_ERROR_IS_OK(err)) {
1742 werr = WERR_NO_SUCH_SERVICE;
1746 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1748 err = smbconf_set_global_parameter(ctx, "security", "user");
1749 if (!SBC_ERROR_IS_OK(err)) {
1750 werr = WERR_NO_SUCH_SERVICE;
1754 err = smbconf_delete_global_parameter(ctx, "workgroup");
1755 if (!SBC_ERROR_IS_OK(err)) {
1756 werr = WERR_NO_SUCH_SERVICE;
1760 smbconf_delete_global_parameter(ctx, "realm");
1764 smbconf_shutdown(ctx);
1768 /****************************************************************
1769 ****************************************************************/
1771 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1775 if (!W_ERROR_IS_OK(r->out.result)) {
1776 return r->out.result;
1779 if (!r->in.modify_config) {
1783 werr = do_join_modify_vals_config(r);
1784 if (!W_ERROR_IS_OK(werr)) {
1788 lp_load_global(get_dyn_CONFIGFILE());
1790 r->out.modified_config = true;
1791 r->out.result = werr;
1796 /****************************************************************
1797 ****************************************************************/
1799 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1803 if (!W_ERROR_IS_OK(r->out.result)) {
1804 return r->out.result;
1807 if (!r->in.modify_config) {
1811 werr = do_unjoin_modify_vals_config(r);
1812 if (!W_ERROR_IS_OK(werr)) {
1816 lp_load_global(get_dyn_CONFIGFILE());
1818 r->out.modified_config = true;
1819 r->out.result = werr;
1824 /****************************************************************
1825 ****************************************************************/
1827 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1828 const char *domain_str,
1829 const char **domain_p,
1832 char *domain = NULL;
1834 const char *p = NULL;
1836 if (!domain_str || !domain_p || !dc_p) {
1840 p = strchr_m(domain_str, '\\');
1843 domain = talloc_strndup(mem_ctx, domain_str,
1844 PTR_DIFF(p, domain_str));
1845 dc = talloc_strdup(mem_ctx, p+1);
1850 domain = talloc_strdup(mem_ctx, domain_str);
1866 /****************************************************************
1867 ****************************************************************/
1869 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1870 struct libnet_JoinCtx *r)
1872 if (!r->in.domain_name) {
1873 libnet_join_set_error_string(mem_ctx, r,
1874 "No domain name defined");
1875 return WERR_INVALID_PARAM;
1878 if (strlen(r->in.machine_name) > 15) {
1879 libnet_join_set_error_string(mem_ctx, r,
1880 "Our netbios name can be at most 15 chars long, "
1881 "\"%s\" is %u chars long\n",
1883 (unsigned int)strlen(r->in.machine_name));
1884 return WERR_INVALID_PARAM;
1887 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1890 libnet_join_set_error_string(mem_ctx, r,
1891 "Failed to parse domain name");
1892 return WERR_INVALID_PARAM;
1896 return WERR_SETUP_DOMAIN_CONTROLLER;
1899 if (!r->in.admin_domain) {
1900 char *admin_domain = NULL;
1901 char *admin_account = NULL;
1902 split_domain_user(mem_ctx,
1903 r->in.admin_account,
1906 r->in.admin_domain = admin_domain;
1907 r->in.admin_account = admin_account;
1910 if (!secrets_init()) {
1911 libnet_join_set_error_string(mem_ctx, r,
1912 "Unable to open secrets database");
1913 return WERR_CAN_NOT_COMPLETE;
1919 /****************************************************************
1920 ****************************************************************/
1922 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1926 /* Try adding dom admins to builtin\admins. Only log failures. */
1927 status = create_builtin_administrators(domain_sid);
1928 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1929 DEBUG(10,("Unable to auto-add domain administrators to "
1930 "BUILTIN\\Administrators during join because "
1931 "winbindd must be running.\n"));
1932 } else if (!NT_STATUS_IS_OK(status)) {
1933 DEBUG(5, ("Failed to auto-add domain administrators to "
1934 "BUILTIN\\Administrators during join: %s\n",
1935 nt_errstr(status)));
1938 /* Try adding dom users to builtin\users. Only log failures. */
1939 status = create_builtin_users(domain_sid);
1940 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1941 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1942 "during join because winbindd must be running.\n"));
1943 } else if (!NT_STATUS_IS_OK(status)) {
1944 DEBUG(5, ("Failed to auto-add domain administrators to "
1945 "BUILTIN\\Administrators during join: %s\n",
1946 nt_errstr(status)));
1950 /****************************************************************
1951 ****************************************************************/
1953 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1954 struct libnet_JoinCtx *r)
1958 if (!W_ERROR_IS_OK(r->out.result)) {
1959 return r->out.result;
1962 werr = do_JoinConfig(r);
1963 if (!W_ERROR_IS_OK(werr)) {
1967 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1971 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1972 if (r->out.dns_domain_name) {
1973 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1977 if (r->out.domain_is_ad &&
1978 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1979 ADS_STATUS ads_status;
1981 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1982 if (!ADS_ERR_OK(ads_status)) {
1983 return WERR_GENERAL_FAILURE;
1986 #endif /* HAVE_ADS */
1988 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1993 /****************************************************************
1994 ****************************************************************/
1996 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1999 ads_destroy(&r->in.ads);
2005 /****************************************************************
2006 ****************************************************************/
2008 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2011 ads_destroy(&r->in.ads);
2017 /****************************************************************
2018 ****************************************************************/
2020 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2021 struct libnet_JoinCtx **r)
2023 struct libnet_JoinCtx *ctx;
2025 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2030 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2032 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2033 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2035 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2042 /****************************************************************
2043 ****************************************************************/
2045 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2046 struct libnet_UnjoinCtx **r)
2048 struct libnet_UnjoinCtx *ctx;
2050 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2055 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2057 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2058 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2065 /****************************************************************
2066 ****************************************************************/
2068 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2069 struct libnet_JoinCtx *r)
2071 bool valid_security = false;
2072 bool valid_workgroup = false;
2073 bool valid_realm = false;
2075 /* check if configuration is already set correctly */
2077 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2079 switch (r->out.domain_is_ad) {
2081 valid_security = (lp_security() == SEC_DOMAIN);
2082 if (valid_workgroup && valid_security) {
2083 /* nothing to be done */
2088 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2089 switch (lp_security()) {
2092 valid_security = true;
2095 if (valid_workgroup && valid_realm && valid_security) {
2096 /* nothing to be done */
2102 /* check if we are supposed to manipulate configuration */
2104 if (!r->in.modify_config) {
2106 char *wrong_conf = talloc_strdup(mem_ctx, "");
2108 if (!valid_workgroup) {
2109 wrong_conf = talloc_asprintf_append(wrong_conf,
2110 "\"workgroup\" set to '%s', should be '%s'",
2111 lp_workgroup(), r->out.netbios_domain_name);
2112 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2116 wrong_conf = talloc_asprintf_append(wrong_conf,
2117 "\"realm\" set to '%s', should be '%s'",
2118 lp_realm(), r->out.dns_domain_name);
2119 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2122 if (!valid_security) {
2123 const char *sec = NULL;
2124 switch (lp_security()) {
2125 case SEC_USER: sec = "user"; break;
2126 case SEC_DOMAIN: sec = "domain"; break;
2127 case SEC_ADS: sec = "ads"; break;
2129 wrong_conf = talloc_asprintf_append(wrong_conf,
2130 "\"security\" set to '%s', should be %s",
2131 sec, r->out.domain_is_ad ?
2132 "either 'domain' or 'ads'" : "'domain'");
2133 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2136 libnet_join_set_error_string(mem_ctx, r,
2137 "Invalid configuration (%s) and configuration modification "
2138 "was not requested", wrong_conf);
2139 return WERR_CAN_NOT_COMPLETE;
2142 /* check if we are able to manipulate configuration */
2144 if (!lp_config_backend_is_registry()) {
2145 libnet_join_set_error_string(mem_ctx, r,
2146 "Configuration manipulation requested but not "
2147 "supported by backend");
2148 return WERR_NOT_SUPPORTED;
2154 /****************************************************************
2155 ****************************************************************/
2157 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2158 struct libnet_JoinCtx *r)
2162 struct cli_state *cli = NULL;
2164 ADS_STATUS ads_status;
2165 #endif /* HAVE_ADS */
2167 if (!r->in.dc_name) {
2168 struct netr_DsRGetDCNameInfo *info;
2170 status = dsgetdcname(mem_ctx,
2175 DS_FORCE_REDISCOVERY |
2176 DS_DIRECTORY_SERVICE_REQUIRED |
2177 DS_WRITABLE_REQUIRED |
2180 if (!NT_STATUS_IS_OK(status)) {
2181 libnet_join_set_error_string(mem_ctx, r,
2182 "failed to find DC for domain %s",
2184 get_friendly_nt_error_msg(status));
2185 return WERR_DCNOTFOUND;
2188 dc = strip_hostname(info->dc_unc);
2189 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2190 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2193 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2194 if (!NT_STATUS_IS_OK(status)) {
2195 libnet_join_set_error_string(mem_ctx, r,
2196 "failed to lookup DC info for domain '%s' over rpc: %s",
2197 r->in.domain_name, get_friendly_nt_error_msg(status));
2198 return ntstatus_to_werror(status);
2201 werr = libnet_join_check_config(mem_ctx, r);
2202 if (!W_ERROR_IS_OK(werr)) {
2208 create_local_private_krb5_conf_for_domain(
2209 r->out.dns_domain_name, r->out.netbios_domain_name,
2210 NULL, smbXcli_conn_remote_sockaddr(cli->conn));
2212 if (r->out.domain_is_ad && r->in.account_ou &&
2213 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2215 ads_status = libnet_join_connect_ads(mem_ctx, r);
2216 if (!ADS_ERR_OK(ads_status)) {
2217 return WERR_DEFAULT_JOIN_REQUIRED;
2220 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2221 if (!ADS_ERR_OK(ads_status)) {
2222 libnet_join_set_error_string(mem_ctx, r,
2223 "failed to precreate account in ou %s: %s",
2225 ads_errstr(ads_status));
2226 return WERR_DEFAULT_JOIN_REQUIRED;
2229 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2231 #endif /* HAVE_ADS */
2233 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2234 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2235 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2237 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2239 if (!NT_STATUS_IS_OK(status)) {
2240 libnet_join_set_error_string(mem_ctx, r,
2241 "failed to join domain '%s' over rpc: %s",
2242 r->in.domain_name, get_friendly_nt_error_msg(status));
2243 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2244 return WERR_SETUP_ALREADY_JOINED;
2246 werr = ntstatus_to_werror(status);
2250 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2251 werr = WERR_SETUP_NOT_JOINED;
2265 /****************************************************************
2266 ****************************************************************/
2268 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2269 struct libnet_JoinCtx *r)
2272 struct libnet_UnjoinCtx *u = NULL;
2274 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2275 if (!W_ERROR_IS_OK(werr)) {
2279 u->in.debug = r->in.debug;
2280 u->in.dc_name = r->in.dc_name;
2281 u->in.domain_name = r->in.domain_name;
2282 u->in.admin_account = r->in.admin_account;
2283 u->in.admin_password = r->in.admin_password;
2284 u->in.modify_config = r->in.modify_config;
2285 u->in.use_kerberos = r->in.use_kerberos;
2286 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2287 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2289 werr = libnet_Unjoin(mem_ctx, u);
2295 /****************************************************************
2296 ****************************************************************/
2298 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2299 struct libnet_JoinCtx *r)
2304 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2307 ZERO_STRUCT(r->out);
2309 werr = libnet_join_pre_processing(mem_ctx, r);
2310 if (!W_ERROR_IS_OK(werr)) {
2314 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2315 werr = libnet_DomainJoin(mem_ctx, r);
2316 if (!W_ERROR_IS_OK(werr)) {
2321 werr = libnet_join_post_processing(mem_ctx, r);
2322 if (!W_ERROR_IS_OK(werr)) {
2326 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2327 werr = libnet_join_post_verify(mem_ctx, r);
2328 if (!W_ERROR_IS_OK(werr)) {
2329 libnet_join_rollback(mem_ctx, r);
2334 r->out.result = werr;
2337 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2342 /****************************************************************
2343 ****************************************************************/
2345 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2346 struct libnet_UnjoinCtx *r)
2350 if (!r->in.domain_sid) {
2352 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2353 libnet_unjoin_set_error_string(mem_ctx, r,
2354 "Unable to fetch domain sid: are we joined?");
2355 return WERR_SETUP_NOT_JOINED;
2357 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2358 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2361 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2362 !r->in.delete_machine_account) {
2363 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2367 if (!r->in.dc_name) {
2368 struct netr_DsRGetDCNameInfo *info;
2370 status = dsgetdcname(mem_ctx,
2375 DS_DIRECTORY_SERVICE_REQUIRED |
2376 DS_WRITABLE_REQUIRED |
2379 if (!NT_STATUS_IS_OK(status)) {
2380 libnet_unjoin_set_error_string(mem_ctx, r,
2381 "failed to find DC for domain %s",
2383 get_friendly_nt_error_msg(status));
2384 return WERR_DCNOTFOUND;
2387 dc = strip_hostname(info->dc_unc);
2388 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2389 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2393 /* for net ads leave, try to delete the account. If it works,
2394 no sense in disabling. If it fails, we can still try to
2397 if (r->in.delete_machine_account) {
2398 ADS_STATUS ads_status;
2399 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2400 if (ADS_ERR_OK(ads_status)) {
2402 r->out.dns_domain_name =
2403 talloc_strdup(mem_ctx,
2404 r->in.ads->server.realm);
2406 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2408 if (!ADS_ERR_OK(ads_status)) {
2409 libnet_unjoin_set_error_string(mem_ctx, r,
2410 "failed to remove machine account from AD: %s",
2411 ads_errstr(ads_status));
2413 r->out.deleted_machine_account = true;
2414 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2415 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2419 #endif /* HAVE_ADS */
2421 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2423 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2424 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2425 if (!NT_STATUS_IS_OK(status)) {
2426 libnet_unjoin_set_error_string(mem_ctx, r,
2427 "failed to disable machine account via rpc: %s",
2428 get_friendly_nt_error_msg(status));
2429 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2430 return WERR_SETUP_NOT_JOINED;
2432 return ntstatus_to_werror(status);
2435 r->out.disabled_machine_account = true;
2438 /* If disable succeeded or was not requested at all, we
2439 should be getting rid of our end of things */
2441 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2446 /****************************************************************
2447 ****************************************************************/
2449 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2450 struct libnet_UnjoinCtx *r)
2452 if (!r->in.domain_name) {
2453 libnet_unjoin_set_error_string(mem_ctx, r,
2454 "No domain name defined");
2455 return WERR_INVALID_PARAM;
2458 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2461 libnet_unjoin_set_error_string(mem_ctx, r,
2462 "Failed to parse domain name");
2463 return WERR_INVALID_PARAM;
2467 return WERR_SETUP_DOMAIN_CONTROLLER;
2470 if (!r->in.admin_domain) {
2471 char *admin_domain = NULL;
2472 char *admin_account = NULL;
2473 split_domain_user(mem_ctx,
2474 r->in.admin_account,
2477 r->in.admin_domain = admin_domain;
2478 r->in.admin_account = admin_account;
2481 if (!secrets_init()) {
2482 libnet_unjoin_set_error_string(mem_ctx, r,
2483 "Unable to open secrets database");
2484 return WERR_CAN_NOT_COMPLETE;
2490 /****************************************************************
2491 ****************************************************************/
2493 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2494 struct libnet_UnjoinCtx *r)
2496 saf_delete(r->out.netbios_domain_name);
2497 saf_delete(r->out.dns_domain_name);
2499 return libnet_unjoin_config(r);
2502 /****************************************************************
2503 ****************************************************************/
2505 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2506 struct libnet_UnjoinCtx *r)
2511 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2514 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2515 if (!W_ERROR_IS_OK(werr)) {
2519 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2520 werr = libnet_DomainUnjoin(mem_ctx, r);
2521 if (!W_ERROR_IS_OK(werr)) {
2522 libnet_unjoin_config(r);
2527 werr = libnet_unjoin_post_processing(mem_ctx, r);
2528 if (!W_ERROR_IS_OK(werr)) {
2533 r->out.result = werr;
2536 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);