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 r->in.upn = talloc_asprintf(mem_ctx,
455 r->out.dns_domain_name);
457 return ADS_ERROR(LDAP_NO_MEMORY);
461 /* now do the mods */
463 mods = ads_init_mods(mem_ctx);
465 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
468 /* fields of primary importance */
470 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
471 if (!ADS_ERR_OK(status)) {
472 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
475 return ads_gen_mod(r->in.ads, r->out.dn, mods);
479 /****************************************************************
480 ****************************************************************/
482 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
483 struct libnet_JoinCtx *r)
489 if (!r->in.os_name || !r->in.os_version ) {
495 status = libnet_join_find_machine_acct(mem_ctx, r);
496 if (!ADS_ERR_OK(status)) {
500 /* now do the mods */
502 mods = ads_init_mods(mem_ctx);
504 return ADS_ERROR(LDAP_NO_MEMORY);
507 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
509 return ADS_ERROR(LDAP_NO_MEMORY);
512 /* fields of primary importance */
514 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
516 if (!ADS_ERR_OK(status)) {
520 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
522 if (!ADS_ERR_OK(status)) {
526 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
528 if (!ADS_ERR_OK(status)) {
532 return ads_gen_mod(r->in.ads, r->out.dn, mods);
535 /****************************************************************
536 ****************************************************************/
538 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
539 struct libnet_JoinCtx *r)
541 if (!USE_SYSTEM_KEYTAB) {
545 if (ads_keytab_create_default(r->in.ads) != 0) {
552 /****************************************************************
553 ****************************************************************/
555 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
556 struct libnet_JoinCtx *r)
558 uint32_t domain_func;
560 const char *salt = NULL;
561 char *std_salt = NULL;
563 status = ads_domain_func_level(r->in.ads, &domain_func);
564 if (!ADS_ERR_OK(status)) {
565 libnet_join_set_error_string(mem_ctx, r,
566 "failed to determine domain functional level: %s",
571 /* go ahead and setup the default salt */
573 std_salt = kerberos_standard_des_salt();
575 libnet_join_set_error_string(mem_ctx, r,
576 "failed to obtain standard DES salt");
580 salt = talloc_strdup(mem_ctx, std_salt);
587 /* if it's a Windows functional domain, we have to look for the UPN */
589 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
592 upn = ads_get_upn(r->in.ads, mem_ctx,
595 salt = talloc_strdup(mem_ctx, upn);
602 return kerberos_secrets_store_des_salt(salt);
605 /****************************************************************
606 ****************************************************************/
608 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
609 struct libnet_JoinCtx *r)
614 status = libnet_join_connect_ads(mem_ctx, r);
615 if (!ADS_ERR_OK(status)) {
620 status = libnet_join_set_machine_spn(mem_ctx, r);
621 if (!ADS_ERR_OK(status)) {
622 libnet_join_set_error_string(mem_ctx, r,
623 "failed to set machine spn: %s",
628 status = libnet_join_set_os_attributes(mem_ctx, r);
629 if (!ADS_ERR_OK(status)) {
630 libnet_join_set_error_string(mem_ctx, r,
631 "failed to set machine os attributes: %s",
636 status = libnet_join_set_machine_upn(mem_ctx, r);
637 if (!ADS_ERR_OK(status)) {
638 libnet_join_set_error_string(mem_ctx, r,
639 "failed to set machine upn: %s",
644 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
645 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
648 if (!libnet_join_create_keytab(mem_ctx, r)) {
649 libnet_join_set_error_string(mem_ctx, r,
650 "failed to create kerberos keytab");
651 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
656 #endif /* HAVE_ADS */
658 /****************************************************************
659 Store the machine password and domain SID
660 ****************************************************************/
662 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
663 struct libnet_JoinCtx *r)
665 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
668 DEBUG(1,("Failed to save domain sid\n"));
672 if (!secrets_store_machine_password(r->in.machine_password,
673 r->out.netbios_domain_name,
674 r->in.secure_channel_type))
676 DEBUG(1,("Failed to save machine password\n"));
683 /****************************************************************
684 Connect dc's IPC$ share
685 ****************************************************************/
687 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
692 struct cli_state **cli)
697 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
700 if (use_kerberos && pass) {
701 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
704 return cli_full_connection(cli, NULL,
712 SMB_SIGNING_DEFAULT);
715 /****************************************************************
716 Lookup domain dc's info
717 ****************************************************************/
719 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
720 struct libnet_JoinCtx *r,
721 struct cli_state **cli)
723 struct rpc_pipe_client *pipe_hnd = NULL;
724 struct policy_handle lsa_pol;
725 NTSTATUS status, result;
726 union lsa_PolicyInformation *info = NULL;
727 struct dcerpc_binding_handle *b;
729 status = libnet_join_connect_dc_ipc(r->in.dc_name,
732 r->in.admin_password,
735 if (!NT_STATUS_IS_OK(status)) {
739 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
741 if (!NT_STATUS_IS_OK(status)) {
742 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
747 b = pipe_hnd->binding_handle;
749 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
750 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
751 if (!NT_STATUS_IS_OK(status)) {
755 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
760 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
761 r->out.domain_is_ad = true;
762 r->out.netbios_domain_name = info->dns.name.string;
763 r->out.dns_domain_name = info->dns.dns_domain.string;
764 r->out.forest_name = info->dns.dns_forest.string;
765 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
766 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
769 if (!NT_STATUS_IS_OK(status)) {
770 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
772 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
775 if (!NT_STATUS_IS_OK(status)) {
778 if (!NT_STATUS_IS_OK(result)) {
783 r->out.netbios_domain_name = info->account_domain.name.string;
784 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
785 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
788 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
789 TALLOC_FREE(pipe_hnd);
795 /****************************************************************
796 Do the domain join unsecure
797 ****************************************************************/
799 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
800 struct libnet_JoinCtx *r,
801 struct cli_state *cli)
803 struct rpc_pipe_client *pipe_hnd = NULL;
804 unsigned char orig_trust_passwd_hash[16];
805 unsigned char new_trust_passwd_hash[16];
806 fstring trust_passwd;
809 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
811 if (!NT_STATUS_IS_OK(status)) {
815 if (!r->in.machine_password) {
816 r->in.machine_password = generate_random_password(mem_ctx,
817 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
818 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
819 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
822 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
824 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
825 fstrcpy(trust_passwd, r->in.admin_password);
826 if (!strlower_m(trust_passwd)) {
827 return NT_STATUS_INVALID_PARAMETER;
831 * Machine names can be 15 characters, but the max length on
832 * a password is 14. --jerry
835 trust_passwd[14] = '\0';
837 E_md4hash(trust_passwd, orig_trust_passwd_hash);
839 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
841 orig_trust_passwd_hash,
842 r->in.machine_password,
843 new_trust_passwd_hash,
844 r->in.secure_channel_type);
849 /****************************************************************
851 ****************************************************************/
853 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
854 struct libnet_JoinCtx *r,
855 struct cli_state *cli)
857 struct rpc_pipe_client *pipe_hnd = NULL;
858 struct policy_handle sam_pol, domain_pol, user_pol;
859 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
861 struct lsa_String lsa_acct_name;
863 uint32_t acct_flags = ACB_WSTRUST;
864 struct samr_Ids user_rids;
865 struct samr_Ids name_types;
866 union samr_UserInfo user_info;
867 struct dcerpc_binding_handle *b = NULL;
868 unsigned int old_timeout = 0;
870 DATA_BLOB session_key = data_blob_null;
871 struct samr_CryptPassword crypt_pwd;
872 struct samr_CryptPasswordEx crypt_pwd_ex;
874 ZERO_STRUCT(sam_pol);
875 ZERO_STRUCT(domain_pol);
876 ZERO_STRUCT(user_pol);
878 switch (r->in.secure_channel_type) {
880 acct_flags = ACB_WSTRUST;
883 acct_flags = ACB_SVRTRUST;
886 return NT_STATUS_INVALID_PARAMETER;
889 if (!r->in.machine_password) {
890 r->in.machine_password = generate_random_password(mem_ctx,
891 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
892 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
893 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
896 /* Open the domain */
898 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
900 if (!NT_STATUS_IS_OK(status)) {
901 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
906 b = pipe_hnd->binding_handle;
908 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
909 if (!NT_STATUS_IS_OK(status)) {
910 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
915 status = dcerpc_samr_Connect2(b, mem_ctx,
917 SAMR_ACCESS_ENUM_DOMAINS
918 | SAMR_ACCESS_LOOKUP_DOMAIN,
921 if (!NT_STATUS_IS_OK(status)) {
924 if (!NT_STATUS_IS_OK(result)) {
929 status = dcerpc_samr_OpenDomain(b, mem_ctx,
931 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
932 | SAMR_DOMAIN_ACCESS_CREATE_USER
933 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
937 if (!NT_STATUS_IS_OK(status)) {
940 if (!NT_STATUS_IS_OK(result)) {
945 /* Create domain user */
947 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
948 if (!strlower_m(acct_name)) {
949 status = NT_STATUS_INVALID_PARAMETER;
953 init_lsa_String(&lsa_acct_name, acct_name);
955 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
956 uint32_t access_desired =
957 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
958 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
959 SAMR_USER_ACCESS_SET_PASSWORD |
960 SAMR_USER_ACCESS_GET_ATTRIBUTES |
961 SAMR_USER_ACCESS_SET_ATTRIBUTES;
962 uint32_t access_granted = 0;
964 DEBUG(10,("Creating account with desired access mask: %d\n",
967 status = dcerpc_samr_CreateUser2(b, mem_ctx,
976 if (!NT_STATUS_IS_OK(status)) {
981 if (!NT_STATUS_IS_OK(status) &&
982 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
984 DEBUG(10,("Creation of workstation account failed: %s\n",
987 /* If NT_STATUS_ACCESS_DENIED then we have a valid
988 username/password combo but the user does not have
989 administrator access. */
991 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
992 libnet_join_set_error_string(mem_ctx, r,
993 "User specified does not have "
994 "administrator privileges");
1000 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1001 if (!(r->in.join_flags &
1002 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1007 /* We *must* do this.... don't ask... */
1009 if (NT_STATUS_IS_OK(status)) {
1010 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1014 status = dcerpc_samr_LookupNames(b, mem_ctx,
1021 if (!NT_STATUS_IS_OK(status)) {
1024 if (!NT_STATUS_IS_OK(result)) {
1029 if (name_types.ids[0] != SID_NAME_USER) {
1030 DEBUG(0,("%s is not a user account (type=%d)\n",
1031 acct_name, name_types.ids[0]));
1032 status = NT_STATUS_INVALID_WORKSTATION;
1036 user_rid = user_rids.ids[0];
1038 /* Open handle on user */
1040 status = dcerpc_samr_OpenUser(b, mem_ctx,
1042 SEC_FLAG_MAXIMUM_ALLOWED,
1046 if (!NT_STATUS_IS_OK(status)) {
1049 if (!NT_STATUS_IS_OK(result)) {
1054 /* Fill in the additional account flags now */
1056 acct_flags |= ACB_PWNOEXP;
1058 /* Set account flags on machine account */
1059 ZERO_STRUCT(user_info.info16);
1060 user_info.info16.acct_flags = acct_flags;
1062 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1067 if (!NT_STATUS_IS_OK(status)) {
1068 dcerpc_samr_DeleteUser(b, mem_ctx,
1072 libnet_join_set_error_string(mem_ctx, r,
1073 "Failed to set account flags for machine account (%s)\n",
1078 if (!NT_STATUS_IS_OK(result)) {
1081 dcerpc_samr_DeleteUser(b, mem_ctx,
1085 libnet_join_set_error_string(mem_ctx, r,
1086 "Failed to set account flags for machine account (%s)\n",
1091 /* Set password on machine account - first try level 26 */
1094 * increase the timeout as password filter modules on the DC
1095 * might delay the operation for a significant amount of time
1097 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1099 init_samr_CryptPasswordEx(r->in.machine_password,
1103 user_info.info26.password = crypt_pwd_ex;
1104 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1106 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1112 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1114 /* retry with level 24 */
1116 init_samr_CryptPassword(r->in.machine_password,
1120 user_info.info24.password = crypt_pwd;
1121 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1123 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1130 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1132 if (!NT_STATUS_IS_OK(status)) {
1134 dcerpc_samr_DeleteUser(b, mem_ctx,
1138 libnet_join_set_error_string(mem_ctx, r,
1139 "Failed to set password for machine account (%s)\n",
1143 if (!NT_STATUS_IS_OK(result)) {
1146 dcerpc_samr_DeleteUser(b, mem_ctx,
1150 libnet_join_set_error_string(mem_ctx, r,
1151 "Failed to set password for machine account (%s)\n",
1156 status = NT_STATUS_OK;
1163 data_blob_clear_free(&session_key);
1165 if (is_valid_policy_hnd(&sam_pol)) {
1166 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1168 if (is_valid_policy_hnd(&domain_pol)) {
1169 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1171 if (is_valid_policy_hnd(&user_pol)) {
1172 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1174 TALLOC_FREE(pipe_hnd);
1179 /****************************************************************
1180 ****************************************************************/
1182 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1183 const char *machine_name,
1184 const char *dc_name,
1185 const bool use_kerberos)
1187 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1188 struct cli_state *cli = NULL;
1189 struct rpc_pipe_client *pipe_hnd = NULL;
1190 struct rpc_pipe_client *netlogon_pipe = NULL;
1192 char *machine_password = NULL;
1193 char *machine_account = NULL;
1197 return NT_STATUS_INVALID_PARAMETER;
1200 if (!secrets_init()) {
1201 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1204 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1206 if (!machine_password) {
1207 return NT_STATUS_NO_TRUST_LSA_SECRET;
1210 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1211 SAFE_FREE(machine_password);
1212 return NT_STATUS_NO_MEMORY;
1216 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1219 status = cli_full_connection(&cli, NULL,
1227 SMB_SIGNING_DEFAULT);
1228 free(machine_account);
1229 free(machine_password);
1231 if (!NT_STATUS_IS_OK(status)) {
1232 status = cli_full_connection(&cli, NULL,
1240 SMB_SIGNING_DEFAULT);
1243 if (!NT_STATUS_IS_OK(status)) {
1247 status = get_schannel_session_key(cli, netbios_domain_name,
1248 &neg_flags, &netlogon_pipe);
1249 if (!NT_STATUS_IS_OK(status)) {
1250 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1252 return NT_STATUS_OK;
1255 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1256 "key from server %s for domain %s. Error was %s\n",
1257 smbXcli_conn_remote_name(cli->conn),
1258 netbios_domain_name, nt_errstr(status)));
1263 if (!lp_client_schannel()) {
1265 return NT_STATUS_OK;
1268 status = cli_rpc_pipe_open_schannel_with_key(
1269 cli, &ndr_table_netlogon, NCACN_NP,
1270 DCERPC_AUTH_LEVEL_PRIVACY,
1271 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1275 if (!NT_STATUS_IS_OK(status)) {
1276 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1277 "on netlogon pipe to server %s for domain %s. "
1279 smbXcli_conn_remote_name(cli->conn),
1280 netbios_domain_name, nt_errstr(status)));
1284 return NT_STATUS_OK;
1287 /****************************************************************
1288 ****************************************************************/
1290 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1291 struct libnet_JoinCtx *r)
1295 status = libnet_join_ok(r->out.netbios_domain_name,
1298 r->in.use_kerberos);
1299 if (!NT_STATUS_IS_OK(status)) {
1300 libnet_join_set_error_string(mem_ctx, r,
1301 "failed to verify domain membership after joining: %s",
1302 get_friendly_nt_error_msg(status));
1303 return WERR_SETUP_NOT_JOINED;
1309 /****************************************************************
1310 ****************************************************************/
1312 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1313 struct libnet_UnjoinCtx *r)
1315 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1319 if (!secrets_delete_domain_sid(lp_workgroup())) {
1326 /****************************************************************
1327 ****************************************************************/
1329 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1330 struct libnet_UnjoinCtx *r)
1332 struct cli_state *cli = NULL;
1333 struct rpc_pipe_client *pipe_hnd = NULL;
1334 struct policy_handle sam_pol, domain_pol, user_pol;
1335 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1338 struct lsa_String lsa_acct_name;
1339 struct samr_Ids user_rids;
1340 struct samr_Ids name_types;
1341 union samr_UserInfo *info = NULL;
1342 struct dcerpc_binding_handle *b = NULL;
1344 ZERO_STRUCT(sam_pol);
1345 ZERO_STRUCT(domain_pol);
1346 ZERO_STRUCT(user_pol);
1348 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1349 r->in.admin_account,
1351 r->in.admin_password,
1354 if (!NT_STATUS_IS_OK(status)) {
1358 /* Open the domain */
1360 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1362 if (!NT_STATUS_IS_OK(status)) {
1363 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1364 nt_errstr(status)));
1368 b = pipe_hnd->binding_handle;
1370 status = dcerpc_samr_Connect2(b, mem_ctx,
1372 SEC_FLAG_MAXIMUM_ALLOWED,
1375 if (!NT_STATUS_IS_OK(status)) {
1378 if (!NT_STATUS_IS_OK(result)) {
1383 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1385 SEC_FLAG_MAXIMUM_ALLOWED,
1389 if (!NT_STATUS_IS_OK(status)) {
1392 if (!NT_STATUS_IS_OK(result)) {
1397 /* Create domain user */
1399 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1400 if (!strlower_m(acct_name)) {
1401 status = NT_STATUS_INVALID_PARAMETER;
1405 init_lsa_String(&lsa_acct_name, acct_name);
1407 status = dcerpc_samr_LookupNames(b, mem_ctx,
1415 if (!NT_STATUS_IS_OK(status)) {
1418 if (!NT_STATUS_IS_OK(result)) {
1423 if (name_types.ids[0] != SID_NAME_USER) {
1424 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1425 name_types.ids[0]));
1426 status = NT_STATUS_INVALID_WORKSTATION;
1430 user_rid = user_rids.ids[0];
1432 /* Open handle on user */
1434 status = dcerpc_samr_OpenUser(b, mem_ctx,
1436 SEC_FLAG_MAXIMUM_ALLOWED,
1440 if (!NT_STATUS_IS_OK(status)) {
1443 if (!NT_STATUS_IS_OK(result)) {
1450 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1455 if (!NT_STATUS_IS_OK(status)) {
1456 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1459 if (!NT_STATUS_IS_OK(result)) {
1461 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1465 /* now disable and setuser info */
1467 info->info16.acct_flags |= ACB_DISABLED;
1469 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1474 if (!NT_STATUS_IS_OK(status)) {
1475 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1478 if (!NT_STATUS_IS_OK(result)) {
1480 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1484 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1487 if (pipe_hnd && b) {
1488 if (is_valid_policy_hnd(&domain_pol)) {
1489 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1491 if (is_valid_policy_hnd(&sam_pol)) {
1492 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1494 TALLOC_FREE(pipe_hnd);
1504 /****************************************************************
1505 ****************************************************************/
1507 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1509 WERROR werr = WERR_OK;
1511 struct smbconf_ctx *ctx;
1513 err = smbconf_init_reg(r, &ctx, NULL);
1514 if (!SBC_ERROR_IS_OK(err)) {
1515 werr = WERR_NO_SUCH_SERVICE;
1519 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1521 err = smbconf_set_global_parameter(ctx, "security", "user");
1522 if (!SBC_ERROR_IS_OK(err)) {
1523 werr = WERR_NO_SUCH_SERVICE;
1527 err = smbconf_set_global_parameter(ctx, "workgroup",
1529 if (!SBC_ERROR_IS_OK(err)) {
1530 werr = WERR_NO_SUCH_SERVICE;
1534 smbconf_delete_global_parameter(ctx, "realm");
1538 err = smbconf_set_global_parameter(ctx, "security", "domain");
1539 if (!SBC_ERROR_IS_OK(err)) {
1540 werr = WERR_NO_SUCH_SERVICE;
1544 err = smbconf_set_global_parameter(ctx, "workgroup",
1545 r->out.netbios_domain_name);
1546 if (!SBC_ERROR_IS_OK(err)) {
1547 werr = WERR_NO_SUCH_SERVICE;
1551 if (r->out.domain_is_ad) {
1552 err = smbconf_set_global_parameter(ctx, "security", "ads");
1553 if (!SBC_ERROR_IS_OK(err)) {
1554 werr = WERR_NO_SUCH_SERVICE;
1558 err = smbconf_set_global_parameter(ctx, "realm",
1559 r->out.dns_domain_name);
1560 if (!SBC_ERROR_IS_OK(err)) {
1561 werr = WERR_NO_SUCH_SERVICE;
1567 smbconf_shutdown(ctx);
1571 /****************************************************************
1572 ****************************************************************/
1574 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1576 WERROR werr = WERR_OK;
1578 struct smbconf_ctx *ctx;
1580 err = smbconf_init_reg(r, &ctx, NULL);
1581 if (!SBC_ERROR_IS_OK(err)) {
1582 werr = WERR_NO_SUCH_SERVICE;
1586 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1588 err = smbconf_set_global_parameter(ctx, "security", "user");
1589 if (!SBC_ERROR_IS_OK(err)) {
1590 werr = WERR_NO_SUCH_SERVICE;
1594 err = smbconf_delete_global_parameter(ctx, "workgroup");
1595 if (!SBC_ERROR_IS_OK(err)) {
1596 werr = WERR_NO_SUCH_SERVICE;
1600 smbconf_delete_global_parameter(ctx, "realm");
1604 smbconf_shutdown(ctx);
1608 /****************************************************************
1609 ****************************************************************/
1611 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1615 if (!W_ERROR_IS_OK(r->out.result)) {
1616 return r->out.result;
1619 if (!r->in.modify_config) {
1623 werr = do_join_modify_vals_config(r);
1624 if (!W_ERROR_IS_OK(werr)) {
1628 lp_load_global(get_dyn_CONFIGFILE());
1630 r->out.modified_config = true;
1631 r->out.result = werr;
1636 /****************************************************************
1637 ****************************************************************/
1639 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1643 if (!W_ERROR_IS_OK(r->out.result)) {
1644 return r->out.result;
1647 if (!r->in.modify_config) {
1651 werr = do_unjoin_modify_vals_config(r);
1652 if (!W_ERROR_IS_OK(werr)) {
1656 lp_load_global(get_dyn_CONFIGFILE());
1658 r->out.modified_config = true;
1659 r->out.result = werr;
1664 /****************************************************************
1665 ****************************************************************/
1667 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1668 const char *domain_str,
1669 const char **domain_p,
1672 char *domain = NULL;
1674 const char *p = NULL;
1676 if (!domain_str || !domain_p || !dc_p) {
1680 p = strchr_m(domain_str, '\\');
1683 domain = talloc_strndup(mem_ctx, domain_str,
1684 PTR_DIFF(p, domain_str));
1685 dc = talloc_strdup(mem_ctx, p+1);
1690 domain = talloc_strdup(mem_ctx, domain_str);
1706 /****************************************************************
1707 ****************************************************************/
1709 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1710 struct libnet_JoinCtx *r)
1712 if (!r->in.domain_name) {
1713 libnet_join_set_error_string(mem_ctx, r,
1714 "No domain name defined");
1715 return WERR_INVALID_PARAM;
1718 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1721 libnet_join_set_error_string(mem_ctx, r,
1722 "Failed to parse domain name");
1723 return WERR_INVALID_PARAM;
1727 return WERR_SETUP_DOMAIN_CONTROLLER;
1730 if (!r->in.admin_domain) {
1731 char *admin_domain = NULL;
1732 char *admin_account = NULL;
1733 split_domain_user(mem_ctx,
1734 r->in.admin_account,
1737 r->in.admin_domain = admin_domain;
1738 r->in.admin_account = admin_account;
1741 if (!secrets_init()) {
1742 libnet_join_set_error_string(mem_ctx, r,
1743 "Unable to open secrets database");
1744 return WERR_CAN_NOT_COMPLETE;
1750 /****************************************************************
1751 ****************************************************************/
1753 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1757 /* Try adding dom admins to builtin\admins. Only log failures. */
1758 status = create_builtin_administrators(domain_sid);
1759 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1760 DEBUG(10,("Unable to auto-add domain administrators to "
1761 "BUILTIN\\Administrators during join because "
1762 "winbindd must be running.\n"));
1763 } else if (!NT_STATUS_IS_OK(status)) {
1764 DEBUG(5, ("Failed to auto-add domain administrators to "
1765 "BUILTIN\\Administrators during join: %s\n",
1766 nt_errstr(status)));
1769 /* Try adding dom users to builtin\users. Only log failures. */
1770 status = create_builtin_users(domain_sid);
1771 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1772 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1773 "during join because winbindd must be running.\n"));
1774 } else if (!NT_STATUS_IS_OK(status)) {
1775 DEBUG(5, ("Failed to auto-add domain administrators to "
1776 "BUILTIN\\Administrators during join: %s\n",
1777 nt_errstr(status)));
1781 /****************************************************************
1782 ****************************************************************/
1784 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1785 struct libnet_JoinCtx *r)
1789 if (!W_ERROR_IS_OK(r->out.result)) {
1790 return r->out.result;
1793 werr = do_JoinConfig(r);
1794 if (!W_ERROR_IS_OK(werr)) {
1798 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1802 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1803 if (r->out.dns_domain_name) {
1804 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1808 if (r->out.domain_is_ad &&
1809 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1810 ADS_STATUS ads_status;
1812 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1813 if (!ADS_ERR_OK(ads_status)) {
1814 return WERR_GENERAL_FAILURE;
1817 #endif /* HAVE_ADS */
1819 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1824 /****************************************************************
1825 ****************************************************************/
1827 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1830 ads_destroy(&r->in.ads);
1836 /****************************************************************
1837 ****************************************************************/
1839 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1842 ads_destroy(&r->in.ads);
1848 /****************************************************************
1849 ****************************************************************/
1851 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1852 struct libnet_JoinCtx **r)
1854 struct libnet_JoinCtx *ctx;
1856 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1861 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1863 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1864 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1866 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1873 /****************************************************************
1874 ****************************************************************/
1876 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1877 struct libnet_UnjoinCtx **r)
1879 struct libnet_UnjoinCtx *ctx;
1881 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1886 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1888 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1889 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1896 /****************************************************************
1897 ****************************************************************/
1899 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1900 struct libnet_JoinCtx *r)
1902 bool valid_security = false;
1903 bool valid_workgroup = false;
1904 bool valid_realm = false;
1906 /* check if configuration is already set correctly */
1908 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1910 switch (r->out.domain_is_ad) {
1912 valid_security = (lp_security() == SEC_DOMAIN);
1913 if (valid_workgroup && valid_security) {
1914 /* nothing to be done */
1919 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1920 switch (lp_security()) {
1923 valid_security = true;
1926 if (valid_workgroup && valid_realm && valid_security) {
1927 /* nothing to be done */
1933 /* check if we are supposed to manipulate configuration */
1935 if (!r->in.modify_config) {
1937 char *wrong_conf = talloc_strdup(mem_ctx, "");
1939 if (!valid_workgroup) {
1940 wrong_conf = talloc_asprintf_append(wrong_conf,
1941 "\"workgroup\" set to '%s', should be '%s'",
1942 lp_workgroup(), r->out.netbios_domain_name);
1943 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1947 wrong_conf = talloc_asprintf_append(wrong_conf,
1948 "\"realm\" set to '%s', should be '%s'",
1949 lp_realm(), r->out.dns_domain_name);
1950 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1953 if (!valid_security) {
1954 const char *sec = NULL;
1955 switch (lp_security()) {
1956 case SEC_USER: sec = "user"; break;
1957 case SEC_DOMAIN: sec = "domain"; break;
1958 case SEC_ADS: sec = "ads"; break;
1960 wrong_conf = talloc_asprintf_append(wrong_conf,
1961 "\"security\" set to '%s', should be %s",
1962 sec, r->out.domain_is_ad ?
1963 "either 'domain' or 'ads'" : "'domain'");
1964 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1967 libnet_join_set_error_string(mem_ctx, r,
1968 "Invalid configuration (%s) and configuration modification "
1969 "was not requested", wrong_conf);
1970 return WERR_CAN_NOT_COMPLETE;
1973 /* check if we are able to manipulate configuration */
1975 if (!lp_config_backend_is_registry()) {
1976 libnet_join_set_error_string(mem_ctx, r,
1977 "Configuration manipulation requested but not "
1978 "supported by backend");
1979 return WERR_NOT_SUPPORTED;
1985 /****************************************************************
1986 ****************************************************************/
1988 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1989 struct libnet_JoinCtx *r)
1993 struct cli_state *cli = NULL;
1995 ADS_STATUS ads_status;
1996 #endif /* HAVE_ADS */
1998 if (!r->in.dc_name) {
1999 struct netr_DsRGetDCNameInfo *info;
2001 status = dsgetdcname(mem_ctx,
2006 DS_FORCE_REDISCOVERY |
2007 DS_DIRECTORY_SERVICE_REQUIRED |
2008 DS_WRITABLE_REQUIRED |
2011 if (!NT_STATUS_IS_OK(status)) {
2012 libnet_join_set_error_string(mem_ctx, r,
2013 "failed to find DC for domain %s",
2015 get_friendly_nt_error_msg(status));
2016 return WERR_DCNOTFOUND;
2019 dc = strip_hostname(info->dc_unc);
2020 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2021 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2024 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2025 if (!NT_STATUS_IS_OK(status)) {
2026 libnet_join_set_error_string(mem_ctx, r,
2027 "failed to lookup DC info for domain '%s' over rpc: %s",
2028 r->in.domain_name, get_friendly_nt_error_msg(status));
2029 return ntstatus_to_werror(status);
2032 werr = libnet_join_check_config(mem_ctx, r);
2033 if (!W_ERROR_IS_OK(werr)) {
2039 create_local_private_krb5_conf_for_domain(
2040 r->out.dns_domain_name, r->out.netbios_domain_name,
2041 NULL, smbXcli_conn_remote_sockaddr(cli->conn),
2042 smbXcli_conn_remote_name(cli->conn));
2044 if (r->out.domain_is_ad && r->in.account_ou &&
2045 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2047 ads_status = libnet_join_connect_ads(mem_ctx, r);
2048 if (!ADS_ERR_OK(ads_status)) {
2049 return WERR_DEFAULT_JOIN_REQUIRED;
2052 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2053 if (!ADS_ERR_OK(ads_status)) {
2054 libnet_join_set_error_string(mem_ctx, r,
2055 "failed to precreate account in ou %s: %s",
2057 ads_errstr(ads_status));
2058 return WERR_DEFAULT_JOIN_REQUIRED;
2061 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2063 #endif /* HAVE_ADS */
2065 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2066 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2067 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2069 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2071 if (!NT_STATUS_IS_OK(status)) {
2072 libnet_join_set_error_string(mem_ctx, r,
2073 "failed to join domain '%s' over rpc: %s",
2074 r->in.domain_name, get_friendly_nt_error_msg(status));
2075 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2076 return WERR_SETUP_ALREADY_JOINED;
2078 werr = ntstatus_to_werror(status);
2082 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2083 werr = WERR_SETUP_NOT_JOINED;
2097 /****************************************************************
2098 ****************************************************************/
2100 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2101 struct libnet_JoinCtx *r)
2104 struct libnet_UnjoinCtx *u = NULL;
2106 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2107 if (!W_ERROR_IS_OK(werr)) {
2111 u->in.debug = r->in.debug;
2112 u->in.dc_name = r->in.dc_name;
2113 u->in.domain_name = r->in.domain_name;
2114 u->in.admin_account = r->in.admin_account;
2115 u->in.admin_password = r->in.admin_password;
2116 u->in.modify_config = r->in.modify_config;
2117 u->in.use_kerberos = r->in.use_kerberos;
2118 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2119 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2121 werr = libnet_Unjoin(mem_ctx, u);
2127 /****************************************************************
2128 ****************************************************************/
2130 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2131 struct libnet_JoinCtx *r)
2136 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2139 ZERO_STRUCT(r->out);
2141 werr = libnet_join_pre_processing(mem_ctx, r);
2142 if (!W_ERROR_IS_OK(werr)) {
2146 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2147 werr = libnet_DomainJoin(mem_ctx, r);
2148 if (!W_ERROR_IS_OK(werr)) {
2153 werr = libnet_join_post_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_join_post_verify(mem_ctx, r);
2160 if (!W_ERROR_IS_OK(werr)) {
2161 libnet_join_rollback(mem_ctx, r);
2166 r->out.result = werr;
2169 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2174 /****************************************************************
2175 ****************************************************************/
2177 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2178 struct libnet_UnjoinCtx *r)
2182 if (!r->in.domain_sid) {
2184 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2185 libnet_unjoin_set_error_string(mem_ctx, r,
2186 "Unable to fetch domain sid: are we joined?");
2187 return WERR_SETUP_NOT_JOINED;
2189 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2190 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2193 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2194 !r->in.delete_machine_account) {
2195 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2199 if (!r->in.dc_name) {
2200 struct netr_DsRGetDCNameInfo *info;
2202 status = dsgetdcname(mem_ctx,
2207 DS_DIRECTORY_SERVICE_REQUIRED |
2208 DS_WRITABLE_REQUIRED |
2211 if (!NT_STATUS_IS_OK(status)) {
2212 libnet_unjoin_set_error_string(mem_ctx, r,
2213 "failed to find DC for domain %s",
2215 get_friendly_nt_error_msg(status));
2216 return WERR_DCNOTFOUND;
2219 dc = strip_hostname(info->dc_unc);
2220 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2221 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2225 /* for net ads leave, try to delete the account. If it works,
2226 no sense in disabling. If it fails, we can still try to
2229 if (r->in.delete_machine_account) {
2230 ADS_STATUS ads_status;
2231 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2232 if (ADS_ERR_OK(ads_status)) {
2234 r->out.dns_domain_name =
2235 talloc_strdup(mem_ctx,
2236 r->in.ads->server.realm);
2238 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2240 if (!ADS_ERR_OK(ads_status)) {
2241 libnet_unjoin_set_error_string(mem_ctx, r,
2242 "failed to remove machine account from AD: %s",
2243 ads_errstr(ads_status));
2245 r->out.deleted_machine_account = true;
2246 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2247 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2251 #endif /* HAVE_ADS */
2253 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2255 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2256 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2257 if (!NT_STATUS_IS_OK(status)) {
2258 libnet_unjoin_set_error_string(mem_ctx, r,
2259 "failed to disable machine account via rpc: %s",
2260 get_friendly_nt_error_msg(status));
2261 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2262 return WERR_SETUP_NOT_JOINED;
2264 return ntstatus_to_werror(status);
2267 r->out.disabled_machine_account = true;
2270 /* If disable succeeded or was not requested at all, we
2271 should be getting rid of our end of things */
2273 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2278 /****************************************************************
2279 ****************************************************************/
2281 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2282 struct libnet_UnjoinCtx *r)
2284 if (!r->in.domain_name) {
2285 libnet_unjoin_set_error_string(mem_ctx, r,
2286 "No domain name defined");
2287 return WERR_INVALID_PARAM;
2290 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2293 libnet_unjoin_set_error_string(mem_ctx, r,
2294 "Failed to parse domain name");
2295 return WERR_INVALID_PARAM;
2299 return WERR_SETUP_DOMAIN_CONTROLLER;
2302 if (!r->in.admin_domain) {
2303 char *admin_domain = NULL;
2304 char *admin_account = NULL;
2305 split_domain_user(mem_ctx,
2306 r->in.admin_account,
2309 r->in.admin_domain = admin_domain;
2310 r->in.admin_account = admin_account;
2313 if (!secrets_init()) {
2314 libnet_unjoin_set_error_string(mem_ctx, r,
2315 "Unable to open secrets database");
2316 return WERR_CAN_NOT_COMPLETE;
2322 /****************************************************************
2323 ****************************************************************/
2325 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2326 struct libnet_UnjoinCtx *r)
2328 saf_delete(r->out.netbios_domain_name);
2329 saf_delete(r->out.dns_domain_name);
2331 return libnet_unjoin_config(r);
2334 /****************************************************************
2335 ****************************************************************/
2337 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2338 struct libnet_UnjoinCtx *r)
2343 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2346 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2347 if (!W_ERROR_IS_OK(werr)) {
2351 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2352 werr = libnet_DomainUnjoin(mem_ctx, r);
2353 if (!W_ERROR_IS_OK(werr)) {
2354 libnet_unjoin_config(r);
2359 werr = libnet_unjoin_post_processing(mem_ctx, r);
2360 if (!W_ERROR_IS_OK(werr)) {
2365 r->out.result = werr;
2368 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);