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 DBG_WARNING("Machine account successfully 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 DBG_WARNING("Failed to create machine account\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 status
= init_samr_CryptPasswordEx(r
->in
.machine_password
,
1541 if (!NT_STATUS_IS_OK(status
)) {
1545 user_info
.info26
.password
= crypt_pwd_ex
;
1546 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1548 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1550 UserInternal5InformationNew
,
1554 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
1556 /* retry with level 24 */
1558 status
= init_samr_CryptPassword(r
->in
.machine_password
,
1561 if (!NT_STATUS_IS_OK(status
)) {
1565 user_info
.info24
.password
= crypt_pwd
;
1566 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1568 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1570 UserInternal5Information
,
1576 old_timeout
= rpccli_set_timeout(pipe_hnd
, old_timeout
);
1578 if (!NT_STATUS_IS_OK(status
)) {
1580 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1584 libnet_join_set_error_string(mem_ctx
, r
,
1585 "Failed to set password for machine account (%s)\n",
1589 if (!NT_STATUS_IS_OK(result
)) {
1592 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1596 libnet_join_set_error_string(mem_ctx
, r
,
1597 "Failed to set password for machine account (%s)\n",
1602 status
= NT_STATUS_OK
;
1609 data_blob_clear_free(&session_key
);
1611 if (is_valid_policy_hnd(&sam_pol
)) {
1612 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1614 if (is_valid_policy_hnd(&domain_pol
)) {
1615 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1617 if (is_valid_policy_hnd(&user_pol
)) {
1618 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1620 TALLOC_FREE(pipe_hnd
);
1625 /****************************************************************
1626 ****************************************************************/
1628 NTSTATUS
libnet_join_ok(struct messaging_context
*msg_ctx
,
1629 const char *netbios_domain_name
,
1630 const char *dc_name
,
1631 const bool use_kerberos
)
1633 TALLOC_CTX
*frame
= talloc_stackframe();
1634 struct cli_state
*cli
= NULL
;
1635 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1636 struct cli_credentials
*cli_creds
= NULL
;
1637 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
1638 struct netlogon_creds_CredentialState
*creds
= NULL
;
1639 uint32_t netlogon_flags
= 0;
1645 return NT_STATUS_INVALID_PARAMETER
;
1648 if (!secrets_init()) {
1650 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1653 status
= pdb_get_trust_credentials(netbios_domain_name
, NULL
,
1655 if (!NT_STATUS_IS_OK(status
)) {
1660 /* we don't want any old password */
1661 cli_credentials_set_old_password(cli_creds
, NULL
, CRED_SPECIFIED
);
1664 cli_credentials_set_kerberos_state(cli_creds
,
1665 CRED_MUST_USE_KERBEROS
);
1668 status
= cli_full_connection_creds(&cli
, NULL
,
1674 SMB_SIGNING_IPC_DEFAULT
);
1676 if (!NT_STATUS_IS_OK(status
)) {
1677 status
= cli_full_connection(&cli
, NULL
,
1685 SMB_SIGNING_IPC_DEFAULT
);
1688 if (!NT_STATUS_IS_OK(status
)) {
1693 status
= rpccli_create_netlogon_creds_ctx(cli_creds
,
1698 if (!NT_STATUS_IS_OK(status
)) {
1704 status
= rpccli_setup_netlogon_creds(cli
, NCACN_NP
,
1706 true, /* force_reauth */
1708 if (!NT_STATUS_IS_OK(status
)) {
1709 DEBUG(0,("connect_to_domain_password_server: "
1710 "unable to open the domain client session to "
1711 "machine %s. Flags[0x%08X] Error was : %s.\n",
1712 dc_name
, (unsigned)netlogon_flags
,
1713 nt_errstr(status
)));
1719 status
= netlogon_creds_cli_get(netlogon_creds
,
1722 if (!NT_STATUS_IS_OK(status
)) {
1727 netlogon_flags
= creds
->negotiate_flags
;
1730 if (!(netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
)) {
1733 return NT_STATUS_OK
;
1736 status
= cli_rpc_pipe_open_schannel_with_creds(
1737 cli
, &ndr_table_netlogon
, NCACN_NP
,
1738 netlogon_creds
, &netlogon_pipe
);
1740 TALLOC_FREE(netlogon_pipe
);
1742 if (!NT_STATUS_IS_OK(status
)) {
1743 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1744 "on netlogon pipe to server %s for domain %s. "
1746 smbXcli_conn_remote_name(cli
->conn
),
1747 netbios_domain_name
, nt_errstr(status
)));
1755 return NT_STATUS_OK
;
1758 /****************************************************************
1759 ****************************************************************/
1761 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1762 struct libnet_JoinCtx
*r
)
1766 status
= libnet_join_ok(r
->in
.msg_ctx
,
1767 r
->out
.netbios_domain_name
,
1769 r
->in
.use_kerberos
);
1770 if (!NT_STATUS_IS_OK(status
)) {
1771 libnet_join_set_error_string(mem_ctx
, r
,
1772 "failed to verify domain membership after joining: %s",
1773 get_friendly_nt_error_msg(status
));
1774 return WERR_NERR_SETUPNOTJOINED
;
1780 /****************************************************************
1781 ****************************************************************/
1783 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1784 struct libnet_UnjoinCtx
*r
)
1787 * TODO: use values from 'struct libnet_UnjoinCtx' ?
1789 return secrets_delete_machine_password_ex(lp_workgroup(), lp_realm());
1792 /****************************************************************
1793 ****************************************************************/
1795 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1796 struct libnet_UnjoinCtx
*r
)
1798 struct cli_state
*cli
= NULL
;
1799 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1800 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1801 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1804 struct lsa_String lsa_acct_name
;
1805 struct samr_Ids user_rids
;
1806 struct samr_Ids name_types
;
1807 union samr_UserInfo
*info
= NULL
;
1808 struct dcerpc_binding_handle
*b
= NULL
;
1810 ZERO_STRUCT(sam_pol
);
1811 ZERO_STRUCT(domain_pol
);
1812 ZERO_STRUCT(user_pol
);
1814 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1815 r
->in
.admin_account
,
1817 r
->in
.admin_password
,
1820 if (!NT_STATUS_IS_OK(status
)) {
1824 /* Open the domain */
1826 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
1828 if (!NT_STATUS_IS_OK(status
)) {
1829 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1830 nt_errstr(status
)));
1834 b
= pipe_hnd
->binding_handle
;
1836 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1838 SEC_FLAG_MAXIMUM_ALLOWED
,
1841 if (!NT_STATUS_IS_OK(status
)) {
1844 if (!NT_STATUS_IS_OK(result
)) {
1849 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1851 SEC_FLAG_MAXIMUM_ALLOWED
,
1855 if (!NT_STATUS_IS_OK(status
)) {
1858 if (!NT_STATUS_IS_OK(result
)) {
1863 /* Create domain user */
1865 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1866 if (!strlower_m(acct_name
)) {
1867 status
= NT_STATUS_INVALID_PARAMETER
;
1871 init_lsa_String(&lsa_acct_name
, acct_name
);
1873 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1881 if (!NT_STATUS_IS_OK(status
)) {
1884 if (!NT_STATUS_IS_OK(result
)) {
1888 if (user_rids
.count
!= 1) {
1889 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1892 if (name_types
.count
!= 1) {
1893 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1897 if (name_types
.ids
[0] != SID_NAME_USER
) {
1898 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1899 name_types
.ids
[0]));
1900 status
= NT_STATUS_INVALID_WORKSTATION
;
1904 user_rid
= user_rids
.ids
[0];
1906 /* Open handle on user */
1908 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1910 SEC_FLAG_MAXIMUM_ALLOWED
,
1914 if (!NT_STATUS_IS_OK(status
)) {
1917 if (!NT_STATUS_IS_OK(result
)) {
1924 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1929 if (!NT_STATUS_IS_OK(status
)) {
1930 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1933 if (!NT_STATUS_IS_OK(result
)) {
1935 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1939 /* now disable and setuser info */
1941 info
->info16
.acct_flags
|= ACB_DISABLED
;
1943 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1948 if (!NT_STATUS_IS_OK(status
)) {
1949 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1952 if (!NT_STATUS_IS_OK(result
)) {
1954 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1958 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1961 if (pipe_hnd
&& b
) {
1962 if (is_valid_policy_hnd(&domain_pol
)) {
1963 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1965 if (is_valid_policy_hnd(&sam_pol
)) {
1966 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1968 TALLOC_FREE(pipe_hnd
);
1978 /****************************************************************
1979 ****************************************************************/
1981 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1983 WERROR werr
= WERR_OK
;
1985 struct smbconf_ctx
*ctx
;
1987 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1988 if (!SBC_ERROR_IS_OK(err
)) {
1989 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
1993 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1995 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1996 if (!SBC_ERROR_IS_OK(err
)) {
1997 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2001 err
= smbconf_set_global_parameter(ctx
, "workgroup",
2003 if (!SBC_ERROR_IS_OK(err
)) {
2004 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2008 smbconf_delete_global_parameter(ctx
, "realm");
2012 err
= smbconf_set_global_parameter(ctx
, "security", "domain");
2013 if (!SBC_ERROR_IS_OK(err
)) {
2014 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2018 err
= smbconf_set_global_parameter(ctx
, "workgroup",
2019 r
->out
.netbios_domain_name
);
2020 if (!SBC_ERROR_IS_OK(err
)) {
2021 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2025 if (r
->out
.domain_is_ad
) {
2026 err
= smbconf_set_global_parameter(ctx
, "security", "ads");
2027 if (!SBC_ERROR_IS_OK(err
)) {
2028 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2032 err
= smbconf_set_global_parameter(ctx
, "realm",
2033 r
->out
.dns_domain_name
);
2034 if (!SBC_ERROR_IS_OK(err
)) {
2035 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2041 smbconf_shutdown(ctx
);
2045 /****************************************************************
2046 ****************************************************************/
2048 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
2050 WERROR werr
= WERR_OK
;
2052 struct smbconf_ctx
*ctx
;
2054 err
= smbconf_init_reg(r
, &ctx
, NULL
);
2055 if (!SBC_ERROR_IS_OK(err
)) {
2056 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2060 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2062 err
= smbconf_set_global_parameter(ctx
, "security", "user");
2063 if (!SBC_ERROR_IS_OK(err
)) {
2064 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2068 err
= smbconf_delete_global_parameter(ctx
, "workgroup");
2069 if (!SBC_ERROR_IS_OK(err
)) {
2070 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2074 smbconf_delete_global_parameter(ctx
, "realm");
2078 smbconf_shutdown(ctx
);
2082 /****************************************************************
2083 ****************************************************************/
2085 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
2089 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2090 return r
->out
.result
;
2093 if (!r
->in
.modify_config
) {
2097 werr
= do_join_modify_vals_config(r
);
2098 if (!W_ERROR_IS_OK(werr
)) {
2102 lp_load_global(get_dyn_CONFIGFILE());
2104 r
->out
.modified_config
= true;
2105 r
->out
.result
= werr
;
2110 /****************************************************************
2111 ****************************************************************/
2113 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
2117 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2118 return r
->out
.result
;
2121 if (!r
->in
.modify_config
) {
2125 werr
= do_unjoin_modify_vals_config(r
);
2126 if (!W_ERROR_IS_OK(werr
)) {
2130 lp_load_global(get_dyn_CONFIGFILE());
2132 r
->out
.modified_config
= true;
2133 r
->out
.result
= werr
;
2138 /****************************************************************
2139 ****************************************************************/
2141 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
2142 const char *domain_str
,
2143 const char **domain_p
,
2146 char *domain
= NULL
;
2148 const char *p
= NULL
;
2150 if (!domain_str
|| !domain_p
|| !dc_p
) {
2154 p
= strchr_m(domain_str
, '\\');
2157 domain
= talloc_strndup(mem_ctx
, domain_str
,
2158 PTR_DIFF(p
, domain_str
));
2159 dc
= talloc_strdup(mem_ctx
, p
+1);
2164 domain
= talloc_strdup(mem_ctx
, domain_str
);
2180 /****************************************************************
2181 ****************************************************************/
2183 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
2184 struct libnet_JoinCtx
*r
)
2186 if (!r
->in
.domain_name
) {
2187 libnet_join_set_error_string(mem_ctx
, r
,
2188 "No domain name defined");
2189 return WERR_INVALID_PARAMETER
;
2192 if (strlen(r
->in
.machine_name
) > 15) {
2193 libnet_join_set_error_string(mem_ctx
, r
,
2194 "Our netbios name can be at most 15 chars long, "
2195 "\"%s\" is %u chars long\n",
2197 (unsigned int)strlen(r
->in
.machine_name
));
2198 return WERR_INVALID_PARAMETER
;
2201 r
->out
.account_name
= talloc_asprintf(mem_ctx
, "%s$",
2202 r
->in
.machine_name
);
2203 if (r
->out
.account_name
== NULL
) {
2204 libnet_join_set_error_string(mem_ctx
, r
,
2205 "Unable to construct r->out.account_name");
2206 return WERR_NOT_ENOUGH_MEMORY
;
2209 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2212 libnet_join_set_error_string(mem_ctx
, r
,
2213 "Failed to parse domain name");
2214 return WERR_INVALID_PARAMETER
;
2217 if (!r
->in
.admin_domain
) {
2218 char *admin_domain
= NULL
;
2219 char *admin_account
= NULL
;
2222 ok
= split_domain_user(mem_ctx
,
2223 r
->in
.admin_account
,
2227 return WERR_NOT_ENOUGH_MEMORY
;
2230 if (admin_domain
!= NULL
) {
2231 r
->in
.admin_domain
= admin_domain
;
2233 r
->in
.admin_domain
= r
->in
.domain_name
;
2235 r
->in
.admin_account
= admin_account
;
2238 if (!secrets_init()) {
2239 libnet_join_set_error_string(mem_ctx
, r
,
2240 "Unable to open secrets database");
2241 return WERR_CAN_NOT_COMPLETE
;
2247 /****************************************************************
2248 ****************************************************************/
2250 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
2254 /* Try adding dom admins to builtin\admins. Only log failures. */
2255 status
= create_builtin_administrators(domain_sid
);
2256 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2257 DEBUG(10,("Unable to auto-add domain administrators to "
2258 "BUILTIN\\Administrators during join because "
2259 "winbindd must be running.\n"));
2260 } else if (!NT_STATUS_IS_OK(status
)) {
2261 DEBUG(5, ("Failed to auto-add domain administrators to "
2262 "BUILTIN\\Administrators during join: %s\n",
2263 nt_errstr(status
)));
2266 /* Try adding dom users to builtin\users. Only log failures. */
2267 status
= create_builtin_users(domain_sid
);
2268 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2269 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2270 "during join because winbindd must be running.\n"));
2271 } else if (!NT_STATUS_IS_OK(status
)) {
2272 DEBUG(5, ("Failed to auto-add domain administrators to "
2273 "BUILTIN\\Administrators during join: %s\n",
2274 nt_errstr(status
)));
2277 /* Try adding dom guests to builtin\guests. Only log failures. */
2278 status
= create_builtin_guests(domain_sid
);
2279 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2280 DEBUG(10,("Unable to auto-add domain guests to "
2281 "BUILTIN\\Guests during join because "
2282 "winbindd must be running.\n"));
2283 } else if (!NT_STATUS_IS_OK(status
)) {
2284 DEBUG(5, ("Failed to auto-add domain guests to "
2285 "BUILTIN\\Guests during join: %s\n",
2286 nt_errstr(status
)));
2290 /****************************************************************
2291 ****************************************************************/
2293 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
2294 struct libnet_JoinCtx
*r
)
2298 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2299 return r
->out
.result
;
2302 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
2303 werr
= do_JoinConfig(r
);
2304 if (!W_ERROR_IS_OK(werr
)) {
2312 if (r
->out
.domain_is_ad
&&
2313 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2314 ADS_STATUS ads_status
;
2316 ads_status
= libnet_join_post_processing_ads_modify(mem_ctx
, r
);
2317 if (!ADS_ERR_OK(ads_status
)) {
2318 return WERR_GEN_FAILURE
;
2321 #endif /* HAVE_ADS */
2323 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
2324 if (r
->out
.dns_domain_name
) {
2325 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
2328 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2329 return WERR_NERR_SETUPNOTJOINED
;
2332 werr
= do_JoinConfig(r
);
2333 if (!W_ERROR_IS_OK(werr
)) {
2338 if (r
->out
.domain_is_ad
&&
2339 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2340 ADS_STATUS ads_status
;
2342 ads_status
= libnet_join_post_processing_ads_sync(mem_ctx
, r
);
2343 if (!ADS_ERR_OK(ads_status
)) {
2344 return WERR_GEN_FAILURE
;
2347 #endif /* HAVE_ADS */
2349 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
2354 /****************************************************************
2355 ****************************************************************/
2357 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
2360 ads_destroy(&r
->in
.ads
);
2366 /****************************************************************
2367 ****************************************************************/
2369 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
2372 ads_destroy(&r
->in
.ads
);
2378 /****************************************************************
2379 ****************************************************************/
2381 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
2382 struct libnet_JoinCtx
**r
)
2384 struct libnet_JoinCtx
*ctx
;
2386 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
2388 return WERR_NOT_ENOUGH_MEMORY
;
2391 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
2393 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
2394 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
2396 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
2398 ctx
->in
.desired_encryption_types
= ENC_CRC32
|
2401 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2402 ctx
->in
.desired_encryption_types
|= ENC_HMAC_SHA1_96_AES128
;
2404 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2405 ctx
->in
.desired_encryption_types
|= ENC_HMAC_SHA1_96_AES256
;
2413 /****************************************************************
2414 ****************************************************************/
2416 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
2417 struct libnet_UnjoinCtx
**r
)
2419 struct libnet_UnjoinCtx
*ctx
;
2421 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
2423 return WERR_NOT_ENOUGH_MEMORY
;
2426 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
2428 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
2429 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
2436 /****************************************************************
2437 ****************************************************************/
2439 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
2440 struct libnet_JoinCtx
*r
)
2442 bool valid_security
= false;
2443 bool valid_workgroup
= false;
2444 bool valid_realm
= false;
2445 bool ignored_realm
= false;
2447 /* check if configuration is already set correctly */
2449 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
2451 switch (r
->out
.domain_is_ad
) {
2453 valid_security
= (lp_security() == SEC_DOMAIN
)
2454 || (lp_server_role() == ROLE_DOMAIN_PDC
)
2455 || (lp_server_role() == ROLE_DOMAIN_BDC
);
2456 if (valid_workgroup
&& valid_security
) {
2457 /* nothing to be done */
2462 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
2463 switch (lp_security()) {
2465 if (!valid_realm
&& lp_winbind_rpc_only()) {
2467 ignored_realm
= true;
2472 valid_security
= true;
2475 if (valid_workgroup
&& valid_realm
&& valid_security
) {
2476 if (ignored_realm
&& !r
->in
.modify_config
)
2478 libnet_join_set_error_string(mem_ctx
, r
,
2479 "Warning: ignoring realm when "
2480 "joining AD domain with "
2481 "'security=domain' and "
2482 "'winbind rpc only = yes'. "
2483 "(realm set to '%s', "
2484 "should be '%s').", lp_realm(),
2485 r
->out
.dns_domain_name
);
2487 /* nothing to be done */
2493 /* check if we are supposed to manipulate configuration */
2495 if (!r
->in
.modify_config
) {
2497 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
2499 if (!valid_workgroup
) {
2500 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2501 "\"workgroup\" set to '%s', should be '%s'",
2502 lp_workgroup(), r
->out
.netbios_domain_name
);
2503 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2507 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2508 "\"realm\" set to '%s', should be '%s'",
2509 lp_realm(), r
->out
.dns_domain_name
);
2510 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2513 if (!valid_security
) {
2514 const char *sec
= NULL
;
2515 switch (lp_security()) {
2516 case SEC_USER
: sec
= "user"; break;
2517 case SEC_DOMAIN
: sec
= "domain"; break;
2518 case SEC_ADS
: sec
= "ads"; break;
2520 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2521 "\"security\" set to '%s', should be %s",
2522 sec
, r
->out
.domain_is_ad
?
2523 "either 'domain' or 'ads'" : "'domain'");
2524 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2527 libnet_join_set_error_string(mem_ctx
, r
,
2528 "Invalid configuration (%s) and configuration modification "
2529 "was not requested", wrong_conf
);
2530 return WERR_CAN_NOT_COMPLETE
;
2533 /* check if we are able to manipulate configuration */
2535 if (!lp_config_backend_is_registry()) {
2536 libnet_join_set_error_string(mem_ctx
, r
,
2537 "Configuration manipulation requested but not "
2538 "supported by backend");
2539 return WERR_NOT_SUPPORTED
;
2545 /****************************************************************
2546 ****************************************************************/
2548 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
2549 struct libnet_JoinCtx
*r
)
2553 struct cli_state
*cli
= NULL
;
2555 ADS_STATUS ads_status
;
2556 #endif /* HAVE_ADS */
2557 const char *pre_connect_realm
= NULL
;
2558 const char *numeric_dcip
= NULL
;
2559 const char *sitename
= NULL
;
2561 /* Before contacting a DC, we can securely know
2562 * the realm only if the user specifies it.
2564 if (r
->in
.use_kerberos
&&
2565 r
->in
.domain_name_type
== JoinDomNameTypeDNS
) {
2566 pre_connect_realm
= r
->in
.domain_name
;
2569 if (!r
->in
.dc_name
) {
2570 struct netr_DsRGetDCNameInfo
*info
;
2572 uint32_t name_type_flags
= 0;
2573 if (r
->in
.domain_name_type
== JoinDomNameTypeDNS
) {
2574 name_type_flags
= DS_IS_DNS_NAME
;
2575 } else if (r
->in
.domain_name_type
== JoinDomNameTypeNBT
) {
2576 name_type_flags
= DS_IS_FLAT_NAME
;
2578 status
= dsgetdcname(mem_ctx
,
2583 DS_FORCE_REDISCOVERY
|
2584 DS_DIRECTORY_SERVICE_REQUIRED
|
2585 DS_WRITABLE_REQUIRED
|
2586 DS_RETURN_DNS_NAME
|
2589 if (!NT_STATUS_IS_OK(status
)) {
2590 libnet_join_set_error_string(mem_ctx
, r
,
2591 "failed to find DC for domain %s - %s",
2593 get_friendly_nt_error_msg(status
));
2594 return WERR_NERR_DCNOTFOUND
;
2597 dc
= strip_hostname(info
->dc_unc
);
2598 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2599 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2601 if (info
->dc_address
== NULL
|| info
->dc_address
[0] != '\\' ||
2602 info
->dc_address
[1] != '\\') {
2603 DBG_ERR("ill-formed DC address '%s'\n",
2605 return WERR_NERR_DCNOTFOUND
;
2608 numeric_dcip
= info
->dc_address
+ 2;
2609 sitename
= info
->dc_site_name
;
2610 /* info goes out of scope but the memory stays
2611 allocated on the talloc context */
2614 if (pre_connect_realm
!= NULL
) {
2615 struct sockaddr_storage ss
= {0};
2617 if (numeric_dcip
!= NULL
) {
2618 if (!interpret_string_addr(&ss
, numeric_dcip
,
2621 "cannot parse IP address '%s' of DC '%s'\n",
2622 numeric_dcip
, r
->in
.dc_name
);
2623 return WERR_NERR_DCNOTFOUND
;
2626 if (!interpret_string_addr(&ss
, r
->in
.dc_name
, 0)) {
2628 "cannot resolve IP address of DC '%s'\n",
2630 return WERR_NERR_DCNOTFOUND
;
2634 /* The domain parameter is only used as modifier
2635 * to krb5.conf file name. _JOIN_ is is not a valid
2636 * NetBIOS name so it cannot clash with another domain
2639 create_local_private_krb5_conf_for_domain(pre_connect_realm
,
2645 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
2646 if (!NT_STATUS_IS_OK(status
)) {
2647 libnet_join_set_error_string(mem_ctx
, r
,
2648 "failed to lookup DC info for domain '%s' over rpc: %s",
2649 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2650 return ntstatus_to_werror(status
);
2653 werr
= libnet_join_check_config(mem_ctx
, r
);
2654 if (!W_ERROR_IS_OK(werr
)) {
2660 if (r
->out
.domain_is_ad
) {
2661 create_local_private_krb5_conf_for_domain(
2662 r
->out
.dns_domain_name
, r
->out
.netbios_domain_name
,
2663 sitename
, smbXcli_conn_remote_sockaddr(cli
->conn
));
2666 if (r
->out
.domain_is_ad
&&
2667 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2669 const char *initial_account_ou
= r
->in
.account_ou
;
2672 * we want to create the msDS-SupportedEncryptionTypes attribute
2673 * as early as possible so always try an LDAP create as the user
2674 * first. We copy r->in.account_ou because it may be changed
2675 * during the machine pre-creation.
2678 ads_status
= libnet_join_connect_ads_user(mem_ctx
, r
);
2679 if (!ADS_ERR_OK(ads_status
)) {
2680 libnet_join_set_error_string(mem_ctx
, r
,
2681 "failed to connect to AD: %s",
2682 ads_errstr(ads_status
));
2683 return WERR_NERR_DEFAULTJOINREQUIRED
;
2686 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
2687 if (ADS_ERR_OK(ads_status
)) {
2690 * LDAP object creation succeeded.
2692 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
2697 if (initial_account_ou
!= NULL
) {
2698 libnet_join_set_error_string(mem_ctx
, r
,
2699 "failed to precreate account in ou %s: %s",
2701 ads_errstr(ads_status
));
2702 return WERR_NERR_DEFAULTJOINREQUIRED
;
2705 DBG_INFO("Failed to pre-create account in OU %s: %s\n",
2706 r
->in
.account_ou
, ads_errstr(ads_status
));
2708 #endif /* HAVE_ADS */
2710 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
2711 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
2712 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2714 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2716 if (!NT_STATUS_IS_OK(status
)) {
2717 libnet_join_set_error_string(mem_ctx
, r
,
2718 "failed to join domain '%s' over rpc: %s",
2719 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2720 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2721 return WERR_NERR_SETUPALREADYJOINED
;
2723 werr
= ntstatus_to_werror(status
);
2737 /****************************************************************
2738 ****************************************************************/
2740 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2741 struct libnet_JoinCtx
*r
)
2744 struct libnet_UnjoinCtx
*u
= NULL
;
2746 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2747 if (!W_ERROR_IS_OK(werr
)) {
2751 u
->in
.debug
= r
->in
.debug
;
2752 u
->in
.dc_name
= r
->in
.dc_name
;
2753 u
->in
.domain_name
= r
->in
.domain_name
;
2754 u
->in
.admin_account
= r
->in
.admin_account
;
2755 u
->in
.admin_password
= r
->in
.admin_password
;
2756 u
->in
.modify_config
= r
->in
.modify_config
;
2757 u
->in
.use_kerberos
= r
->in
.use_kerberos
;
2758 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2759 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2761 werr
= libnet_Unjoin(mem_ctx
, u
);
2767 /****************************************************************
2768 ****************************************************************/
2770 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2771 struct libnet_JoinCtx
*r
)
2776 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2779 ZERO_STRUCT(r
->out
);
2781 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2782 if (!W_ERROR_IS_OK(werr
)) {
2786 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2787 werr
= libnet_DomainJoin(mem_ctx
, r
);
2788 if (!W_ERROR_IS_OK(werr
)) {
2793 werr
= libnet_join_post_processing(mem_ctx
, r
);
2794 if (!W_ERROR_IS_OK(werr
)) {
2798 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2799 werr
= libnet_join_post_verify(mem_ctx
, r
);
2800 if (!W_ERROR_IS_OK(werr
)) {
2801 libnet_join_rollback(mem_ctx
, r
);
2806 r
->out
.result
= werr
;
2809 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2814 /****************************************************************
2815 ****************************************************************/
2817 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2818 struct libnet_UnjoinCtx
*r
)
2822 if (!r
->in
.domain_sid
) {
2824 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2825 libnet_unjoin_set_error_string(mem_ctx
, r
,
2826 "Unable to fetch domain sid: are we joined?");
2827 return WERR_NERR_SETUPNOTJOINED
;
2829 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2830 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2833 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2834 !r
->in
.delete_machine_account
) {
2835 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2839 if (!r
->in
.dc_name
) {
2840 struct netr_DsRGetDCNameInfo
*info
;
2842 status
= dsgetdcname(mem_ctx
,
2847 DS_DIRECTORY_SERVICE_REQUIRED
|
2848 DS_WRITABLE_REQUIRED
|
2851 if (!NT_STATUS_IS_OK(status
)) {
2852 libnet_unjoin_set_error_string(mem_ctx
, r
,
2853 "failed to find DC for domain %s - %s",
2855 get_friendly_nt_error_msg(status
));
2856 return WERR_NERR_DCNOTFOUND
;
2859 dc
= strip_hostname(info
->dc_unc
);
2860 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2861 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2865 /* for net ads leave, try to delete the account. If it works,
2866 no sense in disabling. If it fails, we can still try to
2869 if (r
->in
.delete_machine_account
) {
2870 ADS_STATUS ads_status
;
2871 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2872 if (ADS_ERR_OK(ads_status
)) {
2874 r
->out
.dns_domain_name
=
2875 talloc_strdup(mem_ctx
,
2876 r
->in
.ads
->server
.realm
);
2878 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2880 if (!ADS_ERR_OK(ads_status
)) {
2881 libnet_unjoin_set_error_string(mem_ctx
, r
,
2882 "failed to remove machine account from AD: %s",
2883 ads_errstr(ads_status
));
2885 r
->out
.deleted_machine_account
= true;
2886 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2887 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2891 #endif /* HAVE_ADS */
2893 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2895 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2896 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2897 if (!NT_STATUS_IS_OK(status
)) {
2898 libnet_unjoin_set_error_string(mem_ctx
, r
,
2899 "failed to disable machine account via rpc: %s",
2900 get_friendly_nt_error_msg(status
));
2901 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2902 return WERR_NERR_SETUPNOTJOINED
;
2904 return ntstatus_to_werror(status
);
2907 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
2909 r
->out
.disabled_machine_account
= true;
2912 /* If disable succeeded or was not requested at all, we
2913 should be getting rid of our end of things */
2915 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2920 /****************************************************************
2921 ****************************************************************/
2923 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2924 struct libnet_UnjoinCtx
*r
)
2926 if (!r
->in
.domain_name
) {
2927 libnet_unjoin_set_error_string(mem_ctx
, r
,
2928 "No domain name defined");
2929 return WERR_INVALID_PARAMETER
;
2932 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2935 libnet_unjoin_set_error_string(mem_ctx
, r
,
2936 "Failed to parse domain name");
2937 return WERR_INVALID_PARAMETER
;
2941 return WERR_NERR_SETUPDOMAINCONTROLLER
;
2944 if (!r
->in
.admin_domain
) {
2945 char *admin_domain
= NULL
;
2946 char *admin_account
= NULL
;
2949 ok
= split_domain_user(mem_ctx
,
2950 r
->in
.admin_account
,
2954 return WERR_NOT_ENOUGH_MEMORY
;
2957 if (admin_domain
!= NULL
) {
2958 r
->in
.admin_domain
= admin_domain
;
2960 r
->in
.admin_domain
= r
->in
.domain_name
;
2962 r
->in
.admin_account
= admin_account
;
2965 if (!secrets_init()) {
2966 libnet_unjoin_set_error_string(mem_ctx
, r
,
2967 "Unable to open secrets database");
2968 return WERR_CAN_NOT_COMPLETE
;
2974 /****************************************************************
2975 ****************************************************************/
2977 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2978 struct libnet_UnjoinCtx
*r
)
2980 saf_delete(r
->out
.netbios_domain_name
);
2981 saf_delete(r
->out
.dns_domain_name
);
2983 return libnet_unjoin_config(r
);
2986 /****************************************************************
2987 ****************************************************************/
2989 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2990 struct libnet_UnjoinCtx
*r
)
2995 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2998 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2999 if (!W_ERROR_IS_OK(werr
)) {
3003 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
3004 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
3005 if (!W_ERROR_IS_OK(werr
)) {
3006 libnet_unjoin_config(r
);
3011 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
3012 if (!W_ERROR_IS_OK(werr
)) {
3017 r
->out
.result
= werr
;
3020 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);