2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Gerald (Jerry) Carter 2006
5 * Copyright (C) Guenther Deschner 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "librpc/gen_ndr/ndr_libnet_join.h"
24 #include "libnet/libnet_join.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_lsa_c.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/ndr_netlogon.h"
31 #include "rpc_client/cli_netlogon.h"
32 #include "lib/smbconf/smbconf.h"
33 #include "lib/smbconf/smbconf_reg.h"
34 #include "../libds/common/flags.h"
36 #include "rpc_client/init_lsa.h"
37 #include "rpc_client/cli_pipe.h"
38 #include "../libcli/security/security.h"
40 #include "libsmb/libsmb.h"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/param/loadparm.h"
43 #include "libcli/auth/netlogon_creds_cli.h"
44 #include "auth/credentials/credentials.h"
46 /****************************************************************
47 ****************************************************************/
49 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
52 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
53 DEBUG(1,("libnet_Join:\n%s", str)); \
57 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
58 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
59 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
60 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
62 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
65 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
66 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
70 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
71 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
72 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
73 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
75 /****************************************************************
76 ****************************************************************/
78 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
79 struct libnet_JoinCtx *r,
80 const char *format, ...)
84 if (r->out.error_string) {
88 va_start(args, format);
89 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
93 /****************************************************************
94 ****************************************************************/
96 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
97 struct libnet_UnjoinCtx *r,
98 const char *format, ...)
102 if (r->out.error_string) {
106 va_start(args, format);
107 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
113 /****************************************************************
114 ****************************************************************/
116 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
117 const char *netbios_domain_name,
119 const char *user_name,
120 const char *password,
124 ADS_STRUCT *my_ads = NULL;
127 my_ads = ads_init(dns_domain_name,
131 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
135 SAFE_FREE(my_ads->auth.user_name);
136 my_ads->auth.user_name = SMB_STRDUP(user_name);
137 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
139 SAFE_FREE(my_ads->auth.realm);
140 my_ads->auth.realm = smb_xstrdup(cp);
141 if (!strupper_m(my_ads->auth.realm)) {
142 ads_destroy(&my_ads);
143 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
149 SAFE_FREE(my_ads->auth.password);
150 my_ads->auth.password = SMB_STRDUP(password);
153 status = ads_connect_user_creds(my_ads);
154 if (!ADS_ERR_OK(status)) {
155 ads_destroy(&my_ads);
163 /****************************************************************
164 ****************************************************************/
166 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
167 struct libnet_JoinCtx *r)
171 status = libnet_connect_ads(r->out.dns_domain_name,
172 r->out.netbios_domain_name,
175 r->in.admin_password,
177 if (!ADS_ERR_OK(status)) {
178 libnet_join_set_error_string(mem_ctx, r,
179 "failed to connect to AD: %s",
184 if (!r->out.netbios_domain_name) {
185 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
186 r->in.ads->server.workgroup);
187 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
190 if (!r->out.dns_domain_name) {
191 r->out.dns_domain_name = talloc_strdup(mem_ctx,
192 r->in.ads->config.realm);
193 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
196 r->out.domain_is_ad = true;
201 /****************************************************************
202 ****************************************************************/
204 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
205 struct libnet_UnjoinCtx *r)
209 status = libnet_connect_ads(r->in.domain_name,
213 r->in.admin_password,
215 if (!ADS_ERR_OK(status)) {
216 libnet_unjoin_set_error_string(mem_ctx, r,
217 "failed to connect to AD: %s",
224 /****************************************************************
225 join a domain using ADS (LDAP mods)
226 ****************************************************************/
228 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
229 struct libnet_JoinCtx *r)
232 LDAPMessage *res = NULL;
233 const char *attrs[] = { "dn", NULL };
236 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
237 if (!ADS_ERR_OK(status)) {
241 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
242 if (!ADS_ERR_OK(status)) {
246 if (ads_count_replies(r->in.ads, res) != 1) {
247 ads_msgfree(r->in.ads, res);
248 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
251 ads_msgfree(r->in.ads, res);
253 /* Attempt to create the machine account and bail if this fails.
254 Assume that the admin wants exactly what they requested */
256 status = ads_create_machine_acct(r->in.ads,
260 if (ADS_ERR_OK(status)) {
261 DEBUG(1,("machine account creation created\n"));
263 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
264 (status.err.rc == LDAP_ALREADY_EXISTS)) {
265 status = ADS_SUCCESS;
268 if (!ADS_ERR_OK(status)) {
269 DEBUG(1,("machine account creation failed\n"));
273 status = ads_move_machine_acct(r->in.ads,
277 if (!ADS_ERR_OK(status)) {
278 DEBUG(1,("failure to locate/move pre-existing "
279 "machine account\n"));
283 DEBUG(1,("The machine account %s the specified OU.\n",
284 moved ? "was moved into" : "already exists in"));
289 /****************************************************************
290 ****************************************************************/
292 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
293 struct libnet_UnjoinCtx *r)
298 status = libnet_unjoin_connect_ads(mem_ctx, r);
299 if (!ADS_ERR_OK(status)) {
300 libnet_unjoin_set_error_string(mem_ctx, r,
301 "failed to connect to AD: %s",
307 status = ads_leave_realm(r->in.ads, r->in.machine_name);
308 if (!ADS_ERR_OK(status)) {
309 libnet_unjoin_set_error_string(mem_ctx, r,
310 "failed to leave realm: %s",
318 /****************************************************************
319 ****************************************************************/
321 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
322 struct libnet_JoinCtx *r)
325 LDAPMessage *res = NULL;
328 if (!r->in.machine_name) {
329 return ADS_ERROR(LDAP_NO_MEMORY);
332 status = ads_find_machine_acct(r->in.ads,
335 if (!ADS_ERR_OK(status)) {
339 if (ads_count_replies(r->in.ads, res) != 1) {
340 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
344 dn = ads_get_dn(r->in.ads, mem_ctx, res);
346 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
350 r->out.dn = talloc_strdup(mem_ctx, dn);
352 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
357 ads_msgfree(r->in.ads, res);
363 static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
364 struct libnet_JoinCtx *r,
370 if (r->in.machine_name == NULL) {
371 return ADS_ERROR_SYSTEM(EINVAL);
374 status = ads_get_service_principal_names(mem_ctx,
383 /****************************************************************
384 Set a machines dNSHostName and servicePrincipalName attributes
385 ****************************************************************/
387 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
388 struct libnet_JoinCtx *r)
393 const char **spn_array = NULL;
400 status = libnet_join_find_machine_acct(mem_ctx, r);
401 if (!ADS_ERR_OK(status)) {
405 status = libnet_join_get_machine_spns(mem_ctx,
407 discard_const_p(char **, &spn_array),
409 if (!ADS_ERR_OK(status)) {
410 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
413 /* Windows only creates HOST/shortname & HOST/fqdn. */
415 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
417 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
419 if (!strupper_m(spn)) {
420 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
423 ok = ads_element_in_array(spn_array, num_spns, spn);
425 ok = add_string_to_array(spn_array, spn,
426 &spn_array, &num_spns);
428 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
432 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
433 || (strchr(my_fqdn, '.') == NULL)) {
434 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
435 r->out.dns_domain_name);
438 if (!strlower_m(my_fqdn)) {
439 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
442 if (!strequal(my_fqdn, r->in.machine_name)) {
443 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
445 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
448 ok = ads_element_in_array(spn_array, num_spns, spn);
450 ok = add_string_to_array(spn_array, spn,
451 &spn_array, &num_spns);
453 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
458 /* make sure to NULL terminate the array */
459 spn_array = talloc_realloc(mem_ctx, spn_array, const char *, num_spns + 1);
460 if (spn_array == NULL) {
461 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
463 spn_array[num_spns] = NULL;
465 mods = ads_init_mods(mem_ctx);
467 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
470 /* fields of primary importance */
472 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
473 if (!ADS_ERR_OK(status)) {
474 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
477 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
479 if (!ADS_ERR_OK(status)) {
480 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
483 return ads_gen_mod(r->in.ads, r->out.dn, mods);
486 /****************************************************************
487 ****************************************************************/
489 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
490 struct libnet_JoinCtx *r)
495 if (!r->in.create_upn) {
501 status = libnet_join_find_machine_acct(mem_ctx, r);
502 if (!ADS_ERR_OK(status)) {
507 const char *realm = r->out.dns_domain_name;
509 /* in case we are about to generate a keytab during the join
510 * make sure the default upn we create is usable with kinit -k.
513 if (USE_KERBEROS_KEYTAB) {
514 realm = talloc_strdup_upper(mem_ctx,
515 r->out.dns_domain_name);
519 return ADS_ERROR(LDAP_NO_MEMORY);
522 r->in.upn = talloc_asprintf(mem_ctx,
527 return ADS_ERROR(LDAP_NO_MEMORY);
531 /* now do the mods */
533 mods = ads_init_mods(mem_ctx);
535 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
538 /* fields of primary importance */
540 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
541 if (!ADS_ERR_OK(status)) {
542 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
545 return ads_gen_mod(r->in.ads, r->out.dn, mods);
549 /****************************************************************
550 ****************************************************************/
552 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
553 struct libnet_JoinCtx *r)
559 if (!r->in.os_name || !r->in.os_version ) {
565 status = libnet_join_find_machine_acct(mem_ctx, r);
566 if (!ADS_ERR_OK(status)) {
570 /* now do the mods */
572 mods = ads_init_mods(mem_ctx);
574 return ADS_ERROR(LDAP_NO_MEMORY);
577 if (r->in.os_servicepack) {
579 * if blank string then leave os_sp equal to NULL to force
580 * attribute delete (LDAP_MOD_DELETE)
582 if (!strequal(r->in.os_servicepack,"")) {
583 os_sp = talloc_strdup(mem_ctx, r->in.os_servicepack);
586 os_sp = talloc_asprintf(mem_ctx, "Samba %s",
587 samba_version_string());
589 if (!os_sp && !strequal(r->in.os_servicepack,"")) {
590 return ADS_ERROR(LDAP_NO_MEMORY);
593 /* fields of primary importance */
595 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
597 if (!ADS_ERR_OK(status)) {
601 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
603 if (!ADS_ERR_OK(status)) {
607 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
609 if (!ADS_ERR_OK(status)) {
613 return ads_gen_mod(r->in.ads, r->out.dn, mods);
616 /****************************************************************
617 ****************************************************************/
619 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
620 struct libnet_JoinCtx *r)
622 if (!USE_SYSTEM_KEYTAB) {
626 if (ads_keytab_create_default(r->in.ads) != 0) {
633 /****************************************************************
634 ****************************************************************/
636 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
637 struct libnet_JoinCtx *r)
639 uint32_t domain_func;
641 const char *salt = NULL;
642 char *std_salt = NULL;
644 status = ads_domain_func_level(r->in.ads, &domain_func);
645 if (!ADS_ERR_OK(status)) {
646 libnet_join_set_error_string(mem_ctx, r,
647 "failed to determine domain functional level: %s",
652 /* go ahead and setup the default salt */
654 std_salt = kerberos_standard_des_salt();
656 libnet_join_set_error_string(mem_ctx, r,
657 "failed to obtain standard DES salt");
661 salt = talloc_strdup(mem_ctx, std_salt);
668 /* if it's a Windows functional domain, we have to look for the UPN */
670 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
673 upn = ads_get_upn(r->in.ads, mem_ctx,
676 salt = talloc_strdup(mem_ctx, upn);
683 return kerberos_secrets_store_des_salt(salt);
686 /****************************************************************
687 ****************************************************************/
689 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
690 struct libnet_JoinCtx *r)
695 status = libnet_join_connect_ads(mem_ctx, r);
696 if (!ADS_ERR_OK(status)) {
701 status = libnet_join_set_machine_spn(mem_ctx, r);
702 if (!ADS_ERR_OK(status)) {
703 libnet_join_set_error_string(mem_ctx, r,
704 "Failed to set machine spn: %s\n"
705 "Do you have sufficient permissions to create machine "
711 status = libnet_join_set_os_attributes(mem_ctx, r);
712 if (!ADS_ERR_OK(status)) {
713 libnet_join_set_error_string(mem_ctx, r,
714 "failed to set machine os attributes: %s",
719 status = libnet_join_set_machine_upn(mem_ctx, r);
720 if (!ADS_ERR_OK(status)) {
721 libnet_join_set_error_string(mem_ctx, r,
722 "failed to set machine upn: %s",
727 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
728 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
731 if (!libnet_join_create_keytab(mem_ctx, r)) {
732 libnet_join_set_error_string(mem_ctx, r,
733 "failed to create kerberos keytab");
734 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
739 #endif /* HAVE_ADS */
741 /****************************************************************
742 Store the machine password and domain SID
743 ****************************************************************/
745 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
746 struct libnet_JoinCtx *r)
748 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
751 DEBUG(1,("Failed to save domain sid\n"));
755 if (!secrets_store_machine_password(r->in.machine_password,
756 r->out.netbios_domain_name,
757 r->in.secure_channel_type))
759 DEBUG(1,("Failed to save machine password\n"));
766 /****************************************************************
767 Connect dc's IPC$ share
768 ****************************************************************/
770 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
775 struct cli_state **cli)
780 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
783 if (use_kerberos && pass) {
784 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
787 return cli_full_connection(cli, NULL,
795 SMB_SIGNING_DEFAULT);
798 /****************************************************************
799 Lookup domain dc's info
800 ****************************************************************/
802 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
803 struct libnet_JoinCtx *r,
804 struct cli_state **cli)
806 struct rpc_pipe_client *pipe_hnd = NULL;
807 struct policy_handle lsa_pol;
808 NTSTATUS status, result;
809 union lsa_PolicyInformation *info = NULL;
810 struct dcerpc_binding_handle *b;
812 status = libnet_join_connect_dc_ipc(r->in.dc_name,
815 r->in.admin_password,
818 if (!NT_STATUS_IS_OK(status)) {
822 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
824 if (!NT_STATUS_IS_OK(status)) {
825 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
830 b = pipe_hnd->binding_handle;
832 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
833 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
834 if (!NT_STATUS_IS_OK(status)) {
838 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
843 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
844 r->out.domain_is_ad = true;
845 r->out.netbios_domain_name = info->dns.name.string;
846 r->out.dns_domain_name = info->dns.dns_domain.string;
847 r->out.forest_name = info->dns.dns_forest.string;
848 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
849 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
852 if (!NT_STATUS_IS_OK(status)) {
853 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
855 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
858 if (!NT_STATUS_IS_OK(status)) {
861 if (!NT_STATUS_IS_OK(result)) {
866 r->out.netbios_domain_name = info->account_domain.name.string;
867 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
868 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
871 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
872 TALLOC_FREE(pipe_hnd);
878 /****************************************************************
879 Do the domain join unsecure
880 ****************************************************************/
882 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
883 struct libnet_JoinCtx *r,
884 struct cli_state *cli)
886 TALLOC_CTX *frame = talloc_stackframe();
887 struct rpc_pipe_client *netlogon_pipe = NULL;
888 struct netlogon_creds_cli_context *netlogon_creds = NULL;
889 struct samr_Password current_nt_hash;
890 const char *account_name = NULL;
893 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
895 if (!NT_STATUS_IS_OK(status)) {
900 if (!r->in.machine_password) {
901 r->in.machine_password = generate_random_password(mem_ctx,
902 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
903 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
904 if (r->in.machine_password == NULL) {
906 return NT_STATUS_NO_MEMORY;
910 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
911 E_md4hash(r->in.admin_password, current_nt_hash.hash);
913 account_name = talloc_asprintf(frame, "%s$",
915 if (account_name == NULL) {
917 return NT_STATUS_NO_MEMORY;
920 status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
923 r->in.secure_channel_type,
927 if (!NT_STATUS_IS_OK(status)) {
932 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
934 true, /* force_reauth */
936 NULL); /* previous_nt_hash */
937 if (!NT_STATUS_IS_OK(status)) {
942 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
943 netlogon_pipe->binding_handle,
944 r->in.machine_password,
945 NULL); /* new_version */
946 if (!NT_STATUS_IS_OK(status)) {
955 /****************************************************************
957 ****************************************************************/
959 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
960 struct libnet_JoinCtx *r,
961 struct cli_state *cli)
963 struct rpc_pipe_client *pipe_hnd = NULL;
964 struct policy_handle sam_pol, domain_pol, user_pol;
965 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
967 struct lsa_String lsa_acct_name;
969 uint32_t acct_flags = ACB_WSTRUST;
970 struct samr_Ids user_rids;
971 struct samr_Ids name_types;
972 union samr_UserInfo user_info;
973 struct dcerpc_binding_handle *b = NULL;
974 unsigned int old_timeout = 0;
976 DATA_BLOB session_key = data_blob_null;
977 struct samr_CryptPassword crypt_pwd;
978 struct samr_CryptPasswordEx crypt_pwd_ex;
980 ZERO_STRUCT(sam_pol);
981 ZERO_STRUCT(domain_pol);
982 ZERO_STRUCT(user_pol);
984 switch (r->in.secure_channel_type) {
986 acct_flags = ACB_WSTRUST;
989 acct_flags = ACB_SVRTRUST;
992 return NT_STATUS_INVALID_PARAMETER;
995 if (!r->in.machine_password) {
996 r->in.machine_password = generate_random_password(mem_ctx,
997 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
998 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
999 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
1002 /* Open the domain */
1004 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1006 if (!NT_STATUS_IS_OK(status)) {
1007 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1008 nt_errstr(status)));
1012 b = pipe_hnd->binding_handle;
1014 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1017 nt_errstr(status)));
1021 status = dcerpc_samr_Connect2(b, mem_ctx,
1023 SAMR_ACCESS_ENUM_DOMAINS
1024 | SAMR_ACCESS_LOOKUP_DOMAIN,
1027 if (!NT_STATUS_IS_OK(status)) {
1030 if (!NT_STATUS_IS_OK(result)) {
1035 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1037 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1038 | SAMR_DOMAIN_ACCESS_CREATE_USER
1039 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1043 if (!NT_STATUS_IS_OK(status)) {
1046 if (!NT_STATUS_IS_OK(result)) {
1051 /* Create domain user */
1053 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1054 if (!strlower_m(acct_name)) {
1055 status = NT_STATUS_INVALID_PARAMETER;
1059 init_lsa_String(&lsa_acct_name, acct_name);
1061 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1062 uint32_t access_desired =
1063 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1064 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1065 SAMR_USER_ACCESS_SET_PASSWORD |
1066 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1067 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1068 uint32_t access_granted = 0;
1070 DEBUG(10,("Creating account with desired access mask: %d\n",
1073 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1082 if (!NT_STATUS_IS_OK(status)) {
1087 if (!NT_STATUS_IS_OK(status) &&
1088 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1090 DEBUG(10,("Creation of workstation account failed: %s\n",
1091 nt_errstr(status)));
1093 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1094 username/password combo but the user does not have
1095 administrator access. */
1097 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1098 libnet_join_set_error_string(mem_ctx, r,
1099 "User specified does not have "
1100 "administrator privileges");
1106 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1107 if (!(r->in.join_flags &
1108 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1113 /* We *must* do this.... don't ask... */
1115 if (NT_STATUS_IS_OK(status)) {
1116 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1120 status = dcerpc_samr_LookupNames(b, mem_ctx,
1127 if (!NT_STATUS_IS_OK(status)) {
1130 if (!NT_STATUS_IS_OK(result)) {
1134 if (user_rids.count != 1) {
1135 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1138 if (name_types.count != 1) {
1139 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1143 if (name_types.ids[0] != SID_NAME_USER) {
1144 DEBUG(0,("%s is not a user account (type=%d)\n",
1145 acct_name, name_types.ids[0]));
1146 status = NT_STATUS_INVALID_WORKSTATION;
1150 user_rid = user_rids.ids[0];
1152 /* Open handle on user */
1154 status = dcerpc_samr_OpenUser(b, mem_ctx,
1156 SEC_FLAG_MAXIMUM_ALLOWED,
1160 if (!NT_STATUS_IS_OK(status)) {
1163 if (!NT_STATUS_IS_OK(result)) {
1168 /* Fill in the additional account flags now */
1170 acct_flags |= ACB_PWNOEXP;
1172 /* Set account flags on machine account */
1173 ZERO_STRUCT(user_info.info16);
1174 user_info.info16.acct_flags = acct_flags;
1176 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1181 if (!NT_STATUS_IS_OK(status)) {
1182 dcerpc_samr_DeleteUser(b, mem_ctx,
1186 libnet_join_set_error_string(mem_ctx, r,
1187 "Failed to set account flags for machine account (%s)\n",
1192 if (!NT_STATUS_IS_OK(result)) {
1195 dcerpc_samr_DeleteUser(b, mem_ctx,
1199 libnet_join_set_error_string(mem_ctx, r,
1200 "Failed to set account flags for machine account (%s)\n",
1205 /* Set password on machine account - first try level 26 */
1208 * increase the timeout as password filter modules on the DC
1209 * might delay the operation for a significant amount of time
1211 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1213 init_samr_CryptPasswordEx(r->in.machine_password,
1217 user_info.info26.password = crypt_pwd_ex;
1218 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1220 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1226 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1228 /* retry with level 24 */
1230 init_samr_CryptPassword(r->in.machine_password,
1234 user_info.info24.password = crypt_pwd;
1235 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1237 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1244 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1246 if (!NT_STATUS_IS_OK(status)) {
1248 dcerpc_samr_DeleteUser(b, mem_ctx,
1252 libnet_join_set_error_string(mem_ctx, r,
1253 "Failed to set password for machine account (%s)\n",
1257 if (!NT_STATUS_IS_OK(result)) {
1260 dcerpc_samr_DeleteUser(b, mem_ctx,
1264 libnet_join_set_error_string(mem_ctx, r,
1265 "Failed to set password for machine account (%s)\n",
1270 status = NT_STATUS_OK;
1277 data_blob_clear_free(&session_key);
1279 if (is_valid_policy_hnd(&sam_pol)) {
1280 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1282 if (is_valid_policy_hnd(&domain_pol)) {
1283 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1285 if (is_valid_policy_hnd(&user_pol)) {
1286 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1288 TALLOC_FREE(pipe_hnd);
1293 /****************************************************************
1294 ****************************************************************/
1296 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1297 const char *netbios_domain_name,
1298 const char *dc_name,
1299 const bool use_kerberos)
1301 TALLOC_CTX *frame = talloc_stackframe();
1302 struct cli_state *cli = NULL;
1303 struct rpc_pipe_client *netlogon_pipe = NULL;
1304 struct cli_credentials *cli_creds = NULL;
1305 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1306 struct netlogon_creds_CredentialState *creds = NULL;
1307 uint32_t netlogon_flags = 0;
1309 const char *machine_account = NULL;
1310 const char *machine_domain = NULL;
1311 const char *machine_password = NULL;
1316 return NT_STATUS_INVALID_PARAMETER;
1319 if (!secrets_init()) {
1321 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1324 status = pdb_get_trust_credentials(netbios_domain_name, NULL,
1326 if (!NT_STATUS_IS_OK(status)) {
1331 /* we don't want any old password */
1332 cli_credentials_set_old_password(cli_creds, NULL, CRED_SPECIFIED);
1335 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1338 machine_account = cli_credentials_get_username(cli_creds);
1339 machine_domain = cli_credentials_get_domain(cli_creds);
1340 machine_password = cli_credentials_get_password(cli_creds);
1342 status = cli_full_connection(&cli, NULL,
1350 SMB_SIGNING_DEFAULT);
1352 if (!NT_STATUS_IS_OK(status)) {
1353 status = cli_full_connection(&cli, NULL,
1361 SMB_SIGNING_DEFAULT);
1364 if (!NT_STATUS_IS_OK(status)) {
1369 status = rpccli_create_netlogon_creds_with_creds(cli_creds,
1374 if (!NT_STATUS_IS_OK(status)) {
1380 status = rpccli_setup_netlogon_creds_with_creds(cli, NCACN_NP,
1382 true, /* force_reauth */
1384 if (!NT_STATUS_IS_OK(status)) {
1385 DEBUG(0,("connect_to_domain_password_server: "
1386 "unable to open the domain client session to "
1387 "machine %s. Flags[0x%08X] Error was : %s.\n",
1388 dc_name, (unsigned)netlogon_flags,
1389 nt_errstr(status)));
1395 status = netlogon_creds_cli_get(netlogon_creds,
1398 if (!NT_STATUS_IS_OK(status)) {
1403 netlogon_flags = creds->negotiate_flags;
1406 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1409 return NT_STATUS_OK;
1412 status = cli_rpc_pipe_open_schannel_with_creds(
1413 cli, &ndr_table_netlogon, NCACN_NP,
1415 netlogon_creds, &netlogon_pipe);
1417 TALLOC_FREE(netlogon_pipe);
1419 if (!NT_STATUS_IS_OK(status)) {
1420 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1421 "on netlogon pipe to server %s for domain %s. "
1423 smbXcli_conn_remote_name(cli->conn),
1424 netbios_domain_name, nt_errstr(status)));
1432 return NT_STATUS_OK;
1435 /****************************************************************
1436 ****************************************************************/
1438 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1439 struct libnet_JoinCtx *r)
1443 status = libnet_join_ok(r->in.msg_ctx,
1444 r->out.netbios_domain_name,
1446 r->in.use_kerberos);
1447 if (!NT_STATUS_IS_OK(status)) {
1448 libnet_join_set_error_string(mem_ctx, r,
1449 "failed to verify domain membership after joining: %s",
1450 get_friendly_nt_error_msg(status));
1451 return WERR_SETUP_NOT_JOINED;
1457 /****************************************************************
1458 ****************************************************************/
1460 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1461 struct libnet_UnjoinCtx *r)
1463 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1467 if (!secrets_delete_domain_sid(lp_workgroup())) {
1474 /****************************************************************
1475 ****************************************************************/
1477 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1478 struct libnet_UnjoinCtx *r)
1480 struct cli_state *cli = NULL;
1481 struct rpc_pipe_client *pipe_hnd = NULL;
1482 struct policy_handle sam_pol, domain_pol, user_pol;
1483 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1486 struct lsa_String lsa_acct_name;
1487 struct samr_Ids user_rids;
1488 struct samr_Ids name_types;
1489 union samr_UserInfo *info = NULL;
1490 struct dcerpc_binding_handle *b = NULL;
1492 ZERO_STRUCT(sam_pol);
1493 ZERO_STRUCT(domain_pol);
1494 ZERO_STRUCT(user_pol);
1496 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1497 r->in.admin_account,
1499 r->in.admin_password,
1502 if (!NT_STATUS_IS_OK(status)) {
1506 /* Open the domain */
1508 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1510 if (!NT_STATUS_IS_OK(status)) {
1511 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1512 nt_errstr(status)));
1516 b = pipe_hnd->binding_handle;
1518 status = dcerpc_samr_Connect2(b, mem_ctx,
1520 SEC_FLAG_MAXIMUM_ALLOWED,
1523 if (!NT_STATUS_IS_OK(status)) {
1526 if (!NT_STATUS_IS_OK(result)) {
1531 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1533 SEC_FLAG_MAXIMUM_ALLOWED,
1537 if (!NT_STATUS_IS_OK(status)) {
1540 if (!NT_STATUS_IS_OK(result)) {
1545 /* Create domain user */
1547 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1548 if (!strlower_m(acct_name)) {
1549 status = NT_STATUS_INVALID_PARAMETER;
1553 init_lsa_String(&lsa_acct_name, acct_name);
1555 status = dcerpc_samr_LookupNames(b, mem_ctx,
1563 if (!NT_STATUS_IS_OK(status)) {
1566 if (!NT_STATUS_IS_OK(result)) {
1570 if (user_rids.count != 1) {
1571 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1574 if (name_types.count != 1) {
1575 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1579 if (name_types.ids[0] != SID_NAME_USER) {
1580 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1581 name_types.ids[0]));
1582 status = NT_STATUS_INVALID_WORKSTATION;
1586 user_rid = user_rids.ids[0];
1588 /* Open handle on user */
1590 status = dcerpc_samr_OpenUser(b, mem_ctx,
1592 SEC_FLAG_MAXIMUM_ALLOWED,
1596 if (!NT_STATUS_IS_OK(status)) {
1599 if (!NT_STATUS_IS_OK(result)) {
1606 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1611 if (!NT_STATUS_IS_OK(status)) {
1612 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1615 if (!NT_STATUS_IS_OK(result)) {
1617 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1621 /* now disable and setuser info */
1623 info->info16.acct_flags |= ACB_DISABLED;
1625 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1630 if (!NT_STATUS_IS_OK(status)) {
1631 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1634 if (!NT_STATUS_IS_OK(result)) {
1636 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1640 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1643 if (pipe_hnd && b) {
1644 if (is_valid_policy_hnd(&domain_pol)) {
1645 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1647 if (is_valid_policy_hnd(&sam_pol)) {
1648 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1650 TALLOC_FREE(pipe_hnd);
1660 /****************************************************************
1661 ****************************************************************/
1663 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1665 WERROR werr = WERR_OK;
1667 struct smbconf_ctx *ctx;
1669 err = smbconf_init_reg(r, &ctx, NULL);
1670 if (!SBC_ERROR_IS_OK(err)) {
1671 werr = WERR_NO_SUCH_SERVICE;
1675 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1677 err = smbconf_set_global_parameter(ctx, "security", "user");
1678 if (!SBC_ERROR_IS_OK(err)) {
1679 werr = WERR_NO_SUCH_SERVICE;
1683 err = smbconf_set_global_parameter(ctx, "workgroup",
1685 if (!SBC_ERROR_IS_OK(err)) {
1686 werr = WERR_NO_SUCH_SERVICE;
1690 smbconf_delete_global_parameter(ctx, "realm");
1694 err = smbconf_set_global_parameter(ctx, "security", "domain");
1695 if (!SBC_ERROR_IS_OK(err)) {
1696 werr = WERR_NO_SUCH_SERVICE;
1700 err = smbconf_set_global_parameter(ctx, "workgroup",
1701 r->out.netbios_domain_name);
1702 if (!SBC_ERROR_IS_OK(err)) {
1703 werr = WERR_NO_SUCH_SERVICE;
1707 if (r->out.domain_is_ad) {
1708 err = smbconf_set_global_parameter(ctx, "security", "ads");
1709 if (!SBC_ERROR_IS_OK(err)) {
1710 werr = WERR_NO_SUCH_SERVICE;
1714 err = smbconf_set_global_parameter(ctx, "realm",
1715 r->out.dns_domain_name);
1716 if (!SBC_ERROR_IS_OK(err)) {
1717 werr = WERR_NO_SUCH_SERVICE;
1723 smbconf_shutdown(ctx);
1727 /****************************************************************
1728 ****************************************************************/
1730 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1732 WERROR werr = WERR_OK;
1734 struct smbconf_ctx *ctx;
1736 err = smbconf_init_reg(r, &ctx, NULL);
1737 if (!SBC_ERROR_IS_OK(err)) {
1738 werr = WERR_NO_SUCH_SERVICE;
1742 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1744 err = smbconf_set_global_parameter(ctx, "security", "user");
1745 if (!SBC_ERROR_IS_OK(err)) {
1746 werr = WERR_NO_SUCH_SERVICE;
1750 err = smbconf_delete_global_parameter(ctx, "workgroup");
1751 if (!SBC_ERROR_IS_OK(err)) {
1752 werr = WERR_NO_SUCH_SERVICE;
1756 smbconf_delete_global_parameter(ctx, "realm");
1760 smbconf_shutdown(ctx);
1764 /****************************************************************
1765 ****************************************************************/
1767 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1771 if (!W_ERROR_IS_OK(r->out.result)) {
1772 return r->out.result;
1775 if (!r->in.modify_config) {
1779 werr = do_join_modify_vals_config(r);
1780 if (!W_ERROR_IS_OK(werr)) {
1784 lp_load_global(get_dyn_CONFIGFILE());
1786 r->out.modified_config = true;
1787 r->out.result = werr;
1792 /****************************************************************
1793 ****************************************************************/
1795 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1799 if (!W_ERROR_IS_OK(r->out.result)) {
1800 return r->out.result;
1803 if (!r->in.modify_config) {
1807 werr = do_unjoin_modify_vals_config(r);
1808 if (!W_ERROR_IS_OK(werr)) {
1812 lp_load_global(get_dyn_CONFIGFILE());
1814 r->out.modified_config = true;
1815 r->out.result = werr;
1820 /****************************************************************
1821 ****************************************************************/
1823 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1824 const char *domain_str,
1825 const char **domain_p,
1828 char *domain = NULL;
1830 const char *p = NULL;
1832 if (!domain_str || !domain_p || !dc_p) {
1836 p = strchr_m(domain_str, '\\');
1839 domain = talloc_strndup(mem_ctx, domain_str,
1840 PTR_DIFF(p, domain_str));
1841 dc = talloc_strdup(mem_ctx, p+1);
1846 domain = talloc_strdup(mem_ctx, domain_str);
1862 /****************************************************************
1863 ****************************************************************/
1865 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1866 struct libnet_JoinCtx *r)
1868 if (!r->in.domain_name) {
1869 libnet_join_set_error_string(mem_ctx, r,
1870 "No domain name defined");
1871 return WERR_INVALID_PARAM;
1874 if (strlen(r->in.machine_name) > 15) {
1875 libnet_join_set_error_string(mem_ctx, r,
1876 "Our netbios name can be at most 15 chars long, "
1877 "\"%s\" is %u chars long\n",
1879 (unsigned int)strlen(r->in.machine_name));
1880 return WERR_INVALID_PARAM;
1883 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1886 libnet_join_set_error_string(mem_ctx, r,
1887 "Failed to parse domain name");
1888 return WERR_INVALID_PARAM;
1891 if (!r->in.admin_domain) {
1892 char *admin_domain = NULL;
1893 char *admin_account = NULL;
1894 split_domain_user(mem_ctx,
1895 r->in.admin_account,
1898 r->in.admin_domain = admin_domain;
1899 r->in.admin_account = admin_account;
1902 if (!secrets_init()) {
1903 libnet_join_set_error_string(mem_ctx, r,
1904 "Unable to open secrets database");
1905 return WERR_CAN_NOT_COMPLETE;
1911 /****************************************************************
1912 ****************************************************************/
1914 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1918 /* Try adding dom admins to builtin\admins. Only log failures. */
1919 status = create_builtin_administrators(domain_sid);
1920 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1921 DEBUG(10,("Unable to auto-add domain administrators to "
1922 "BUILTIN\\Administrators during join because "
1923 "winbindd must be running.\n"));
1924 } else if (!NT_STATUS_IS_OK(status)) {
1925 DEBUG(5, ("Failed to auto-add domain administrators to "
1926 "BUILTIN\\Administrators during join: %s\n",
1927 nt_errstr(status)));
1930 /* Try adding dom users to builtin\users. Only log failures. */
1931 status = create_builtin_users(domain_sid);
1932 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1933 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1934 "during join because winbindd must be running.\n"));
1935 } else if (!NT_STATUS_IS_OK(status)) {
1936 DEBUG(5, ("Failed to auto-add domain administrators to "
1937 "BUILTIN\\Administrators during join: %s\n",
1938 nt_errstr(status)));
1942 /****************************************************************
1943 ****************************************************************/
1945 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1946 struct libnet_JoinCtx *r)
1950 if (!W_ERROR_IS_OK(r->out.result)) {
1951 return r->out.result;
1954 werr = do_JoinConfig(r);
1955 if (!W_ERROR_IS_OK(werr)) {
1959 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1963 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1964 if (r->out.dns_domain_name) {
1965 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1969 if (r->out.domain_is_ad &&
1970 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1971 ADS_STATUS ads_status;
1973 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1974 if (!ADS_ERR_OK(ads_status)) {
1975 return WERR_GENERAL_FAILURE;
1978 #endif /* HAVE_ADS */
1980 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1985 /****************************************************************
1986 ****************************************************************/
1988 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1991 ads_destroy(&r->in.ads);
1997 /****************************************************************
1998 ****************************************************************/
2000 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2003 ads_destroy(&r->in.ads);
2009 /****************************************************************
2010 ****************************************************************/
2012 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2013 struct libnet_JoinCtx **r)
2015 struct libnet_JoinCtx *ctx;
2017 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2022 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2024 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2025 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2027 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2034 /****************************************************************
2035 ****************************************************************/
2037 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2038 struct libnet_UnjoinCtx **r)
2040 struct libnet_UnjoinCtx *ctx;
2042 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2047 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2049 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2050 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2057 /****************************************************************
2058 ****************************************************************/
2060 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2061 struct libnet_JoinCtx *r)
2063 bool valid_security = false;
2064 bool valid_workgroup = false;
2065 bool valid_realm = false;
2067 /* check if configuration is already set correctly */
2069 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2071 switch (r->out.domain_is_ad) {
2073 valid_security = (lp_security() == SEC_DOMAIN)
2074 || (lp_server_role() == ROLE_DOMAIN_PDC)
2075 || (lp_server_role() == ROLE_DOMAIN_BDC);
2076 if (valid_workgroup && valid_security) {
2077 /* nothing to be done */
2082 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2083 switch (lp_security()) {
2086 valid_security = true;
2089 if (valid_workgroup && valid_realm && valid_security) {
2090 /* nothing to be done */
2096 /* check if we are supposed to manipulate configuration */
2098 if (!r->in.modify_config) {
2100 char *wrong_conf = talloc_strdup(mem_ctx, "");
2102 if (!valid_workgroup) {
2103 wrong_conf = talloc_asprintf_append(wrong_conf,
2104 "\"workgroup\" set to '%s', should be '%s'",
2105 lp_workgroup(), r->out.netbios_domain_name);
2106 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2110 wrong_conf = talloc_asprintf_append(wrong_conf,
2111 "\"realm\" set to '%s', should be '%s'",
2112 lp_realm(), r->out.dns_domain_name);
2113 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2116 if (!valid_security) {
2117 const char *sec = NULL;
2118 switch (lp_security()) {
2119 case SEC_USER: sec = "user"; break;
2120 case SEC_DOMAIN: sec = "domain"; break;
2121 case SEC_ADS: sec = "ads"; break;
2123 wrong_conf = talloc_asprintf_append(wrong_conf,
2124 "\"security\" set to '%s', should be %s",
2125 sec, r->out.domain_is_ad ?
2126 "either 'domain' or 'ads'" : "'domain'");
2127 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2130 libnet_join_set_error_string(mem_ctx, r,
2131 "Invalid configuration (%s) and configuration modification "
2132 "was not requested", wrong_conf);
2133 return WERR_CAN_NOT_COMPLETE;
2136 /* check if we are able to manipulate configuration */
2138 if (!lp_config_backend_is_registry()) {
2139 libnet_join_set_error_string(mem_ctx, r,
2140 "Configuration manipulation requested but not "
2141 "supported by backend");
2142 return WERR_NOT_SUPPORTED;
2148 /****************************************************************
2149 ****************************************************************/
2151 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2152 struct libnet_JoinCtx *r)
2156 struct cli_state *cli = NULL;
2158 ADS_STATUS ads_status;
2159 #endif /* HAVE_ADS */
2160 const char *pre_connect_realm = NULL;
2161 const char *numeric_dcip = NULL;
2162 const char *sitename = NULL;
2164 /* Before contacting a DC, we can securely know
2165 * the realm only if the user specifies it.
2167 if (r->in.use_kerberos &&
2168 r->in.domain_name_type == JoinDomNameTypeDNS) {
2169 pre_connect_realm = r->in.domain_name;
2172 if (!r->in.dc_name) {
2173 struct netr_DsRGetDCNameInfo *info;
2175 uint32_t name_type_flags = 0;
2176 if (r->in.domain_name_type == JoinDomNameTypeDNS) {
2177 name_type_flags = DS_IS_DNS_NAME;
2178 } else if (r->in.domain_name_type == JoinDomNameTypeNBT) {
2179 name_type_flags = DS_IS_FLAT_NAME;
2181 status = dsgetdcname(mem_ctx,
2186 DS_FORCE_REDISCOVERY |
2187 DS_DIRECTORY_SERVICE_REQUIRED |
2188 DS_WRITABLE_REQUIRED |
2189 DS_RETURN_DNS_NAME |
2192 if (!NT_STATUS_IS_OK(status)) {
2193 libnet_join_set_error_string(mem_ctx, r,
2194 "failed to find DC for domain %s",
2196 get_friendly_nt_error_msg(status));
2197 return WERR_DCNOTFOUND;
2200 dc = strip_hostname(info->dc_unc);
2201 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2202 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2204 if (info->dc_address == NULL || info->dc_address[0] != '\\' ||
2205 info->dc_address[1] != '\\') {
2206 DBG_ERR("ill-formed DC address '%s'\n",
2208 return WERR_DCNOTFOUND;
2211 numeric_dcip = info->dc_address + 2;
2212 sitename = info->dc_site_name;
2213 /* info goes out of scope but the memory stays
2214 allocated on the talloc context */
2217 if (pre_connect_realm != NULL) {
2218 struct sockaddr_storage ss = {0};
2220 if (numeric_dcip != NULL) {
2221 if (!interpret_string_addr(&ss, numeric_dcip,
2224 "cannot parse IP address '%s' of DC '%s'\n",
2225 numeric_dcip, r->in.dc_name);
2226 return WERR_DCNOTFOUND;
2229 if (!interpret_string_addr(&ss, r->in.dc_name, 0)) {
2231 "cannot resolve IP address of DC '%s'\n",
2233 return WERR_DCNOTFOUND;
2237 /* The domain parameter is only used as modifier
2238 * to krb5.conf file name. .JOIN is is not a valid
2239 * NetBIOS name so it cannot clash with another domain
2242 create_local_private_krb5_conf_for_domain(
2243 pre_connect_realm, ".JOIN", sitename, &ss);
2246 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2247 if (!NT_STATUS_IS_OK(status)) {
2248 libnet_join_set_error_string(mem_ctx, r,
2249 "failed to lookup DC info for domain '%s' over rpc: %s",
2250 r->in.domain_name, get_friendly_nt_error_msg(status));
2251 return ntstatus_to_werror(status);
2254 werr = libnet_join_check_config(mem_ctx, r);
2255 if (!W_ERROR_IS_OK(werr)) {
2261 create_local_private_krb5_conf_for_domain(
2262 r->out.dns_domain_name, r->out.netbios_domain_name,
2263 NULL, smbXcli_conn_remote_sockaddr(cli->conn));
2265 if (r->out.domain_is_ad && r->in.account_ou &&
2266 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2268 ads_status = libnet_join_connect_ads(mem_ctx, r);
2269 if (!ADS_ERR_OK(ads_status)) {
2270 return WERR_DEFAULT_JOIN_REQUIRED;
2273 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2274 if (!ADS_ERR_OK(ads_status)) {
2275 libnet_join_set_error_string(mem_ctx, r,
2276 "failed to precreate account in ou %s: %s",
2278 ads_errstr(ads_status));
2279 return WERR_DEFAULT_JOIN_REQUIRED;
2282 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2284 #endif /* HAVE_ADS */
2286 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2287 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2288 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2290 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2292 if (!NT_STATUS_IS_OK(status)) {
2293 libnet_join_set_error_string(mem_ctx, r,
2294 "failed to join domain '%s' over rpc: %s",
2295 r->in.domain_name, get_friendly_nt_error_msg(status));
2296 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2297 return WERR_SETUP_ALREADY_JOINED;
2299 werr = ntstatus_to_werror(status);
2303 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2304 werr = WERR_SETUP_NOT_JOINED;
2318 /****************************************************************
2319 ****************************************************************/
2321 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2322 struct libnet_JoinCtx *r)
2325 struct libnet_UnjoinCtx *u = NULL;
2327 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2328 if (!W_ERROR_IS_OK(werr)) {
2332 u->in.debug = r->in.debug;
2333 u->in.dc_name = r->in.dc_name;
2334 u->in.domain_name = r->in.domain_name;
2335 u->in.admin_account = r->in.admin_account;
2336 u->in.admin_password = r->in.admin_password;
2337 u->in.modify_config = r->in.modify_config;
2338 u->in.use_kerberos = r->in.use_kerberos;
2339 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2340 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2342 werr = libnet_Unjoin(mem_ctx, u);
2348 /****************************************************************
2349 ****************************************************************/
2351 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2352 struct libnet_JoinCtx *r)
2357 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2360 ZERO_STRUCT(r->out);
2362 werr = libnet_join_pre_processing(mem_ctx, r);
2363 if (!W_ERROR_IS_OK(werr)) {
2367 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2368 werr = libnet_DomainJoin(mem_ctx, r);
2369 if (!W_ERROR_IS_OK(werr)) {
2374 werr = libnet_join_post_processing(mem_ctx, r);
2375 if (!W_ERROR_IS_OK(werr)) {
2379 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2380 werr = libnet_join_post_verify(mem_ctx, r);
2381 if (!W_ERROR_IS_OK(werr)) {
2382 libnet_join_rollback(mem_ctx, r);
2387 r->out.result = werr;
2390 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2395 /****************************************************************
2396 ****************************************************************/
2398 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2399 struct libnet_UnjoinCtx *r)
2403 if (!r->in.domain_sid) {
2405 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2406 libnet_unjoin_set_error_string(mem_ctx, r,
2407 "Unable to fetch domain sid: are we joined?");
2408 return WERR_SETUP_NOT_JOINED;
2410 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2411 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2414 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2415 !r->in.delete_machine_account) {
2416 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2420 if (!r->in.dc_name) {
2421 struct netr_DsRGetDCNameInfo *info;
2423 status = dsgetdcname(mem_ctx,
2428 DS_DIRECTORY_SERVICE_REQUIRED |
2429 DS_WRITABLE_REQUIRED |
2432 if (!NT_STATUS_IS_OK(status)) {
2433 libnet_unjoin_set_error_string(mem_ctx, r,
2434 "failed to find DC for domain %s",
2436 get_friendly_nt_error_msg(status));
2437 return WERR_DCNOTFOUND;
2440 dc = strip_hostname(info->dc_unc);
2441 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2442 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2446 /* for net ads leave, try to delete the account. If it works,
2447 no sense in disabling. If it fails, we can still try to
2450 if (r->in.delete_machine_account) {
2451 ADS_STATUS ads_status;
2452 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2453 if (ADS_ERR_OK(ads_status)) {
2455 r->out.dns_domain_name =
2456 talloc_strdup(mem_ctx,
2457 r->in.ads->server.realm);
2459 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2461 if (!ADS_ERR_OK(ads_status)) {
2462 libnet_unjoin_set_error_string(mem_ctx, r,
2463 "failed to remove machine account from AD: %s",
2464 ads_errstr(ads_status));
2466 r->out.deleted_machine_account = true;
2467 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2468 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2472 #endif /* HAVE_ADS */
2474 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2476 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2477 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2478 if (!NT_STATUS_IS_OK(status)) {
2479 libnet_unjoin_set_error_string(mem_ctx, r,
2480 "failed to disable machine account via rpc: %s",
2481 get_friendly_nt_error_msg(status));
2482 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2483 return WERR_SETUP_NOT_JOINED;
2485 return ntstatus_to_werror(status);
2488 r->out.disabled_machine_account = true;
2491 /* If disable succeeded or was not requested at all, we
2492 should be getting rid of our end of things */
2494 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2499 /****************************************************************
2500 ****************************************************************/
2502 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2503 struct libnet_UnjoinCtx *r)
2505 if (!r->in.domain_name) {
2506 libnet_unjoin_set_error_string(mem_ctx, r,
2507 "No domain name defined");
2508 return WERR_INVALID_PARAM;
2511 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2514 libnet_unjoin_set_error_string(mem_ctx, r,
2515 "Failed to parse domain name");
2516 return WERR_INVALID_PARAM;
2520 return WERR_SETUP_DOMAIN_CONTROLLER;
2523 if (!r->in.admin_domain) {
2524 char *admin_domain = NULL;
2525 char *admin_account = NULL;
2526 split_domain_user(mem_ctx,
2527 r->in.admin_account,
2530 r->in.admin_domain = admin_domain;
2531 r->in.admin_account = admin_account;
2534 if (!secrets_init()) {
2535 libnet_unjoin_set_error_string(mem_ctx, r,
2536 "Unable to open secrets database");
2537 return WERR_CAN_NOT_COMPLETE;
2543 /****************************************************************
2544 ****************************************************************/
2546 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2547 struct libnet_UnjoinCtx *r)
2549 saf_delete(r->out.netbios_domain_name);
2550 saf_delete(r->out.dns_domain_name);
2552 return libnet_unjoin_config(r);
2555 /****************************************************************
2556 ****************************************************************/
2558 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2559 struct libnet_UnjoinCtx *r)
2564 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2567 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2568 if (!W_ERROR_IS_OK(werr)) {
2572 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2573 werr = libnet_DomainUnjoin(mem_ctx, r);
2574 if (!W_ERROR_IS_OK(werr)) {
2575 libnet_unjoin_config(r);
2580 werr = libnet_unjoin_post_processing(mem_ctx, r);
2581 if (!W_ERROR_IS_OK(werr)) {
2586 r->out.result = werr;
2589 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);