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/>.
22 #include "libnet/libnet.h"
23 #include "libcli/auth/libcli_auth.h"
24 #include "../librpc/gen_ndr/cli_samr.h"
25 #include "../librpc/gen_ndr/cli_lsa.h"
26 #include "rpc_client/cli_lsarpc.h"
27 #include "../librpc/gen_ndr/cli_netlogon.h"
28 #include "rpc_client/cli_netlogon.h"
29 #include "lib/smbconf/smbconf.h"
30 #include "lib/smbconf/smbconf_reg.h"
32 /****************************************************************
33 ****************************************************************/
35 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
38 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
39 DEBUG(1,("libnet_Join:\n%s", str)); \
43 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
44 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
45 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
46 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
48 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
51 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
52 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
56 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
57 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
58 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
59 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
61 /****************************************************************
62 ****************************************************************/
64 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
65 struct libnet_JoinCtx *r,
66 const char *format, ...)
70 if (r->out.error_string) {
74 va_start(args, format);
75 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
79 /****************************************************************
80 ****************************************************************/
82 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
83 struct libnet_UnjoinCtx *r,
84 const char *format, ...)
88 if (r->out.error_string) {
92 va_start(args, format);
93 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
99 /****************************************************************
100 ****************************************************************/
102 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
103 const char *netbios_domain_name,
105 const char *user_name,
106 const char *password,
110 ADS_STRUCT *my_ads = NULL;
112 my_ads = ads_init(dns_domain_name,
116 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
120 SAFE_FREE(my_ads->auth.user_name);
121 my_ads->auth.user_name = SMB_STRDUP(user_name);
125 SAFE_FREE(my_ads->auth.password);
126 my_ads->auth.password = SMB_STRDUP(password);
129 status = ads_connect_user_creds(my_ads);
130 if (!ADS_ERR_OK(status)) {
131 ads_destroy(&my_ads);
139 /****************************************************************
140 ****************************************************************/
142 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
143 struct libnet_JoinCtx *r)
147 status = libnet_connect_ads(r->out.dns_domain_name,
148 r->out.netbios_domain_name,
151 r->in.admin_password,
153 if (!ADS_ERR_OK(status)) {
154 libnet_join_set_error_string(mem_ctx, r,
155 "failed to connect to AD: %s",
160 if (!r->out.netbios_domain_name) {
161 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
162 r->in.ads->server.workgroup);
163 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
166 if (!r->out.dns_domain_name) {
167 r->out.dns_domain_name = talloc_strdup(mem_ctx,
168 r->in.ads->config.realm);
169 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
172 r->out.domain_is_ad = true;
177 /****************************************************************
178 ****************************************************************/
180 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
181 struct libnet_UnjoinCtx *r)
185 status = libnet_connect_ads(r->in.domain_name,
189 r->in.admin_password,
191 if (!ADS_ERR_OK(status)) {
192 libnet_unjoin_set_error_string(mem_ctx, r,
193 "failed to connect to AD: %s",
200 /****************************************************************
201 join a domain using ADS (LDAP mods)
202 ****************************************************************/
204 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
205 struct libnet_JoinCtx *r)
208 LDAPMessage *res = NULL;
209 const char *attrs[] = { "dn", NULL };
212 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
213 if (!ADS_ERR_OK(status)) {
217 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
218 if (!ADS_ERR_OK(status)) {
222 if (ads_count_replies(r->in.ads, res) != 1) {
223 ads_msgfree(r->in.ads, res);
224 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
227 ads_msgfree(r->in.ads, res);
229 /* Attempt to create the machine account and bail if this fails.
230 Assume that the admin wants exactly what they requested */
232 status = ads_create_machine_acct(r->in.ads,
236 if (ADS_ERR_OK(status)) {
237 DEBUG(1,("machine account creation created\n"));
239 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
240 (status.err.rc == LDAP_ALREADY_EXISTS)) {
241 status = ADS_SUCCESS;
244 if (!ADS_ERR_OK(status)) {
245 DEBUG(1,("machine account creation failed\n"));
249 status = ads_move_machine_acct(r->in.ads,
253 if (!ADS_ERR_OK(status)) {
254 DEBUG(1,("failure to locate/move pre-existing "
255 "machine account\n"));
259 DEBUG(1,("The machine account %s the specified OU.\n",
260 moved ? "was moved into" : "already exists in"));
265 /****************************************************************
266 ****************************************************************/
268 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
269 struct libnet_UnjoinCtx *r)
274 status = libnet_unjoin_connect_ads(mem_ctx, r);
275 if (!ADS_ERR_OK(status)) {
276 libnet_unjoin_set_error_string(mem_ctx, r,
277 "failed to connect to AD: %s",
283 status = ads_leave_realm(r->in.ads, r->in.machine_name);
284 if (!ADS_ERR_OK(status)) {
285 libnet_unjoin_set_error_string(mem_ctx, r,
286 "failed to leave realm: %s",
294 /****************************************************************
295 ****************************************************************/
297 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
298 struct libnet_JoinCtx *r)
301 LDAPMessage *res = NULL;
304 if (!r->in.machine_name) {
305 return ADS_ERROR(LDAP_NO_MEMORY);
308 status = ads_find_machine_acct(r->in.ads,
311 if (!ADS_ERR_OK(status)) {
315 if (ads_count_replies(r->in.ads, res) != 1) {
316 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
320 dn = ads_get_dn(r->in.ads, mem_ctx, res);
322 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
326 r->out.dn = talloc_strdup(mem_ctx, dn);
328 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
333 ads_msgfree(r->in.ads, res);
339 /****************************************************************
340 Set a machines dNSHostName and servicePrincipalName attributes
341 ****************************************************************/
343 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
344 struct libnet_JoinCtx *r)
349 const char *spn_array[3] = {NULL, NULL, NULL};
354 status = libnet_join_find_machine_acct(mem_ctx, r);
355 if (!ADS_ERR_OK(status)) {
359 /* Windows only creates HOST/shortname & HOST/fqdn. */
361 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
363 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
368 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
369 || (strchr(my_fqdn, '.') == NULL)) {
370 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
371 r->out.dns_domain_name);
376 if (!strequal(my_fqdn, r->in.machine_name)) {
377 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
379 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
384 mods = ads_init_mods(mem_ctx);
386 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
389 /* fields of primary importance */
391 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
392 if (!ADS_ERR_OK(status)) {
393 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
396 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
398 if (!ADS_ERR_OK(status)) {
399 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
402 return ads_gen_mod(r->in.ads, r->out.dn, mods);
405 /****************************************************************
406 ****************************************************************/
408 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
409 struct libnet_JoinCtx *r)
414 if (!r->in.create_upn) {
420 status = libnet_join_find_machine_acct(mem_ctx, r);
421 if (!ADS_ERR_OK(status)) {
426 r->in.upn = talloc_asprintf(mem_ctx,
429 r->out.dns_domain_name);
431 return ADS_ERROR(LDAP_NO_MEMORY);
435 /* now do the mods */
437 mods = ads_init_mods(mem_ctx);
439 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
442 /* fields of primary importance */
444 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
445 if (!ADS_ERR_OK(status)) {
446 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
449 return ads_gen_mod(r->in.ads, r->out.dn, mods);
453 /****************************************************************
454 ****************************************************************/
456 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
457 struct libnet_JoinCtx *r)
463 if (!r->in.os_name || !r->in.os_version ) {
469 status = libnet_join_find_machine_acct(mem_ctx, r);
470 if (!ADS_ERR_OK(status)) {
474 /* now do the mods */
476 mods = ads_init_mods(mem_ctx);
478 return ADS_ERROR(LDAP_NO_MEMORY);
481 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
483 return ADS_ERROR(LDAP_NO_MEMORY);
486 /* fields of primary importance */
488 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
490 if (!ADS_ERR_OK(status)) {
494 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
496 if (!ADS_ERR_OK(status)) {
500 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
502 if (!ADS_ERR_OK(status)) {
506 return ads_gen_mod(r->in.ads, r->out.dn, mods);
509 /****************************************************************
510 ****************************************************************/
512 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
513 struct libnet_JoinCtx *r)
515 if (!USE_SYSTEM_KEYTAB) {
519 if (ads_keytab_create_default(r->in.ads) != 0) {
526 /****************************************************************
527 ****************************************************************/
529 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
530 struct libnet_JoinCtx *r)
532 uint32_t domain_func;
534 const char *salt = NULL;
535 char *std_salt = NULL;
537 status = ads_domain_func_level(r->in.ads, &domain_func);
538 if (!ADS_ERR_OK(status)) {
539 libnet_join_set_error_string(mem_ctx, r,
540 "failed to determine domain functional level: %s",
545 /* go ahead and setup the default salt */
547 std_salt = kerberos_standard_des_salt();
549 libnet_join_set_error_string(mem_ctx, r,
550 "failed to obtain standard DES salt");
554 salt = talloc_strdup(mem_ctx, std_salt);
561 /* if it's a Windows functional domain, we have to look for the UPN */
563 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
566 upn = ads_get_upn(r->in.ads, mem_ctx,
569 salt = talloc_strdup(mem_ctx, upn);
576 return kerberos_secrets_store_des_salt(salt);
579 /****************************************************************
580 ****************************************************************/
582 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
583 struct libnet_JoinCtx *r)
588 status = libnet_join_connect_ads(mem_ctx, r);
589 if (!ADS_ERR_OK(status)) {
594 status = libnet_join_set_machine_spn(mem_ctx, r);
595 if (!ADS_ERR_OK(status)) {
596 libnet_join_set_error_string(mem_ctx, r,
597 "failed to set machine spn: %s",
602 status = libnet_join_set_os_attributes(mem_ctx, r);
603 if (!ADS_ERR_OK(status)) {
604 libnet_join_set_error_string(mem_ctx, r,
605 "failed to set machine os attributes: %s",
610 status = libnet_join_set_machine_upn(mem_ctx, r);
611 if (!ADS_ERR_OK(status)) {
612 libnet_join_set_error_string(mem_ctx, r,
613 "failed to set machine upn: %s",
618 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
619 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
622 if (!libnet_join_create_keytab(mem_ctx, r)) {
623 libnet_join_set_error_string(mem_ctx, r,
624 "failed to create kerberos keytab");
625 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
630 #endif /* WITH_ADS */
632 /****************************************************************
633 Store the machine password and domain SID
634 ****************************************************************/
636 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
637 struct libnet_JoinCtx *r)
639 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
642 DEBUG(1,("Failed to save domain sid\n"));
646 if (!secrets_store_machine_password(r->in.machine_password,
647 r->out.netbios_domain_name,
648 r->in.secure_channel_type))
650 DEBUG(1,("Failed to save machine password\n"));
657 /****************************************************************
658 Connect dc's IPC$ share
659 ****************************************************************/
661 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
665 struct cli_state **cli)
670 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
673 if (use_kerberos && pass) {
674 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
677 return cli_full_connection(cli, NULL,
688 /****************************************************************
689 Lookup domain dc's info
690 ****************************************************************/
692 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
693 struct libnet_JoinCtx *r,
694 struct cli_state **cli)
696 struct rpc_pipe_client *pipe_hnd = NULL;
697 struct policy_handle lsa_pol;
698 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
699 union lsa_PolicyInformation *info = NULL;
701 status = libnet_join_connect_dc_ipc(r->in.dc_name,
703 r->in.admin_password,
706 if (!NT_STATUS_IS_OK(status)) {
710 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
712 if (!NT_STATUS_IS_OK(status)) {
713 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
718 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
719 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
720 if (!NT_STATUS_IS_OK(status)) {
724 status = rpccli_lsa_QueryInfoPolicy2(pipe_hnd, mem_ctx,
728 if (NT_STATUS_IS_OK(status)) {
729 r->out.domain_is_ad = true;
730 r->out.netbios_domain_name = info->dns.name.string;
731 r->out.dns_domain_name = info->dns.dns_domain.string;
732 r->out.forest_name = info->dns.dns_forest.string;
733 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->dns.sid);
734 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
737 if (!NT_STATUS_IS_OK(status)) {
738 status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
740 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
742 if (!NT_STATUS_IS_OK(status)) {
746 r->out.netbios_domain_name = info->account_domain.name.string;
747 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->account_domain.sid);
748 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
751 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
752 TALLOC_FREE(pipe_hnd);
758 /****************************************************************
759 Do the domain join unsecure
760 ****************************************************************/
762 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
763 struct libnet_JoinCtx *r,
764 struct cli_state *cli)
766 struct rpc_pipe_client *pipe_hnd = NULL;
767 unsigned char orig_trust_passwd_hash[16];
768 unsigned char new_trust_passwd_hash[16];
769 fstring trust_passwd;
772 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
774 if (!NT_STATUS_IS_OK(status)) {
778 if (!r->in.machine_password) {
779 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
780 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
783 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
785 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
786 fstrcpy(trust_passwd, r->in.admin_password);
787 strlower_m(trust_passwd);
790 * Machine names can be 15 characters, but the max length on
791 * a password is 14. --jerry
794 trust_passwd[14] = '\0';
796 E_md4hash(trust_passwd, orig_trust_passwd_hash);
798 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
800 orig_trust_passwd_hash,
801 r->in.machine_password,
802 new_trust_passwd_hash,
803 r->in.secure_channel_type);
808 /****************************************************************
810 ****************************************************************/
812 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
813 struct libnet_JoinCtx *r,
814 struct cli_state *cli)
816 struct rpc_pipe_client *pipe_hnd = NULL;
817 struct policy_handle sam_pol, domain_pol, user_pol;
818 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
820 struct lsa_String lsa_acct_name;
822 uint32_t acct_flags = ACB_WSTRUST;
823 struct samr_Ids user_rids;
824 struct samr_Ids name_types;
825 union samr_UserInfo user_info;
827 struct samr_CryptPassword crypt_pwd;
828 struct samr_CryptPasswordEx crypt_pwd_ex;
830 ZERO_STRUCT(sam_pol);
831 ZERO_STRUCT(domain_pol);
832 ZERO_STRUCT(user_pol);
834 switch (r->in.secure_channel_type) {
836 acct_flags = ACB_WSTRUST;
839 acct_flags = ACB_SVRTRUST;
842 return NT_STATUS_INVALID_PARAMETER;
845 if (!r->in.machine_password) {
846 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
847 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
850 /* Open the domain */
852 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
854 if (!NT_STATUS_IS_OK(status)) {
855 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
860 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
862 SAMR_ACCESS_ENUM_DOMAINS
863 | SAMR_ACCESS_LOOKUP_DOMAIN,
865 if (!NT_STATUS_IS_OK(status)) {
869 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
871 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
872 | SAMR_DOMAIN_ACCESS_CREATE_USER
873 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
876 if (!NT_STATUS_IS_OK(status)) {
880 /* Create domain user */
882 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
883 strlower_m(acct_name);
885 init_lsa_String(&lsa_acct_name, acct_name);
887 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
888 uint32_t access_desired =
889 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
890 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
891 SAMR_USER_ACCESS_SET_PASSWORD |
892 SAMR_USER_ACCESS_GET_ATTRIBUTES |
893 SAMR_USER_ACCESS_SET_ATTRIBUTES;
894 uint32_t access_granted = 0;
896 DEBUG(10,("Creating account with desired access mask: %d\n",
899 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
907 if (!NT_STATUS_IS_OK(status) &&
908 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
910 DEBUG(10,("Creation of workstation account failed: %s\n",
913 /* If NT_STATUS_ACCESS_DENIED then we have a valid
914 username/password combo but the user does not have
915 administrator access. */
917 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
918 libnet_join_set_error_string(mem_ctx, r,
919 "User specified does not have "
920 "administrator privileges");
926 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
927 if (!(r->in.join_flags &
928 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
933 /* We *must* do this.... don't ask... */
935 if (NT_STATUS_IS_OK(status)) {
936 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
940 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
946 if (!NT_STATUS_IS_OK(status)) {
950 if (name_types.ids[0] != SID_NAME_USER) {
951 DEBUG(0,("%s is not a user account (type=%d)\n",
952 acct_name, name_types.ids[0]));
953 status = NT_STATUS_INVALID_WORKSTATION;
957 user_rid = user_rids.ids[0];
959 /* Open handle on user */
961 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
963 SEC_FLAG_MAXIMUM_ALLOWED,
966 if (!NT_STATUS_IS_OK(status)) {
970 /* Fill in the additional account flags now */
972 acct_flags |= ACB_PWNOEXP;
973 if (r->out.domain_is_ad) {
974 #if !defined(ENCTYPE_ARCFOUR_HMAC)
975 acct_flags |= ACB_USE_DES_KEY_ONLY;
980 /* Set account flags on machine account */
981 ZERO_STRUCT(user_info.info16);
982 user_info.info16.acct_flags = acct_flags;
984 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
989 if (!NT_STATUS_IS_OK(status)) {
991 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
994 libnet_join_set_error_string(mem_ctx, r,
995 "Failed to set account flags for machine account (%s)\n",
1000 /* Set password on machine account - first try level 26 */
1002 init_samr_CryptPasswordEx(r->in.machine_password,
1003 &cli->user_session_key,
1006 user_info.info26.password = crypt_pwd_ex;
1007 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1009 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
1014 if (NT_STATUS_EQUAL(status, NT_STATUS(DCERPC_FAULT_INVALID_TAG))) {
1016 /* retry with level 24 */
1018 init_samr_CryptPassword(r->in.machine_password,
1019 &cli->user_session_key,
1022 user_info.info24.password = crypt_pwd;
1023 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1025 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
1031 if (!NT_STATUS_IS_OK(status)) {
1033 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
1036 libnet_join_set_error_string(mem_ctx, r,
1037 "Failed to set password for machine account (%s)\n",
1042 status = NT_STATUS_OK;
1049 if (is_valid_policy_hnd(&sam_pol)) {
1050 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1052 if (is_valid_policy_hnd(&domain_pol)) {
1053 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1055 if (is_valid_policy_hnd(&user_pol)) {
1056 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1058 TALLOC_FREE(pipe_hnd);
1063 /****************************************************************
1064 ****************************************************************/
1066 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1067 const char *machine_name,
1068 const char *dc_name)
1070 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1071 struct cli_state *cli = NULL;
1072 struct rpc_pipe_client *pipe_hnd = NULL;
1073 struct rpc_pipe_client *netlogon_pipe = NULL;
1075 char *machine_password = NULL;
1076 char *machine_account = NULL;
1079 return NT_STATUS_INVALID_PARAMETER;
1082 if (!secrets_init()) {
1083 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1086 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1088 if (!machine_password) {
1089 return NT_STATUS_NO_TRUST_LSA_SECRET;
1092 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1093 SAFE_FREE(machine_password);
1094 return NT_STATUS_NO_MEMORY;
1097 status = cli_full_connection(&cli, NULL,
1106 free(machine_account);
1107 free(machine_password);
1109 if (!NT_STATUS_IS_OK(status)) {
1110 status = cli_full_connection(&cli, NULL,
1121 if (!NT_STATUS_IS_OK(status)) {
1125 status = get_schannel_session_key(cli, netbios_domain_name,
1126 &neg_flags, &netlogon_pipe);
1127 if (!NT_STATUS_IS_OK(status)) {
1128 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1130 return NT_STATUS_OK;
1133 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1134 "key from server %s for domain %s. Error was %s\n",
1135 cli->desthost, netbios_domain_name, nt_errstr(status)));
1140 if (!lp_client_schannel()) {
1142 return NT_STATUS_OK;
1145 status = cli_rpc_pipe_open_schannel_with_key(
1146 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
1147 DCERPC_AUTH_LEVEL_PRIVACY,
1148 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1152 if (!NT_STATUS_IS_OK(status)) {
1153 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1154 "on netlogon pipe to server %s for domain %s. "
1156 cli->desthost, netbios_domain_name, nt_errstr(status)));
1160 return NT_STATUS_OK;
1163 /****************************************************************
1164 ****************************************************************/
1166 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1167 struct libnet_JoinCtx *r)
1171 status = libnet_join_ok(r->out.netbios_domain_name,
1174 if (!NT_STATUS_IS_OK(status)) {
1175 libnet_join_set_error_string(mem_ctx, r,
1176 "failed to verify domain membership after joining: %s",
1177 get_friendly_nt_error_msg(status));
1178 return WERR_SETUP_NOT_JOINED;
1184 /****************************************************************
1185 ****************************************************************/
1187 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1188 struct libnet_UnjoinCtx *r)
1190 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1194 if (!secrets_delete_domain_sid(lp_workgroup())) {
1201 /****************************************************************
1202 ****************************************************************/
1204 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1205 struct libnet_UnjoinCtx *r)
1207 struct cli_state *cli = NULL;
1208 struct rpc_pipe_client *pipe_hnd = NULL;
1209 struct policy_handle sam_pol, domain_pol, user_pol;
1210 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1213 struct lsa_String lsa_acct_name;
1214 struct samr_Ids user_rids;
1215 struct samr_Ids name_types;
1216 union samr_UserInfo *info = NULL;
1218 ZERO_STRUCT(sam_pol);
1219 ZERO_STRUCT(domain_pol);
1220 ZERO_STRUCT(user_pol);
1222 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1223 r->in.admin_account,
1224 r->in.admin_password,
1227 if (!NT_STATUS_IS_OK(status)) {
1231 /* Open the domain */
1233 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1235 if (!NT_STATUS_IS_OK(status)) {
1236 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1237 nt_errstr(status)));
1241 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1243 SEC_FLAG_MAXIMUM_ALLOWED,
1245 if (!NT_STATUS_IS_OK(status)) {
1249 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1251 SEC_FLAG_MAXIMUM_ALLOWED,
1254 if (!NT_STATUS_IS_OK(status)) {
1258 /* Create domain user */
1260 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1261 strlower_m(acct_name);
1263 init_lsa_String(&lsa_acct_name, acct_name);
1265 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1272 if (!NT_STATUS_IS_OK(status)) {
1276 if (name_types.ids[0] != SID_NAME_USER) {
1277 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1278 name_types.ids[0]));
1279 status = NT_STATUS_INVALID_WORKSTATION;
1283 user_rid = user_rids.ids[0];
1285 /* Open handle on user */
1287 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1289 SEC_FLAG_MAXIMUM_ALLOWED,
1292 if (!NT_STATUS_IS_OK(status)) {
1298 status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1302 if (!NT_STATUS_IS_OK(status)) {
1303 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1307 /* now disable and setuser info */
1309 info->info16.acct_flags |= ACB_DISABLED;
1311 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1316 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1320 if (is_valid_policy_hnd(&domain_pol)) {
1321 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1323 if (is_valid_policy_hnd(&sam_pol)) {
1324 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1326 TALLOC_FREE(pipe_hnd);
1336 /****************************************************************
1337 ****************************************************************/
1339 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1342 struct smbconf_ctx *ctx;
1344 werr = smbconf_init_reg(r, &ctx, NULL);
1345 if (!W_ERROR_IS_OK(werr)) {
1349 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1351 werr = smbconf_set_global_parameter(ctx, "security", "user");
1352 W_ERROR_NOT_OK_GOTO_DONE(werr);
1354 werr = smbconf_set_global_parameter(ctx, "workgroup",
1357 smbconf_delete_global_parameter(ctx, "realm");
1361 werr = smbconf_set_global_parameter(ctx, "security", "domain");
1362 W_ERROR_NOT_OK_GOTO_DONE(werr);
1364 werr = smbconf_set_global_parameter(ctx, "workgroup",
1365 r->out.netbios_domain_name);
1366 W_ERROR_NOT_OK_GOTO_DONE(werr);
1368 if (r->out.domain_is_ad) {
1369 werr = smbconf_set_global_parameter(ctx, "security", "ads");
1370 W_ERROR_NOT_OK_GOTO_DONE(werr);
1372 werr = smbconf_set_global_parameter(ctx, "realm",
1373 r->out.dns_domain_name);
1374 W_ERROR_NOT_OK_GOTO_DONE(werr);
1378 smbconf_shutdown(ctx);
1382 /****************************************************************
1383 ****************************************************************/
1385 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1387 WERROR werr = WERR_OK;
1388 struct smbconf_ctx *ctx;
1390 werr = smbconf_init_reg(r, &ctx, NULL);
1391 if (!W_ERROR_IS_OK(werr)) {
1395 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1397 werr = smbconf_set_global_parameter(ctx, "security", "user");
1398 W_ERROR_NOT_OK_GOTO_DONE(werr);
1400 werr = smbconf_delete_global_parameter(ctx, "workgroup");
1401 W_ERROR_NOT_OK_GOTO_DONE(werr);
1403 smbconf_delete_global_parameter(ctx, "realm");
1407 smbconf_shutdown(ctx);
1411 /****************************************************************
1412 ****************************************************************/
1414 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1418 if (!W_ERROR_IS_OK(r->out.result)) {
1419 return r->out.result;
1422 if (!r->in.modify_config) {
1426 werr = do_join_modify_vals_config(r);
1427 if (!W_ERROR_IS_OK(werr)) {
1431 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1433 r->out.modified_config = true;
1434 r->out.result = werr;
1439 /****************************************************************
1440 ****************************************************************/
1442 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1446 if (!W_ERROR_IS_OK(r->out.result)) {
1447 return r->out.result;
1450 if (!r->in.modify_config) {
1454 werr = do_unjoin_modify_vals_config(r);
1455 if (!W_ERROR_IS_OK(werr)) {
1459 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1461 r->out.modified_config = true;
1462 r->out.result = werr;
1467 /****************************************************************
1468 ****************************************************************/
1470 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1471 const char *domain_str,
1472 const char **domain_p,
1475 char *domain = NULL;
1477 const char *p = NULL;
1479 if (!domain_str || !domain_p || !dc_p) {
1483 p = strchr_m(domain_str, '\\');
1486 domain = talloc_strndup(mem_ctx, domain_str,
1487 PTR_DIFF(p, domain_str));
1488 dc = talloc_strdup(mem_ctx, p+1);
1493 domain = talloc_strdup(mem_ctx, domain_str);
1509 /****************************************************************
1510 ****************************************************************/
1512 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1513 struct libnet_JoinCtx *r)
1515 if (!r->in.domain_name) {
1516 libnet_join_set_error_string(mem_ctx, r,
1517 "No domain name defined");
1518 return WERR_INVALID_PARAM;
1521 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1524 libnet_join_set_error_string(mem_ctx, r,
1525 "Failed to parse domain name");
1526 return WERR_INVALID_PARAM;
1530 return WERR_SETUP_DOMAIN_CONTROLLER;
1533 if (!secrets_init()) {
1534 libnet_join_set_error_string(mem_ctx, r,
1535 "Unable to open secrets database");
1536 return WERR_CAN_NOT_COMPLETE;
1542 /****************************************************************
1543 ****************************************************************/
1545 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1549 /* Try adding dom admins to builtin\admins. Only log failures. */
1550 status = create_builtin_administrators(domain_sid);
1551 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1552 DEBUG(10,("Unable to auto-add domain administrators to "
1553 "BUILTIN\\Administrators during join because "
1554 "winbindd must be running."));
1555 } else if (!NT_STATUS_IS_OK(status)) {
1556 DEBUG(5, ("Failed to auto-add domain administrators to "
1557 "BUILTIN\\Administrators during join: %s\n",
1558 nt_errstr(status)));
1561 /* Try adding dom users to builtin\users. Only log failures. */
1562 status = create_builtin_users(domain_sid);
1563 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1564 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1565 "during join because winbindd must be running."));
1566 } else if (!NT_STATUS_IS_OK(status)) {
1567 DEBUG(5, ("Failed to auto-add domain administrators to "
1568 "BUILTIN\\Administrators during join: %s\n",
1569 nt_errstr(status)));
1573 /****************************************************************
1574 ****************************************************************/
1576 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1577 struct libnet_JoinCtx *r)
1581 if (!W_ERROR_IS_OK(r->out.result)) {
1582 return r->out.result;
1585 werr = do_JoinConfig(r);
1586 if (!W_ERROR_IS_OK(werr)) {
1590 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1594 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1595 if (r->out.dns_domain_name) {
1596 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1600 if (r->out.domain_is_ad &&
1601 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1602 ADS_STATUS ads_status;
1604 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1605 if (!ADS_ERR_OK(ads_status)) {
1606 return WERR_GENERAL_FAILURE;
1609 #endif /* WITH_ADS */
1611 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1616 /****************************************************************
1617 ****************************************************************/
1619 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1621 const char *krb5_cc_env = NULL;
1624 ads_destroy(&r->in.ads);
1627 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1628 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1629 unsetenv(KRB5_ENV_CCNAME);
1635 /****************************************************************
1636 ****************************************************************/
1638 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1640 const char *krb5_cc_env = NULL;
1643 ads_destroy(&r->in.ads);
1646 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1647 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1648 unsetenv(KRB5_ENV_CCNAME);
1654 /****************************************************************
1655 ****************************************************************/
1657 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1658 struct libnet_JoinCtx **r)
1660 struct libnet_JoinCtx *ctx;
1661 const char *krb5_cc_env = NULL;
1663 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1668 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1670 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1671 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1673 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1674 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1675 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1676 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1677 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1680 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1687 /****************************************************************
1688 ****************************************************************/
1690 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1691 struct libnet_UnjoinCtx **r)
1693 struct libnet_UnjoinCtx *ctx;
1694 const char *krb5_cc_env = NULL;
1696 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1701 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1703 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1704 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1706 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1707 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1708 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1709 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1710 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1718 /****************************************************************
1719 ****************************************************************/
1721 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1722 struct libnet_JoinCtx *r)
1724 bool valid_security = false;
1725 bool valid_workgroup = false;
1726 bool valid_realm = false;
1728 /* check if configuration is already set correctly */
1730 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1732 switch (r->out.domain_is_ad) {
1734 valid_security = (lp_security() == SEC_DOMAIN);
1735 if (valid_workgroup && valid_security) {
1736 /* nothing to be done */
1741 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1742 switch (lp_security()) {
1745 valid_security = true;
1748 if (valid_workgroup && valid_realm && valid_security) {
1749 /* nothing to be done */
1755 /* check if we are supposed to manipulate configuration */
1757 if (!r->in.modify_config) {
1759 char *wrong_conf = talloc_strdup(mem_ctx, "");
1761 if (!valid_workgroup) {
1762 wrong_conf = talloc_asprintf_append(wrong_conf,
1763 "\"workgroup\" set to '%s', should be '%s'",
1764 lp_workgroup(), r->out.netbios_domain_name);
1765 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1769 wrong_conf = talloc_asprintf_append(wrong_conf,
1770 "\"realm\" set to '%s', should be '%s'",
1771 lp_realm(), r->out.dns_domain_name);
1772 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1775 if (!valid_security) {
1776 const char *sec = NULL;
1777 switch (lp_security()) {
1778 case SEC_SHARE: sec = "share"; break;
1779 case SEC_USER: sec = "user"; break;
1780 case SEC_DOMAIN: sec = "domain"; break;
1781 case SEC_ADS: sec = "ads"; break;
1783 wrong_conf = talloc_asprintf_append(wrong_conf,
1784 "\"security\" set to '%s', should be %s",
1785 sec, r->out.domain_is_ad ?
1786 "either 'domain' or 'ads'" : "'domain'");
1787 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1790 libnet_join_set_error_string(mem_ctx, r,
1791 "Invalid configuration (%s) and configuration modification "
1792 "was not requested", wrong_conf);
1793 return WERR_CAN_NOT_COMPLETE;
1796 /* check if we are able to manipulate configuration */
1798 if (!lp_config_backend_is_registry()) {
1799 libnet_join_set_error_string(mem_ctx, r,
1800 "Configuration manipulation requested but not "
1801 "supported by backend");
1802 return WERR_NOT_SUPPORTED;
1808 /****************************************************************
1809 ****************************************************************/
1811 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1812 struct libnet_JoinCtx *r)
1816 struct cli_state *cli = NULL;
1818 ADS_STATUS ads_status;
1819 #endif /* WITH_ADS */
1821 if (!r->in.dc_name) {
1822 struct netr_DsRGetDCNameInfo *info;
1824 status = dsgetdcname(mem_ctx,
1829 DS_FORCE_REDISCOVERY |
1830 DS_DIRECTORY_SERVICE_REQUIRED |
1831 DS_WRITABLE_REQUIRED |
1834 if (!NT_STATUS_IS_OK(status)) {
1835 libnet_join_set_error_string(mem_ctx, r,
1836 "failed to find DC for domain %s",
1838 get_friendly_nt_error_msg(status));
1839 return WERR_DCNOTFOUND;
1842 dc = strip_hostname(info->dc_unc);
1843 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1844 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1847 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1848 if (!NT_STATUS_IS_OK(status)) {
1849 libnet_join_set_error_string(mem_ctx, r,
1850 "failed to lookup DC info for domain '%s' over rpc: %s",
1851 r->in.domain_name, get_friendly_nt_error_msg(status));
1852 return ntstatus_to_werror(status);
1855 werr = libnet_join_check_config(mem_ctx, r);
1856 if (!W_ERROR_IS_OK(werr)) {
1861 if (r->out.domain_is_ad && r->in.account_ou &&
1862 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1864 ads_status = libnet_join_connect_ads(mem_ctx, r);
1865 if (!ADS_ERR_OK(ads_status)) {
1866 return WERR_DEFAULT_JOIN_REQUIRED;
1869 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1870 if (!ADS_ERR_OK(ads_status)) {
1871 libnet_join_set_error_string(mem_ctx, r,
1872 "failed to precreate account in ou %s: %s",
1874 ads_errstr(ads_status));
1875 return WERR_DEFAULT_JOIN_REQUIRED;
1878 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1880 #endif /* WITH_ADS */
1882 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
1883 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
1884 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
1886 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
1888 if (!NT_STATUS_IS_OK(status)) {
1889 libnet_join_set_error_string(mem_ctx, r,
1890 "failed to join domain '%s' over rpc: %s",
1891 r->in.domain_name, get_friendly_nt_error_msg(status));
1892 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1893 return WERR_SETUP_ALREADY_JOINED;
1895 werr = ntstatus_to_werror(status);
1899 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1900 werr = WERR_SETUP_NOT_JOINED;
1914 /****************************************************************
1915 ****************************************************************/
1917 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
1918 struct libnet_JoinCtx *r)
1921 struct libnet_UnjoinCtx *u = NULL;
1923 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
1924 if (!W_ERROR_IS_OK(werr)) {
1928 u->in.debug = r->in.debug;
1929 u->in.dc_name = r->in.dc_name;
1930 u->in.domain_name = r->in.domain_name;
1931 u->in.admin_account = r->in.admin_account;
1932 u->in.admin_password = r->in.admin_password;
1933 u->in.modify_config = r->in.modify_config;
1934 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
1935 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
1937 werr = libnet_Unjoin(mem_ctx, u);
1943 /****************************************************************
1944 ****************************************************************/
1946 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1947 struct libnet_JoinCtx *r)
1952 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1955 werr = libnet_join_pre_processing(mem_ctx, r);
1956 if (!W_ERROR_IS_OK(werr)) {
1960 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1961 werr = libnet_DomainJoin(mem_ctx, r);
1962 if (!W_ERROR_IS_OK(werr)) {
1967 werr = libnet_join_post_processing(mem_ctx, r);
1968 if (!W_ERROR_IS_OK(werr)) {
1972 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1973 werr = libnet_join_post_verify(mem_ctx, r);
1974 if (!W_ERROR_IS_OK(werr)) {
1975 libnet_join_rollback(mem_ctx, r);
1980 r->out.result = werr;
1983 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1988 /****************************************************************
1989 ****************************************************************/
1991 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1992 struct libnet_UnjoinCtx *r)
1996 if (!r->in.domain_sid) {
1998 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1999 libnet_unjoin_set_error_string(mem_ctx, r,
2000 "Unable to fetch domain sid: are we joined?");
2001 return WERR_SETUP_NOT_JOINED;
2003 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
2004 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2007 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2008 !r->in.delete_machine_account) {
2009 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2013 if (!r->in.dc_name) {
2014 struct netr_DsRGetDCNameInfo *info;
2016 status = dsgetdcname(mem_ctx,
2021 DS_DIRECTORY_SERVICE_REQUIRED |
2022 DS_WRITABLE_REQUIRED |
2025 if (!NT_STATUS_IS_OK(status)) {
2026 libnet_unjoin_set_error_string(mem_ctx, r,
2027 "failed to find DC for domain %s",
2029 get_friendly_nt_error_msg(status));
2030 return WERR_DCNOTFOUND;
2033 dc = strip_hostname(info->dc_unc);
2034 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2035 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2039 /* for net ads leave, try to delete the account. If it works,
2040 no sense in disabling. If it fails, we can still try to
2043 if (r->in.delete_machine_account) {
2044 ADS_STATUS ads_status;
2045 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2046 if (ADS_ERR_OK(ads_status)) {
2048 r->out.dns_domain_name =
2049 talloc_strdup(mem_ctx,
2050 r->in.ads->server.realm);
2052 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2054 if (!ADS_ERR_OK(ads_status)) {
2055 libnet_unjoin_set_error_string(mem_ctx, r,
2056 "failed to remove machine account from AD: %s",
2057 ads_errstr(ads_status));
2059 r->out.deleted_machine_account = true;
2060 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2061 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2065 #endif /* WITH_ADS */
2067 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2069 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2070 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2071 if (!NT_STATUS_IS_OK(status)) {
2072 libnet_unjoin_set_error_string(mem_ctx, r,
2073 "failed to disable machine account via rpc: %s",
2074 get_friendly_nt_error_msg(status));
2075 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2076 return WERR_SETUP_NOT_JOINED;
2078 return ntstatus_to_werror(status);
2081 r->out.disabled_machine_account = true;
2084 /* If disable succeeded or was not requested at all, we
2085 should be getting rid of our end of things */
2087 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2092 /****************************************************************
2093 ****************************************************************/
2095 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2096 struct libnet_UnjoinCtx *r)
2098 if (!r->in.domain_name) {
2099 libnet_unjoin_set_error_string(mem_ctx, r,
2100 "No domain name defined");
2101 return WERR_INVALID_PARAM;
2104 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2107 libnet_unjoin_set_error_string(mem_ctx, r,
2108 "Failed to parse domain name");
2109 return WERR_INVALID_PARAM;
2113 return WERR_SETUP_DOMAIN_CONTROLLER;
2116 if (!secrets_init()) {
2117 libnet_unjoin_set_error_string(mem_ctx, r,
2118 "Unable to open secrets database");
2119 return WERR_CAN_NOT_COMPLETE;
2125 /****************************************************************
2126 ****************************************************************/
2128 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2129 struct libnet_UnjoinCtx *r)
2131 saf_delete(r->out.netbios_domain_name);
2132 saf_delete(r->out.dns_domain_name);
2134 return libnet_unjoin_config(r);
2137 /****************************************************************
2138 ****************************************************************/
2140 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2141 struct libnet_UnjoinCtx *r)
2146 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2149 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2150 if (!W_ERROR_IS_OK(werr)) {
2154 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2155 werr = libnet_DomainUnjoin(mem_ctx, r);
2156 if (!W_ERROR_IS_OK(werr)) {
2157 libnet_unjoin_config(r);
2162 werr = libnet_unjoin_post_processing(mem_ctx, r);
2163 if (!W_ERROR_IS_OK(werr)) {
2168 r->out.result = werr;
2171 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);