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
,
146 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
149 my_ads
->auth
.flags
|= ADS_AUTH_ALLOW_NTLMSSP
;
152 SAFE_FREE(my_ads
->auth
.user_name
);
153 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
154 if ((cp
= strchr_m(my_ads
->auth
.user_name
, '@'))!=0) {
156 SAFE_FREE(my_ads
->auth
.realm
);
157 my_ads
->auth
.realm
= smb_xstrdup(cp
);
158 if (!strupper_m(my_ads
->auth
.realm
)) {
159 ads_destroy(&my_ads
);
160 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
166 SAFE_FREE(my_ads
->auth
.password
);
167 my_ads
->auth
.password
= SMB_STRDUP(password
);
170 if (ccname
!= NULL
) {
171 SAFE_FREE(my_ads
->auth
.ccache_name
);
172 my_ads
->auth
.ccache_name
= SMB_STRDUP(ccname
);
173 setenv(KRB5_ENV_CCNAME
, my_ads
->auth
.ccache_name
, 1);
176 status
= ads_connect_user_creds(my_ads
);
177 if (!ADS_ERR_OK(status
)) {
178 ads_destroy(&my_ads
);
186 /****************************************************************
187 ****************************************************************/
189 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
190 struct libnet_JoinCtx
*r
,
191 bool use_machine_creds
)
194 const char *username
;
195 const char *password
;
196 const char *ccname
= NULL
;
198 if (use_machine_creds
) {
199 if (r
->in
.machine_name
== NULL
||
200 r
->in
.machine_password
== NULL
) {
201 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
203 username
= talloc_asprintf(mem_ctx
, "%s$",
205 if (username
== NULL
) {
206 return ADS_ERROR(LDAP_NO_MEMORY
);
208 password
= r
->in
.machine_password
;
209 ccname
= "MEMORY:libnet_join_machine_creds";
213 username
= r
->in
.admin_account
;
215 p
= strchr(r
->in
.admin_account
, '@');
217 username
= talloc_asprintf(mem_ctx
, "%s@%s",
221 if (username
== NULL
) {
222 return ADS_ERROR(LDAP_NO_MEMORY
);
224 password
= r
->in
.admin_password
;
227 * when r->in.use_kerberos is set to allow "net ads join -k" we
228 * may not override the provided credential cache - gd
231 if (!r
->in
.use_kerberos
) {
232 ccname
= "MEMORY:libnet_join_user_creds";
236 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
237 r
->out
.netbios_domain_name
,
243 if (!ADS_ERR_OK(status
)) {
244 libnet_join_set_error_string(mem_ctx
, r
,
245 "failed to connect to AD: %s",
250 if (!r
->out
.netbios_domain_name
) {
251 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
252 r
->in
.ads
->server
.workgroup
);
253 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
256 if (!r
->out
.dns_domain_name
) {
257 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
258 r
->in
.ads
->config
.realm
);
259 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
262 r
->out
.domain_is_ad
= true;
267 /****************************************************************
268 ****************************************************************/
270 static ADS_STATUS
libnet_join_connect_ads_user(TALLOC_CTX
*mem_ctx
,
271 struct libnet_JoinCtx
*r
)
273 return libnet_join_connect_ads(mem_ctx
, r
, false);
276 /****************************************************************
277 ****************************************************************/
279 static ADS_STATUS
libnet_join_connect_ads_machine(TALLOC_CTX
*mem_ctx
,
280 struct libnet_JoinCtx
*r
)
282 return libnet_join_connect_ads(mem_ctx
, r
, true);
285 /****************************************************************
286 ****************************************************************/
288 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
289 struct libnet_UnjoinCtx
*r
)
293 status
= libnet_connect_ads(r
->in
.domain_name
,
297 r
->in
.admin_password
,
300 if (!ADS_ERR_OK(status
)) {
301 libnet_unjoin_set_error_string(mem_ctx
, r
,
302 "failed to connect to AD: %s",
309 /****************************************************************
310 join a domain using ADS (LDAP mods)
311 ****************************************************************/
313 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
314 struct libnet_JoinCtx
*r
)
317 LDAPMessage
*res
= NULL
;
318 const char *attrs
[] = { "dn", NULL
};
321 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
322 if (!ADS_ERR_OK(status
)) {
326 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
327 if (!ADS_ERR_OK(status
)) {
331 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
332 ads_msgfree(r
->in
.ads
, res
);
333 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
336 ads_msgfree(r
->in
.ads
, res
);
338 /* Attempt to create the machine account and bail if this fails.
339 Assume that the admin wants exactly what they requested */
341 if (r
->in
.machine_password
== NULL
) {
342 r
->in
.machine_password
=
343 trust_pw_new_value(mem_ctx
,
344 r
->in
.secure_channel_type
,
346 if (r
->in
.machine_password
== NULL
) {
347 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
351 status
= ads_create_machine_acct(r
->in
.ads
,
353 r
->in
.machine_password
,
355 r
->in
.desired_encryption_types
,
356 r
->out
.dns_domain_name
);
358 if (ADS_ERR_OK(status
)) {
359 DEBUG(1,("machine account creation created\n"));
361 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
362 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
363 status
= ADS_SUCCESS
;
366 if (!ADS_ERR_OK(status
)) {
367 DEBUG(1,("machine account creation failed\n"));
371 status
= ads_move_machine_acct(r
->in
.ads
,
375 if (!ADS_ERR_OK(status
)) {
376 DEBUG(1,("failure to locate/move pre-existing "
377 "machine account\n"));
381 DEBUG(1,("The machine account %s the specified OU.\n",
382 moved
? "was moved into" : "already exists in"));
387 /****************************************************************
388 ****************************************************************/
390 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
391 struct libnet_UnjoinCtx
*r
)
396 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
397 if (!ADS_ERR_OK(status
)) {
398 libnet_unjoin_set_error_string(mem_ctx
, r
,
399 "failed to connect to AD: %s",
405 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
406 if (!ADS_ERR_OK(status
)) {
407 libnet_unjoin_set_error_string(mem_ctx
, r
,
408 "failed to leave realm: %s",
416 /****************************************************************
417 ****************************************************************/
419 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
420 struct libnet_JoinCtx
*r
)
423 LDAPMessage
*res
= NULL
;
426 if (!r
->in
.machine_name
) {
427 return ADS_ERROR(LDAP_NO_MEMORY
);
430 status
= ads_find_machine_acct(r
->in
.ads
,
433 if (!ADS_ERR_OK(status
)) {
437 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
438 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
442 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
444 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
448 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
450 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
454 if (!ads_pull_uint32(r
->in
.ads
, res
, "msDS-SupportedEncryptionTypes",
455 &r
->out
.set_encryption_types
)) {
456 r
->out
.set_encryption_types
= 0;
460 ads_msgfree(r
->in
.ads
, res
);
466 static ADS_STATUS
libnet_join_get_machine_spns(TALLOC_CTX
*mem_ctx
,
467 struct libnet_JoinCtx
*r
,
473 if (r
->in
.machine_name
== NULL
) {
474 return ADS_ERROR_SYSTEM(EINVAL
);
477 status
= ads_get_service_principal_names(mem_ctx
,
486 /****************************************************************
487 Set a machines dNSHostName and servicePrincipalName attributes
488 ****************************************************************/
490 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
491 struct libnet_JoinCtx
*r
)
496 const char **spn_array
= NULL
;
500 const char **netbios_aliases
= NULL
;
504 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
505 if (!ADS_ERR_OK(status
)) {
509 status
= libnet_join_get_machine_spns(mem_ctx
,
511 discard_const_p(char **, &spn_array
),
513 if (!ADS_ERR_OK(status
)) {
514 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
517 /* Windows only creates HOST/shortname & HOST/fqdn. */
519 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
521 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
523 if (!strupper_m(spn
)) {
524 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
527 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
529 ok
= add_string_to_array(spn_array
, spn
,
530 &spn_array
, &num_spns
);
532 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
536 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
537 || (strchr(my_fqdn
, '.') == NULL
)) {
538 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
539 r
->out
.dns_domain_name
);
542 if (!strlower_m(my_fqdn
)) {
543 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
546 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
547 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
549 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
552 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
554 ok
= add_string_to_array(spn_array
, spn
,
555 &spn_array
, &num_spns
);
557 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
562 netbios_aliases
= lp_netbios_aliases();
563 if (netbios_aliases
!= NULL
) {
564 for (; *netbios_aliases
!= NULL
; netbios_aliases
++) {
566 * Add HOST/NETBIOSNAME
568 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", *netbios_aliases
);
571 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
573 if (!strupper_m(spn
)) {
575 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
578 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
583 ok
= add_string_to_array(spn_array
, spn
,
584 &spn_array
, &num_spns
);
587 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
592 * Add HOST/netbiosname.domainname
594 if (r
->out
.dns_domain_name
== NULL
) {
597 fstr_sprintf(my_fqdn
, "%s.%s",
599 r
->out
.dns_domain_name
);
601 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
603 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
606 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
611 ok
= add_string_to_array(spn_array
, spn
,
612 &spn_array
, &num_spns
);
615 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
621 /* make sure to NULL terminate the array */
622 spn_array
= talloc_realloc(mem_ctx
, spn_array
, const char *, num_spns
+ 1);
623 if (spn_array
== NULL
) {
624 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
626 spn_array
[num_spns
] = NULL
;
628 mods
= ads_init_mods(mem_ctx
);
630 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
633 /* fields of primary importance */
635 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
636 if (!ADS_ERR_OK(status
)) {
637 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
640 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
642 if (!ADS_ERR_OK(status
)) {
643 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
646 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
649 /****************************************************************
650 ****************************************************************/
652 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
653 struct libnet_JoinCtx
*r
)
658 if (!r
->in
.create_upn
) {
664 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
665 if (!ADS_ERR_OK(status
)) {
670 const char *realm
= r
->out
.dns_domain_name
;
672 /* in case we are about to generate a keytab during the join
673 * make sure the default upn we create is usable with kinit -k.
676 if (USE_KERBEROS_KEYTAB
) {
677 realm
= talloc_strdup_upper(mem_ctx
,
678 r
->out
.dns_domain_name
);
682 return ADS_ERROR(LDAP_NO_MEMORY
);
685 r
->in
.upn
= talloc_asprintf(mem_ctx
,
690 return ADS_ERROR(LDAP_NO_MEMORY
);
694 /* now do the mods */
696 mods
= ads_init_mods(mem_ctx
);
698 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
701 /* fields of primary importance */
703 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
704 if (!ADS_ERR_OK(status
)) {
705 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
708 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
712 /****************************************************************
713 ****************************************************************/
715 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
716 struct libnet_JoinCtx
*r
)
722 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
728 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
729 if (!ADS_ERR_OK(status
)) {
733 /* now do the mods */
735 mods
= ads_init_mods(mem_ctx
);
737 return ADS_ERROR(LDAP_NO_MEMORY
);
740 if (r
->in
.os_servicepack
) {
742 * if blank string then leave os_sp equal to NULL to force
743 * attribute delete (LDAP_MOD_DELETE)
745 if (!strequal(r
->in
.os_servicepack
,"")) {
746 os_sp
= talloc_strdup(mem_ctx
, r
->in
.os_servicepack
);
749 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s",
750 samba_version_string());
752 if (!os_sp
&& !strequal(r
->in
.os_servicepack
,"")) {
753 return ADS_ERROR(LDAP_NO_MEMORY
);
756 /* fields of primary importance */
758 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
760 if (!ADS_ERR_OK(status
)) {
764 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
766 if (!ADS_ERR_OK(status
)) {
770 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
772 if (!ADS_ERR_OK(status
)) {
776 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
779 /****************************************************************
780 ****************************************************************/
782 static ADS_STATUS
libnet_join_set_etypes(TALLOC_CTX
*mem_ctx
,
783 struct libnet_JoinCtx
*r
)
787 const char *etype_list_str
;
789 etype_list_str
= talloc_asprintf(mem_ctx
, "%d",
790 r
->in
.desired_encryption_types
);
791 if (!etype_list_str
) {
792 return ADS_ERROR(LDAP_NO_MEMORY
);
797 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
798 if (!ADS_ERR_OK(status
)) {
802 if (r
->in
.desired_encryption_types
== r
->out
.set_encryption_types
) {
806 /* now do the mods */
808 mods
= ads_init_mods(mem_ctx
);
810 return ADS_ERROR(LDAP_NO_MEMORY
);
813 status
= ads_mod_str(mem_ctx
, &mods
, "msDS-SupportedEncryptionTypes",
815 if (!ADS_ERR_OK(status
)) {
819 status
= ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
820 if (!ADS_ERR_OK(status
)) {
824 r
->out
.set_encryption_types
= r
->in
.desired_encryption_types
;
829 /****************************************************************
830 ****************************************************************/
832 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
833 struct libnet_JoinCtx
*r
)
835 if (!USE_SYSTEM_KEYTAB
) {
839 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
846 /****************************************************************
847 ****************************************************************/
849 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
850 struct libnet_JoinCtx
*r
)
852 uint32_t domain_func
;
854 const char *salt
= NULL
;
855 char *std_salt
= NULL
;
857 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
858 if (!ADS_ERR_OK(status
)) {
859 libnet_join_set_error_string(mem_ctx
, r
,
860 "failed to determine domain functional level: %s",
865 /* go ahead and setup the default salt */
867 std_salt
= kerberos_standard_des_salt();
869 libnet_join_set_error_string(mem_ctx
, r
,
870 "failed to obtain standard DES salt");
874 salt
= talloc_strdup(mem_ctx
, std_salt
);
881 /* if it's a Windows functional domain, we have to look for the UPN */
883 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
886 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
889 salt
= talloc_strdup(mem_ctx
, upn
);
896 r
->out
.krb5_salt
= salt
;
900 /****************************************************************
901 ****************************************************************/
903 static ADS_STATUS
libnet_join_post_processing_ads_modify(TALLOC_CTX
*mem_ctx
,
904 struct libnet_JoinCtx
*r
)
907 bool need_etype_update
= false;
910 status
= libnet_join_connect_ads_user(mem_ctx
, r
);
911 if (!ADS_ERR_OK(status
)) {
916 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
917 if (!ADS_ERR_OK(status
)) {
918 libnet_join_set_error_string(mem_ctx
, r
,
919 "Failed to set machine spn: %s\n"
920 "Do you have sufficient permissions to create machine "
926 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
927 if (!ADS_ERR_OK(status
)) {
928 libnet_join_set_error_string(mem_ctx
, r
,
929 "failed to set machine os attributes: %s",
934 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
935 if (!ADS_ERR_OK(status
)) {
936 libnet_join_set_error_string(mem_ctx
, r
,
937 "failed to set machine upn: %s",
942 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
943 if (!ADS_ERR_OK(status
)) {
947 if (r
->in
.desired_encryption_types
!= r
->out
.set_encryption_types
) {
948 uint32_t func_level
= 0;
950 status
= ads_domain_func_level(r
->in
.ads
, &func_level
);
951 if (!ADS_ERR_OK(status
)) {
952 libnet_join_set_error_string(mem_ctx
, r
,
953 "failed to query domain controller functional level: %s",
958 if (func_level
>= DS_DOMAIN_FUNCTION_2008
) {
959 need_etype_update
= true;
963 if (need_etype_update
) {
965 * We need to reconnect as machine account in order
966 * to update msDS-SupportedEncryptionTypes reliable
969 if (r
->in
.ads
->auth
.ccache_name
!= NULL
) {
970 ads_kdestroy(r
->in
.ads
->auth
.ccache_name
);
973 ads_destroy(&r
->in
.ads
);
975 status
= libnet_join_connect_ads_machine(mem_ctx
, r
);
976 if (!ADS_ERR_OK(status
)) {
977 libnet_join_set_error_string(mem_ctx
, r
,
978 "Failed to connect as machine account: %s",
983 status
= libnet_join_set_etypes(mem_ctx
, r
);
984 if (!ADS_ERR_OK(status
)) {
985 libnet_join_set_error_string(mem_ctx
, r
,
986 "failed to set machine kerberos encryption types: %s",
992 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
993 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
999 static ADS_STATUS
libnet_join_post_processing_ads_sync(TALLOC_CTX
*mem_ctx
,
1000 struct libnet_JoinCtx
*r
)
1002 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
1003 libnet_join_set_error_string(mem_ctx
, r
,
1004 "failed to create kerberos keytab");
1005 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
1010 #endif /* HAVE_ADS */
1012 /****************************************************************
1013 Store the machine password and domain SID
1014 ****************************************************************/
1016 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
1017 struct libnet_JoinCtx
*r
)
1021 status
= secrets_store_JoinCtx(r
);
1022 if (!NT_STATUS_IS_OK(status
)) {
1023 DBG_ERR("secrets_store_JoinCtx() failed %s\n",
1031 /****************************************************************
1032 Connect dc's IPC$ share
1033 ****************************************************************/
1035 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
1040 struct cli_state
**cli
)
1045 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
1048 if (use_kerberos
&& pass
) {
1049 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
1052 return cli_full_connection(cli
, NULL
,
1060 SMB_SIGNING_IPC_DEFAULT
);
1063 /****************************************************************
1064 Lookup domain dc's info
1065 ****************************************************************/
1067 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
1068 struct libnet_JoinCtx
*r
,
1069 struct cli_state
**cli
)
1071 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1072 struct policy_handle lsa_pol
;
1073 NTSTATUS status
, result
;
1074 union lsa_PolicyInformation
*info
= NULL
;
1075 struct dcerpc_binding_handle
*b
;
1076 const char *account
= r
->in
.admin_account
;
1077 const char *domain
= r
->in
.admin_domain
;
1078 const char *password
= r
->in
.admin_password
;
1079 bool use_kerberos
= r
->in
.use_kerberos
;
1081 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) {
1085 use_kerberos
= false;
1088 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1094 if (!NT_STATUS_IS_OK(status
)) {
1098 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
,
1100 if (!NT_STATUS_IS_OK(status
)) {
1101 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
1102 nt_errstr(status
)));
1106 b
= pipe_hnd
->binding_handle
;
1108 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
1109 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1110 if (!NT_STATUS_IS_OK(status
)) {
1114 status
= dcerpc_lsa_QueryInfoPolicy2(b
, mem_ctx
,
1116 LSA_POLICY_INFO_DNS
,
1119 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
1120 r
->out
.domain_is_ad
= true;
1121 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
1122 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
1123 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
1124 r
->out
.domain_guid
= info
->dns
.domain_guid
;
1125 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
1126 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
1129 if (!NT_STATUS_IS_OK(status
)) {
1130 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
1132 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
1135 if (!NT_STATUS_IS_OK(status
)) {
1138 if (!NT_STATUS_IS_OK(result
)) {
1143 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
1144 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
1145 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
1148 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
1149 TALLOC_FREE(pipe_hnd
);
1155 /****************************************************************
1156 Do the domain join unsecure
1157 ****************************************************************/
1159 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
1160 struct libnet_JoinCtx
*r
,
1161 struct cli_state
*cli
)
1163 TALLOC_CTX
*frame
= talloc_stackframe();
1164 struct rpc_pipe_client
*authenticate_pipe
= NULL
;
1165 struct rpc_pipe_client
*passwordset_pipe
= NULL
;
1166 struct cli_credentials
*cli_creds
;
1167 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
1168 struct netlogon_creds_CredentialState
*creds
= NULL
;
1169 uint32_t netlogon_flags
= 0;
1172 DATA_BLOB new_trust_blob
= data_blob_null
;
1175 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
,
1176 &authenticate_pipe
);
1177 if (!NT_STATUS_IS_OK(status
)) {
1182 if (!r
->in
.machine_password
) {
1183 int security
= r
->in
.ads
? SEC_ADS
: SEC_DOMAIN
;
1185 r
->in
.machine_password
= trust_pw_new_value(mem_ctx
,
1186 r
->in
.secure_channel_type
,
1188 if (r
->in
.machine_password
== NULL
) {
1190 return NT_STATUS_NO_MEMORY
;
1194 cli_creds
= cli_credentials_init(talloc_tos());
1195 if (cli_creds
== NULL
) {
1197 return NT_STATUS_NO_MEMORY
;
1200 cli_credentials_set_username(cli_creds
, r
->out
.account_name
,
1202 cli_credentials_set_domain(cli_creds
, r
->in
.domain_name
,
1204 cli_credentials_set_realm(cli_creds
, "", CRED_SPECIFIED
);
1205 cli_credentials_set_secure_channel_type(cli_creds
,
1206 r
->in
.secure_channel_type
);
1208 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
1209 cli_credentials_set_password(cli_creds
, r
->in
.admin_password
,
1212 status
= rpccli_create_netlogon_creds_ctx(
1213 cli_creds
, authenticate_pipe
->desthost
, r
->in
.msg_ctx
,
1214 frame
, &netlogon_creds
);
1215 if (!NT_STATUS_IS_OK(status
)) {
1220 status
= rpccli_setup_netlogon_creds(
1221 cli
, NCACN_NP
, netlogon_creds
, true /* force_reauth */,
1223 if (!NT_STATUS_IS_OK(status
)) {
1228 status
= netlogon_creds_cli_get(netlogon_creds
, frame
, &creds
);
1229 if (!NT_STATUS_IS_OK(status
)) {
1234 netlogon_flags
= creds
->negotiate_flags
;
1237 if (netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
) {
1238 status
= cli_rpc_pipe_open_schannel_with_creds(cli
,
1239 &ndr_table_netlogon
,
1243 if (!NT_STATUS_IS_OK(status
)) {
1248 passwordset_pipe
= authenticate_pipe
;
1251 len
= strlen(r
->in
.machine_password
);
1252 ok
= convert_string_talloc(frame
, CH_UNIX
, CH_UTF16
,
1253 r
->in
.machine_password
, len
,
1254 (void **)&new_trust_blob
.data
,
1255 &new_trust_blob
.length
);
1257 status
= NT_STATUS_UNMAPPABLE_CHARACTER
;
1258 if (errno
== ENOMEM
) {
1259 status
= NT_STATUS_NO_MEMORY
;
1265 status
= netlogon_creds_cli_ServerPasswordSet(netlogon_creds
,
1266 passwordset_pipe
->binding_handle
,
1268 NULL
); /* new_version */
1269 if (!NT_STATUS_IS_OK(status
)) {
1275 return NT_STATUS_OK
;
1278 /****************************************************************
1280 ****************************************************************/
1282 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
1283 struct libnet_JoinCtx
*r
,
1284 struct cli_state
*cli
)
1286 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1287 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1288 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1290 struct lsa_String lsa_acct_name
;
1292 uint32_t acct_flags
= ACB_WSTRUST
;
1293 struct samr_Ids user_rids
;
1294 struct samr_Ids name_types
;
1295 union samr_UserInfo user_info
;
1296 struct dcerpc_binding_handle
*b
= NULL
;
1297 unsigned int old_timeout
= 0;
1299 DATA_BLOB session_key
= data_blob_null
;
1300 struct samr_CryptPassword crypt_pwd
;
1301 struct samr_CryptPasswordEx crypt_pwd_ex
;
1303 ZERO_STRUCT(sam_pol
);
1304 ZERO_STRUCT(domain_pol
);
1305 ZERO_STRUCT(user_pol
);
1307 switch (r
->in
.secure_channel_type
) {
1308 case SEC_CHAN_WKSTA
:
1309 acct_flags
= ACB_WSTRUST
;
1312 acct_flags
= ACB_SVRTRUST
;
1315 return NT_STATUS_INVALID_PARAMETER
;
1318 if (!r
->in
.machine_password
) {
1319 int security
= r
->in
.ads
? SEC_ADS
: SEC_DOMAIN
;
1321 r
->in
.machine_password
= trust_pw_new_value(mem_ctx
,
1322 r
->in
.secure_channel_type
,
1324 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
1327 /* Open the domain */
1329 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
1331 if (!NT_STATUS_IS_OK(status
)) {
1332 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1333 nt_errstr(status
)));
1337 b
= pipe_hnd
->binding_handle
;
1339 status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
1340 if (!NT_STATUS_IS_OK(status
)) {
1341 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1342 nt_errstr(status
)));
1346 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1348 SAMR_ACCESS_ENUM_DOMAINS
1349 | SAMR_ACCESS_LOOKUP_DOMAIN
,
1352 if (!NT_STATUS_IS_OK(status
)) {
1355 if (!NT_STATUS_IS_OK(result
)) {
1360 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1362 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1363 | SAMR_DOMAIN_ACCESS_CREATE_USER
1364 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
1368 if (!NT_STATUS_IS_OK(status
)) {
1371 if (!NT_STATUS_IS_OK(result
)) {
1376 /* Create domain user */
1378 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1379 if (!strlower_m(acct_name
)) {
1380 status
= NT_STATUS_INVALID_PARAMETER
;
1384 init_lsa_String(&lsa_acct_name
, acct_name
);
1386 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
1387 uint32_t access_desired
=
1388 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
1389 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
1390 SAMR_USER_ACCESS_SET_PASSWORD
|
1391 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
1392 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
1393 uint32_t access_granted
= 0;
1395 DEBUG(10,("Creating account with desired access mask: %d\n",
1398 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
1407 if (!NT_STATUS_IS_OK(status
)) {
1412 if (!NT_STATUS_IS_OK(status
) &&
1413 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1415 DEBUG(10,("Creation of workstation account failed: %s\n",
1416 nt_errstr(status
)));
1418 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1419 username/password combo but the user does not have
1420 administrator access. */
1422 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
1423 libnet_join_set_error_string(mem_ctx
, r
,
1424 "User specified does not have "
1425 "administrator privileges");
1431 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1432 if (!(r
->in
.join_flags
&
1433 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
1438 /* We *must* do this.... don't ask... */
1440 if (NT_STATUS_IS_OK(status
)) {
1441 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1445 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1452 if (!NT_STATUS_IS_OK(status
)) {
1455 if (!NT_STATUS_IS_OK(result
)) {
1459 if (user_rids
.count
!= 1) {
1460 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1463 if (name_types
.count
!= 1) {
1464 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1468 if (name_types
.ids
[0] != SID_NAME_USER
) {
1469 DEBUG(0,("%s is not a user account (type=%d)\n",
1470 acct_name
, name_types
.ids
[0]));
1471 status
= NT_STATUS_INVALID_WORKSTATION
;
1475 user_rid
= user_rids
.ids
[0];
1477 /* Open handle on user */
1479 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1481 SEC_FLAG_MAXIMUM_ALLOWED
,
1485 if (!NT_STATUS_IS_OK(status
)) {
1488 if (!NT_STATUS_IS_OK(result
)) {
1493 /* Fill in the additional account flags now */
1495 acct_flags
|= ACB_PWNOEXP
;
1497 /* Set account flags on machine account */
1498 ZERO_STRUCT(user_info
.info16
);
1499 user_info
.info16
.acct_flags
= acct_flags
;
1501 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1503 UserControlInformation
,
1506 if (!NT_STATUS_IS_OK(status
)) {
1507 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1511 libnet_join_set_error_string(mem_ctx
, r
,
1512 "Failed to set account flags for machine account (%s)\n",
1517 if (!NT_STATUS_IS_OK(result
)) {
1520 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1524 libnet_join_set_error_string(mem_ctx
, r
,
1525 "Failed to set account flags for machine account (%s)\n",
1530 /* Set password on machine account - first try level 26 */
1533 * increase the timeout as password filter modules on the DC
1534 * might delay the operation for a significant amount of time
1536 old_timeout
= rpccli_set_timeout(pipe_hnd
, 600000);
1538 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1542 user_info
.info26
.password
= crypt_pwd_ex
;
1543 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1545 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1547 UserInternal5InformationNew
,
1551 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
1553 /* retry with level 24 */
1555 init_samr_CryptPassword(r
->in
.machine_password
,
1559 user_info
.info24
.password
= crypt_pwd
;
1560 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1562 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1564 UserInternal5Information
,
1569 old_timeout
= rpccli_set_timeout(pipe_hnd
, old_timeout
);
1571 if (!NT_STATUS_IS_OK(status
)) {
1573 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1577 libnet_join_set_error_string(mem_ctx
, r
,
1578 "Failed to set password for machine account (%s)\n",
1582 if (!NT_STATUS_IS_OK(result
)) {
1585 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1589 libnet_join_set_error_string(mem_ctx
, r
,
1590 "Failed to set password for machine account (%s)\n",
1595 status
= NT_STATUS_OK
;
1602 data_blob_clear_free(&session_key
);
1604 if (is_valid_policy_hnd(&sam_pol
)) {
1605 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1607 if (is_valid_policy_hnd(&domain_pol
)) {
1608 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1610 if (is_valid_policy_hnd(&user_pol
)) {
1611 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1613 TALLOC_FREE(pipe_hnd
);
1618 /****************************************************************
1619 ****************************************************************/
1621 NTSTATUS
libnet_join_ok(struct messaging_context
*msg_ctx
,
1622 const char *netbios_domain_name
,
1623 const char *dc_name
,
1624 const bool use_kerberos
)
1626 TALLOC_CTX
*frame
= talloc_stackframe();
1627 struct cli_state
*cli
= NULL
;
1628 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1629 struct cli_credentials
*cli_creds
= NULL
;
1630 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
1631 struct netlogon_creds_CredentialState
*creds
= NULL
;
1632 uint32_t netlogon_flags
= 0;
1638 return NT_STATUS_INVALID_PARAMETER
;
1641 if (!secrets_init()) {
1643 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1646 status
= pdb_get_trust_credentials(netbios_domain_name
, NULL
,
1648 if (!NT_STATUS_IS_OK(status
)) {
1653 /* we don't want any old password */
1654 cli_credentials_set_old_password(cli_creds
, NULL
, CRED_SPECIFIED
);
1657 cli_credentials_set_kerberos_state(cli_creds
,
1658 CRED_MUST_USE_KERBEROS
);
1661 status
= cli_full_connection_creds(&cli
, NULL
,
1667 SMB_SIGNING_IPC_DEFAULT
);
1669 if (!NT_STATUS_IS_OK(status
)) {
1670 status
= cli_full_connection(&cli
, NULL
,
1678 SMB_SIGNING_IPC_DEFAULT
);
1681 if (!NT_STATUS_IS_OK(status
)) {
1686 status
= rpccli_create_netlogon_creds_ctx(cli_creds
,
1691 if (!NT_STATUS_IS_OK(status
)) {
1697 status
= rpccli_setup_netlogon_creds(cli
, NCACN_NP
,
1699 true, /* force_reauth */
1701 if (!NT_STATUS_IS_OK(status
)) {
1702 DEBUG(0,("connect_to_domain_password_server: "
1703 "unable to open the domain client session to "
1704 "machine %s. Flags[0x%08X] Error was : %s.\n",
1705 dc_name
, (unsigned)netlogon_flags
,
1706 nt_errstr(status
)));
1712 status
= netlogon_creds_cli_get(netlogon_creds
,
1715 if (!NT_STATUS_IS_OK(status
)) {
1720 netlogon_flags
= creds
->negotiate_flags
;
1723 if (!(netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
)) {
1726 return NT_STATUS_OK
;
1729 status
= cli_rpc_pipe_open_schannel_with_creds(
1730 cli
, &ndr_table_netlogon
, NCACN_NP
,
1731 netlogon_creds
, &netlogon_pipe
);
1733 TALLOC_FREE(netlogon_pipe
);
1735 if (!NT_STATUS_IS_OK(status
)) {
1736 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1737 "on netlogon pipe to server %s for domain %s. "
1739 smbXcli_conn_remote_name(cli
->conn
),
1740 netbios_domain_name
, nt_errstr(status
)));
1748 return NT_STATUS_OK
;
1751 /****************************************************************
1752 ****************************************************************/
1754 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1755 struct libnet_JoinCtx
*r
)
1759 status
= libnet_join_ok(r
->in
.msg_ctx
,
1760 r
->out
.netbios_domain_name
,
1762 r
->in
.use_kerberos
);
1763 if (!NT_STATUS_IS_OK(status
)) {
1764 libnet_join_set_error_string(mem_ctx
, r
,
1765 "failed to verify domain membership after joining: %s",
1766 get_friendly_nt_error_msg(status
));
1767 return WERR_NERR_SETUPNOTJOINED
;
1773 /****************************************************************
1774 ****************************************************************/
1776 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1777 struct libnet_UnjoinCtx
*r
)
1780 * TODO: use values from 'struct libnet_UnjoinCtx' ?
1782 return secrets_delete_machine_password_ex(lp_workgroup(), lp_realm());
1785 /****************************************************************
1786 ****************************************************************/
1788 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1789 struct libnet_UnjoinCtx
*r
)
1791 struct cli_state
*cli
= NULL
;
1792 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1793 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1794 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1797 struct lsa_String lsa_acct_name
;
1798 struct samr_Ids user_rids
;
1799 struct samr_Ids name_types
;
1800 union samr_UserInfo
*info
= NULL
;
1801 struct dcerpc_binding_handle
*b
= NULL
;
1803 ZERO_STRUCT(sam_pol
);
1804 ZERO_STRUCT(domain_pol
);
1805 ZERO_STRUCT(user_pol
);
1807 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1808 r
->in
.admin_account
,
1810 r
->in
.admin_password
,
1813 if (!NT_STATUS_IS_OK(status
)) {
1817 /* Open the domain */
1819 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
1821 if (!NT_STATUS_IS_OK(status
)) {
1822 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1823 nt_errstr(status
)));
1827 b
= pipe_hnd
->binding_handle
;
1829 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1831 SEC_FLAG_MAXIMUM_ALLOWED
,
1834 if (!NT_STATUS_IS_OK(status
)) {
1837 if (!NT_STATUS_IS_OK(result
)) {
1842 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1844 SEC_FLAG_MAXIMUM_ALLOWED
,
1848 if (!NT_STATUS_IS_OK(status
)) {
1851 if (!NT_STATUS_IS_OK(result
)) {
1856 /* Create domain user */
1858 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1859 if (!strlower_m(acct_name
)) {
1860 status
= NT_STATUS_INVALID_PARAMETER
;
1864 init_lsa_String(&lsa_acct_name
, acct_name
);
1866 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1874 if (!NT_STATUS_IS_OK(status
)) {
1877 if (!NT_STATUS_IS_OK(result
)) {
1881 if (user_rids
.count
!= 1) {
1882 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1885 if (name_types
.count
!= 1) {
1886 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1890 if (name_types
.ids
[0] != SID_NAME_USER
) {
1891 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1892 name_types
.ids
[0]));
1893 status
= NT_STATUS_INVALID_WORKSTATION
;
1897 user_rid
= user_rids
.ids
[0];
1899 /* Open handle on user */
1901 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1903 SEC_FLAG_MAXIMUM_ALLOWED
,
1907 if (!NT_STATUS_IS_OK(status
)) {
1910 if (!NT_STATUS_IS_OK(result
)) {
1917 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1922 if (!NT_STATUS_IS_OK(status
)) {
1923 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1926 if (!NT_STATUS_IS_OK(result
)) {
1928 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1932 /* now disable and setuser info */
1934 info
->info16
.acct_flags
|= ACB_DISABLED
;
1936 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1941 if (!NT_STATUS_IS_OK(status
)) {
1942 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1945 if (!NT_STATUS_IS_OK(result
)) {
1947 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1951 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1954 if (pipe_hnd
&& b
) {
1955 if (is_valid_policy_hnd(&domain_pol
)) {
1956 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1958 if (is_valid_policy_hnd(&sam_pol
)) {
1959 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1961 TALLOC_FREE(pipe_hnd
);
1971 /****************************************************************
1972 ****************************************************************/
1974 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1976 WERROR werr
= WERR_OK
;
1978 struct smbconf_ctx
*ctx
;
1980 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1981 if (!SBC_ERROR_IS_OK(err
)) {
1982 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
1986 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1988 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1989 if (!SBC_ERROR_IS_OK(err
)) {
1990 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
1994 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1996 if (!SBC_ERROR_IS_OK(err
)) {
1997 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2001 smbconf_delete_global_parameter(ctx
, "realm");
2005 err
= smbconf_set_global_parameter(ctx
, "security", "domain");
2006 if (!SBC_ERROR_IS_OK(err
)) {
2007 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2011 err
= smbconf_set_global_parameter(ctx
, "workgroup",
2012 r
->out
.netbios_domain_name
);
2013 if (!SBC_ERROR_IS_OK(err
)) {
2014 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2018 if (r
->out
.domain_is_ad
) {
2019 err
= smbconf_set_global_parameter(ctx
, "security", "ads");
2020 if (!SBC_ERROR_IS_OK(err
)) {
2021 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2025 err
= smbconf_set_global_parameter(ctx
, "realm",
2026 r
->out
.dns_domain_name
);
2027 if (!SBC_ERROR_IS_OK(err
)) {
2028 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2034 smbconf_shutdown(ctx
);
2038 /****************************************************************
2039 ****************************************************************/
2041 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
2043 WERROR werr
= WERR_OK
;
2045 struct smbconf_ctx
*ctx
;
2047 err
= smbconf_init_reg(r
, &ctx
, NULL
);
2048 if (!SBC_ERROR_IS_OK(err
)) {
2049 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2053 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2055 err
= smbconf_set_global_parameter(ctx
, "security", "user");
2056 if (!SBC_ERROR_IS_OK(err
)) {
2057 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2061 err
= smbconf_delete_global_parameter(ctx
, "workgroup");
2062 if (!SBC_ERROR_IS_OK(err
)) {
2063 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2067 smbconf_delete_global_parameter(ctx
, "realm");
2071 smbconf_shutdown(ctx
);
2075 /****************************************************************
2076 ****************************************************************/
2078 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
2082 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2083 return r
->out
.result
;
2086 if (!r
->in
.modify_config
) {
2090 werr
= do_join_modify_vals_config(r
);
2091 if (!W_ERROR_IS_OK(werr
)) {
2095 lp_load_global(get_dyn_CONFIGFILE());
2097 r
->out
.modified_config
= true;
2098 r
->out
.result
= werr
;
2103 /****************************************************************
2104 ****************************************************************/
2106 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
2110 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2111 return r
->out
.result
;
2114 if (!r
->in
.modify_config
) {
2118 werr
= do_unjoin_modify_vals_config(r
);
2119 if (!W_ERROR_IS_OK(werr
)) {
2123 lp_load_global(get_dyn_CONFIGFILE());
2125 r
->out
.modified_config
= true;
2126 r
->out
.result
= werr
;
2131 /****************************************************************
2132 ****************************************************************/
2134 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
2135 const char *domain_str
,
2136 const char **domain_p
,
2139 char *domain
= NULL
;
2141 const char *p
= NULL
;
2143 if (!domain_str
|| !domain_p
|| !dc_p
) {
2147 p
= strchr_m(domain_str
, '\\');
2150 domain
= talloc_strndup(mem_ctx
, domain_str
,
2151 PTR_DIFF(p
, domain_str
));
2152 dc
= talloc_strdup(mem_ctx
, p
+1);
2157 domain
= talloc_strdup(mem_ctx
, domain_str
);
2173 /****************************************************************
2174 ****************************************************************/
2176 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
2177 struct libnet_JoinCtx
*r
)
2179 if (!r
->in
.domain_name
) {
2180 libnet_join_set_error_string(mem_ctx
, r
,
2181 "No domain name defined");
2182 return WERR_INVALID_PARAMETER
;
2185 if (strlen(r
->in
.machine_name
) > 15) {
2186 libnet_join_set_error_string(mem_ctx
, r
,
2187 "Our netbios name can be at most 15 chars long, "
2188 "\"%s\" is %u chars long\n",
2190 (unsigned int)strlen(r
->in
.machine_name
));
2191 return WERR_INVALID_PARAMETER
;
2194 r
->out
.account_name
= talloc_asprintf(mem_ctx
, "%s$",
2195 r
->in
.machine_name
);
2196 if (r
->out
.account_name
== NULL
) {
2197 libnet_join_set_error_string(mem_ctx
, r
,
2198 "Unable to construct r->out.account_name");
2199 return WERR_NOT_ENOUGH_MEMORY
;
2202 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2205 libnet_join_set_error_string(mem_ctx
, r
,
2206 "Failed to parse domain name");
2207 return WERR_INVALID_PARAMETER
;
2210 if (!r
->in
.admin_domain
) {
2211 char *admin_domain
= NULL
;
2212 char *admin_account
= NULL
;
2215 ok
= split_domain_user(mem_ctx
,
2216 r
->in
.admin_account
,
2220 return WERR_NOT_ENOUGH_MEMORY
;
2223 if (admin_domain
!= NULL
) {
2224 r
->in
.admin_domain
= admin_domain
;
2226 r
->in
.admin_domain
= r
->in
.domain_name
;
2228 r
->in
.admin_account
= admin_account
;
2231 if (!secrets_init()) {
2232 libnet_join_set_error_string(mem_ctx
, r
,
2233 "Unable to open secrets database");
2234 return WERR_CAN_NOT_COMPLETE
;
2240 /****************************************************************
2241 ****************************************************************/
2243 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
2247 /* Try adding dom admins to builtin\admins. Only log failures. */
2248 status
= create_builtin_administrators(domain_sid
);
2249 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2250 DEBUG(10,("Unable to auto-add domain administrators to "
2251 "BUILTIN\\Administrators during join because "
2252 "winbindd must be running.\n"));
2253 } else if (!NT_STATUS_IS_OK(status
)) {
2254 DEBUG(5, ("Failed to auto-add domain administrators to "
2255 "BUILTIN\\Administrators during join: %s\n",
2256 nt_errstr(status
)));
2259 /* Try adding dom users to builtin\users. Only log failures. */
2260 status
= create_builtin_users(domain_sid
);
2261 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2262 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2263 "during join because winbindd must be running.\n"));
2264 } else if (!NT_STATUS_IS_OK(status
)) {
2265 DEBUG(5, ("Failed to auto-add domain administrators to "
2266 "BUILTIN\\Administrators during join: %s\n",
2267 nt_errstr(status
)));
2270 /* Try adding dom guests to builtin\guests. Only log failures. */
2271 status
= create_builtin_guests(domain_sid
);
2272 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2273 DEBUG(10,("Unable to auto-add domain guests to "
2274 "BUILTIN\\Guests during join because "
2275 "winbindd must be running.\n"));
2276 } else if (!NT_STATUS_IS_OK(status
)) {
2277 DEBUG(5, ("Failed to auto-add domain guests to "
2278 "BUILTIN\\Guests during join: %s\n",
2279 nt_errstr(status
)));
2283 /****************************************************************
2284 ****************************************************************/
2286 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
2287 struct libnet_JoinCtx
*r
)
2291 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2292 return r
->out
.result
;
2295 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
2296 werr
= do_JoinConfig(r
);
2297 if (!W_ERROR_IS_OK(werr
)) {
2305 if (r
->out
.domain_is_ad
&&
2306 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2307 ADS_STATUS ads_status
;
2309 ads_status
= libnet_join_post_processing_ads_modify(mem_ctx
, r
);
2310 if (!ADS_ERR_OK(ads_status
)) {
2311 return WERR_GEN_FAILURE
;
2314 #endif /* HAVE_ADS */
2316 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
2317 if (r
->out
.dns_domain_name
) {
2318 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
2321 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2322 return WERR_NERR_SETUPNOTJOINED
;
2325 werr
= do_JoinConfig(r
);
2326 if (!W_ERROR_IS_OK(werr
)) {
2331 if (r
->out
.domain_is_ad
&&
2332 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2333 ADS_STATUS ads_status
;
2335 ads_status
= libnet_join_post_processing_ads_sync(mem_ctx
, r
);
2336 if (!ADS_ERR_OK(ads_status
)) {
2337 return WERR_GEN_FAILURE
;
2340 #endif /* HAVE_ADS */
2342 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
2347 /****************************************************************
2348 ****************************************************************/
2350 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
2353 ads_destroy(&r
->in
.ads
);
2359 /****************************************************************
2360 ****************************************************************/
2362 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
2365 ads_destroy(&r
->in
.ads
);
2371 /****************************************************************
2372 ****************************************************************/
2374 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
2375 struct libnet_JoinCtx
**r
)
2377 struct libnet_JoinCtx
*ctx
;
2379 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
2381 return WERR_NOT_ENOUGH_MEMORY
;
2384 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
2386 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
2387 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
2389 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
2391 ctx
->in
.desired_encryption_types
= ENC_CRC32
|
2394 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2395 ctx
->in
.desired_encryption_types
|= ENC_HMAC_SHA1_96_AES128
;
2397 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2398 ctx
->in
.desired_encryption_types
|= ENC_HMAC_SHA1_96_AES256
;
2406 /****************************************************************
2407 ****************************************************************/
2409 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
2410 struct libnet_UnjoinCtx
**r
)
2412 struct libnet_UnjoinCtx
*ctx
;
2414 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
2416 return WERR_NOT_ENOUGH_MEMORY
;
2419 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
2421 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
2422 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
2429 /****************************************************************
2430 ****************************************************************/
2432 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
2433 struct libnet_JoinCtx
*r
)
2435 bool valid_security
= false;
2436 bool valid_workgroup
= false;
2437 bool valid_realm
= false;
2438 bool ignored_realm
= false;
2440 /* check if configuration is already set correctly */
2442 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
2444 switch (r
->out
.domain_is_ad
) {
2446 valid_security
= (lp_security() == SEC_DOMAIN
)
2447 || (lp_server_role() == ROLE_DOMAIN_PDC
)
2448 || (lp_server_role() == ROLE_DOMAIN_BDC
);
2449 if (valid_workgroup
&& valid_security
) {
2450 /* nothing to be done */
2455 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
2456 switch (lp_security()) {
2458 if (!valid_realm
&& lp_winbind_rpc_only()) {
2460 ignored_realm
= true;
2465 valid_security
= true;
2468 if (valid_workgroup
&& valid_realm
&& valid_security
) {
2469 if (ignored_realm
&& !r
->in
.modify_config
)
2471 libnet_join_set_error_string(mem_ctx
, r
,
2472 "Warning: ignoring realm when "
2473 "joining AD domain with "
2474 "'security=domain' and "
2475 "'winbind rpc only = yes'. "
2476 "(realm set to '%s', "
2477 "should be '%s').", lp_realm(),
2478 r
->out
.dns_domain_name
);
2480 /* nothing to be done */
2486 /* check if we are supposed to manipulate configuration */
2488 if (!r
->in
.modify_config
) {
2490 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
2492 if (!valid_workgroup
) {
2493 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2494 "\"workgroup\" set to '%s', should be '%s'",
2495 lp_workgroup(), r
->out
.netbios_domain_name
);
2496 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2500 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2501 "\"realm\" set to '%s', should be '%s'",
2502 lp_realm(), r
->out
.dns_domain_name
);
2503 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2506 if (!valid_security
) {
2507 const char *sec
= NULL
;
2508 switch (lp_security()) {
2509 case SEC_USER
: sec
= "user"; break;
2510 case SEC_DOMAIN
: sec
= "domain"; break;
2511 case SEC_ADS
: sec
= "ads"; break;
2513 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2514 "\"security\" set to '%s', should be %s",
2515 sec
, r
->out
.domain_is_ad
?
2516 "either 'domain' or 'ads'" : "'domain'");
2517 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2520 libnet_join_set_error_string(mem_ctx
, r
,
2521 "Invalid configuration (%s) and configuration modification "
2522 "was not requested", wrong_conf
);
2523 return WERR_CAN_NOT_COMPLETE
;
2526 /* check if we are able to manipulate configuration */
2528 if (!lp_config_backend_is_registry()) {
2529 libnet_join_set_error_string(mem_ctx
, r
,
2530 "Configuration manipulation requested but not "
2531 "supported by backend");
2532 return WERR_NOT_SUPPORTED
;
2538 /****************************************************************
2539 ****************************************************************/
2541 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
2542 struct libnet_JoinCtx
*r
)
2546 struct cli_state
*cli
= NULL
;
2548 ADS_STATUS ads_status
;
2549 #endif /* HAVE_ADS */
2550 const char *pre_connect_realm
= NULL
;
2551 const char *numeric_dcip
= NULL
;
2552 const char *sitename
= NULL
;
2554 /* Before contacting a DC, we can securely know
2555 * the realm only if the user specifies it.
2557 if (r
->in
.use_kerberos
&&
2558 r
->in
.domain_name_type
== JoinDomNameTypeDNS
) {
2559 pre_connect_realm
= r
->in
.domain_name
;
2562 if (!r
->in
.dc_name
) {
2563 struct netr_DsRGetDCNameInfo
*info
;
2565 uint32_t name_type_flags
= 0;
2566 if (r
->in
.domain_name_type
== JoinDomNameTypeDNS
) {
2567 name_type_flags
= DS_IS_DNS_NAME
;
2568 } else if (r
->in
.domain_name_type
== JoinDomNameTypeNBT
) {
2569 name_type_flags
= DS_IS_FLAT_NAME
;
2571 status
= dsgetdcname(mem_ctx
,
2576 DS_FORCE_REDISCOVERY
|
2577 DS_DIRECTORY_SERVICE_REQUIRED
|
2578 DS_WRITABLE_REQUIRED
|
2579 DS_RETURN_DNS_NAME
|
2582 if (!NT_STATUS_IS_OK(status
)) {
2583 libnet_join_set_error_string(mem_ctx
, r
,
2584 "failed to find DC for domain %s - %s",
2586 get_friendly_nt_error_msg(status
));
2587 return WERR_NERR_DCNOTFOUND
;
2590 dc
= strip_hostname(info
->dc_unc
);
2591 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2592 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2594 if (info
->dc_address
== NULL
|| info
->dc_address
[0] != '\\' ||
2595 info
->dc_address
[1] != '\\') {
2596 DBG_ERR("ill-formed DC address '%s'\n",
2598 return WERR_NERR_DCNOTFOUND
;
2601 numeric_dcip
= info
->dc_address
+ 2;
2602 sitename
= info
->dc_site_name
;
2603 /* info goes out of scope but the memory stays
2604 allocated on the talloc context */
2607 if (pre_connect_realm
!= NULL
) {
2608 struct sockaddr_storage ss
= {0};
2610 if (numeric_dcip
!= NULL
) {
2611 if (!interpret_string_addr(&ss
, numeric_dcip
,
2614 "cannot parse IP address '%s' of DC '%s'\n",
2615 numeric_dcip
, r
->in
.dc_name
);
2616 return WERR_NERR_DCNOTFOUND
;
2619 if (!interpret_string_addr(&ss
, r
->in
.dc_name
, 0)) {
2621 "cannot resolve IP address of DC '%s'\n",
2623 return WERR_NERR_DCNOTFOUND
;
2627 /* The domain parameter is only used as modifier
2628 * to krb5.conf file name. _JOIN_ is is not a valid
2629 * NetBIOS name so it cannot clash with another domain
2632 create_local_private_krb5_conf_for_domain(pre_connect_realm
,
2638 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
2639 if (!NT_STATUS_IS_OK(status
)) {
2640 libnet_join_set_error_string(mem_ctx
, r
,
2641 "failed to lookup DC info for domain '%s' over rpc: %s",
2642 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2643 return ntstatus_to_werror(status
);
2646 werr
= libnet_join_check_config(mem_ctx
, r
);
2647 if (!W_ERROR_IS_OK(werr
)) {
2653 if (r
->out
.domain_is_ad
) {
2654 create_local_private_krb5_conf_for_domain(
2655 r
->out
.dns_domain_name
, r
->out
.netbios_domain_name
,
2656 sitename
, smbXcli_conn_remote_sockaddr(cli
->conn
));
2659 if (r
->out
.domain_is_ad
&&
2660 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2662 const char *initial_account_ou
= r
->in
.account_ou
;
2665 * we want to create the msDS-SupportedEncryptionTypes attribute
2666 * as early as possible so always try an LDAP create as the user
2667 * first. We copy r->in.account_ou because it may be changed
2668 * during the machine pre-creation.
2671 ads_status
= libnet_join_connect_ads_user(mem_ctx
, r
);
2672 if (!ADS_ERR_OK(ads_status
)) {
2673 libnet_join_set_error_string(mem_ctx
, r
,
2674 "failed to connect to AD: %s",
2675 ads_errstr(ads_status
));
2676 return WERR_NERR_DEFAULTJOINREQUIRED
;
2679 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
2680 if (ADS_ERR_OK(ads_status
)) {
2683 * LDAP object creation succeeded.
2685 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
2690 if (initial_account_ou
!= NULL
) {
2691 libnet_join_set_error_string(mem_ctx
, r
,
2692 "failed to precreate account in ou %s: %s",
2694 ads_errstr(ads_status
));
2695 return WERR_NERR_DEFAULTJOINREQUIRED
;
2698 DBG_INFO("Failed to pre-create account in OU %s: %s\n",
2699 r
->in
.account_ou
, ads_errstr(ads_status
));
2701 #endif /* HAVE_ADS */
2703 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
2704 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
2705 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2707 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2709 if (!NT_STATUS_IS_OK(status
)) {
2710 libnet_join_set_error_string(mem_ctx
, r
,
2711 "failed to join domain '%s' over rpc: %s",
2712 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2713 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2714 return WERR_NERR_SETUPALREADYJOINED
;
2716 werr
= ntstatus_to_werror(status
);
2730 /****************************************************************
2731 ****************************************************************/
2733 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2734 struct libnet_JoinCtx
*r
)
2737 struct libnet_UnjoinCtx
*u
= NULL
;
2739 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2740 if (!W_ERROR_IS_OK(werr
)) {
2744 u
->in
.debug
= r
->in
.debug
;
2745 u
->in
.dc_name
= r
->in
.dc_name
;
2746 u
->in
.domain_name
= r
->in
.domain_name
;
2747 u
->in
.admin_account
= r
->in
.admin_account
;
2748 u
->in
.admin_password
= r
->in
.admin_password
;
2749 u
->in
.modify_config
= r
->in
.modify_config
;
2750 u
->in
.use_kerberos
= r
->in
.use_kerberos
;
2751 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2752 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2754 werr
= libnet_Unjoin(mem_ctx
, u
);
2760 /****************************************************************
2761 ****************************************************************/
2763 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2764 struct libnet_JoinCtx
*r
)
2769 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2772 ZERO_STRUCT(r
->out
);
2774 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2775 if (!W_ERROR_IS_OK(werr
)) {
2779 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2780 werr
= libnet_DomainJoin(mem_ctx
, r
);
2781 if (!W_ERROR_IS_OK(werr
)) {
2786 werr
= libnet_join_post_processing(mem_ctx
, r
);
2787 if (!W_ERROR_IS_OK(werr
)) {
2791 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2792 werr
= libnet_join_post_verify(mem_ctx
, r
);
2793 if (!W_ERROR_IS_OK(werr
)) {
2794 libnet_join_rollback(mem_ctx
, r
);
2799 r
->out
.result
= werr
;
2802 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2807 /****************************************************************
2808 ****************************************************************/
2810 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2811 struct libnet_UnjoinCtx
*r
)
2815 if (!r
->in
.domain_sid
) {
2817 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2818 libnet_unjoin_set_error_string(mem_ctx
, r
,
2819 "Unable to fetch domain sid: are we joined?");
2820 return WERR_NERR_SETUPNOTJOINED
;
2822 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2823 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2826 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2827 !r
->in
.delete_machine_account
) {
2828 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2832 if (!r
->in
.dc_name
) {
2833 struct netr_DsRGetDCNameInfo
*info
;
2835 status
= dsgetdcname(mem_ctx
,
2840 DS_DIRECTORY_SERVICE_REQUIRED
|
2841 DS_WRITABLE_REQUIRED
|
2844 if (!NT_STATUS_IS_OK(status
)) {
2845 libnet_unjoin_set_error_string(mem_ctx
, r
,
2846 "failed to find DC for domain %s - %s",
2848 get_friendly_nt_error_msg(status
));
2849 return WERR_NERR_DCNOTFOUND
;
2852 dc
= strip_hostname(info
->dc_unc
);
2853 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2854 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2858 /* for net ads leave, try to delete the account. If it works,
2859 no sense in disabling. If it fails, we can still try to
2862 if (r
->in
.delete_machine_account
) {
2863 ADS_STATUS ads_status
;
2864 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2865 if (ADS_ERR_OK(ads_status
)) {
2867 r
->out
.dns_domain_name
=
2868 talloc_strdup(mem_ctx
,
2869 r
->in
.ads
->server
.realm
);
2871 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2873 if (!ADS_ERR_OK(ads_status
)) {
2874 libnet_unjoin_set_error_string(mem_ctx
, r
,
2875 "failed to remove machine account from AD: %s",
2876 ads_errstr(ads_status
));
2878 r
->out
.deleted_machine_account
= true;
2879 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2880 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2884 #endif /* HAVE_ADS */
2886 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2888 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2889 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2890 if (!NT_STATUS_IS_OK(status
)) {
2891 libnet_unjoin_set_error_string(mem_ctx
, r
,
2892 "failed to disable machine account via rpc: %s",
2893 get_friendly_nt_error_msg(status
));
2894 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2895 return WERR_NERR_SETUPNOTJOINED
;
2897 return ntstatus_to_werror(status
);
2900 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
2902 r
->out
.disabled_machine_account
= true;
2905 /* If disable succeeded or was not requested at all, we
2906 should be getting rid of our end of things */
2908 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2913 /****************************************************************
2914 ****************************************************************/
2916 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2917 struct libnet_UnjoinCtx
*r
)
2919 if (!r
->in
.domain_name
) {
2920 libnet_unjoin_set_error_string(mem_ctx
, r
,
2921 "No domain name defined");
2922 return WERR_INVALID_PARAMETER
;
2925 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2928 libnet_unjoin_set_error_string(mem_ctx
, r
,
2929 "Failed to parse domain name");
2930 return WERR_INVALID_PARAMETER
;
2934 return WERR_NERR_SETUPDOMAINCONTROLLER
;
2937 if (!r
->in
.admin_domain
) {
2938 char *admin_domain
= NULL
;
2939 char *admin_account
= NULL
;
2942 ok
= split_domain_user(mem_ctx
,
2943 r
->in
.admin_account
,
2947 return WERR_NOT_ENOUGH_MEMORY
;
2950 if (admin_domain
!= NULL
) {
2951 r
->in
.admin_domain
= admin_domain
;
2953 r
->in
.admin_domain
= r
->in
.domain_name
;
2955 r
->in
.admin_account
= admin_account
;
2958 if (!secrets_init()) {
2959 libnet_unjoin_set_error_string(mem_ctx
, r
,
2960 "Unable to open secrets database");
2961 return WERR_CAN_NOT_COMPLETE
;
2967 /****************************************************************
2968 ****************************************************************/
2970 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2971 struct libnet_UnjoinCtx
*r
)
2973 saf_delete(r
->out
.netbios_domain_name
);
2974 saf_delete(r
->out
.dns_domain_name
);
2976 return libnet_unjoin_config(r
);
2979 /****************************************************************
2980 ****************************************************************/
2982 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2983 struct libnet_UnjoinCtx
*r
)
2988 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2991 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2992 if (!W_ERROR_IS_OK(werr
)) {
2996 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2997 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2998 if (!W_ERROR_IS_OK(werr
)) {
2999 libnet_unjoin_config(r
);
3004 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
3005 if (!W_ERROR_IS_OK(werr
)) {
3010 r
->out
.result
= werr
;
3013 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);