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 "libsmb/namequery.h"
24 #include "librpc/gen_ndr/ndr_libnet_join.h"
25 #include "libnet/libnet_join.h"
26 #include "libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/init_samr.h"
29 #include "../librpc/gen_ndr/ndr_lsa_c.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../librpc/gen_ndr/ndr_netlogon.h"
32 #include "rpc_client/cli_netlogon.h"
33 #include "lib/smbconf/smbconf.h"
34 #include "lib/smbconf/smbconf_reg.h"
35 #include "../libds/common/flags.h"
37 #include "rpc_client/init_lsa.h"
38 #include "rpc_client/cli_pipe.h"
39 #include "../libcli/security/security.h"
41 #include "libsmb/libsmb.h"
42 #include "../libcli/smb/smbXcli_base.h"
43 #include "lib/param/loadparm.h"
44 #include "libcli/auth/netlogon_creds_cli.h"
45 #include "auth/credentials/credentials.h"
47 #include "libsmb/dsgetdcname.h"
49 /****************************************************************
50 ****************************************************************/
52 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
55 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
56 DEBUG(1,("libnet_Join:\n%s", str)); \
60 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
61 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
62 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
63 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
65 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
68 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
69 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
73 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
74 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
75 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
76 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
78 /****************************************************************
79 ****************************************************************/
81 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
82 struct libnet_JoinCtx
*r
,
83 const char *format
, ...)
84 PRINTF_ATTRIBUTE(3,4);
86 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
87 struct libnet_JoinCtx
*r
,
88 const char *format
, ...)
92 if (r
->out
.error_string
) {
96 va_start(args
, format
);
97 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
101 /****************************************************************
102 ****************************************************************/
104 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
105 struct libnet_UnjoinCtx
*r
,
106 const char *format
, ...)
107 PRINTF_ATTRIBUTE(3,4);
109 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
110 struct libnet_UnjoinCtx
*r
,
111 const char *format
, ...)
115 if (r
->out
.error_string
) {
119 va_start(args
, format
);
120 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
126 /****************************************************************
127 ****************************************************************/
129 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
130 const char *netbios_domain_name
,
132 const char *user_name
,
133 const char *password
,
138 ADS_STRUCT
*my_ads
= NULL
;
141 my_ads
= ads_init(dns_domain_name
,
145 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
149 SAFE_FREE(my_ads
->auth
.user_name
);
150 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
151 if ((cp
= strchr_m(my_ads
->auth
.user_name
, '@'))!=0) {
153 SAFE_FREE(my_ads
->auth
.realm
);
154 my_ads
->auth
.realm
= smb_xstrdup(cp
);
155 if (!strupper_m(my_ads
->auth
.realm
)) {
156 ads_destroy(&my_ads
);
157 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
163 SAFE_FREE(my_ads
->auth
.password
);
164 my_ads
->auth
.password
= SMB_STRDUP(password
);
167 if (ccname
!= NULL
) {
168 SAFE_FREE(my_ads
->auth
.ccache_name
);
169 my_ads
->auth
.ccache_name
= SMB_STRDUP(ccname
);
170 setenv(KRB5_ENV_CCNAME
, my_ads
->auth
.ccache_name
, 1);
173 status
= ads_connect_user_creds(my_ads
);
174 if (!ADS_ERR_OK(status
)) {
175 ads_destroy(&my_ads
);
183 /****************************************************************
184 ****************************************************************/
186 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
187 struct libnet_JoinCtx
*r
,
188 bool use_machine_creds
)
191 const char *username
;
192 const char *password
;
193 const char *ccname
= NULL
;
195 if (use_machine_creds
) {
196 if (r
->in
.machine_name
== NULL
||
197 r
->in
.machine_password
== NULL
) {
198 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
200 username
= talloc_asprintf(mem_ctx
, "%s$",
202 if (username
== NULL
) {
203 return ADS_ERROR(LDAP_NO_MEMORY
);
205 password
= r
->in
.machine_password
;
206 ccname
= "MEMORY:libnet_join_machine_creds";
208 username
= r
->in
.admin_account
;
209 password
= r
->in
.admin_password
;
212 * when r->in.use_kerberos is set to allow "net ads join -k" we
213 * may not override the provided credential cache - gd
216 if (!r
->in
.use_kerberos
) {
217 ccname
= "MEMORY:libnet_join_user_creds";
221 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
222 r
->out
.netbios_domain_name
,
228 if (!ADS_ERR_OK(status
)) {
229 libnet_join_set_error_string(mem_ctx
, r
,
230 "failed to connect to AD: %s",
235 if (!r
->out
.netbios_domain_name
) {
236 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
237 r
->in
.ads
->server
.workgroup
);
238 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
241 if (!r
->out
.dns_domain_name
) {
242 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
243 r
->in
.ads
->config
.realm
);
244 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
247 r
->out
.domain_is_ad
= true;
252 /****************************************************************
253 ****************************************************************/
255 static ADS_STATUS
libnet_join_connect_ads_user(TALLOC_CTX
*mem_ctx
,
256 struct libnet_JoinCtx
*r
)
258 return libnet_join_connect_ads(mem_ctx
, r
, false);
261 /****************************************************************
262 ****************************************************************/
264 static ADS_STATUS
libnet_join_connect_ads_machine(TALLOC_CTX
*mem_ctx
,
265 struct libnet_JoinCtx
*r
)
267 return libnet_join_connect_ads(mem_ctx
, r
, true);
270 /****************************************************************
271 ****************************************************************/
273 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
274 struct libnet_UnjoinCtx
*r
)
278 status
= libnet_connect_ads(r
->in
.domain_name
,
282 r
->in
.admin_password
,
285 if (!ADS_ERR_OK(status
)) {
286 libnet_unjoin_set_error_string(mem_ctx
, r
,
287 "failed to connect to AD: %s",
294 /****************************************************************
295 join a domain using ADS (LDAP mods)
296 ****************************************************************/
298 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
299 struct libnet_JoinCtx
*r
)
302 LDAPMessage
*res
= NULL
;
303 const char *attrs
[] = { "dn", NULL
};
306 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
307 if (!ADS_ERR_OK(status
)) {
311 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
312 if (!ADS_ERR_OK(status
)) {
316 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
317 ads_msgfree(r
->in
.ads
, res
);
318 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
321 ads_msgfree(r
->in
.ads
, res
);
323 /* Attempt to create the machine account and bail if this fails.
324 Assume that the admin wants exactly what they requested */
326 status
= ads_create_machine_acct(r
->in
.ads
,
329 r
->in
.desired_encryption_types
);
331 if (ADS_ERR_OK(status
)) {
332 DEBUG(1,("machine account creation created\n"));
334 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
335 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
336 status
= ADS_SUCCESS
;
339 if (!ADS_ERR_OK(status
)) {
340 DEBUG(1,("machine account creation failed\n"));
344 status
= ads_move_machine_acct(r
->in
.ads
,
348 if (!ADS_ERR_OK(status
)) {
349 DEBUG(1,("failure to locate/move pre-existing "
350 "machine account\n"));
354 DEBUG(1,("The machine account %s the specified OU.\n",
355 moved
? "was moved into" : "already exists in"));
360 /****************************************************************
361 ****************************************************************/
363 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
364 struct libnet_UnjoinCtx
*r
)
369 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
370 if (!ADS_ERR_OK(status
)) {
371 libnet_unjoin_set_error_string(mem_ctx
, r
,
372 "failed to connect to AD: %s",
378 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
379 if (!ADS_ERR_OK(status
)) {
380 libnet_unjoin_set_error_string(mem_ctx
, r
,
381 "failed to leave realm: %s",
389 /****************************************************************
390 ****************************************************************/
392 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
393 struct libnet_JoinCtx
*r
)
396 LDAPMessage
*res
= NULL
;
399 if (!r
->in
.machine_name
) {
400 return ADS_ERROR(LDAP_NO_MEMORY
);
403 status
= ads_find_machine_acct(r
->in
.ads
,
406 if (!ADS_ERR_OK(status
)) {
410 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
411 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
415 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
417 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
421 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
423 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
427 if (!ads_pull_uint32(r
->in
.ads
, res
, "msDS-SupportedEncryptionTypes",
428 &r
->out
.set_encryption_types
)) {
429 r
->out
.set_encryption_types
= 0;
433 ads_msgfree(r
->in
.ads
, res
);
439 static ADS_STATUS
libnet_join_get_machine_spns(TALLOC_CTX
*mem_ctx
,
440 struct libnet_JoinCtx
*r
,
446 if (r
->in
.machine_name
== NULL
) {
447 return ADS_ERROR_SYSTEM(EINVAL
);
450 status
= ads_get_service_principal_names(mem_ctx
,
459 /****************************************************************
460 Set a machines dNSHostName and servicePrincipalName attributes
461 ****************************************************************/
463 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
464 struct libnet_JoinCtx
*r
)
469 const char **spn_array
= NULL
;
473 const char **netbios_aliases
= NULL
;
477 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
478 if (!ADS_ERR_OK(status
)) {
482 status
= libnet_join_get_machine_spns(mem_ctx
,
484 discard_const_p(char **, &spn_array
),
486 if (!ADS_ERR_OK(status
)) {
487 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
490 /* Windows only creates HOST/shortname & HOST/fqdn. */
492 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
494 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
496 if (!strupper_m(spn
)) {
497 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
500 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
502 ok
= add_string_to_array(spn_array
, spn
,
503 &spn_array
, &num_spns
);
505 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
509 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
510 || (strchr(my_fqdn
, '.') == NULL
)) {
511 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
512 r
->out
.dns_domain_name
);
515 if (!strlower_m(my_fqdn
)) {
516 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
519 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
520 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
522 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
525 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
527 ok
= add_string_to_array(spn_array
, spn
,
528 &spn_array
, &num_spns
);
530 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
535 netbios_aliases
= lp_netbios_aliases();
536 if (netbios_aliases
!= NULL
) {
537 for (; *netbios_aliases
!= NULL
; netbios_aliases
++) {
539 * Add HOST/NETBIOSNAME
541 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", *netbios_aliases
);
544 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
546 if (!strupper_m(spn
)) {
548 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
551 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
556 ok
= add_string_to_array(spn_array
, spn
,
557 &spn_array
, &num_spns
);
560 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
565 * Add HOST/netbiosname.domainname
567 if (r
->out
.dns_domain_name
== NULL
) {
570 fstr_sprintf(my_fqdn
, "%s.%s",
572 r
->out
.dns_domain_name
);
574 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
576 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
579 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
584 ok
= add_string_to_array(spn_array
, spn
,
585 &spn_array
, &num_spns
);
588 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
594 /* make sure to NULL terminate the array */
595 spn_array
= talloc_realloc(mem_ctx
, spn_array
, const char *, num_spns
+ 1);
596 if (spn_array
== NULL
) {
597 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
599 spn_array
[num_spns
] = NULL
;
601 mods
= ads_init_mods(mem_ctx
);
603 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
606 /* fields of primary importance */
608 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
609 if (!ADS_ERR_OK(status
)) {
610 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
613 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
615 if (!ADS_ERR_OK(status
)) {
616 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
619 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
622 /****************************************************************
623 ****************************************************************/
625 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
626 struct libnet_JoinCtx
*r
)
631 if (!r
->in
.create_upn
) {
637 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
638 if (!ADS_ERR_OK(status
)) {
643 const char *realm
= r
->out
.dns_domain_name
;
645 /* in case we are about to generate a keytab during the join
646 * make sure the default upn we create is usable with kinit -k.
649 if (USE_KERBEROS_KEYTAB
) {
650 realm
= talloc_strdup_upper(mem_ctx
,
651 r
->out
.dns_domain_name
);
655 return ADS_ERROR(LDAP_NO_MEMORY
);
658 r
->in
.upn
= talloc_asprintf(mem_ctx
,
663 return ADS_ERROR(LDAP_NO_MEMORY
);
667 /* now do the mods */
669 mods
= ads_init_mods(mem_ctx
);
671 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
674 /* fields of primary importance */
676 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
677 if (!ADS_ERR_OK(status
)) {
678 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
681 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
685 /****************************************************************
686 ****************************************************************/
688 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
689 struct libnet_JoinCtx
*r
)
695 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
701 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
702 if (!ADS_ERR_OK(status
)) {
706 /* now do the mods */
708 mods
= ads_init_mods(mem_ctx
);
710 return ADS_ERROR(LDAP_NO_MEMORY
);
713 if (r
->in
.os_servicepack
) {
715 * if blank string then leave os_sp equal to NULL to force
716 * attribute delete (LDAP_MOD_DELETE)
718 if (!strequal(r
->in
.os_servicepack
,"")) {
719 os_sp
= talloc_strdup(mem_ctx
, r
->in
.os_servicepack
);
722 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s",
723 samba_version_string());
725 if (!os_sp
&& !strequal(r
->in
.os_servicepack
,"")) {
726 return ADS_ERROR(LDAP_NO_MEMORY
);
729 /* fields of primary importance */
731 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
733 if (!ADS_ERR_OK(status
)) {
737 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
739 if (!ADS_ERR_OK(status
)) {
743 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
745 if (!ADS_ERR_OK(status
)) {
749 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
752 /****************************************************************
753 ****************************************************************/
755 static ADS_STATUS
libnet_join_set_etypes(TALLOC_CTX
*mem_ctx
,
756 struct libnet_JoinCtx
*r
)
760 const char *etype_list_str
;
762 etype_list_str
= talloc_asprintf(mem_ctx
, "%d",
763 r
->in
.desired_encryption_types
);
764 if (!etype_list_str
) {
765 return ADS_ERROR(LDAP_NO_MEMORY
);
770 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
771 if (!ADS_ERR_OK(status
)) {
775 if (r
->in
.desired_encryption_types
== r
->out
.set_encryption_types
) {
779 /* now do the mods */
781 mods
= ads_init_mods(mem_ctx
);
783 return ADS_ERROR(LDAP_NO_MEMORY
);
786 status
= ads_mod_str(mem_ctx
, &mods
, "msDS-SupportedEncryptionTypes",
788 if (!ADS_ERR_OK(status
)) {
792 status
= ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
793 if (!ADS_ERR_OK(status
)) {
797 r
->out
.set_encryption_types
= r
->in
.desired_encryption_types
;
802 /****************************************************************
803 ****************************************************************/
805 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
806 struct libnet_JoinCtx
*r
)
808 if (!USE_SYSTEM_KEYTAB
) {
812 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
819 /****************************************************************
820 ****************************************************************/
822 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
823 struct libnet_JoinCtx
*r
)
825 uint32_t domain_func
;
827 const char *salt
= NULL
;
828 char *std_salt
= NULL
;
830 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
831 if (!ADS_ERR_OK(status
)) {
832 libnet_join_set_error_string(mem_ctx
, r
,
833 "failed to determine domain functional level: %s",
838 /* go ahead and setup the default salt */
840 std_salt
= kerberos_standard_des_salt();
842 libnet_join_set_error_string(mem_ctx
, r
,
843 "failed to obtain standard DES salt");
847 salt
= talloc_strdup(mem_ctx
, std_salt
);
854 /* if it's a Windows functional domain, we have to look for the UPN */
856 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
859 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
862 salt
= talloc_strdup(mem_ctx
, upn
);
869 r
->out
.krb5_salt
= salt
;
873 /****************************************************************
874 ****************************************************************/
876 static ADS_STATUS
libnet_join_post_processing_ads_modify(TALLOC_CTX
*mem_ctx
,
877 struct libnet_JoinCtx
*r
)
880 bool need_etype_update
= false;
883 status
= libnet_join_connect_ads_user(mem_ctx
, r
);
884 if (!ADS_ERR_OK(status
)) {
889 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
890 if (!ADS_ERR_OK(status
)) {
891 libnet_join_set_error_string(mem_ctx
, r
,
892 "Failed to set machine spn: %s\n"
893 "Do you have sufficient permissions to create machine "
899 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
900 if (!ADS_ERR_OK(status
)) {
901 libnet_join_set_error_string(mem_ctx
, r
,
902 "failed to set machine os attributes: %s",
907 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
908 if (!ADS_ERR_OK(status
)) {
909 libnet_join_set_error_string(mem_ctx
, r
,
910 "failed to set machine upn: %s",
915 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
916 if (!ADS_ERR_OK(status
)) {
920 if (r
->in
.desired_encryption_types
!= r
->out
.set_encryption_types
) {
921 uint32_t func_level
= 0;
923 status
= ads_domain_func_level(r
->in
.ads
, &func_level
);
924 if (!ADS_ERR_OK(status
)) {
925 libnet_join_set_error_string(mem_ctx
, r
,
926 "failed to query domain controller functional level: %s",
931 if (func_level
>= DS_DOMAIN_FUNCTION_2008
) {
932 need_etype_update
= true;
936 if (need_etype_update
) {
938 * We need to reconnect as machine account in order
939 * to update msDS-SupportedEncryptionTypes reliable
942 if (r
->in
.ads
->auth
.ccache_name
!= NULL
) {
943 ads_kdestroy(r
->in
.ads
->auth
.ccache_name
);
946 ads_destroy(&r
->in
.ads
);
948 status
= libnet_join_connect_ads_machine(mem_ctx
, r
);
949 if (!ADS_ERR_OK(status
)) {
950 libnet_join_set_error_string(mem_ctx
, r
,
951 "Failed to connect as machine account: %s",
956 status
= libnet_join_set_etypes(mem_ctx
, r
);
957 if (!ADS_ERR_OK(status
)) {
958 libnet_join_set_error_string(mem_ctx
, r
,
959 "failed to set machine kerberos encryption types: %s",
965 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
966 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
972 static ADS_STATUS
libnet_join_post_processing_ads_sync(TALLOC_CTX
*mem_ctx
,
973 struct libnet_JoinCtx
*r
)
975 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
976 libnet_join_set_error_string(mem_ctx
, r
,
977 "failed to create kerberos keytab");
978 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
983 #endif /* HAVE_ADS */
985 /****************************************************************
986 Store the machine password and domain SID
987 ****************************************************************/
989 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
990 struct libnet_JoinCtx
*r
)
994 status
= secrets_store_JoinCtx(r
);
995 if (!NT_STATUS_IS_OK(status
)) {
996 DBG_ERR("secrets_store_JoinCtx() failed %s\n",
1004 /****************************************************************
1005 Connect dc's IPC$ share
1006 ****************************************************************/
1008 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
1013 struct cli_state
**cli
)
1018 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
1021 if (use_kerberos
&& pass
) {
1022 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
1025 return cli_full_connection(cli
, NULL
,
1033 SMB_SIGNING_IPC_DEFAULT
);
1036 /****************************************************************
1037 Lookup domain dc's info
1038 ****************************************************************/
1040 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
1041 struct libnet_JoinCtx
*r
,
1042 struct cli_state
**cli
)
1044 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1045 struct policy_handle lsa_pol
;
1046 NTSTATUS status
, result
;
1047 union lsa_PolicyInformation
*info
= NULL
;
1048 struct dcerpc_binding_handle
*b
;
1049 const char *account
= r
->in
.admin_account
;
1050 const char *domain
= r
->in
.admin_domain
;
1051 const char *password
= r
->in
.admin_password
;
1052 bool use_kerberos
= r
->in
.use_kerberos
;
1054 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) {
1058 use_kerberos
= false;
1061 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1067 if (!NT_STATUS_IS_OK(status
)) {
1071 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
,
1073 if (!NT_STATUS_IS_OK(status
)) {
1074 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
1075 nt_errstr(status
)));
1079 b
= pipe_hnd
->binding_handle
;
1081 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
1082 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1083 if (!NT_STATUS_IS_OK(status
)) {
1087 status
= dcerpc_lsa_QueryInfoPolicy2(b
, mem_ctx
,
1089 LSA_POLICY_INFO_DNS
,
1092 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
1093 r
->out
.domain_is_ad
= true;
1094 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
1095 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
1096 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
1097 r
->out
.domain_guid
= info
->dns
.domain_guid
;
1098 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
1099 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
1102 if (!NT_STATUS_IS_OK(status
)) {
1103 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
1105 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
1108 if (!NT_STATUS_IS_OK(status
)) {
1111 if (!NT_STATUS_IS_OK(result
)) {
1116 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
1117 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
1118 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
1121 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
1122 TALLOC_FREE(pipe_hnd
);
1128 /****************************************************************
1129 Do the domain join unsecure
1130 ****************************************************************/
1132 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
1133 struct libnet_JoinCtx
*r
,
1134 struct cli_state
*cli
)
1136 TALLOC_CTX
*frame
= talloc_stackframe();
1137 struct rpc_pipe_client
*authenticate_pipe
= NULL
;
1138 struct rpc_pipe_client
*passwordset_pipe
= NULL
;
1139 struct cli_credentials
*cli_creds
;
1140 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
1141 struct netlogon_creds_CredentialState
*creds
= NULL
;
1142 uint32_t netlogon_flags
= 0;
1145 DATA_BLOB new_trust_blob
= data_blob_null
;
1148 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
,
1149 &authenticate_pipe
);
1150 if (!NT_STATUS_IS_OK(status
)) {
1155 if (!r
->in
.machine_password
) {
1156 int security
= r
->in
.ads
? SEC_ADS
: SEC_DOMAIN
;
1158 r
->in
.machine_password
= trust_pw_new_value(mem_ctx
,
1159 r
->in
.secure_channel_type
,
1161 if (r
->in
.machine_password
== NULL
) {
1163 return NT_STATUS_NO_MEMORY
;
1167 cli_creds
= cli_credentials_init(talloc_tos());
1168 if (cli_creds
== NULL
) {
1170 return NT_STATUS_NO_MEMORY
;
1173 cli_credentials_set_username(cli_creds
, r
->out
.account_name
,
1175 cli_credentials_set_domain(cli_creds
, r
->in
.domain_name
,
1177 cli_credentials_set_realm(cli_creds
, "", CRED_SPECIFIED
);
1178 cli_credentials_set_secure_channel_type(cli_creds
,
1179 r
->in
.secure_channel_type
);
1181 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
1182 cli_credentials_set_password(cli_creds
, r
->in
.admin_password
,
1185 status
= rpccli_create_netlogon_creds_ctx(
1186 cli_creds
, authenticate_pipe
->desthost
, r
->in
.msg_ctx
,
1187 frame
, &netlogon_creds
);
1188 if (!NT_STATUS_IS_OK(status
)) {
1193 status
= rpccli_setup_netlogon_creds(
1194 cli
, NCACN_NP
, netlogon_creds
, true /* force_reauth */,
1196 if (!NT_STATUS_IS_OK(status
)) {
1201 status
= netlogon_creds_cli_get(netlogon_creds
, frame
, &creds
);
1202 if (!NT_STATUS_IS_OK(status
)) {
1207 netlogon_flags
= creds
->negotiate_flags
;
1210 if (netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
) {
1211 status
= cli_rpc_pipe_open_schannel_with_creds(cli
,
1212 &ndr_table_netlogon
,
1216 if (!NT_STATUS_IS_OK(status
)) {
1221 passwordset_pipe
= authenticate_pipe
;
1224 len
= strlen(r
->in
.machine_password
);
1225 ok
= convert_string_talloc(frame
, CH_UNIX
, CH_UTF16
,
1226 r
->in
.machine_password
, len
,
1227 (void **)&new_trust_blob
.data
,
1228 &new_trust_blob
.length
);
1230 status
= NT_STATUS_UNMAPPABLE_CHARACTER
;
1231 if (errno
== ENOMEM
) {
1232 status
= NT_STATUS_NO_MEMORY
;
1238 status
= netlogon_creds_cli_ServerPasswordSet(netlogon_creds
,
1239 passwordset_pipe
->binding_handle
,
1241 NULL
); /* new_version */
1242 if (!NT_STATUS_IS_OK(status
)) {
1248 return NT_STATUS_OK
;
1251 /****************************************************************
1253 ****************************************************************/
1255 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
1256 struct libnet_JoinCtx
*r
,
1257 struct cli_state
*cli
)
1259 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1260 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1261 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1263 struct lsa_String lsa_acct_name
;
1265 uint32_t acct_flags
= ACB_WSTRUST
;
1266 struct samr_Ids user_rids
;
1267 struct samr_Ids name_types
;
1268 union samr_UserInfo user_info
;
1269 struct dcerpc_binding_handle
*b
= NULL
;
1270 unsigned int old_timeout
= 0;
1272 DATA_BLOB session_key
= data_blob_null
;
1273 struct samr_CryptPassword crypt_pwd
;
1274 struct samr_CryptPasswordEx crypt_pwd_ex
;
1276 ZERO_STRUCT(sam_pol
);
1277 ZERO_STRUCT(domain_pol
);
1278 ZERO_STRUCT(user_pol
);
1280 switch (r
->in
.secure_channel_type
) {
1281 case SEC_CHAN_WKSTA
:
1282 acct_flags
= ACB_WSTRUST
;
1285 acct_flags
= ACB_SVRTRUST
;
1288 return NT_STATUS_INVALID_PARAMETER
;
1291 if (!r
->in
.machine_password
) {
1292 int security
= r
->in
.ads
? SEC_ADS
: SEC_DOMAIN
;
1294 r
->in
.machine_password
= trust_pw_new_value(mem_ctx
,
1295 r
->in
.secure_channel_type
,
1297 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
1300 /* Open the domain */
1302 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
1304 if (!NT_STATUS_IS_OK(status
)) {
1305 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1306 nt_errstr(status
)));
1310 b
= pipe_hnd
->binding_handle
;
1312 status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
1313 if (!NT_STATUS_IS_OK(status
)) {
1314 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1315 nt_errstr(status
)));
1319 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1321 SAMR_ACCESS_ENUM_DOMAINS
1322 | SAMR_ACCESS_LOOKUP_DOMAIN
,
1325 if (!NT_STATUS_IS_OK(status
)) {
1328 if (!NT_STATUS_IS_OK(result
)) {
1333 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1335 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1336 | SAMR_DOMAIN_ACCESS_CREATE_USER
1337 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
1341 if (!NT_STATUS_IS_OK(status
)) {
1344 if (!NT_STATUS_IS_OK(result
)) {
1349 /* Create domain user */
1351 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1352 if (!strlower_m(acct_name
)) {
1353 status
= NT_STATUS_INVALID_PARAMETER
;
1357 init_lsa_String(&lsa_acct_name
, acct_name
);
1359 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
1360 uint32_t access_desired
=
1361 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
1362 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
1363 SAMR_USER_ACCESS_SET_PASSWORD
|
1364 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
1365 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
1366 uint32_t access_granted
= 0;
1368 DEBUG(10,("Creating account with desired access mask: %d\n",
1371 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
1380 if (!NT_STATUS_IS_OK(status
)) {
1385 if (!NT_STATUS_IS_OK(status
) &&
1386 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1388 DEBUG(10,("Creation of workstation account failed: %s\n",
1389 nt_errstr(status
)));
1391 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1392 username/password combo but the user does not have
1393 administrator access. */
1395 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
1396 libnet_join_set_error_string(mem_ctx
, r
,
1397 "User specified does not have "
1398 "administrator privileges");
1404 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1405 if (!(r
->in
.join_flags
&
1406 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
1411 /* We *must* do this.... don't ask... */
1413 if (NT_STATUS_IS_OK(status
)) {
1414 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1418 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1425 if (!NT_STATUS_IS_OK(status
)) {
1428 if (!NT_STATUS_IS_OK(result
)) {
1432 if (user_rids
.count
!= 1) {
1433 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1436 if (name_types
.count
!= 1) {
1437 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1441 if (name_types
.ids
[0] != SID_NAME_USER
) {
1442 DEBUG(0,("%s is not a user account (type=%d)\n",
1443 acct_name
, name_types
.ids
[0]));
1444 status
= NT_STATUS_INVALID_WORKSTATION
;
1448 user_rid
= user_rids
.ids
[0];
1450 /* Open handle on user */
1452 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1454 SEC_FLAG_MAXIMUM_ALLOWED
,
1458 if (!NT_STATUS_IS_OK(status
)) {
1461 if (!NT_STATUS_IS_OK(result
)) {
1466 /* Fill in the additional account flags now */
1468 acct_flags
|= ACB_PWNOEXP
;
1470 /* Set account flags on machine account */
1471 ZERO_STRUCT(user_info
.info16
);
1472 user_info
.info16
.acct_flags
= acct_flags
;
1474 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1476 UserControlInformation
,
1479 if (!NT_STATUS_IS_OK(status
)) {
1480 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1484 libnet_join_set_error_string(mem_ctx
, r
,
1485 "Failed to set account flags for machine account (%s)\n",
1490 if (!NT_STATUS_IS_OK(result
)) {
1493 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1497 libnet_join_set_error_string(mem_ctx
, r
,
1498 "Failed to set account flags for machine account (%s)\n",
1503 /* Set password on machine account - first try level 26 */
1506 * increase the timeout as password filter modules on the DC
1507 * might delay the operation for a significant amount of time
1509 old_timeout
= rpccli_set_timeout(pipe_hnd
, 600000);
1511 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1515 user_info
.info26
.password
= crypt_pwd_ex
;
1516 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1518 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1520 UserInternal5InformationNew
,
1524 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
1526 /* retry with level 24 */
1528 init_samr_CryptPassword(r
->in
.machine_password
,
1532 user_info
.info24
.password
= crypt_pwd
;
1533 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1535 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1537 UserInternal5Information
,
1542 old_timeout
= rpccli_set_timeout(pipe_hnd
, old_timeout
);
1544 if (!NT_STATUS_IS_OK(status
)) {
1546 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1550 libnet_join_set_error_string(mem_ctx
, r
,
1551 "Failed to set password for machine account (%s)\n",
1555 if (!NT_STATUS_IS_OK(result
)) {
1558 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1562 libnet_join_set_error_string(mem_ctx
, r
,
1563 "Failed to set password for machine account (%s)\n",
1568 status
= NT_STATUS_OK
;
1575 data_blob_clear_free(&session_key
);
1577 if (is_valid_policy_hnd(&sam_pol
)) {
1578 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1580 if (is_valid_policy_hnd(&domain_pol
)) {
1581 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1583 if (is_valid_policy_hnd(&user_pol
)) {
1584 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1586 TALLOC_FREE(pipe_hnd
);
1591 /****************************************************************
1592 ****************************************************************/
1594 NTSTATUS
libnet_join_ok(struct messaging_context
*msg_ctx
,
1595 const char *netbios_domain_name
,
1596 const char *dc_name
,
1597 const bool use_kerberos
)
1599 TALLOC_CTX
*frame
= talloc_stackframe();
1600 struct cli_state
*cli
= NULL
;
1601 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1602 struct cli_credentials
*cli_creds
= NULL
;
1603 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
1604 struct netlogon_creds_CredentialState
*creds
= NULL
;
1605 uint32_t netlogon_flags
= 0;
1611 return NT_STATUS_INVALID_PARAMETER
;
1614 if (!secrets_init()) {
1616 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1619 status
= pdb_get_trust_credentials(netbios_domain_name
, NULL
,
1621 if (!NT_STATUS_IS_OK(status
)) {
1626 /* we don't want any old password */
1627 cli_credentials_set_old_password(cli_creds
, NULL
, CRED_SPECIFIED
);
1630 cli_credentials_set_kerberos_state(cli_creds
,
1631 CRED_MUST_USE_KERBEROS
);
1634 status
= cli_full_connection_creds(&cli
, NULL
,
1640 SMB_SIGNING_IPC_DEFAULT
);
1642 if (!NT_STATUS_IS_OK(status
)) {
1643 status
= cli_full_connection(&cli
, NULL
,
1651 SMB_SIGNING_IPC_DEFAULT
);
1654 if (!NT_STATUS_IS_OK(status
)) {
1659 status
= rpccli_create_netlogon_creds_ctx(cli_creds
,
1664 if (!NT_STATUS_IS_OK(status
)) {
1670 status
= rpccli_setup_netlogon_creds(cli
, NCACN_NP
,
1672 true, /* force_reauth */
1674 if (!NT_STATUS_IS_OK(status
)) {
1675 DEBUG(0,("connect_to_domain_password_server: "
1676 "unable to open the domain client session to "
1677 "machine %s. Flags[0x%08X] Error was : %s.\n",
1678 dc_name
, (unsigned)netlogon_flags
,
1679 nt_errstr(status
)));
1685 status
= netlogon_creds_cli_get(netlogon_creds
,
1688 if (!NT_STATUS_IS_OK(status
)) {
1693 netlogon_flags
= creds
->negotiate_flags
;
1696 if (!(netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
)) {
1699 return NT_STATUS_OK
;
1702 status
= cli_rpc_pipe_open_schannel_with_creds(
1703 cli
, &ndr_table_netlogon
, NCACN_NP
,
1704 netlogon_creds
, &netlogon_pipe
);
1706 TALLOC_FREE(netlogon_pipe
);
1708 if (!NT_STATUS_IS_OK(status
)) {
1709 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1710 "on netlogon pipe to server %s for domain %s. "
1712 smbXcli_conn_remote_name(cli
->conn
),
1713 netbios_domain_name
, nt_errstr(status
)));
1721 return NT_STATUS_OK
;
1724 /****************************************************************
1725 ****************************************************************/
1727 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1728 struct libnet_JoinCtx
*r
)
1732 status
= libnet_join_ok(r
->in
.msg_ctx
,
1733 r
->out
.netbios_domain_name
,
1735 r
->in
.use_kerberos
);
1736 if (!NT_STATUS_IS_OK(status
)) {
1737 libnet_join_set_error_string(mem_ctx
, r
,
1738 "failed to verify domain membership after joining: %s",
1739 get_friendly_nt_error_msg(status
));
1740 return WERR_NERR_SETUPNOTJOINED
;
1746 /****************************************************************
1747 ****************************************************************/
1749 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1750 struct libnet_UnjoinCtx
*r
)
1753 * TODO: use values from 'struct libnet_UnjoinCtx' ?
1755 return secrets_delete_machine_password_ex(lp_workgroup(), lp_realm());
1758 /****************************************************************
1759 ****************************************************************/
1761 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1762 struct libnet_UnjoinCtx
*r
)
1764 struct cli_state
*cli
= NULL
;
1765 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1766 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1767 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1770 struct lsa_String lsa_acct_name
;
1771 struct samr_Ids user_rids
;
1772 struct samr_Ids name_types
;
1773 union samr_UserInfo
*info
= NULL
;
1774 struct dcerpc_binding_handle
*b
= NULL
;
1776 ZERO_STRUCT(sam_pol
);
1777 ZERO_STRUCT(domain_pol
);
1778 ZERO_STRUCT(user_pol
);
1780 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1781 r
->in
.admin_account
,
1783 r
->in
.admin_password
,
1786 if (!NT_STATUS_IS_OK(status
)) {
1790 /* Open the domain */
1792 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
1794 if (!NT_STATUS_IS_OK(status
)) {
1795 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1796 nt_errstr(status
)));
1800 b
= pipe_hnd
->binding_handle
;
1802 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1804 SEC_FLAG_MAXIMUM_ALLOWED
,
1807 if (!NT_STATUS_IS_OK(status
)) {
1810 if (!NT_STATUS_IS_OK(result
)) {
1815 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1817 SEC_FLAG_MAXIMUM_ALLOWED
,
1821 if (!NT_STATUS_IS_OK(status
)) {
1824 if (!NT_STATUS_IS_OK(result
)) {
1829 /* Create domain user */
1831 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1832 if (!strlower_m(acct_name
)) {
1833 status
= NT_STATUS_INVALID_PARAMETER
;
1837 init_lsa_String(&lsa_acct_name
, acct_name
);
1839 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1847 if (!NT_STATUS_IS_OK(status
)) {
1850 if (!NT_STATUS_IS_OK(result
)) {
1854 if (user_rids
.count
!= 1) {
1855 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1858 if (name_types
.count
!= 1) {
1859 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1863 if (name_types
.ids
[0] != SID_NAME_USER
) {
1864 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1865 name_types
.ids
[0]));
1866 status
= NT_STATUS_INVALID_WORKSTATION
;
1870 user_rid
= user_rids
.ids
[0];
1872 /* Open handle on user */
1874 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1876 SEC_FLAG_MAXIMUM_ALLOWED
,
1880 if (!NT_STATUS_IS_OK(status
)) {
1883 if (!NT_STATUS_IS_OK(result
)) {
1890 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1895 if (!NT_STATUS_IS_OK(status
)) {
1896 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1899 if (!NT_STATUS_IS_OK(result
)) {
1901 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1905 /* now disable and setuser info */
1907 info
->info16
.acct_flags
|= ACB_DISABLED
;
1909 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1914 if (!NT_STATUS_IS_OK(status
)) {
1915 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1918 if (!NT_STATUS_IS_OK(result
)) {
1920 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1924 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1927 if (pipe_hnd
&& b
) {
1928 if (is_valid_policy_hnd(&domain_pol
)) {
1929 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1931 if (is_valid_policy_hnd(&sam_pol
)) {
1932 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1934 TALLOC_FREE(pipe_hnd
);
1944 /****************************************************************
1945 ****************************************************************/
1947 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1949 WERROR werr
= WERR_OK
;
1951 struct smbconf_ctx
*ctx
;
1953 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1954 if (!SBC_ERROR_IS_OK(err
)) {
1955 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
1959 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1961 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1962 if (!SBC_ERROR_IS_OK(err
)) {
1963 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
1967 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1969 if (!SBC_ERROR_IS_OK(err
)) {
1970 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
1974 smbconf_delete_global_parameter(ctx
, "realm");
1978 err
= smbconf_set_global_parameter(ctx
, "security", "domain");
1979 if (!SBC_ERROR_IS_OK(err
)) {
1980 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
1984 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1985 r
->out
.netbios_domain_name
);
1986 if (!SBC_ERROR_IS_OK(err
)) {
1987 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
1991 if (r
->out
.domain_is_ad
) {
1992 err
= smbconf_set_global_parameter(ctx
, "security", "ads");
1993 if (!SBC_ERROR_IS_OK(err
)) {
1994 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
1998 err
= smbconf_set_global_parameter(ctx
, "realm",
1999 r
->out
.dns_domain_name
);
2000 if (!SBC_ERROR_IS_OK(err
)) {
2001 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2007 smbconf_shutdown(ctx
);
2011 /****************************************************************
2012 ****************************************************************/
2014 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
2016 WERROR werr
= WERR_OK
;
2018 struct smbconf_ctx
*ctx
;
2020 err
= smbconf_init_reg(r
, &ctx
, NULL
);
2021 if (!SBC_ERROR_IS_OK(err
)) {
2022 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2026 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2028 err
= smbconf_set_global_parameter(ctx
, "security", "user");
2029 if (!SBC_ERROR_IS_OK(err
)) {
2030 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2034 err
= smbconf_delete_global_parameter(ctx
, "workgroup");
2035 if (!SBC_ERROR_IS_OK(err
)) {
2036 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2040 smbconf_delete_global_parameter(ctx
, "realm");
2044 smbconf_shutdown(ctx
);
2048 /****************************************************************
2049 ****************************************************************/
2051 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
2055 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2056 return r
->out
.result
;
2059 if (!r
->in
.modify_config
) {
2063 werr
= do_join_modify_vals_config(r
);
2064 if (!W_ERROR_IS_OK(werr
)) {
2068 lp_load_global(get_dyn_CONFIGFILE());
2070 r
->out
.modified_config
= true;
2071 r
->out
.result
= werr
;
2076 /****************************************************************
2077 ****************************************************************/
2079 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
2083 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2084 return r
->out
.result
;
2087 if (!r
->in
.modify_config
) {
2091 werr
= do_unjoin_modify_vals_config(r
);
2092 if (!W_ERROR_IS_OK(werr
)) {
2096 lp_load_global(get_dyn_CONFIGFILE());
2098 r
->out
.modified_config
= true;
2099 r
->out
.result
= werr
;
2104 /****************************************************************
2105 ****************************************************************/
2107 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
2108 const char *domain_str
,
2109 const char **domain_p
,
2112 char *domain
= NULL
;
2114 const char *p
= NULL
;
2116 if (!domain_str
|| !domain_p
|| !dc_p
) {
2120 p
= strchr_m(domain_str
, '\\');
2123 domain
= talloc_strndup(mem_ctx
, domain_str
,
2124 PTR_DIFF(p
, domain_str
));
2125 dc
= talloc_strdup(mem_ctx
, p
+1);
2130 domain
= talloc_strdup(mem_ctx
, domain_str
);
2146 /****************************************************************
2147 ****************************************************************/
2149 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
2150 struct libnet_JoinCtx
*r
)
2152 if (!r
->in
.domain_name
) {
2153 libnet_join_set_error_string(mem_ctx
, r
,
2154 "No domain name defined");
2155 return WERR_INVALID_PARAMETER
;
2158 if (strlen(r
->in
.machine_name
) > 15) {
2159 libnet_join_set_error_string(mem_ctx
, r
,
2160 "Our netbios name can be at most 15 chars long, "
2161 "\"%s\" is %u chars long\n",
2163 (unsigned int)strlen(r
->in
.machine_name
));
2164 return WERR_INVALID_PARAMETER
;
2167 r
->out
.account_name
= talloc_asprintf(mem_ctx
, "%s$",
2168 r
->in
.machine_name
);
2169 if (r
->out
.account_name
== NULL
) {
2170 libnet_join_set_error_string(mem_ctx
, r
,
2171 "Unable to construct r->out.account_name");
2172 return WERR_NOT_ENOUGH_MEMORY
;
2175 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2178 libnet_join_set_error_string(mem_ctx
, r
,
2179 "Failed to parse domain name");
2180 return WERR_INVALID_PARAMETER
;
2183 if (!r
->in
.admin_domain
) {
2184 char *admin_domain
= NULL
;
2185 char *admin_account
= NULL
;
2188 ok
= split_domain_user(mem_ctx
,
2189 r
->in
.admin_account
,
2193 return WERR_NOT_ENOUGH_MEMORY
;
2196 if (admin_domain
!= NULL
) {
2197 r
->in
.admin_domain
= admin_domain
;
2199 r
->in
.admin_domain
= r
->in
.domain_name
;
2201 r
->in
.admin_account
= admin_account
;
2204 if (!secrets_init()) {
2205 libnet_join_set_error_string(mem_ctx
, r
,
2206 "Unable to open secrets database");
2207 return WERR_CAN_NOT_COMPLETE
;
2213 /****************************************************************
2214 ****************************************************************/
2216 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
2220 /* Try adding dom admins to builtin\admins. Only log failures. */
2221 status
= create_builtin_administrators(domain_sid
);
2222 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2223 DEBUG(10,("Unable to auto-add domain administrators to "
2224 "BUILTIN\\Administrators during join because "
2225 "winbindd must be running.\n"));
2226 } else if (!NT_STATUS_IS_OK(status
)) {
2227 DEBUG(5, ("Failed to auto-add domain administrators to "
2228 "BUILTIN\\Administrators during join: %s\n",
2229 nt_errstr(status
)));
2232 /* Try adding dom users to builtin\users. Only log failures. */
2233 status
= create_builtin_users(domain_sid
);
2234 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2235 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2236 "during join because winbindd must be running.\n"));
2237 } else if (!NT_STATUS_IS_OK(status
)) {
2238 DEBUG(5, ("Failed to auto-add domain administrators to "
2239 "BUILTIN\\Administrators during join: %s\n",
2240 nt_errstr(status
)));
2243 /* Try adding dom guests to builtin\guests. Only log failures. */
2244 status
= create_builtin_guests(domain_sid
);
2245 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2246 DEBUG(10,("Unable to auto-add domain guests to "
2247 "BUILTIN\\Guests during join because "
2248 "winbindd must be running.\n"));
2249 } else if (!NT_STATUS_IS_OK(status
)) {
2250 DEBUG(5, ("Failed to auto-add domain guests to "
2251 "BUILTIN\\Guests during join: %s\n",
2252 nt_errstr(status
)));
2256 /****************************************************************
2257 ****************************************************************/
2259 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
2260 struct libnet_JoinCtx
*r
)
2264 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2265 return r
->out
.result
;
2268 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
2269 werr
= do_JoinConfig(r
);
2270 if (!W_ERROR_IS_OK(werr
)) {
2278 if (r
->out
.domain_is_ad
&&
2279 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2280 ADS_STATUS ads_status
;
2282 ads_status
= libnet_join_post_processing_ads_modify(mem_ctx
, r
);
2283 if (!ADS_ERR_OK(ads_status
)) {
2284 return WERR_GEN_FAILURE
;
2287 #endif /* HAVE_ADS */
2289 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
2290 if (r
->out
.dns_domain_name
) {
2291 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
2294 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2295 return WERR_NERR_SETUPNOTJOINED
;
2298 werr
= do_JoinConfig(r
);
2299 if (!W_ERROR_IS_OK(werr
)) {
2304 if (r
->out
.domain_is_ad
&&
2305 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2306 ADS_STATUS ads_status
;
2308 ads_status
= libnet_join_post_processing_ads_sync(mem_ctx
, r
);
2309 if (!ADS_ERR_OK(ads_status
)) {
2310 return WERR_GEN_FAILURE
;
2313 #endif /* HAVE_ADS */
2315 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
2320 /****************************************************************
2321 ****************************************************************/
2323 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
2326 ads_destroy(&r
->in
.ads
);
2332 /****************************************************************
2333 ****************************************************************/
2335 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
2338 ads_destroy(&r
->in
.ads
);
2344 /****************************************************************
2345 ****************************************************************/
2347 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
2348 struct libnet_JoinCtx
**r
)
2350 struct libnet_JoinCtx
*ctx
;
2352 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
2354 return WERR_NOT_ENOUGH_MEMORY
;
2357 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
2359 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
2360 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
2362 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
2364 ctx
->in
.desired_encryption_types
= ENC_CRC32
|
2367 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2368 ctx
->in
.desired_encryption_types
|= ENC_HMAC_SHA1_96_AES128
;
2370 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2371 ctx
->in
.desired_encryption_types
|= ENC_HMAC_SHA1_96_AES256
;
2379 /****************************************************************
2380 ****************************************************************/
2382 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
2383 struct libnet_UnjoinCtx
**r
)
2385 struct libnet_UnjoinCtx
*ctx
;
2387 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
2389 return WERR_NOT_ENOUGH_MEMORY
;
2392 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
2394 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
2395 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
2402 /****************************************************************
2403 ****************************************************************/
2405 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
2406 struct libnet_JoinCtx
*r
)
2408 bool valid_security
= false;
2409 bool valid_workgroup
= false;
2410 bool valid_realm
= false;
2411 bool ignored_realm
= false;
2413 /* check if configuration is already set correctly */
2415 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
2417 switch (r
->out
.domain_is_ad
) {
2419 valid_security
= (lp_security() == SEC_DOMAIN
)
2420 || (lp_server_role() == ROLE_DOMAIN_PDC
)
2421 || (lp_server_role() == ROLE_DOMAIN_BDC
);
2422 if (valid_workgroup
&& valid_security
) {
2423 /* nothing to be done */
2428 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
2429 switch (lp_security()) {
2431 if (!valid_realm
&& lp_winbind_rpc_only()) {
2433 ignored_realm
= true;
2438 valid_security
= true;
2441 if (valid_workgroup
&& valid_realm
&& valid_security
) {
2442 if (ignored_realm
&& !r
->in
.modify_config
)
2444 libnet_join_set_error_string(mem_ctx
, r
,
2445 "Warning: ignoring realm when "
2446 "joining AD domain with "
2447 "'security=domain' and "
2448 "'winbind rpc only = yes'. "
2449 "(realm set to '%s', "
2450 "should be '%s').", lp_realm(),
2451 r
->out
.dns_domain_name
);
2453 /* nothing to be done */
2459 /* check if we are supposed to manipulate configuration */
2461 if (!r
->in
.modify_config
) {
2463 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
2465 if (!valid_workgroup
) {
2466 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2467 "\"workgroup\" set to '%s', should be '%s'",
2468 lp_workgroup(), r
->out
.netbios_domain_name
);
2469 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2473 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2474 "\"realm\" set to '%s', should be '%s'",
2475 lp_realm(), r
->out
.dns_domain_name
);
2476 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2479 if (!valid_security
) {
2480 const char *sec
= NULL
;
2481 switch (lp_security()) {
2482 case SEC_USER
: sec
= "user"; break;
2483 case SEC_DOMAIN
: sec
= "domain"; break;
2484 case SEC_ADS
: sec
= "ads"; break;
2486 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2487 "\"security\" set to '%s', should be %s",
2488 sec
, r
->out
.domain_is_ad
?
2489 "either 'domain' or 'ads'" : "'domain'");
2490 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2493 libnet_join_set_error_string(mem_ctx
, r
,
2494 "Invalid configuration (%s) and configuration modification "
2495 "was not requested", wrong_conf
);
2496 return WERR_CAN_NOT_COMPLETE
;
2499 /* check if we are able to manipulate configuration */
2501 if (!lp_config_backend_is_registry()) {
2502 libnet_join_set_error_string(mem_ctx
, r
,
2503 "Configuration manipulation requested but not "
2504 "supported by backend");
2505 return WERR_NOT_SUPPORTED
;
2511 /****************************************************************
2512 ****************************************************************/
2514 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
2515 struct libnet_JoinCtx
*r
)
2519 struct cli_state
*cli
= NULL
;
2521 ADS_STATUS ads_status
;
2522 #endif /* HAVE_ADS */
2523 const char *pre_connect_realm
= NULL
;
2524 const char *numeric_dcip
= NULL
;
2525 const char *sitename
= NULL
;
2527 /* Before contacting a DC, we can securely know
2528 * the realm only if the user specifies it.
2530 if (r
->in
.use_kerberos
&&
2531 r
->in
.domain_name_type
== JoinDomNameTypeDNS
) {
2532 pre_connect_realm
= r
->in
.domain_name
;
2535 if (!r
->in
.dc_name
) {
2536 struct netr_DsRGetDCNameInfo
*info
;
2538 uint32_t name_type_flags
= 0;
2539 if (r
->in
.domain_name_type
== JoinDomNameTypeDNS
) {
2540 name_type_flags
= DS_IS_DNS_NAME
;
2541 } else if (r
->in
.domain_name_type
== JoinDomNameTypeNBT
) {
2542 name_type_flags
= DS_IS_FLAT_NAME
;
2544 status
= dsgetdcname(mem_ctx
,
2549 DS_FORCE_REDISCOVERY
|
2550 DS_DIRECTORY_SERVICE_REQUIRED
|
2551 DS_WRITABLE_REQUIRED
|
2552 DS_RETURN_DNS_NAME
|
2555 if (!NT_STATUS_IS_OK(status
)) {
2556 libnet_join_set_error_string(mem_ctx
, r
,
2557 "failed to find DC for domain %s - %s",
2559 get_friendly_nt_error_msg(status
));
2560 return WERR_NERR_DCNOTFOUND
;
2563 dc
= strip_hostname(info
->dc_unc
);
2564 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2565 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2567 if (info
->dc_address
== NULL
|| info
->dc_address
[0] != '\\' ||
2568 info
->dc_address
[1] != '\\') {
2569 DBG_ERR("ill-formed DC address '%s'\n",
2571 return WERR_NERR_DCNOTFOUND
;
2574 numeric_dcip
= info
->dc_address
+ 2;
2575 sitename
= info
->dc_site_name
;
2576 /* info goes out of scope but the memory stays
2577 allocated on the talloc context */
2580 if (pre_connect_realm
!= NULL
) {
2581 struct sockaddr_storage ss
= {0};
2583 if (numeric_dcip
!= NULL
) {
2584 if (!interpret_string_addr(&ss
, numeric_dcip
,
2587 "cannot parse IP address '%s' of DC '%s'\n",
2588 numeric_dcip
, r
->in
.dc_name
);
2589 return WERR_NERR_DCNOTFOUND
;
2592 if (!interpret_string_addr(&ss
, r
->in
.dc_name
, 0)) {
2594 "cannot resolve IP address of DC '%s'\n",
2596 return WERR_NERR_DCNOTFOUND
;
2600 /* The domain parameter is only used as modifier
2601 * to krb5.conf file name. .JOIN is is not a valid
2602 * NetBIOS name so it cannot clash with another domain
2605 create_local_private_krb5_conf_for_domain(
2606 pre_connect_realm
, ".JOIN", sitename
, &ss
);
2609 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
2610 if (!NT_STATUS_IS_OK(status
)) {
2611 libnet_join_set_error_string(mem_ctx
, r
,
2612 "failed to lookup DC info for domain '%s' over rpc: %s",
2613 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2614 return ntstatus_to_werror(status
);
2617 werr
= libnet_join_check_config(mem_ctx
, r
);
2618 if (!W_ERROR_IS_OK(werr
)) {
2624 if (r
->out
.domain_is_ad
) {
2625 create_local_private_krb5_conf_for_domain(
2626 r
->out
.dns_domain_name
, r
->out
.netbios_domain_name
,
2627 sitename
, smbXcli_conn_remote_sockaddr(cli
->conn
));
2630 if (r
->out
.domain_is_ad
&&
2631 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2633 const char *initial_account_ou
= r
->in
.account_ou
;
2636 * we want to create the msDS-SupportedEncryptionTypes attribute
2637 * as early as possible so always try an LDAP create as the user
2638 * first. We copy r->in.account_ou because it may be changed
2639 * during the machine pre-creation.
2642 ads_status
= libnet_join_connect_ads_user(mem_ctx
, r
);
2643 if (!ADS_ERR_OK(ads_status
)) {
2644 return WERR_NERR_DEFAULTJOINREQUIRED
;
2647 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
2648 if (ADS_ERR_OK(ads_status
)) {
2651 * LDAP object create succeeded, now go to the rpc
2652 * password set routines
2655 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
2659 if (initial_account_ou
!= NULL
) {
2660 libnet_join_set_error_string(mem_ctx
, r
,
2661 "failed to precreate account in ou %s: %s",
2663 ads_errstr(ads_status
));
2664 return WERR_NERR_DEFAULTJOINREQUIRED
;
2667 DEBUG(5, ("failed to precreate account in ou %s: %s",
2668 r
->in
.account_ou
, ads_errstr(ads_status
)));
2672 #endif /* HAVE_ADS */
2674 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
2675 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
2676 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2678 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2680 if (!NT_STATUS_IS_OK(status
)) {
2681 libnet_join_set_error_string(mem_ctx
, r
,
2682 "failed to join domain '%s' over rpc: %s",
2683 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2684 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2685 return WERR_NERR_SETUPALREADYJOINED
;
2687 werr
= ntstatus_to_werror(status
);
2701 /****************************************************************
2702 ****************************************************************/
2704 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2705 struct libnet_JoinCtx
*r
)
2708 struct libnet_UnjoinCtx
*u
= NULL
;
2710 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2711 if (!W_ERROR_IS_OK(werr
)) {
2715 u
->in
.debug
= r
->in
.debug
;
2716 u
->in
.dc_name
= r
->in
.dc_name
;
2717 u
->in
.domain_name
= r
->in
.domain_name
;
2718 u
->in
.admin_account
= r
->in
.admin_account
;
2719 u
->in
.admin_password
= r
->in
.admin_password
;
2720 u
->in
.modify_config
= r
->in
.modify_config
;
2721 u
->in
.use_kerberos
= r
->in
.use_kerberos
;
2722 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2723 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2725 werr
= libnet_Unjoin(mem_ctx
, u
);
2731 /****************************************************************
2732 ****************************************************************/
2734 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2735 struct libnet_JoinCtx
*r
)
2740 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2743 ZERO_STRUCT(r
->out
);
2745 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2746 if (!W_ERROR_IS_OK(werr
)) {
2750 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2751 werr
= libnet_DomainJoin(mem_ctx
, r
);
2752 if (!W_ERROR_IS_OK(werr
)) {
2757 werr
= libnet_join_post_processing(mem_ctx
, r
);
2758 if (!W_ERROR_IS_OK(werr
)) {
2762 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2763 werr
= libnet_join_post_verify(mem_ctx
, r
);
2764 if (!W_ERROR_IS_OK(werr
)) {
2765 libnet_join_rollback(mem_ctx
, r
);
2770 r
->out
.result
= werr
;
2773 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2778 /****************************************************************
2779 ****************************************************************/
2781 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2782 struct libnet_UnjoinCtx
*r
)
2786 if (!r
->in
.domain_sid
) {
2788 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2789 libnet_unjoin_set_error_string(mem_ctx
, r
,
2790 "Unable to fetch domain sid: are we joined?");
2791 return WERR_NERR_SETUPNOTJOINED
;
2793 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2794 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2797 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2798 !r
->in
.delete_machine_account
) {
2799 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2803 if (!r
->in
.dc_name
) {
2804 struct netr_DsRGetDCNameInfo
*info
;
2806 status
= dsgetdcname(mem_ctx
,
2811 DS_DIRECTORY_SERVICE_REQUIRED
|
2812 DS_WRITABLE_REQUIRED
|
2815 if (!NT_STATUS_IS_OK(status
)) {
2816 libnet_unjoin_set_error_string(mem_ctx
, r
,
2817 "failed to find DC for domain %s - %s",
2819 get_friendly_nt_error_msg(status
));
2820 return WERR_NERR_DCNOTFOUND
;
2823 dc
= strip_hostname(info
->dc_unc
);
2824 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2825 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2829 /* for net ads leave, try to delete the account. If it works,
2830 no sense in disabling. If it fails, we can still try to
2833 if (r
->in
.delete_machine_account
) {
2834 ADS_STATUS ads_status
;
2835 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2836 if (ADS_ERR_OK(ads_status
)) {
2838 r
->out
.dns_domain_name
=
2839 talloc_strdup(mem_ctx
,
2840 r
->in
.ads
->server
.realm
);
2842 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2844 if (!ADS_ERR_OK(ads_status
)) {
2845 libnet_unjoin_set_error_string(mem_ctx
, r
,
2846 "failed to remove machine account from AD: %s",
2847 ads_errstr(ads_status
));
2849 r
->out
.deleted_machine_account
= true;
2850 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2851 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2855 #endif /* HAVE_ADS */
2857 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2859 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2860 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2861 if (!NT_STATUS_IS_OK(status
)) {
2862 libnet_unjoin_set_error_string(mem_ctx
, r
,
2863 "failed to disable machine account via rpc: %s",
2864 get_friendly_nt_error_msg(status
));
2865 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2866 return WERR_NERR_SETUPNOTJOINED
;
2868 return ntstatus_to_werror(status
);
2871 r
->out
.disabled_machine_account
= true;
2874 /* If disable succeeded or was not requested at all, we
2875 should be getting rid of our end of things */
2877 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2882 /****************************************************************
2883 ****************************************************************/
2885 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2886 struct libnet_UnjoinCtx
*r
)
2888 if (!r
->in
.domain_name
) {
2889 libnet_unjoin_set_error_string(mem_ctx
, r
,
2890 "No domain name defined");
2891 return WERR_INVALID_PARAMETER
;
2894 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2897 libnet_unjoin_set_error_string(mem_ctx
, r
,
2898 "Failed to parse domain name");
2899 return WERR_INVALID_PARAMETER
;
2903 return WERR_NERR_SETUPDOMAINCONTROLLER
;
2906 if (!r
->in
.admin_domain
) {
2907 char *admin_domain
= NULL
;
2908 char *admin_account
= NULL
;
2911 ok
= split_domain_user(mem_ctx
,
2912 r
->in
.admin_account
,
2916 return WERR_NOT_ENOUGH_MEMORY
;
2919 if (admin_domain
!= NULL
) {
2920 r
->in
.admin_domain
= admin_domain
;
2922 r
->in
.admin_domain
= r
->in
.domain_name
;
2924 r
->in
.admin_account
= admin_account
;
2927 if (!secrets_init()) {
2928 libnet_unjoin_set_error_string(mem_ctx
, r
,
2929 "Unable to open secrets database");
2930 return WERR_CAN_NOT_COMPLETE
;
2936 /****************************************************************
2937 ****************************************************************/
2939 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2940 struct libnet_UnjoinCtx
*r
)
2942 saf_delete(r
->out
.netbios_domain_name
);
2943 saf_delete(r
->out
.dns_domain_name
);
2945 return libnet_unjoin_config(r
);
2948 /****************************************************************
2949 ****************************************************************/
2951 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2952 struct libnet_UnjoinCtx
*r
)
2957 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2960 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2961 if (!W_ERROR_IS_OK(werr
)) {
2965 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2966 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2967 if (!W_ERROR_IS_OK(werr
)) {
2968 libnet_unjoin_config(r
);
2973 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2974 if (!W_ERROR_IS_OK(werr
)) {
2979 r
->out
.result
= werr
;
2982 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);