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 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)) {
1031 if (name_types.ids[0] != SID_NAME_USER) {
1032 DEBUG(0,("%s is not a user account (type=%d)\n",
1033 acct_name, name_types.ids[0]));
1034 status = NT_STATUS_INVALID_WORKSTATION;
1038 user_rid = user_rids.ids[0];
1040 /* Open handle on user */
1042 status = dcerpc_samr_OpenUser(b, mem_ctx,
1044 SEC_FLAG_MAXIMUM_ALLOWED,
1048 if (!NT_STATUS_IS_OK(status)) {
1051 if (!NT_STATUS_IS_OK(result)) {
1056 /* Fill in the additional account flags now */
1058 acct_flags |= ACB_PWNOEXP;
1060 /* Set account flags on machine account */
1061 ZERO_STRUCT(user_info.info16);
1062 user_info.info16.acct_flags = acct_flags;
1064 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1069 if (!NT_STATUS_IS_OK(status)) {
1070 dcerpc_samr_DeleteUser(b, mem_ctx,
1074 libnet_join_set_error_string(mem_ctx, r,
1075 "Failed to set account flags for machine account (%s)\n",
1080 if (!NT_STATUS_IS_OK(result)) {
1083 dcerpc_samr_DeleteUser(b, mem_ctx,
1087 libnet_join_set_error_string(mem_ctx, r,
1088 "Failed to set account flags for machine account (%s)\n",
1093 /* Set password on machine account - first try level 26 */
1096 * increase the timeout as password filter modules on the DC
1097 * might delay the operation for a significant amount of time
1099 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1101 init_samr_CryptPasswordEx(r->in.machine_password,
1105 user_info.info26.password = crypt_pwd_ex;
1106 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1108 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1114 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1116 /* retry with level 24 */
1118 init_samr_CryptPassword(r->in.machine_password,
1122 user_info.info24.password = crypt_pwd;
1123 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1125 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1132 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1134 if (!NT_STATUS_IS_OK(status)) {
1136 dcerpc_samr_DeleteUser(b, mem_ctx,
1140 libnet_join_set_error_string(mem_ctx, r,
1141 "Failed to set password for machine account (%s)\n",
1145 if (!NT_STATUS_IS_OK(result)) {
1148 dcerpc_samr_DeleteUser(b, mem_ctx,
1152 libnet_join_set_error_string(mem_ctx, r,
1153 "Failed to set password for machine account (%s)\n",
1158 status = NT_STATUS_OK;
1165 data_blob_clear_free(&session_key);
1167 if (is_valid_policy_hnd(&sam_pol)) {
1168 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1170 if (is_valid_policy_hnd(&domain_pol)) {
1171 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1173 if (is_valid_policy_hnd(&user_pol)) {
1174 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1176 TALLOC_FREE(pipe_hnd);
1181 /****************************************************************
1182 ****************************************************************/
1184 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1185 const char *machine_name,
1186 const char *dc_name,
1187 const bool use_kerberos)
1189 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS |
1190 NETLOGON_NEG_SUPPORTS_AES;
1191 struct cli_state *cli = NULL;
1192 struct rpc_pipe_client *pipe_hnd = NULL;
1193 struct rpc_pipe_client *netlogon_pipe = NULL;
1195 char *machine_password = NULL;
1196 char *machine_account = NULL;
1200 return NT_STATUS_INVALID_PARAMETER;
1203 if (!secrets_init()) {
1204 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1207 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1209 if (!machine_password) {
1210 return NT_STATUS_NO_TRUST_LSA_SECRET;
1213 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1214 SAFE_FREE(machine_password);
1215 return NT_STATUS_NO_MEMORY;
1219 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1222 status = cli_full_connection(&cli, NULL,
1230 SMB_SIGNING_DEFAULT);
1231 free(machine_account);
1232 free(machine_password);
1234 if (!NT_STATUS_IS_OK(status)) {
1235 status = cli_full_connection(&cli, NULL,
1243 SMB_SIGNING_DEFAULT);
1246 if (!NT_STATUS_IS_OK(status)) {
1250 status = get_schannel_session_key(cli, netbios_domain_name,
1251 &neg_flags, &netlogon_pipe);
1252 if (!NT_STATUS_IS_OK(status)) {
1253 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1255 return NT_STATUS_OK;
1258 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1259 "key from server %s for domain %s. Error was %s\n",
1260 smbXcli_conn_remote_name(cli->conn),
1261 netbios_domain_name, nt_errstr(status)));
1266 if (!lp_client_schannel()) {
1268 return NT_STATUS_OK;
1271 status = cli_rpc_pipe_open_schannel_with_key(
1272 cli, &ndr_table_netlogon, NCACN_NP,
1273 DCERPC_AUTH_LEVEL_PRIVACY,
1274 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1278 if (!NT_STATUS_IS_OK(status)) {
1279 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1280 "on netlogon pipe to server %s for domain %s. "
1282 smbXcli_conn_remote_name(cli->conn),
1283 netbios_domain_name, nt_errstr(status)));
1287 return NT_STATUS_OK;
1290 /****************************************************************
1291 ****************************************************************/
1293 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1294 struct libnet_JoinCtx *r)
1298 status = libnet_join_ok(r->out.netbios_domain_name,
1301 r->in.use_kerberos);
1302 if (!NT_STATUS_IS_OK(status)) {
1303 libnet_join_set_error_string(mem_ctx, r,
1304 "failed to verify domain membership after joining: %s",
1305 get_friendly_nt_error_msg(status));
1306 return WERR_SETUP_NOT_JOINED;
1312 /****************************************************************
1313 ****************************************************************/
1315 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1316 struct libnet_UnjoinCtx *r)
1318 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1322 if (!secrets_delete_domain_sid(lp_workgroup())) {
1329 /****************************************************************
1330 ****************************************************************/
1332 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1333 struct libnet_UnjoinCtx *r)
1335 struct cli_state *cli = NULL;
1336 struct rpc_pipe_client *pipe_hnd = NULL;
1337 struct policy_handle sam_pol, domain_pol, user_pol;
1338 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1341 struct lsa_String lsa_acct_name;
1342 struct samr_Ids user_rids;
1343 struct samr_Ids name_types;
1344 union samr_UserInfo *info = NULL;
1345 struct dcerpc_binding_handle *b = NULL;
1347 ZERO_STRUCT(sam_pol);
1348 ZERO_STRUCT(domain_pol);
1349 ZERO_STRUCT(user_pol);
1351 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1352 r->in.admin_account,
1354 r->in.admin_password,
1357 if (!NT_STATUS_IS_OK(status)) {
1361 /* Open the domain */
1363 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1365 if (!NT_STATUS_IS_OK(status)) {
1366 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1367 nt_errstr(status)));
1371 b = pipe_hnd->binding_handle;
1373 status = dcerpc_samr_Connect2(b, mem_ctx,
1375 SEC_FLAG_MAXIMUM_ALLOWED,
1378 if (!NT_STATUS_IS_OK(status)) {
1381 if (!NT_STATUS_IS_OK(result)) {
1386 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1388 SEC_FLAG_MAXIMUM_ALLOWED,
1392 if (!NT_STATUS_IS_OK(status)) {
1395 if (!NT_STATUS_IS_OK(result)) {
1400 /* Create domain user */
1402 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1403 if (!strlower_m(acct_name)) {
1404 status = NT_STATUS_INVALID_PARAMETER;
1408 init_lsa_String(&lsa_acct_name, acct_name);
1410 status = dcerpc_samr_LookupNames(b, mem_ctx,
1418 if (!NT_STATUS_IS_OK(status)) {
1421 if (!NT_STATUS_IS_OK(result)) {
1426 if (name_types.ids[0] != SID_NAME_USER) {
1427 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1428 name_types.ids[0]));
1429 status = NT_STATUS_INVALID_WORKSTATION;
1433 user_rid = user_rids.ids[0];
1435 /* Open handle on user */
1437 status = dcerpc_samr_OpenUser(b, mem_ctx,
1439 SEC_FLAG_MAXIMUM_ALLOWED,
1443 if (!NT_STATUS_IS_OK(status)) {
1446 if (!NT_STATUS_IS_OK(result)) {
1453 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1458 if (!NT_STATUS_IS_OK(status)) {
1459 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1462 if (!NT_STATUS_IS_OK(result)) {
1464 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1468 /* now disable and setuser info */
1470 info->info16.acct_flags |= ACB_DISABLED;
1472 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1477 if (!NT_STATUS_IS_OK(status)) {
1478 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1481 if (!NT_STATUS_IS_OK(result)) {
1483 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1487 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1490 if (pipe_hnd && b) {
1491 if (is_valid_policy_hnd(&domain_pol)) {
1492 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1494 if (is_valid_policy_hnd(&sam_pol)) {
1495 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1497 TALLOC_FREE(pipe_hnd);
1507 /****************************************************************
1508 ****************************************************************/
1510 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1512 WERROR werr = WERR_OK;
1514 struct smbconf_ctx *ctx;
1516 err = smbconf_init_reg(r, &ctx, NULL);
1517 if (!SBC_ERROR_IS_OK(err)) {
1518 werr = WERR_NO_SUCH_SERVICE;
1522 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1524 err = smbconf_set_global_parameter(ctx, "security", "user");
1525 if (!SBC_ERROR_IS_OK(err)) {
1526 werr = WERR_NO_SUCH_SERVICE;
1530 err = smbconf_set_global_parameter(ctx, "workgroup",
1532 if (!SBC_ERROR_IS_OK(err)) {
1533 werr = WERR_NO_SUCH_SERVICE;
1537 smbconf_delete_global_parameter(ctx, "realm");
1541 err = smbconf_set_global_parameter(ctx, "security", "domain");
1542 if (!SBC_ERROR_IS_OK(err)) {
1543 werr = WERR_NO_SUCH_SERVICE;
1547 err = smbconf_set_global_parameter(ctx, "workgroup",
1548 r->out.netbios_domain_name);
1549 if (!SBC_ERROR_IS_OK(err)) {
1550 werr = WERR_NO_SUCH_SERVICE;
1554 if (r->out.domain_is_ad) {
1555 err = smbconf_set_global_parameter(ctx, "security", "ads");
1556 if (!SBC_ERROR_IS_OK(err)) {
1557 werr = WERR_NO_SUCH_SERVICE;
1561 err = smbconf_set_global_parameter(ctx, "realm",
1562 r->out.dns_domain_name);
1563 if (!SBC_ERROR_IS_OK(err)) {
1564 werr = WERR_NO_SUCH_SERVICE;
1570 smbconf_shutdown(ctx);
1574 /****************************************************************
1575 ****************************************************************/
1577 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1579 WERROR werr = WERR_OK;
1581 struct smbconf_ctx *ctx;
1583 err = smbconf_init_reg(r, &ctx, NULL);
1584 if (!SBC_ERROR_IS_OK(err)) {
1585 werr = WERR_NO_SUCH_SERVICE;
1589 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1591 err = smbconf_set_global_parameter(ctx, "security", "user");
1592 if (!SBC_ERROR_IS_OK(err)) {
1593 werr = WERR_NO_SUCH_SERVICE;
1597 err = smbconf_delete_global_parameter(ctx, "workgroup");
1598 if (!SBC_ERROR_IS_OK(err)) {
1599 werr = WERR_NO_SUCH_SERVICE;
1603 smbconf_delete_global_parameter(ctx, "realm");
1607 smbconf_shutdown(ctx);
1611 /****************************************************************
1612 ****************************************************************/
1614 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1618 if (!W_ERROR_IS_OK(r->out.result)) {
1619 return r->out.result;
1622 if (!r->in.modify_config) {
1626 werr = do_join_modify_vals_config(r);
1627 if (!W_ERROR_IS_OK(werr)) {
1631 lp_load_global(get_dyn_CONFIGFILE());
1633 r->out.modified_config = true;
1634 r->out.result = werr;
1639 /****************************************************************
1640 ****************************************************************/
1642 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1646 if (!W_ERROR_IS_OK(r->out.result)) {
1647 return r->out.result;
1650 if (!r->in.modify_config) {
1654 werr = do_unjoin_modify_vals_config(r);
1655 if (!W_ERROR_IS_OK(werr)) {
1659 lp_load_global(get_dyn_CONFIGFILE());
1661 r->out.modified_config = true;
1662 r->out.result = werr;
1667 /****************************************************************
1668 ****************************************************************/
1670 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1671 const char *domain_str,
1672 const char **domain_p,
1675 char *domain = NULL;
1677 const char *p = NULL;
1679 if (!domain_str || !domain_p || !dc_p) {
1683 p = strchr_m(domain_str, '\\');
1686 domain = talloc_strndup(mem_ctx, domain_str,
1687 PTR_DIFF(p, domain_str));
1688 dc = talloc_strdup(mem_ctx, p+1);
1693 domain = talloc_strdup(mem_ctx, domain_str);
1709 /****************************************************************
1710 ****************************************************************/
1712 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1713 struct libnet_JoinCtx *r)
1715 if (!r->in.domain_name) {
1716 libnet_join_set_error_string(mem_ctx, r,
1717 "No domain name defined");
1718 return WERR_INVALID_PARAM;
1721 if (strlen(r->in.machine_name) > 15) {
1722 libnet_join_set_error_string(mem_ctx, r,
1723 "Our netbios name can be at most 15 chars long, "
1724 "\"%s\" is %u chars long\n",
1726 (unsigned int)strlen(r->in.machine_name));
1727 return WERR_INVALID_PARAM;
1730 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1733 libnet_join_set_error_string(mem_ctx, r,
1734 "Failed to parse domain name");
1735 return WERR_INVALID_PARAM;
1739 return WERR_SETUP_DOMAIN_CONTROLLER;
1742 if (!r->in.admin_domain) {
1743 char *admin_domain = NULL;
1744 char *admin_account = NULL;
1745 split_domain_user(mem_ctx,
1746 r->in.admin_account,
1749 r->in.admin_domain = admin_domain;
1750 r->in.admin_account = admin_account;
1753 if (!secrets_init()) {
1754 libnet_join_set_error_string(mem_ctx, r,
1755 "Unable to open secrets database");
1756 return WERR_CAN_NOT_COMPLETE;
1762 /****************************************************************
1763 ****************************************************************/
1765 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1769 /* Try adding dom admins to builtin\admins. Only log failures. */
1770 status = create_builtin_administrators(domain_sid);
1771 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1772 DEBUG(10,("Unable to auto-add domain administrators to "
1773 "BUILTIN\\Administrators during join because "
1774 "winbindd must be running.\n"));
1775 } else if (!NT_STATUS_IS_OK(status)) {
1776 DEBUG(5, ("Failed to auto-add domain administrators to "
1777 "BUILTIN\\Administrators during join: %s\n",
1778 nt_errstr(status)));
1781 /* Try adding dom users to builtin\users. Only log failures. */
1782 status = create_builtin_users(domain_sid);
1783 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1784 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1785 "during join because winbindd must be running.\n"));
1786 } else if (!NT_STATUS_IS_OK(status)) {
1787 DEBUG(5, ("Failed to auto-add domain administrators to "
1788 "BUILTIN\\Administrators during join: %s\n",
1789 nt_errstr(status)));
1793 /****************************************************************
1794 ****************************************************************/
1796 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1797 struct libnet_JoinCtx *r)
1801 if (!W_ERROR_IS_OK(r->out.result)) {
1802 return r->out.result;
1805 werr = do_JoinConfig(r);
1806 if (!W_ERROR_IS_OK(werr)) {
1810 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1814 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1815 if (r->out.dns_domain_name) {
1816 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1820 if (r->out.domain_is_ad &&
1821 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1822 ADS_STATUS ads_status;
1824 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1825 if (!ADS_ERR_OK(ads_status)) {
1826 return WERR_GENERAL_FAILURE;
1829 #endif /* HAVE_ADS */
1831 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1836 /****************************************************************
1837 ****************************************************************/
1839 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1842 ads_destroy(&r->in.ads);
1848 /****************************************************************
1849 ****************************************************************/
1851 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1854 ads_destroy(&r->in.ads);
1860 /****************************************************************
1861 ****************************************************************/
1863 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1864 struct libnet_JoinCtx **r)
1866 struct libnet_JoinCtx *ctx;
1868 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1873 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1875 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1876 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1878 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1885 /****************************************************************
1886 ****************************************************************/
1888 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1889 struct libnet_UnjoinCtx **r)
1891 struct libnet_UnjoinCtx *ctx;
1893 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1898 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1900 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1901 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1908 /****************************************************************
1909 ****************************************************************/
1911 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1912 struct libnet_JoinCtx *r)
1914 bool valid_security = false;
1915 bool valid_workgroup = false;
1916 bool valid_realm = false;
1918 /* check if configuration is already set correctly */
1920 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1922 switch (r->out.domain_is_ad) {
1924 valid_security = (lp_security() == SEC_DOMAIN);
1925 if (valid_workgroup && valid_security) {
1926 /* nothing to be done */
1931 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1932 switch (lp_security()) {
1935 valid_security = true;
1938 if (valid_workgroup && valid_realm && valid_security) {
1939 /* nothing to be done */
1945 /* check if we are supposed to manipulate configuration */
1947 if (!r->in.modify_config) {
1949 char *wrong_conf = talloc_strdup(mem_ctx, "");
1951 if (!valid_workgroup) {
1952 wrong_conf = talloc_asprintf_append(wrong_conf,
1953 "\"workgroup\" set to '%s', should be '%s'",
1954 lp_workgroup(), r->out.netbios_domain_name);
1955 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1959 wrong_conf = talloc_asprintf_append(wrong_conf,
1960 "\"realm\" set to '%s', should be '%s'",
1961 lp_realm(), r->out.dns_domain_name);
1962 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1965 if (!valid_security) {
1966 const char *sec = NULL;
1967 switch (lp_security()) {
1968 case SEC_USER: sec = "user"; break;
1969 case SEC_DOMAIN: sec = "domain"; break;
1970 case SEC_ADS: sec = "ads"; break;
1972 wrong_conf = talloc_asprintf_append(wrong_conf,
1973 "\"security\" set to '%s', should be %s",
1974 sec, r->out.domain_is_ad ?
1975 "either 'domain' or 'ads'" : "'domain'");
1976 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1979 libnet_join_set_error_string(mem_ctx, r,
1980 "Invalid configuration (%s) and configuration modification "
1981 "was not requested", wrong_conf);
1982 return WERR_CAN_NOT_COMPLETE;
1985 /* check if we are able to manipulate configuration */
1987 if (!lp_config_backend_is_registry()) {
1988 libnet_join_set_error_string(mem_ctx, r,
1989 "Configuration manipulation requested but not "
1990 "supported by backend");
1991 return WERR_NOT_SUPPORTED;
1997 /****************************************************************
1998 ****************************************************************/
2000 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2001 struct libnet_JoinCtx *r)
2005 struct cli_state *cli = NULL;
2007 ADS_STATUS ads_status;
2008 #endif /* HAVE_ADS */
2010 if (!r->in.dc_name) {
2011 struct netr_DsRGetDCNameInfo *info;
2013 status = dsgetdcname(mem_ctx,
2018 DS_FORCE_REDISCOVERY |
2019 DS_DIRECTORY_SERVICE_REQUIRED |
2020 DS_WRITABLE_REQUIRED |
2023 if (!NT_STATUS_IS_OK(status)) {
2024 libnet_join_set_error_string(mem_ctx, r,
2025 "failed to find DC for domain %s",
2027 get_friendly_nt_error_msg(status));
2028 return WERR_DCNOTFOUND;
2031 dc = strip_hostname(info->dc_unc);
2032 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2033 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2036 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2037 if (!NT_STATUS_IS_OK(status)) {
2038 libnet_join_set_error_string(mem_ctx, r,
2039 "failed to lookup DC info for domain '%s' over rpc: %s",
2040 r->in.domain_name, get_friendly_nt_error_msg(status));
2041 return ntstatus_to_werror(status);
2044 werr = libnet_join_check_config(mem_ctx, r);
2045 if (!W_ERROR_IS_OK(werr)) {
2051 create_local_private_krb5_conf_for_domain(
2052 r->out.dns_domain_name, r->out.netbios_domain_name,
2053 NULL, smbXcli_conn_remote_sockaddr(cli->conn),
2054 smbXcli_conn_remote_name(cli->conn));
2056 if (r->out.domain_is_ad && r->in.account_ou &&
2057 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2059 ads_status = libnet_join_connect_ads(mem_ctx, r);
2060 if (!ADS_ERR_OK(ads_status)) {
2061 return WERR_DEFAULT_JOIN_REQUIRED;
2064 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2065 if (!ADS_ERR_OK(ads_status)) {
2066 libnet_join_set_error_string(mem_ctx, r,
2067 "failed to precreate account in ou %s: %s",
2069 ads_errstr(ads_status));
2070 return WERR_DEFAULT_JOIN_REQUIRED;
2073 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2075 #endif /* HAVE_ADS */
2077 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2078 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2079 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2081 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2083 if (!NT_STATUS_IS_OK(status)) {
2084 libnet_join_set_error_string(mem_ctx, r,
2085 "failed to join domain '%s' over rpc: %s",
2086 r->in.domain_name, get_friendly_nt_error_msg(status));
2087 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2088 return WERR_SETUP_ALREADY_JOINED;
2090 werr = ntstatus_to_werror(status);
2094 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2095 werr = WERR_SETUP_NOT_JOINED;
2109 /****************************************************************
2110 ****************************************************************/
2112 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2113 struct libnet_JoinCtx *r)
2116 struct libnet_UnjoinCtx *u = NULL;
2118 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2119 if (!W_ERROR_IS_OK(werr)) {
2123 u->in.debug = r->in.debug;
2124 u->in.dc_name = r->in.dc_name;
2125 u->in.domain_name = r->in.domain_name;
2126 u->in.admin_account = r->in.admin_account;
2127 u->in.admin_password = r->in.admin_password;
2128 u->in.modify_config = r->in.modify_config;
2129 u->in.use_kerberos = r->in.use_kerberos;
2130 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2131 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2133 werr = libnet_Unjoin(mem_ctx, u);
2139 /****************************************************************
2140 ****************************************************************/
2142 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2143 struct libnet_JoinCtx *r)
2148 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2151 ZERO_STRUCT(r->out);
2153 werr = libnet_join_pre_processing(mem_ctx, r);
2154 if (!W_ERROR_IS_OK(werr)) {
2158 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2159 werr = libnet_DomainJoin(mem_ctx, r);
2160 if (!W_ERROR_IS_OK(werr)) {
2165 werr = libnet_join_post_processing(mem_ctx, r);
2166 if (!W_ERROR_IS_OK(werr)) {
2170 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2171 werr = libnet_join_post_verify(mem_ctx, r);
2172 if (!W_ERROR_IS_OK(werr)) {
2173 libnet_join_rollback(mem_ctx, r);
2178 r->out.result = werr;
2181 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2186 /****************************************************************
2187 ****************************************************************/
2189 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2190 struct libnet_UnjoinCtx *r)
2194 if (!r->in.domain_sid) {
2196 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2197 libnet_unjoin_set_error_string(mem_ctx, r,
2198 "Unable to fetch domain sid: are we joined?");
2199 return WERR_SETUP_NOT_JOINED;
2201 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2202 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2205 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2206 !r->in.delete_machine_account) {
2207 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2211 if (!r->in.dc_name) {
2212 struct netr_DsRGetDCNameInfo *info;
2214 status = dsgetdcname(mem_ctx,
2219 DS_DIRECTORY_SERVICE_REQUIRED |
2220 DS_WRITABLE_REQUIRED |
2223 if (!NT_STATUS_IS_OK(status)) {
2224 libnet_unjoin_set_error_string(mem_ctx, r,
2225 "failed to find DC for domain %s",
2227 get_friendly_nt_error_msg(status));
2228 return WERR_DCNOTFOUND;
2231 dc = strip_hostname(info->dc_unc);
2232 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2233 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2237 /* for net ads leave, try to delete the account. If it works,
2238 no sense in disabling. If it fails, we can still try to
2241 if (r->in.delete_machine_account) {
2242 ADS_STATUS ads_status;
2243 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2244 if (ADS_ERR_OK(ads_status)) {
2246 r->out.dns_domain_name =
2247 talloc_strdup(mem_ctx,
2248 r->in.ads->server.realm);
2250 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2252 if (!ADS_ERR_OK(ads_status)) {
2253 libnet_unjoin_set_error_string(mem_ctx, r,
2254 "failed to remove machine account from AD: %s",
2255 ads_errstr(ads_status));
2257 r->out.deleted_machine_account = true;
2258 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2259 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2263 #endif /* HAVE_ADS */
2265 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2267 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2268 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2269 if (!NT_STATUS_IS_OK(status)) {
2270 libnet_unjoin_set_error_string(mem_ctx, r,
2271 "failed to disable machine account via rpc: %s",
2272 get_friendly_nt_error_msg(status));
2273 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2274 return WERR_SETUP_NOT_JOINED;
2276 return ntstatus_to_werror(status);
2279 r->out.disabled_machine_account = true;
2282 /* If disable succeeded or was not requested at all, we
2283 should be getting rid of our end of things */
2285 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2290 /****************************************************************
2291 ****************************************************************/
2293 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2294 struct libnet_UnjoinCtx *r)
2296 if (!r->in.domain_name) {
2297 libnet_unjoin_set_error_string(mem_ctx, r,
2298 "No domain name defined");
2299 return WERR_INVALID_PARAM;
2302 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2305 libnet_unjoin_set_error_string(mem_ctx, r,
2306 "Failed to parse domain name");
2307 return WERR_INVALID_PARAM;
2311 return WERR_SETUP_DOMAIN_CONTROLLER;
2314 if (!r->in.admin_domain) {
2315 char *admin_domain = NULL;
2316 char *admin_account = NULL;
2317 split_domain_user(mem_ctx,
2318 r->in.admin_account,
2321 r->in.admin_domain = admin_domain;
2322 r->in.admin_account = admin_account;
2325 if (!secrets_init()) {
2326 libnet_unjoin_set_error_string(mem_ctx, r,
2327 "Unable to open secrets database");
2328 return WERR_CAN_NOT_COMPLETE;
2334 /****************************************************************
2335 ****************************************************************/
2337 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2338 struct libnet_UnjoinCtx *r)
2340 saf_delete(r->out.netbios_domain_name);
2341 saf_delete(r->out.dns_domain_name);
2343 return libnet_unjoin_config(r);
2346 /****************************************************************
2347 ****************************************************************/
2349 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2350 struct libnet_UnjoinCtx *r)
2355 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2358 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2359 if (!W_ERROR_IS_OK(werr)) {
2363 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2364 werr = libnet_DomainUnjoin(mem_ctx, r);
2365 if (!W_ERROR_IS_OK(werr)) {
2366 libnet_unjoin_config(r);
2371 werr = libnet_unjoin_post_processing(mem_ctx, r);
2372 if (!W_ERROR_IS_OK(werr)) {
2377 r->out.result = werr;
2380 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);