2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Gerald (Jerry) Carter 2006
5 * Copyright (C) Guenther Deschner 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "librpc/gen_ndr/ndr_libnet_join.h"
24 #include "libnet/libnet_join.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_lsa_c.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/ndr_netlogon.h"
31 #include "rpc_client/cli_netlogon.h"
32 #include "lib/smbconf/smbconf.h"
33 #include "lib/smbconf/smbconf_reg.h"
34 #include "../libds/common/flags.h"
36 #include "rpc_client/init_lsa.h"
37 #include "rpc_client/cli_pipe.h"
38 #include "../libcli/security/security.h"
40 #include "libsmb/libsmb.h"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/param/loadparm.h"
44 /****************************************************************
45 ****************************************************************/
47 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
50 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
51 DEBUG(1,("libnet_Join:\n%s", str)); \
55 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
56 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
57 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
58 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
60 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
63 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
64 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
68 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
69 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
70 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
71 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
73 /****************************************************************
74 ****************************************************************/
76 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
77 struct libnet_JoinCtx *r,
78 const char *format, ...)
82 if (r->out.error_string) {
86 va_start(args, format);
87 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
91 /****************************************************************
92 ****************************************************************/
94 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
95 struct libnet_UnjoinCtx *r,
96 const char *format, ...)
100 if (r->out.error_string) {
104 va_start(args, format);
105 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
111 /****************************************************************
112 ****************************************************************/
114 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
115 const char *netbios_domain_name,
117 const char *user_name,
118 const char *password,
122 ADS_STRUCT *my_ads = NULL;
125 my_ads = ads_init(dns_domain_name,
129 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
133 SAFE_FREE(my_ads->auth.user_name);
134 my_ads->auth.user_name = SMB_STRDUP(user_name);
135 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
137 SAFE_FREE(my_ads->auth.realm);
138 my_ads->auth.realm = smb_xstrdup(cp);
139 if (!strupper_m(my_ads->auth.realm)) {
140 ads_destroy(&my_ads);
141 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
147 SAFE_FREE(my_ads->auth.password);
148 my_ads->auth.password = SMB_STRDUP(password);
151 status = ads_connect_user_creds(my_ads);
152 if (!ADS_ERR_OK(status)) {
153 ads_destroy(&my_ads);
161 /****************************************************************
162 ****************************************************************/
164 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
165 struct libnet_JoinCtx *r)
169 status = libnet_connect_ads(r->out.dns_domain_name,
170 r->out.netbios_domain_name,
173 r->in.admin_password,
175 if (!ADS_ERR_OK(status)) {
176 libnet_join_set_error_string(mem_ctx, r,
177 "failed to connect to AD: %s",
182 if (!r->out.netbios_domain_name) {
183 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
184 r->in.ads->server.workgroup);
185 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
188 if (!r->out.dns_domain_name) {
189 r->out.dns_domain_name = talloc_strdup(mem_ctx,
190 r->in.ads->config.realm);
191 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
194 r->out.domain_is_ad = true;
199 /****************************************************************
200 ****************************************************************/
202 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
203 struct libnet_UnjoinCtx *r)
207 status = libnet_connect_ads(r->in.domain_name,
211 r->in.admin_password,
213 if (!ADS_ERR_OK(status)) {
214 libnet_unjoin_set_error_string(mem_ctx, r,
215 "failed to connect to AD: %s",
222 /****************************************************************
223 join a domain using ADS (LDAP mods)
224 ****************************************************************/
226 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
227 struct libnet_JoinCtx *r)
230 LDAPMessage *res = NULL;
231 const char *attrs[] = { "dn", NULL };
234 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
235 if (!ADS_ERR_OK(status)) {
239 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
240 if (!ADS_ERR_OK(status)) {
244 if (ads_count_replies(r->in.ads, res) != 1) {
245 ads_msgfree(r->in.ads, res);
246 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
249 ads_msgfree(r->in.ads, res);
251 /* Attempt to create the machine account and bail if this fails.
252 Assume that the admin wants exactly what they requested */
254 status = ads_create_machine_acct(r->in.ads,
258 if (ADS_ERR_OK(status)) {
259 DEBUG(1,("machine account creation created\n"));
261 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
262 (status.err.rc == LDAP_ALREADY_EXISTS)) {
263 status = ADS_SUCCESS;
266 if (!ADS_ERR_OK(status)) {
267 DEBUG(1,("machine account creation failed\n"));
271 status = ads_move_machine_acct(r->in.ads,
275 if (!ADS_ERR_OK(status)) {
276 DEBUG(1,("failure to locate/move pre-existing "
277 "machine account\n"));
281 DEBUG(1,("The machine account %s the specified OU.\n",
282 moved ? "was moved into" : "already exists in"));
287 /****************************************************************
288 ****************************************************************/
290 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
291 struct libnet_UnjoinCtx *r)
296 status = libnet_unjoin_connect_ads(mem_ctx, r);
297 if (!ADS_ERR_OK(status)) {
298 libnet_unjoin_set_error_string(mem_ctx, r,
299 "failed to connect to AD: %s",
305 status = ads_leave_realm(r->in.ads, r->in.machine_name);
306 if (!ADS_ERR_OK(status)) {
307 libnet_unjoin_set_error_string(mem_ctx, r,
308 "failed to leave realm: %s",
316 /****************************************************************
317 ****************************************************************/
319 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
320 struct libnet_JoinCtx *r)
323 LDAPMessage *res = NULL;
326 if (!r->in.machine_name) {
327 return ADS_ERROR(LDAP_NO_MEMORY);
330 status = ads_find_machine_acct(r->in.ads,
333 if (!ADS_ERR_OK(status)) {
337 if (ads_count_replies(r->in.ads, res) != 1) {
338 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
342 dn = ads_get_dn(r->in.ads, mem_ctx, res);
344 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
348 r->out.dn = talloc_strdup(mem_ctx, dn);
350 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
355 ads_msgfree(r->in.ads, res);
361 /****************************************************************
362 Set a machines dNSHostName and servicePrincipalName attributes
363 ****************************************************************/
365 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
366 struct libnet_JoinCtx *r)
371 const char *spn_array[3] = {NULL, NULL, NULL};
376 status = libnet_join_find_machine_acct(mem_ctx, r);
377 if (!ADS_ERR_OK(status)) {
381 /* Windows only creates HOST/shortname & HOST/fqdn. */
383 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
385 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
387 if (!strupper_m(spn)) {
388 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
392 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
393 || (strchr(my_fqdn, '.') == NULL)) {
394 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
395 r->out.dns_domain_name);
398 if (!strlower_m(my_fqdn)) {
399 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
402 if (!strequal(my_fqdn, r->in.machine_name)) {
403 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
405 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
410 mods = ads_init_mods(mem_ctx);
412 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
415 /* fields of primary importance */
417 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
418 if (!ADS_ERR_OK(status)) {
419 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
422 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
424 if (!ADS_ERR_OK(status)) {
425 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
428 return ads_gen_mod(r->in.ads, r->out.dn, mods);
431 /****************************************************************
432 ****************************************************************/
434 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
435 struct libnet_JoinCtx *r)
440 if (!r->in.create_upn) {
446 status = libnet_join_find_machine_acct(mem_ctx, r);
447 if (!ADS_ERR_OK(status)) {
452 const char *realm = r->out.dns_domain_name;
454 /* in case we are about to generate a keytab during the join
455 * make sure the default upn we create is usable with kinit -k.
458 if (USE_KERBEROS_KEYTAB) {
459 realm = talloc_strdup_upper(mem_ctx,
460 r->out.dns_domain_name);
464 return ADS_ERROR(LDAP_NO_MEMORY);
467 r->in.upn = talloc_asprintf(mem_ctx,
472 return ADS_ERROR(LDAP_NO_MEMORY);
476 /* now do the mods */
478 mods = ads_init_mods(mem_ctx);
480 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
483 /* fields of primary importance */
485 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
486 if (!ADS_ERR_OK(status)) {
487 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
490 return ads_gen_mod(r->in.ads, r->out.dn, mods);
494 /****************************************************************
495 ****************************************************************/
497 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
498 struct libnet_JoinCtx *r)
504 if (!r->in.os_name || !r->in.os_version ) {
510 status = libnet_join_find_machine_acct(mem_ctx, r);
511 if (!ADS_ERR_OK(status)) {
515 /* now do the mods */
517 mods = ads_init_mods(mem_ctx);
519 return ADS_ERROR(LDAP_NO_MEMORY);
522 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
524 return ADS_ERROR(LDAP_NO_MEMORY);
527 /* fields of primary importance */
529 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
531 if (!ADS_ERR_OK(status)) {
535 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
537 if (!ADS_ERR_OK(status)) {
541 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
543 if (!ADS_ERR_OK(status)) {
547 return ads_gen_mod(r->in.ads, r->out.dn, mods);
550 /****************************************************************
551 ****************************************************************/
553 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
554 struct libnet_JoinCtx *r)
556 if (!USE_SYSTEM_KEYTAB) {
560 if (ads_keytab_create_default(r->in.ads) != 0) {
567 /****************************************************************
568 ****************************************************************/
570 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
571 struct libnet_JoinCtx *r)
573 uint32_t domain_func;
575 const char *salt = NULL;
576 char *std_salt = NULL;
578 status = ads_domain_func_level(r->in.ads, &domain_func);
579 if (!ADS_ERR_OK(status)) {
580 libnet_join_set_error_string(mem_ctx, r,
581 "failed to determine domain functional level: %s",
586 /* go ahead and setup the default salt */
588 std_salt = kerberos_standard_des_salt();
590 libnet_join_set_error_string(mem_ctx, r,
591 "failed to obtain standard DES salt");
595 salt = talloc_strdup(mem_ctx, std_salt);
602 /* if it's a Windows functional domain, we have to look for the UPN */
604 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
607 upn = ads_get_upn(r->in.ads, mem_ctx,
610 salt = talloc_strdup(mem_ctx, upn);
617 return kerberos_secrets_store_des_salt(salt);
620 /****************************************************************
621 ****************************************************************/
623 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
624 struct libnet_JoinCtx *r)
629 status = libnet_join_connect_ads(mem_ctx, r);
630 if (!ADS_ERR_OK(status)) {
635 status = libnet_join_set_machine_spn(mem_ctx, r);
636 if (!ADS_ERR_OK(status)) {
637 libnet_join_set_error_string(mem_ctx, r,
638 "failed to set machine spn: %s",
643 status = libnet_join_set_os_attributes(mem_ctx, r);
644 if (!ADS_ERR_OK(status)) {
645 libnet_join_set_error_string(mem_ctx, r,
646 "failed to set machine os attributes: %s",
651 status = libnet_join_set_machine_upn(mem_ctx, r);
652 if (!ADS_ERR_OK(status)) {
653 libnet_join_set_error_string(mem_ctx, r,
654 "failed to set machine upn: %s",
659 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
660 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
663 if (!libnet_join_create_keytab(mem_ctx, r)) {
664 libnet_join_set_error_string(mem_ctx, r,
665 "failed to create kerberos keytab");
666 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
671 #endif /* HAVE_ADS */
673 /****************************************************************
674 Store the machine password and domain SID
675 ****************************************************************/
677 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
678 struct libnet_JoinCtx *r)
680 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
683 DEBUG(1,("Failed to save domain sid\n"));
687 if (!secrets_store_machine_password(r->in.machine_password,
688 r->out.netbios_domain_name,
689 r->in.secure_channel_type))
691 DEBUG(1,("Failed to save machine password\n"));
698 /****************************************************************
699 Connect dc's IPC$ share
700 ****************************************************************/
702 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
707 struct cli_state **cli)
712 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
715 if (use_kerberos && pass) {
716 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
719 return cli_full_connection(cli, NULL,
727 SMB_SIGNING_DEFAULT);
730 /****************************************************************
731 Lookup domain dc's info
732 ****************************************************************/
734 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
735 struct libnet_JoinCtx *r,
736 struct cli_state **cli)
738 struct rpc_pipe_client *pipe_hnd = NULL;
739 struct policy_handle lsa_pol;
740 NTSTATUS status, result;
741 union lsa_PolicyInformation *info = NULL;
742 struct dcerpc_binding_handle *b;
744 status = libnet_join_connect_dc_ipc(r->in.dc_name,
747 r->in.admin_password,
750 if (!NT_STATUS_IS_OK(status)) {
754 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
756 if (!NT_STATUS_IS_OK(status)) {
757 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
762 b = pipe_hnd->binding_handle;
764 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
765 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
766 if (!NT_STATUS_IS_OK(status)) {
770 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
775 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
776 r->out.domain_is_ad = true;
777 r->out.netbios_domain_name = info->dns.name.string;
778 r->out.dns_domain_name = info->dns.dns_domain.string;
779 r->out.forest_name = info->dns.dns_forest.string;
780 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
781 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
784 if (!NT_STATUS_IS_OK(status)) {
785 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
787 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
790 if (!NT_STATUS_IS_OK(status)) {
793 if (!NT_STATUS_IS_OK(result)) {
798 r->out.netbios_domain_name = info->account_domain.name.string;
799 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
800 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
803 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
804 TALLOC_FREE(pipe_hnd);
810 /****************************************************************
811 Do the domain join unsecure
812 ****************************************************************/
814 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
815 struct libnet_JoinCtx *r,
816 struct cli_state *cli)
818 struct rpc_pipe_client *pipe_hnd = NULL;
819 unsigned char orig_trust_passwd_hash[16];
820 unsigned char new_trust_passwd_hash[16];
823 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
825 if (!NT_STATUS_IS_OK(status)) {
829 if (!r->in.machine_password) {
830 r->in.machine_password = generate_random_password(mem_ctx,
831 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
832 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
833 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
836 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
838 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
839 E_md4hash(r->in.admin_password, orig_trust_passwd_hash);
841 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
843 orig_trust_passwd_hash,
844 r->in.machine_password,
845 new_trust_passwd_hash,
846 r->in.secure_channel_type);
851 /****************************************************************
853 ****************************************************************/
855 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
856 struct libnet_JoinCtx *r,
857 struct cli_state *cli)
859 struct rpc_pipe_client *pipe_hnd = NULL;
860 struct policy_handle sam_pol, domain_pol, user_pol;
861 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
863 struct lsa_String lsa_acct_name;
865 uint32_t acct_flags = ACB_WSTRUST;
866 struct samr_Ids user_rids;
867 struct samr_Ids name_types;
868 union samr_UserInfo user_info;
869 struct dcerpc_binding_handle *b = NULL;
870 unsigned int old_timeout = 0;
872 DATA_BLOB session_key = data_blob_null;
873 struct samr_CryptPassword crypt_pwd;
874 struct samr_CryptPasswordEx crypt_pwd_ex;
876 ZERO_STRUCT(sam_pol);
877 ZERO_STRUCT(domain_pol);
878 ZERO_STRUCT(user_pol);
880 switch (r->in.secure_channel_type) {
882 acct_flags = ACB_WSTRUST;
885 acct_flags = ACB_SVRTRUST;
888 return NT_STATUS_INVALID_PARAMETER;
891 if (!r->in.machine_password) {
892 r->in.machine_password = generate_random_password(mem_ctx,
893 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
894 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
895 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
898 /* Open the domain */
900 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
902 if (!NT_STATUS_IS_OK(status)) {
903 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
908 b = pipe_hnd->binding_handle;
910 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
911 if (!NT_STATUS_IS_OK(status)) {
912 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
917 status = dcerpc_samr_Connect2(b, mem_ctx,
919 SAMR_ACCESS_ENUM_DOMAINS
920 | SAMR_ACCESS_LOOKUP_DOMAIN,
923 if (!NT_STATUS_IS_OK(status)) {
926 if (!NT_STATUS_IS_OK(result)) {
931 status = dcerpc_samr_OpenDomain(b, mem_ctx,
933 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
934 | SAMR_DOMAIN_ACCESS_CREATE_USER
935 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
939 if (!NT_STATUS_IS_OK(status)) {
942 if (!NT_STATUS_IS_OK(result)) {
947 /* Create domain user */
949 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
950 if (!strlower_m(acct_name)) {
951 status = NT_STATUS_INVALID_PARAMETER;
955 init_lsa_String(&lsa_acct_name, acct_name);
957 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
958 uint32_t access_desired =
959 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
960 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
961 SAMR_USER_ACCESS_SET_PASSWORD |
962 SAMR_USER_ACCESS_GET_ATTRIBUTES |
963 SAMR_USER_ACCESS_SET_ATTRIBUTES;
964 uint32_t access_granted = 0;
966 DEBUG(10,("Creating account with desired access mask: %d\n",
969 status = dcerpc_samr_CreateUser2(b, mem_ctx,
978 if (!NT_STATUS_IS_OK(status)) {
983 if (!NT_STATUS_IS_OK(status) &&
984 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
986 DEBUG(10,("Creation of workstation account failed: %s\n",
989 /* If NT_STATUS_ACCESS_DENIED then we have a valid
990 username/password combo but the user does not have
991 administrator access. */
993 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
994 libnet_join_set_error_string(mem_ctx, r,
995 "User specified does not have "
996 "administrator privileges");
1002 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1003 if (!(r->in.join_flags &
1004 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1009 /* We *must* do this.... don't ask... */
1011 if (NT_STATUS_IS_OK(status)) {
1012 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1016 status = dcerpc_samr_LookupNames(b, mem_ctx,
1023 if (!NT_STATUS_IS_OK(status)) {
1026 if (!NT_STATUS_IS_OK(result)) {
1030 if (user_rids.count != 1) {
1031 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1034 if (name_types.count != 1) {
1035 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1039 if (name_types.ids[0] != SID_NAME_USER) {
1040 DEBUG(0,("%s is not a user account (type=%d)\n",
1041 acct_name, name_types.ids[0]));
1042 status = NT_STATUS_INVALID_WORKSTATION;
1046 user_rid = user_rids.ids[0];
1048 /* Open handle on user */
1050 status = dcerpc_samr_OpenUser(b, mem_ctx,
1052 SEC_FLAG_MAXIMUM_ALLOWED,
1056 if (!NT_STATUS_IS_OK(status)) {
1059 if (!NT_STATUS_IS_OK(result)) {
1064 /* Fill in the additional account flags now */
1066 acct_flags |= ACB_PWNOEXP;
1068 /* Set account flags on machine account */
1069 ZERO_STRUCT(user_info.info16);
1070 user_info.info16.acct_flags = acct_flags;
1072 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1077 if (!NT_STATUS_IS_OK(status)) {
1078 dcerpc_samr_DeleteUser(b, mem_ctx,
1082 libnet_join_set_error_string(mem_ctx, r,
1083 "Failed to set account flags for machine account (%s)\n",
1088 if (!NT_STATUS_IS_OK(result)) {
1091 dcerpc_samr_DeleteUser(b, mem_ctx,
1095 libnet_join_set_error_string(mem_ctx, r,
1096 "Failed to set account flags for machine account (%s)\n",
1101 /* Set password on machine account - first try level 26 */
1104 * increase the timeout as password filter modules on the DC
1105 * might delay the operation for a significant amount of time
1107 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1109 init_samr_CryptPasswordEx(r->in.machine_password,
1113 user_info.info26.password = crypt_pwd_ex;
1114 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1116 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1122 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1124 /* retry with level 24 */
1126 init_samr_CryptPassword(r->in.machine_password,
1130 user_info.info24.password = crypt_pwd;
1131 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1133 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1140 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1142 if (!NT_STATUS_IS_OK(status)) {
1144 dcerpc_samr_DeleteUser(b, mem_ctx,
1148 libnet_join_set_error_string(mem_ctx, r,
1149 "Failed to set password for machine account (%s)\n",
1153 if (!NT_STATUS_IS_OK(result)) {
1156 dcerpc_samr_DeleteUser(b, mem_ctx,
1160 libnet_join_set_error_string(mem_ctx, r,
1161 "Failed to set password for machine account (%s)\n",
1166 status = NT_STATUS_OK;
1173 data_blob_clear_free(&session_key);
1175 if (is_valid_policy_hnd(&sam_pol)) {
1176 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1178 if (is_valid_policy_hnd(&domain_pol)) {
1179 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1181 if (is_valid_policy_hnd(&user_pol)) {
1182 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1184 TALLOC_FREE(pipe_hnd);
1189 /****************************************************************
1190 ****************************************************************/
1192 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1193 const char *machine_name,
1194 const char *dc_name,
1195 const bool use_kerberos)
1197 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS |
1198 NETLOGON_NEG_SUPPORTS_AES;
1199 struct cli_state *cli = NULL;
1200 struct rpc_pipe_client *pipe_hnd = NULL;
1201 struct rpc_pipe_client *netlogon_pipe = NULL;
1203 char *machine_password = NULL;
1204 char *machine_account = NULL;
1208 return NT_STATUS_INVALID_PARAMETER;
1211 if (!secrets_init()) {
1212 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1215 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1217 if (!machine_password) {
1218 return NT_STATUS_NO_TRUST_LSA_SECRET;
1221 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1222 SAFE_FREE(machine_password);
1223 return NT_STATUS_NO_MEMORY;
1227 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1230 status = cli_full_connection(&cli, NULL,
1238 SMB_SIGNING_DEFAULT);
1239 free(machine_account);
1240 free(machine_password);
1242 if (!NT_STATUS_IS_OK(status)) {
1243 status = cli_full_connection(&cli, NULL,
1251 SMB_SIGNING_DEFAULT);
1254 if (!NT_STATUS_IS_OK(status)) {
1258 status = get_schannel_session_key(cli, netbios_domain_name,
1259 &neg_flags, &netlogon_pipe);
1260 if (!NT_STATUS_IS_OK(status)) {
1261 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1263 return NT_STATUS_OK;
1266 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1267 "key from server %s for domain %s. Error was %s\n",
1268 smbXcli_conn_remote_name(cli->conn),
1269 netbios_domain_name, nt_errstr(status)));
1274 if (!lp_client_schannel()) {
1276 return NT_STATUS_OK;
1279 status = cli_rpc_pipe_open_schannel_with_key(
1280 cli, &ndr_table_netlogon, NCACN_NP,
1281 netbios_domain_name,
1282 netlogon_pipe->netlogon_creds, &pipe_hnd);
1286 if (!NT_STATUS_IS_OK(status)) {
1287 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1288 "on netlogon pipe to server %s for domain %s. "
1290 smbXcli_conn_remote_name(cli->conn),
1291 netbios_domain_name, nt_errstr(status)));
1295 return NT_STATUS_OK;
1298 /****************************************************************
1299 ****************************************************************/
1301 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1302 struct libnet_JoinCtx *r)
1306 status = libnet_join_ok(r->out.netbios_domain_name,
1309 r->in.use_kerberos);
1310 if (!NT_STATUS_IS_OK(status)) {
1311 libnet_join_set_error_string(mem_ctx, r,
1312 "failed to verify domain membership after joining: %s",
1313 get_friendly_nt_error_msg(status));
1314 return WERR_SETUP_NOT_JOINED;
1320 /****************************************************************
1321 ****************************************************************/
1323 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1324 struct libnet_UnjoinCtx *r)
1326 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1330 if (!secrets_delete_domain_sid(lp_workgroup())) {
1337 /****************************************************************
1338 ****************************************************************/
1340 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1341 struct libnet_UnjoinCtx *r)
1343 struct cli_state *cli = NULL;
1344 struct rpc_pipe_client *pipe_hnd = NULL;
1345 struct policy_handle sam_pol, domain_pol, user_pol;
1346 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1349 struct lsa_String lsa_acct_name;
1350 struct samr_Ids user_rids;
1351 struct samr_Ids name_types;
1352 union samr_UserInfo *info = NULL;
1353 struct dcerpc_binding_handle *b = NULL;
1355 ZERO_STRUCT(sam_pol);
1356 ZERO_STRUCT(domain_pol);
1357 ZERO_STRUCT(user_pol);
1359 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1360 r->in.admin_account,
1362 r->in.admin_password,
1365 if (!NT_STATUS_IS_OK(status)) {
1369 /* Open the domain */
1371 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1373 if (!NT_STATUS_IS_OK(status)) {
1374 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1375 nt_errstr(status)));
1379 b = pipe_hnd->binding_handle;
1381 status = dcerpc_samr_Connect2(b, mem_ctx,
1383 SEC_FLAG_MAXIMUM_ALLOWED,
1386 if (!NT_STATUS_IS_OK(status)) {
1389 if (!NT_STATUS_IS_OK(result)) {
1394 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1396 SEC_FLAG_MAXIMUM_ALLOWED,
1400 if (!NT_STATUS_IS_OK(status)) {
1403 if (!NT_STATUS_IS_OK(result)) {
1408 /* Create domain user */
1410 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1411 if (!strlower_m(acct_name)) {
1412 status = NT_STATUS_INVALID_PARAMETER;
1416 init_lsa_String(&lsa_acct_name, acct_name);
1418 status = dcerpc_samr_LookupNames(b, mem_ctx,
1426 if (!NT_STATUS_IS_OK(status)) {
1429 if (!NT_STATUS_IS_OK(result)) {
1433 if (user_rids.count != 1) {
1434 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1437 if (name_types.count != 1) {
1438 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1442 if (name_types.ids[0] != SID_NAME_USER) {
1443 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1444 name_types.ids[0]));
1445 status = NT_STATUS_INVALID_WORKSTATION;
1449 user_rid = user_rids.ids[0];
1451 /* Open handle on user */
1453 status = dcerpc_samr_OpenUser(b, mem_ctx,
1455 SEC_FLAG_MAXIMUM_ALLOWED,
1459 if (!NT_STATUS_IS_OK(status)) {
1462 if (!NT_STATUS_IS_OK(result)) {
1469 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1474 if (!NT_STATUS_IS_OK(status)) {
1475 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1478 if (!NT_STATUS_IS_OK(result)) {
1480 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1484 /* now disable and setuser info */
1486 info->info16.acct_flags |= ACB_DISABLED;
1488 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1493 if (!NT_STATUS_IS_OK(status)) {
1494 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1497 if (!NT_STATUS_IS_OK(result)) {
1499 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1503 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1506 if (pipe_hnd && b) {
1507 if (is_valid_policy_hnd(&domain_pol)) {
1508 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1510 if (is_valid_policy_hnd(&sam_pol)) {
1511 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1513 TALLOC_FREE(pipe_hnd);
1523 /****************************************************************
1524 ****************************************************************/
1526 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1528 WERROR werr = WERR_OK;
1530 struct smbconf_ctx *ctx;
1532 err = smbconf_init_reg(r, &ctx, NULL);
1533 if (!SBC_ERROR_IS_OK(err)) {
1534 werr = WERR_NO_SUCH_SERVICE;
1538 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1540 err = smbconf_set_global_parameter(ctx, "security", "user");
1541 if (!SBC_ERROR_IS_OK(err)) {
1542 werr = WERR_NO_SUCH_SERVICE;
1546 err = smbconf_set_global_parameter(ctx, "workgroup",
1548 if (!SBC_ERROR_IS_OK(err)) {
1549 werr = WERR_NO_SUCH_SERVICE;
1553 smbconf_delete_global_parameter(ctx, "realm");
1557 err = smbconf_set_global_parameter(ctx, "security", "domain");
1558 if (!SBC_ERROR_IS_OK(err)) {
1559 werr = WERR_NO_SUCH_SERVICE;
1563 err = smbconf_set_global_parameter(ctx, "workgroup",
1564 r->out.netbios_domain_name);
1565 if (!SBC_ERROR_IS_OK(err)) {
1566 werr = WERR_NO_SUCH_SERVICE;
1570 if (r->out.domain_is_ad) {
1571 err = smbconf_set_global_parameter(ctx, "security", "ads");
1572 if (!SBC_ERROR_IS_OK(err)) {
1573 werr = WERR_NO_SUCH_SERVICE;
1577 err = smbconf_set_global_parameter(ctx, "realm",
1578 r->out.dns_domain_name);
1579 if (!SBC_ERROR_IS_OK(err)) {
1580 werr = WERR_NO_SUCH_SERVICE;
1586 smbconf_shutdown(ctx);
1590 /****************************************************************
1591 ****************************************************************/
1593 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1595 WERROR werr = WERR_OK;
1597 struct smbconf_ctx *ctx;
1599 err = smbconf_init_reg(r, &ctx, NULL);
1600 if (!SBC_ERROR_IS_OK(err)) {
1601 werr = WERR_NO_SUCH_SERVICE;
1605 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1607 err = smbconf_set_global_parameter(ctx, "security", "user");
1608 if (!SBC_ERROR_IS_OK(err)) {
1609 werr = WERR_NO_SUCH_SERVICE;
1613 err = smbconf_delete_global_parameter(ctx, "workgroup");
1614 if (!SBC_ERROR_IS_OK(err)) {
1615 werr = WERR_NO_SUCH_SERVICE;
1619 smbconf_delete_global_parameter(ctx, "realm");
1623 smbconf_shutdown(ctx);
1627 /****************************************************************
1628 ****************************************************************/
1630 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1634 if (!W_ERROR_IS_OK(r->out.result)) {
1635 return r->out.result;
1638 if (!r->in.modify_config) {
1642 werr = do_join_modify_vals_config(r);
1643 if (!W_ERROR_IS_OK(werr)) {
1647 lp_load_global(get_dyn_CONFIGFILE());
1649 r->out.modified_config = true;
1650 r->out.result = werr;
1655 /****************************************************************
1656 ****************************************************************/
1658 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1662 if (!W_ERROR_IS_OK(r->out.result)) {
1663 return r->out.result;
1666 if (!r->in.modify_config) {
1670 werr = do_unjoin_modify_vals_config(r);
1671 if (!W_ERROR_IS_OK(werr)) {
1675 lp_load_global(get_dyn_CONFIGFILE());
1677 r->out.modified_config = true;
1678 r->out.result = werr;
1683 /****************************************************************
1684 ****************************************************************/
1686 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1687 const char *domain_str,
1688 const char **domain_p,
1691 char *domain = NULL;
1693 const char *p = NULL;
1695 if (!domain_str || !domain_p || !dc_p) {
1699 p = strchr_m(domain_str, '\\');
1702 domain = talloc_strndup(mem_ctx, domain_str,
1703 PTR_DIFF(p, domain_str));
1704 dc = talloc_strdup(mem_ctx, p+1);
1709 domain = talloc_strdup(mem_ctx, domain_str);
1725 /****************************************************************
1726 ****************************************************************/
1728 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1729 struct libnet_JoinCtx *r)
1731 if (!r->in.domain_name) {
1732 libnet_join_set_error_string(mem_ctx, r,
1733 "No domain name defined");
1734 return WERR_INVALID_PARAM;
1737 if (strlen(r->in.machine_name) > 15) {
1738 libnet_join_set_error_string(mem_ctx, r,
1739 "Our netbios name can be at most 15 chars long, "
1740 "\"%s\" is %u chars long\n",
1742 (unsigned int)strlen(r->in.machine_name));
1743 return WERR_INVALID_PARAM;
1746 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1749 libnet_join_set_error_string(mem_ctx, r,
1750 "Failed to parse domain name");
1751 return WERR_INVALID_PARAM;
1755 return WERR_SETUP_DOMAIN_CONTROLLER;
1758 if (!r->in.admin_domain) {
1759 char *admin_domain = NULL;
1760 char *admin_account = NULL;
1761 split_domain_user(mem_ctx,
1762 r->in.admin_account,
1765 r->in.admin_domain = admin_domain;
1766 r->in.admin_account = admin_account;
1769 if (!secrets_init()) {
1770 libnet_join_set_error_string(mem_ctx, r,
1771 "Unable to open secrets database");
1772 return WERR_CAN_NOT_COMPLETE;
1778 /****************************************************************
1779 ****************************************************************/
1781 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1785 /* Try adding dom admins to builtin\admins. Only log failures. */
1786 status = create_builtin_administrators(domain_sid);
1787 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1788 DEBUG(10,("Unable to auto-add domain administrators to "
1789 "BUILTIN\\Administrators during join because "
1790 "winbindd must be running.\n"));
1791 } else if (!NT_STATUS_IS_OK(status)) {
1792 DEBUG(5, ("Failed to auto-add domain administrators to "
1793 "BUILTIN\\Administrators during join: %s\n",
1794 nt_errstr(status)));
1797 /* Try adding dom users to builtin\users. Only log failures. */
1798 status = create_builtin_users(domain_sid);
1799 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1800 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1801 "during join because winbindd must be running.\n"));
1802 } else if (!NT_STATUS_IS_OK(status)) {
1803 DEBUG(5, ("Failed to auto-add domain administrators to "
1804 "BUILTIN\\Administrators during join: %s\n",
1805 nt_errstr(status)));
1809 /****************************************************************
1810 ****************************************************************/
1812 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1813 struct libnet_JoinCtx *r)
1817 if (!W_ERROR_IS_OK(r->out.result)) {
1818 return r->out.result;
1821 werr = do_JoinConfig(r);
1822 if (!W_ERROR_IS_OK(werr)) {
1826 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1830 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1831 if (r->out.dns_domain_name) {
1832 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1836 if (r->out.domain_is_ad &&
1837 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1838 ADS_STATUS ads_status;
1840 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1841 if (!ADS_ERR_OK(ads_status)) {
1842 return WERR_GENERAL_FAILURE;
1845 #endif /* HAVE_ADS */
1847 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1852 /****************************************************************
1853 ****************************************************************/
1855 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1858 ads_destroy(&r->in.ads);
1864 /****************************************************************
1865 ****************************************************************/
1867 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1870 ads_destroy(&r->in.ads);
1876 /****************************************************************
1877 ****************************************************************/
1879 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1880 struct libnet_JoinCtx **r)
1882 struct libnet_JoinCtx *ctx;
1884 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1889 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1891 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1892 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1894 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1901 /****************************************************************
1902 ****************************************************************/
1904 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1905 struct libnet_UnjoinCtx **r)
1907 struct libnet_UnjoinCtx *ctx;
1909 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1914 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1916 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1917 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1924 /****************************************************************
1925 ****************************************************************/
1927 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1928 struct libnet_JoinCtx *r)
1930 bool valid_security = false;
1931 bool valid_workgroup = false;
1932 bool valid_realm = false;
1934 /* check if configuration is already set correctly */
1936 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1938 switch (r->out.domain_is_ad) {
1940 valid_security = (lp_security() == SEC_DOMAIN);
1941 if (valid_workgroup && valid_security) {
1942 /* nothing to be done */
1947 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1948 switch (lp_security()) {
1951 valid_security = true;
1954 if (valid_workgroup && valid_realm && valid_security) {
1955 /* nothing to be done */
1961 /* check if we are supposed to manipulate configuration */
1963 if (!r->in.modify_config) {
1965 char *wrong_conf = talloc_strdup(mem_ctx, "");
1967 if (!valid_workgroup) {
1968 wrong_conf = talloc_asprintf_append(wrong_conf,
1969 "\"workgroup\" set to '%s', should be '%s'",
1970 lp_workgroup(), r->out.netbios_domain_name);
1971 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1975 wrong_conf = talloc_asprintf_append(wrong_conf,
1976 "\"realm\" set to '%s', should be '%s'",
1977 lp_realm(), r->out.dns_domain_name);
1978 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1981 if (!valid_security) {
1982 const char *sec = NULL;
1983 switch (lp_security()) {
1984 case SEC_USER: sec = "user"; break;
1985 case SEC_DOMAIN: sec = "domain"; break;
1986 case SEC_ADS: sec = "ads"; break;
1988 wrong_conf = talloc_asprintf_append(wrong_conf,
1989 "\"security\" set to '%s', should be %s",
1990 sec, r->out.domain_is_ad ?
1991 "either 'domain' or 'ads'" : "'domain'");
1992 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1995 libnet_join_set_error_string(mem_ctx, r,
1996 "Invalid configuration (%s) and configuration modification "
1997 "was not requested", wrong_conf);
1998 return WERR_CAN_NOT_COMPLETE;
2001 /* check if we are able to manipulate configuration */
2003 if (!lp_config_backend_is_registry()) {
2004 libnet_join_set_error_string(mem_ctx, r,
2005 "Configuration manipulation requested but not "
2006 "supported by backend");
2007 return WERR_NOT_SUPPORTED;
2013 /****************************************************************
2014 ****************************************************************/
2016 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2017 struct libnet_JoinCtx *r)
2021 struct cli_state *cli = NULL;
2023 ADS_STATUS ads_status;
2024 #endif /* HAVE_ADS */
2026 if (!r->in.dc_name) {
2027 struct netr_DsRGetDCNameInfo *info;
2029 status = dsgetdcname(mem_ctx,
2034 DS_FORCE_REDISCOVERY |
2035 DS_DIRECTORY_SERVICE_REQUIRED |
2036 DS_WRITABLE_REQUIRED |
2039 if (!NT_STATUS_IS_OK(status)) {
2040 libnet_join_set_error_string(mem_ctx, r,
2041 "failed to find DC for domain %s",
2043 get_friendly_nt_error_msg(status));
2044 return WERR_DCNOTFOUND;
2047 dc = strip_hostname(info->dc_unc);
2048 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2049 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2052 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2053 if (!NT_STATUS_IS_OK(status)) {
2054 libnet_join_set_error_string(mem_ctx, r,
2055 "failed to lookup DC info for domain '%s' over rpc: %s",
2056 r->in.domain_name, get_friendly_nt_error_msg(status));
2057 return ntstatus_to_werror(status);
2060 werr = libnet_join_check_config(mem_ctx, r);
2061 if (!W_ERROR_IS_OK(werr)) {
2067 create_local_private_krb5_conf_for_domain(
2068 r->out.dns_domain_name, r->out.netbios_domain_name,
2069 NULL, smbXcli_conn_remote_sockaddr(cli->conn),
2070 smbXcli_conn_remote_name(cli->conn));
2072 if (r->out.domain_is_ad && r->in.account_ou &&
2073 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2075 ads_status = libnet_join_connect_ads(mem_ctx, r);
2076 if (!ADS_ERR_OK(ads_status)) {
2077 return WERR_DEFAULT_JOIN_REQUIRED;
2080 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2081 if (!ADS_ERR_OK(ads_status)) {
2082 libnet_join_set_error_string(mem_ctx, r,
2083 "failed to precreate account in ou %s: %s",
2085 ads_errstr(ads_status));
2086 return WERR_DEFAULT_JOIN_REQUIRED;
2089 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2091 #endif /* HAVE_ADS */
2093 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2094 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2095 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2097 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2099 if (!NT_STATUS_IS_OK(status)) {
2100 libnet_join_set_error_string(mem_ctx, r,
2101 "failed to join domain '%s' over rpc: %s",
2102 r->in.domain_name, get_friendly_nt_error_msg(status));
2103 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2104 return WERR_SETUP_ALREADY_JOINED;
2106 werr = ntstatus_to_werror(status);
2110 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2111 werr = WERR_SETUP_NOT_JOINED;
2125 /****************************************************************
2126 ****************************************************************/
2128 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2129 struct libnet_JoinCtx *r)
2132 struct libnet_UnjoinCtx *u = NULL;
2134 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2135 if (!W_ERROR_IS_OK(werr)) {
2139 u->in.debug = r->in.debug;
2140 u->in.dc_name = r->in.dc_name;
2141 u->in.domain_name = r->in.domain_name;
2142 u->in.admin_account = r->in.admin_account;
2143 u->in.admin_password = r->in.admin_password;
2144 u->in.modify_config = r->in.modify_config;
2145 u->in.use_kerberos = r->in.use_kerberos;
2146 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2147 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2149 werr = libnet_Unjoin(mem_ctx, u);
2155 /****************************************************************
2156 ****************************************************************/
2158 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2159 struct libnet_JoinCtx *r)
2164 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2167 ZERO_STRUCT(r->out);
2169 werr = libnet_join_pre_processing(mem_ctx, r);
2170 if (!W_ERROR_IS_OK(werr)) {
2174 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2175 werr = libnet_DomainJoin(mem_ctx, r);
2176 if (!W_ERROR_IS_OK(werr)) {
2181 werr = libnet_join_post_processing(mem_ctx, r);
2182 if (!W_ERROR_IS_OK(werr)) {
2186 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2187 werr = libnet_join_post_verify(mem_ctx, r);
2188 if (!W_ERROR_IS_OK(werr)) {
2189 libnet_join_rollback(mem_ctx, r);
2194 r->out.result = werr;
2197 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2202 /****************************************************************
2203 ****************************************************************/
2205 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2206 struct libnet_UnjoinCtx *r)
2210 if (!r->in.domain_sid) {
2212 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2213 libnet_unjoin_set_error_string(mem_ctx, r,
2214 "Unable to fetch domain sid: are we joined?");
2215 return WERR_SETUP_NOT_JOINED;
2217 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2218 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2221 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2222 !r->in.delete_machine_account) {
2223 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2227 if (!r->in.dc_name) {
2228 struct netr_DsRGetDCNameInfo *info;
2230 status = dsgetdcname(mem_ctx,
2235 DS_DIRECTORY_SERVICE_REQUIRED |
2236 DS_WRITABLE_REQUIRED |
2239 if (!NT_STATUS_IS_OK(status)) {
2240 libnet_unjoin_set_error_string(mem_ctx, r,
2241 "failed to find DC for domain %s",
2243 get_friendly_nt_error_msg(status));
2244 return WERR_DCNOTFOUND;
2247 dc = strip_hostname(info->dc_unc);
2248 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2249 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2253 /* for net ads leave, try to delete the account. If it works,
2254 no sense in disabling. If it fails, we can still try to
2257 if (r->in.delete_machine_account) {
2258 ADS_STATUS ads_status;
2259 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2260 if (ADS_ERR_OK(ads_status)) {
2262 r->out.dns_domain_name =
2263 talloc_strdup(mem_ctx,
2264 r->in.ads->server.realm);
2266 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2268 if (!ADS_ERR_OK(ads_status)) {
2269 libnet_unjoin_set_error_string(mem_ctx, r,
2270 "failed to remove machine account from AD: %s",
2271 ads_errstr(ads_status));
2273 r->out.deleted_machine_account = true;
2274 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2275 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2279 #endif /* HAVE_ADS */
2281 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2283 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2284 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2285 if (!NT_STATUS_IS_OK(status)) {
2286 libnet_unjoin_set_error_string(mem_ctx, r,
2287 "failed to disable machine account via rpc: %s",
2288 get_friendly_nt_error_msg(status));
2289 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2290 return WERR_SETUP_NOT_JOINED;
2292 return ntstatus_to_werror(status);
2295 r->out.disabled_machine_account = true;
2298 /* If disable succeeded or was not requested at all, we
2299 should be getting rid of our end of things */
2301 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2306 /****************************************************************
2307 ****************************************************************/
2309 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2310 struct libnet_UnjoinCtx *r)
2312 if (!r->in.domain_name) {
2313 libnet_unjoin_set_error_string(mem_ctx, r,
2314 "No domain name defined");
2315 return WERR_INVALID_PARAM;
2318 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2321 libnet_unjoin_set_error_string(mem_ctx, r,
2322 "Failed to parse domain name");
2323 return WERR_INVALID_PARAM;
2327 return WERR_SETUP_DOMAIN_CONTROLLER;
2330 if (!r->in.admin_domain) {
2331 char *admin_domain = NULL;
2332 char *admin_account = NULL;
2333 split_domain_user(mem_ctx,
2334 r->in.admin_account,
2337 r->in.admin_domain = admin_domain;
2338 r->in.admin_account = admin_account;
2341 if (!secrets_init()) {
2342 libnet_unjoin_set_error_string(mem_ctx, r,
2343 "Unable to open secrets database");
2344 return WERR_CAN_NOT_COMPLETE;
2350 /****************************************************************
2351 ****************************************************************/
2353 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2354 struct libnet_UnjoinCtx *r)
2356 saf_delete(r->out.netbios_domain_name);
2357 saf_delete(r->out.dns_domain_name);
2359 return libnet_unjoin_config(r);
2362 /****************************************************************
2363 ****************************************************************/
2365 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2366 struct libnet_UnjoinCtx *r)
2371 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2374 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2375 if (!W_ERROR_IS_OK(werr)) {
2379 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2380 werr = libnet_DomainUnjoin(mem_ctx, r);
2381 if (!W_ERROR_IS_OK(werr)) {
2382 libnet_unjoin_config(r);
2387 werr = libnet_unjoin_post_processing(mem_ctx, r);
2388 if (!W_ERROR_IS_OK(werr)) {
2393 r->out.result = werr;
2396 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);