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 static ADS_STATUS
add_uniq_spn(TALLOC_CTX
*mem_ctx
, const char *spn
,
487 const char ***array
, size_t *num
)
489 bool ok
= ads_element_in_array(*array
, *num
, spn
);
491 ok
= add_string_to_array(mem_ctx
, spn
, array
, num
);
493 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
499 /****************************************************************
500 Set a machines dNSHostName and servicePrincipalName attributes
501 ****************************************************************/
503 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
504 struct libnet_JoinCtx
*r
)
506 TALLOC_CTX
*frame
= talloc_stackframe();
511 const char **spn_array
= NULL
;
514 const char **netbios_aliases
= NULL
;
515 const char **addl_hostnames
= NULL
;
519 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
520 if (!ADS_ERR_OK(status
)) {
524 status
= libnet_join_get_machine_spns(frame
,
526 discard_const_p(char **, &spn_array
),
528 if (!ADS_ERR_OK(status
)) {
529 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
532 /* Windows only creates HOST/shortname & HOST/fqdn. */
534 spn
= talloc_asprintf(frame
, "HOST/%s", r
->in
.machine_name
);
536 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
539 if (!strupper_m(spn
)) {
540 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
544 status
= add_uniq_spn(frame
, spn
, &spn_array
, &num_spns
);
545 if (!ADS_ERR_OK(status
)) {
549 if (r
->in
.dnshostname
!= NULL
) {
550 fstr_sprintf(my_fqdn
, "%s", r
->in
.dnshostname
);
552 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
556 if (!strlower_m(my_fqdn
)) {
557 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
561 spn
= talloc_asprintf(frame
, "HOST/%s", my_fqdn
);
563 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
567 status
= add_uniq_spn(frame
, spn
, &spn_array
, &num_spns
);
568 if (!ADS_ERR_OK(status
)) {
572 for (netbios_aliases
= lp_netbios_aliases();
573 netbios_aliases
!= NULL
&& *netbios_aliases
!= NULL
;
576 * Add HOST/NETBIOSNAME
578 spn
= talloc_asprintf(frame
, "HOST/%s", *netbios_aliases
);
580 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
583 if (!strupper_m(spn
)) {
584 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
588 status
= add_uniq_spn(frame
, spn
, &spn_array
, &num_spns
);
589 if (!ADS_ERR_OK(status
)) {
594 * Add HOST/netbiosname.domainname
596 fstr_sprintf(my_alias
, "%s.%s",
600 spn
= talloc_asprintf(frame
, "HOST/%s", my_alias
);
602 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
606 status
= add_uniq_spn(frame
, spn
, &spn_array
, &num_spns
);
607 if (!ADS_ERR_OK(status
)) {
612 for (addl_hostnames
= lp_additional_dns_hostnames();
613 addl_hostnames
!= NULL
&& *addl_hostnames
!= NULL
;
616 spn
= talloc_asprintf(frame
, "HOST/%s", *addl_hostnames
);
618 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
622 status
= add_uniq_spn(frame
, spn
, &spn_array
, &num_spns
);
623 if (!ADS_ERR_OK(status
)) {
628 /* make sure to NULL terminate the array */
629 spn_array
= talloc_realloc(frame
, spn_array
, const char *, num_spns
+ 1);
630 if (spn_array
== NULL
) {
631 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
634 spn_array
[num_spns
] = NULL
;
636 mods
= ads_init_mods(mem_ctx
);
638 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
642 /* fields of primary importance */
644 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
645 if (!ADS_ERR_OK(status
)) {
649 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
651 if (!ADS_ERR_OK(status
)) {
655 addl_hostnames
= lp_additional_dns_hostnames();
656 if (addl_hostnames
!= NULL
&& *addl_hostnames
!= NULL
) {
657 status
= ads_mod_strlist(mem_ctx
, &mods
,
658 "msDS-AdditionalDnsHostName",
660 if (!ADS_ERR_OK(status
)) {
665 status
= ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
672 /****************************************************************
673 ****************************************************************/
675 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
676 struct libnet_JoinCtx
*r
)
681 if (!r
->in
.create_upn
) {
687 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
688 if (!ADS_ERR_OK(status
)) {
693 const char *realm
= r
->out
.dns_domain_name
;
695 /* in case we are about to generate a keytab during the join
696 * make sure the default upn we create is usable with kinit -k.
699 if (USE_KERBEROS_KEYTAB
) {
700 realm
= talloc_strdup_upper(mem_ctx
,
701 r
->out
.dns_domain_name
);
705 return ADS_ERROR(LDAP_NO_MEMORY
);
708 r
->in
.upn
= talloc_asprintf(mem_ctx
,
713 return ADS_ERROR(LDAP_NO_MEMORY
);
717 /* now do the mods */
719 mods
= ads_init_mods(mem_ctx
);
721 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
724 /* fields of primary importance */
726 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
727 if (!ADS_ERR_OK(status
)) {
728 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
731 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
735 /****************************************************************
736 ****************************************************************/
738 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
739 struct libnet_JoinCtx
*r
)
745 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
751 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
752 if (!ADS_ERR_OK(status
)) {
756 /* now do the mods */
758 mods
= ads_init_mods(mem_ctx
);
760 return ADS_ERROR(LDAP_NO_MEMORY
);
763 if (r
->in
.os_servicepack
) {
765 * if blank string then leave os_sp equal to NULL to force
766 * attribute delete (LDAP_MOD_DELETE)
768 if (!strequal(r
->in
.os_servicepack
,"")) {
769 os_sp
= talloc_strdup(mem_ctx
, r
->in
.os_servicepack
);
772 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s",
773 samba_version_string());
775 if (!os_sp
&& !strequal(r
->in
.os_servicepack
,"")) {
776 return ADS_ERROR(LDAP_NO_MEMORY
);
779 /* fields of primary importance */
781 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
783 if (!ADS_ERR_OK(status
)) {
787 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
789 if (!ADS_ERR_OK(status
)) {
793 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
795 if (!ADS_ERR_OK(status
)) {
799 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
802 /****************************************************************
803 ****************************************************************/
805 static ADS_STATUS
libnet_join_set_etypes(TALLOC_CTX
*mem_ctx
,
806 struct libnet_JoinCtx
*r
)
810 const char *etype_list_str
;
812 etype_list_str
= talloc_asprintf(mem_ctx
, "%d",
813 r
->in
.desired_encryption_types
);
814 if (!etype_list_str
) {
815 return ADS_ERROR(LDAP_NO_MEMORY
);
820 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
821 if (!ADS_ERR_OK(status
)) {
825 if (r
->in
.desired_encryption_types
== r
->out
.set_encryption_types
) {
829 /* now do the mods */
831 mods
= ads_init_mods(mem_ctx
);
833 return ADS_ERROR(LDAP_NO_MEMORY
);
836 status
= ads_mod_str(mem_ctx
, &mods
, "msDS-SupportedEncryptionTypes",
838 if (!ADS_ERR_OK(status
)) {
842 status
= ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
843 if (!ADS_ERR_OK(status
)) {
847 r
->out
.set_encryption_types
= r
->in
.desired_encryption_types
;
852 /****************************************************************
853 ****************************************************************/
855 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
856 struct libnet_JoinCtx
*r
)
858 if (!USE_SYSTEM_KEYTAB
) {
862 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
869 /****************************************************************
870 ****************************************************************/
872 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
873 struct libnet_JoinCtx
*r
)
875 uint32_t domain_func
;
877 const char *salt
= NULL
;
878 char *std_salt
= NULL
;
880 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
881 if (!ADS_ERR_OK(status
)) {
882 libnet_join_set_error_string(mem_ctx
, r
,
883 "failed to determine domain functional level: %s",
888 /* go ahead and setup the default salt */
890 std_salt
= kerberos_standard_des_salt();
892 libnet_join_set_error_string(mem_ctx
, r
,
893 "failed to obtain standard DES salt");
897 salt
= talloc_strdup(mem_ctx
, std_salt
);
904 /* if it's a Windows functional domain, we have to look for the UPN */
906 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
909 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
912 salt
= talloc_strdup(mem_ctx
, upn
);
919 r
->out
.krb5_salt
= salt
;
923 /****************************************************************
924 ****************************************************************/
926 static ADS_STATUS
libnet_join_post_processing_ads_modify(TALLOC_CTX
*mem_ctx
,
927 struct libnet_JoinCtx
*r
)
930 bool need_etype_update
= false;
933 status
= libnet_join_connect_ads_user(mem_ctx
, r
);
934 if (!ADS_ERR_OK(status
)) {
939 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
940 if (!ADS_ERR_OK(status
)) {
941 libnet_join_set_error_string(mem_ctx
, r
,
942 "Failed to set machine spn: %s\n"
943 "Do you have sufficient permissions to create machine "
949 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
950 if (!ADS_ERR_OK(status
)) {
951 libnet_join_set_error_string(mem_ctx
, r
,
952 "failed to set machine os attributes: %s",
957 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
958 if (!ADS_ERR_OK(status
)) {
959 libnet_join_set_error_string(mem_ctx
, r
,
960 "failed to set machine upn: %s",
965 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
966 if (!ADS_ERR_OK(status
)) {
970 if (r
->in
.desired_encryption_types
!= r
->out
.set_encryption_types
) {
971 uint32_t func_level
= 0;
973 status
= ads_domain_func_level(r
->in
.ads
, &func_level
);
974 if (!ADS_ERR_OK(status
)) {
975 libnet_join_set_error_string(mem_ctx
, r
,
976 "failed to query domain controller functional level: %s",
981 if (func_level
>= DS_DOMAIN_FUNCTION_2008
) {
982 need_etype_update
= true;
986 if (need_etype_update
) {
988 * We need to reconnect as machine account in order
989 * to update msDS-SupportedEncryptionTypes reliable
992 if (r
->in
.ads
->auth
.ccache_name
!= NULL
) {
993 ads_kdestroy(r
->in
.ads
->auth
.ccache_name
);
994 r
->in
.ads
->auth
.ccache_name
= NULL
;
997 ads_destroy(&r
->in
.ads
);
999 status
= libnet_join_connect_ads_machine(mem_ctx
, r
);
1000 if (!ADS_ERR_OK(status
)) {
1001 libnet_join_set_error_string(mem_ctx
, r
,
1002 "Failed to connect as machine account: %s",
1003 ads_errstr(status
));
1007 status
= libnet_join_set_etypes(mem_ctx
, r
);
1008 if (!ADS_ERR_OK(status
)) {
1009 libnet_join_set_error_string(mem_ctx
, r
,
1010 "failed to set machine kerberos encryption types: %s",
1011 ads_errstr(status
));
1016 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
1017 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
1023 static ADS_STATUS
libnet_join_post_processing_ads_sync(TALLOC_CTX
*mem_ctx
,
1024 struct libnet_JoinCtx
*r
)
1026 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
1027 libnet_join_set_error_string(mem_ctx
, r
,
1028 "failed to create kerberos keytab");
1029 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
1034 #endif /* HAVE_ADS */
1036 /****************************************************************
1037 Store the machine password and domain SID
1038 ****************************************************************/
1040 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
1041 struct libnet_JoinCtx
*r
)
1045 status
= secrets_store_JoinCtx(r
);
1046 if (!NT_STATUS_IS_OK(status
)) {
1047 DBG_ERR("secrets_store_JoinCtx() failed %s\n",
1055 /****************************************************************
1056 Connect dc's IPC$ share
1057 ****************************************************************/
1059 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
1064 struct cli_state
**cli
)
1066 TALLOC_CTX
*frame
= talloc_stackframe();
1067 bool fallback_after_kerberos
= false;
1068 bool use_ccache
= false;
1069 bool pw_nt_hash
= false;
1070 struct cli_credentials
*creds
= NULL
;
1074 if (use_kerberos
&& pass
) {
1075 fallback_after_kerberos
= true;
1078 creds
= cli_session_creds_init(frame
,
1081 NULL
, /* realm (use default) */
1084 fallback_after_kerberos
,
1087 if (creds
== NULL
) {
1089 return NT_STATUS_NO_MEMORY
;
1092 status
= cli_full_connection_creds(cli
,
1099 SMB_SIGNING_IPC_DEFAULT
);
1100 if (!NT_STATUS_IS_OK(status
)) {
1106 return NT_STATUS_OK
;
1109 /****************************************************************
1110 Lookup domain dc's info
1111 ****************************************************************/
1113 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
1114 struct libnet_JoinCtx
*r
,
1115 struct cli_state
**cli
)
1117 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1118 struct policy_handle lsa_pol
;
1119 NTSTATUS status
, result
;
1120 union lsa_PolicyInformation
*info
= NULL
;
1121 struct dcerpc_binding_handle
*b
;
1122 const char *account
= r
->in
.admin_account
;
1123 const char *domain
= r
->in
.admin_domain
;
1124 const char *password
= r
->in
.admin_password
;
1125 bool use_kerberos
= r
->in
.use_kerberos
;
1127 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) {
1131 use_kerberos
= false;
1134 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1140 if (!NT_STATUS_IS_OK(status
)) {
1144 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
,
1146 if (!NT_STATUS_IS_OK(status
)) {
1147 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
1148 nt_errstr(status
)));
1152 b
= pipe_hnd
->binding_handle
;
1154 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
1155 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1156 if (!NT_STATUS_IS_OK(status
)) {
1160 status
= dcerpc_lsa_QueryInfoPolicy2(b
, mem_ctx
,
1162 LSA_POLICY_INFO_DNS
,
1165 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
1166 r
->out
.domain_is_ad
= true;
1167 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
1168 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
1169 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
1170 r
->out
.domain_guid
= info
->dns
.domain_guid
;
1171 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
1172 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
1175 if (!NT_STATUS_IS_OK(status
)) {
1176 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
1178 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
1181 if (!NT_STATUS_IS_OK(status
)) {
1184 if (!NT_STATUS_IS_OK(result
)) {
1189 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
1190 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
1191 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
1194 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
1195 TALLOC_FREE(pipe_hnd
);
1201 /****************************************************************
1202 Do the domain join unsecure
1203 ****************************************************************/
1205 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
1206 struct libnet_JoinCtx
*r
,
1207 struct cli_state
*cli
)
1209 TALLOC_CTX
*frame
= talloc_stackframe();
1210 struct rpc_pipe_client
*authenticate_pipe
= NULL
;
1211 struct rpc_pipe_client
*passwordset_pipe
= NULL
;
1212 struct cli_credentials
*cli_creds
;
1213 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
1214 struct netlogon_creds_CredentialState
*creds
= NULL
;
1215 uint32_t netlogon_flags
= 0;
1218 DATA_BLOB new_trust_blob
= data_blob_null
;
1221 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
,
1222 &authenticate_pipe
);
1223 if (!NT_STATUS_IS_OK(status
)) {
1228 if (!r
->in
.machine_password
) {
1229 int security
= r
->in
.ads
? SEC_ADS
: SEC_DOMAIN
;
1231 r
->in
.machine_password
= trust_pw_new_value(mem_ctx
,
1232 r
->in
.secure_channel_type
,
1234 if (r
->in
.machine_password
== NULL
) {
1236 return NT_STATUS_NO_MEMORY
;
1240 cli_creds
= cli_credentials_init(talloc_tos());
1241 if (cli_creds
== NULL
) {
1243 return NT_STATUS_NO_MEMORY
;
1246 cli_credentials_set_username(cli_creds
, r
->out
.account_name
,
1248 cli_credentials_set_domain(cli_creds
, r
->in
.domain_name
,
1250 cli_credentials_set_realm(cli_creds
, "", CRED_SPECIFIED
);
1251 cli_credentials_set_secure_channel_type(cli_creds
,
1252 r
->in
.secure_channel_type
);
1254 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
1255 cli_credentials_set_password(cli_creds
, r
->in
.admin_password
,
1258 status
= rpccli_create_netlogon_creds_ctx(
1259 cli_creds
, authenticate_pipe
->desthost
, r
->in
.msg_ctx
,
1260 frame
, &netlogon_creds
);
1261 if (!NT_STATUS_IS_OK(status
)) {
1266 status
= rpccli_setup_netlogon_creds(
1267 cli
, NCACN_NP
, netlogon_creds
, true /* force_reauth */,
1269 if (!NT_STATUS_IS_OK(status
)) {
1274 status
= netlogon_creds_cli_get(netlogon_creds
, frame
, &creds
);
1275 if (!NT_STATUS_IS_OK(status
)) {
1280 netlogon_flags
= creds
->negotiate_flags
;
1283 if (netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
) {
1284 status
= cli_rpc_pipe_open_schannel_with_creds(cli
,
1285 &ndr_table_netlogon
,
1289 if (!NT_STATUS_IS_OK(status
)) {
1294 passwordset_pipe
= authenticate_pipe
;
1297 len
= strlen(r
->in
.machine_password
);
1298 ok
= convert_string_talloc(frame
, CH_UNIX
, CH_UTF16
,
1299 r
->in
.machine_password
, len
,
1300 (void **)&new_trust_blob
.data
,
1301 &new_trust_blob
.length
);
1303 status
= NT_STATUS_UNMAPPABLE_CHARACTER
;
1304 if (errno
== ENOMEM
) {
1305 status
= NT_STATUS_NO_MEMORY
;
1311 status
= netlogon_creds_cli_ServerPasswordSet(netlogon_creds
,
1312 passwordset_pipe
->binding_handle
,
1314 NULL
); /* new_version */
1315 if (!NT_STATUS_IS_OK(status
)) {
1321 return NT_STATUS_OK
;
1324 /****************************************************************
1326 ****************************************************************/
1328 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
1329 struct libnet_JoinCtx
*r
,
1330 struct cli_state
*cli
)
1332 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1333 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1334 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1336 struct lsa_String lsa_acct_name
;
1338 uint32_t acct_flags
= ACB_WSTRUST
;
1339 struct samr_Ids user_rids
;
1340 struct samr_Ids name_types
;
1341 union samr_UserInfo user_info
;
1342 struct dcerpc_binding_handle
*b
= NULL
;
1343 unsigned int old_timeout
= 0;
1345 DATA_BLOB session_key
= data_blob_null
;
1346 struct samr_CryptPassword crypt_pwd
;
1347 struct samr_CryptPasswordEx crypt_pwd_ex
;
1349 ZERO_STRUCT(sam_pol
);
1350 ZERO_STRUCT(domain_pol
);
1351 ZERO_STRUCT(user_pol
);
1353 switch (r
->in
.secure_channel_type
) {
1354 case SEC_CHAN_WKSTA
:
1355 acct_flags
= ACB_WSTRUST
;
1358 acct_flags
= ACB_SVRTRUST
;
1361 return NT_STATUS_INVALID_PARAMETER
;
1364 if (!r
->in
.machine_password
) {
1365 int security
= r
->in
.ads
? SEC_ADS
: SEC_DOMAIN
;
1367 r
->in
.machine_password
= trust_pw_new_value(mem_ctx
,
1368 r
->in
.secure_channel_type
,
1370 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
1373 /* Open the domain */
1375 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
1377 if (!NT_STATUS_IS_OK(status
)) {
1378 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1379 nt_errstr(status
)));
1383 b
= pipe_hnd
->binding_handle
;
1385 status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
1386 if (!NT_STATUS_IS_OK(status
)) {
1387 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1388 nt_errstr(status
)));
1392 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1394 SAMR_ACCESS_ENUM_DOMAINS
1395 | SAMR_ACCESS_LOOKUP_DOMAIN
,
1398 if (!NT_STATUS_IS_OK(status
)) {
1401 if (!NT_STATUS_IS_OK(result
)) {
1406 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1408 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1409 | SAMR_DOMAIN_ACCESS_CREATE_USER
1410 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
1414 if (!NT_STATUS_IS_OK(status
)) {
1417 if (!NT_STATUS_IS_OK(result
)) {
1422 /* Create domain user */
1424 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1425 if (!strlower_m(acct_name
)) {
1426 status
= NT_STATUS_INVALID_PARAMETER
;
1430 init_lsa_String(&lsa_acct_name
, acct_name
);
1432 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
1433 uint32_t access_desired
=
1434 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
1435 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
1436 SAMR_USER_ACCESS_SET_PASSWORD
|
1437 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
1438 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
1439 uint32_t access_granted
= 0;
1441 DEBUG(10,("Creating account with desired access mask: %d\n",
1444 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
1453 if (!NT_STATUS_IS_OK(status
)) {
1458 if (!NT_STATUS_IS_OK(status
) &&
1459 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1461 DEBUG(10,("Creation of workstation account failed: %s\n",
1462 nt_errstr(status
)));
1464 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1465 username/password combo but the user does not have
1466 administrator access. */
1468 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
1469 libnet_join_set_error_string(mem_ctx
, r
,
1470 "User specified does not have "
1471 "administrator privileges");
1477 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1478 if (!(r
->in
.join_flags
&
1479 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
1484 /* We *must* do this.... don't ask... */
1486 if (NT_STATUS_IS_OK(status
)) {
1487 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1491 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1498 if (!NT_STATUS_IS_OK(status
)) {
1501 if (!NT_STATUS_IS_OK(result
)) {
1505 if (user_rids
.count
!= 1) {
1506 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1509 if (name_types
.count
!= 1) {
1510 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1514 if (name_types
.ids
[0] != SID_NAME_USER
) {
1515 DEBUG(0,("%s is not a user account (type=%d)\n",
1516 acct_name
, name_types
.ids
[0]));
1517 status
= NT_STATUS_INVALID_WORKSTATION
;
1521 user_rid
= user_rids
.ids
[0];
1523 /* Open handle on user */
1525 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1527 SEC_FLAG_MAXIMUM_ALLOWED
,
1531 if (!NT_STATUS_IS_OK(status
)) {
1534 if (!NT_STATUS_IS_OK(result
)) {
1539 /* Fill in the additional account flags now */
1541 acct_flags
|= ACB_PWNOEXP
;
1543 /* Set account flags on machine account */
1544 ZERO_STRUCT(user_info
.info16
);
1545 user_info
.info16
.acct_flags
= acct_flags
;
1547 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1549 UserControlInformation
,
1552 if (!NT_STATUS_IS_OK(status
)) {
1553 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1557 libnet_join_set_error_string(mem_ctx
, r
,
1558 "Failed to set account flags for machine account (%s)\n",
1563 if (!NT_STATUS_IS_OK(result
)) {
1566 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1570 libnet_join_set_error_string(mem_ctx
, r
,
1571 "Failed to set account flags for machine account (%s)\n",
1576 /* Set password on machine account - first try level 26 */
1579 * increase the timeout as password filter modules on the DC
1580 * might delay the operation for a significant amount of time
1582 old_timeout
= rpccli_set_timeout(pipe_hnd
, 600000);
1584 status
= init_samr_CryptPasswordEx(r
->in
.machine_password
,
1587 if (!NT_STATUS_IS_OK(status
)) {
1591 user_info
.info26
.password
= crypt_pwd_ex
;
1592 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1594 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1596 UserInternal5InformationNew
,
1600 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
1602 /* retry with level 24 */
1604 status
= init_samr_CryptPassword(r
->in
.machine_password
,
1607 if (!NT_STATUS_IS_OK(status
)) {
1611 user_info
.info24
.password
= crypt_pwd
;
1612 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1614 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1616 UserInternal5Information
,
1622 old_timeout
= rpccli_set_timeout(pipe_hnd
, old_timeout
);
1624 if (!NT_STATUS_IS_OK(status
)) {
1626 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1630 libnet_join_set_error_string(mem_ctx
, r
,
1631 "Failed to set password for machine account (%s)\n",
1635 if (!NT_STATUS_IS_OK(result
)) {
1638 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1642 libnet_join_set_error_string(mem_ctx
, r
,
1643 "Failed to set password for machine account (%s)\n",
1648 status
= NT_STATUS_OK
;
1655 data_blob_clear_free(&session_key
);
1657 if (is_valid_policy_hnd(&sam_pol
)) {
1658 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1660 if (is_valid_policy_hnd(&domain_pol
)) {
1661 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1663 if (is_valid_policy_hnd(&user_pol
)) {
1664 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1666 TALLOC_FREE(pipe_hnd
);
1671 /****************************************************************
1672 ****************************************************************/
1674 NTSTATUS
libnet_join_ok(struct messaging_context
*msg_ctx
,
1675 const char *netbios_domain_name
,
1676 const char *dc_name
,
1677 const bool use_kerberos
)
1679 TALLOC_CTX
*frame
= talloc_stackframe();
1680 struct cli_state
*cli
= NULL
;
1681 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1682 struct cli_credentials
*cli_creds
= NULL
;
1683 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
1684 struct netlogon_creds_CredentialState
*creds
= NULL
;
1685 uint32_t netlogon_flags
= 0;
1691 return NT_STATUS_INVALID_PARAMETER
;
1694 if (!secrets_init()) {
1696 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1699 status
= pdb_get_trust_credentials(netbios_domain_name
, NULL
,
1701 if (!NT_STATUS_IS_OK(status
)) {
1706 /* we don't want any old password */
1707 cli_credentials_set_old_password(cli_creds
, NULL
, CRED_SPECIFIED
);
1710 cli_credentials_set_kerberos_state(cli_creds
,
1711 CRED_MUST_USE_KERBEROS
);
1714 status
= cli_full_connection_creds(&cli
, NULL
,
1720 SMB_SIGNING_IPC_DEFAULT
);
1722 if (!NT_STATUS_IS_OK(status
)) {
1723 struct cli_credentials
*anon_creds
= NULL
;
1725 anon_creds
= cli_credentials_init_anon(frame
);
1726 if (anon_creds
== NULL
) {
1728 return NT_STATUS_NO_MEMORY
;
1731 status
= cli_full_connection_creds(&cli
,
1741 if (!NT_STATUS_IS_OK(status
)) {
1746 status
= rpccli_create_netlogon_creds_ctx(cli_creds
,
1751 if (!NT_STATUS_IS_OK(status
)) {
1757 status
= rpccli_setup_netlogon_creds(cli
, NCACN_NP
,
1759 true, /* force_reauth */
1761 if (!NT_STATUS_IS_OK(status
)) {
1762 DEBUG(0,("connect_to_domain_password_server: "
1763 "unable to open the domain client session to "
1764 "machine %s. Flags[0x%08X] Error was : %s.\n",
1765 dc_name
, (unsigned)netlogon_flags
,
1766 nt_errstr(status
)));
1772 status
= netlogon_creds_cli_get(netlogon_creds
,
1775 if (!NT_STATUS_IS_OK(status
)) {
1780 netlogon_flags
= creds
->negotiate_flags
;
1783 if (!(netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
)) {
1786 return NT_STATUS_OK
;
1789 status
= cli_rpc_pipe_open_schannel_with_creds(
1790 cli
, &ndr_table_netlogon
, NCACN_NP
,
1791 netlogon_creds
, &netlogon_pipe
);
1793 TALLOC_FREE(netlogon_pipe
);
1795 if (!NT_STATUS_IS_OK(status
)) {
1796 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1797 "on netlogon pipe to server %s for domain %s. "
1799 smbXcli_conn_remote_name(cli
->conn
),
1800 netbios_domain_name
, nt_errstr(status
)));
1808 return NT_STATUS_OK
;
1811 /****************************************************************
1812 ****************************************************************/
1814 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1815 struct libnet_JoinCtx
*r
)
1819 status
= libnet_join_ok(r
->in
.msg_ctx
,
1820 r
->out
.netbios_domain_name
,
1822 r
->in
.use_kerberos
);
1823 if (!NT_STATUS_IS_OK(status
)) {
1824 libnet_join_set_error_string(mem_ctx
, r
,
1825 "failed to verify domain membership after joining: %s",
1826 get_friendly_nt_error_msg(status
));
1827 return WERR_NERR_SETUPNOTJOINED
;
1833 /****************************************************************
1834 ****************************************************************/
1836 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1837 struct libnet_UnjoinCtx
*r
)
1840 * TODO: use values from 'struct libnet_UnjoinCtx' ?
1842 return secrets_delete_machine_password_ex(lp_workgroup(), lp_realm());
1845 /****************************************************************
1846 ****************************************************************/
1848 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1849 struct libnet_UnjoinCtx
*r
)
1851 struct cli_state
*cli
= NULL
;
1852 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1853 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1854 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1857 struct lsa_String lsa_acct_name
;
1858 struct samr_Ids user_rids
;
1859 struct samr_Ids name_types
;
1860 union samr_UserInfo
*info
= NULL
;
1861 struct dcerpc_binding_handle
*b
= NULL
;
1863 ZERO_STRUCT(sam_pol
);
1864 ZERO_STRUCT(domain_pol
);
1865 ZERO_STRUCT(user_pol
);
1867 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1868 r
->in
.admin_account
,
1870 r
->in
.admin_password
,
1873 if (!NT_STATUS_IS_OK(status
)) {
1877 /* Open the domain */
1879 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
1881 if (!NT_STATUS_IS_OK(status
)) {
1882 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1883 nt_errstr(status
)));
1887 b
= pipe_hnd
->binding_handle
;
1889 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1891 SEC_FLAG_MAXIMUM_ALLOWED
,
1894 if (!NT_STATUS_IS_OK(status
)) {
1897 if (!NT_STATUS_IS_OK(result
)) {
1902 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1904 SEC_FLAG_MAXIMUM_ALLOWED
,
1908 if (!NT_STATUS_IS_OK(status
)) {
1911 if (!NT_STATUS_IS_OK(result
)) {
1916 /* Create domain user */
1918 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1919 if (!strlower_m(acct_name
)) {
1920 status
= NT_STATUS_INVALID_PARAMETER
;
1924 init_lsa_String(&lsa_acct_name
, acct_name
);
1926 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1934 if (!NT_STATUS_IS_OK(status
)) {
1937 if (!NT_STATUS_IS_OK(result
)) {
1941 if (user_rids
.count
!= 1) {
1942 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1945 if (name_types
.count
!= 1) {
1946 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1950 if (name_types
.ids
[0] != SID_NAME_USER
) {
1951 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1952 name_types
.ids
[0]));
1953 status
= NT_STATUS_INVALID_WORKSTATION
;
1957 user_rid
= user_rids
.ids
[0];
1959 /* Open handle on user */
1961 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1963 SEC_FLAG_MAXIMUM_ALLOWED
,
1967 if (!NT_STATUS_IS_OK(status
)) {
1970 if (!NT_STATUS_IS_OK(result
)) {
1977 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1982 if (!NT_STATUS_IS_OK(status
)) {
1983 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1986 if (!NT_STATUS_IS_OK(result
)) {
1988 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1992 /* now disable and setuser info */
1994 info
->info16
.acct_flags
|= ACB_DISABLED
;
1996 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
2001 if (!NT_STATUS_IS_OK(status
)) {
2002 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
2005 if (!NT_STATUS_IS_OK(result
)) {
2007 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
2011 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
2014 if (pipe_hnd
&& b
) {
2015 if (is_valid_policy_hnd(&domain_pol
)) {
2016 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
2018 if (is_valid_policy_hnd(&sam_pol
)) {
2019 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
2021 TALLOC_FREE(pipe_hnd
);
2031 /****************************************************************
2032 ****************************************************************/
2034 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
2036 WERROR werr
= WERR_OK
;
2038 struct smbconf_ctx
*ctx
;
2040 err
= smbconf_init_reg(r
, &ctx
, NULL
);
2041 if (!SBC_ERROR_IS_OK(err
)) {
2042 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2046 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
2048 err
= smbconf_set_global_parameter(ctx
, "security", "user");
2049 if (!SBC_ERROR_IS_OK(err
)) {
2050 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2054 err
= smbconf_set_global_parameter(ctx
, "workgroup",
2056 if (!SBC_ERROR_IS_OK(err
)) {
2057 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2061 smbconf_delete_global_parameter(ctx
, "realm");
2065 err
= smbconf_set_global_parameter(ctx
, "security", "domain");
2066 if (!SBC_ERROR_IS_OK(err
)) {
2067 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2071 err
= smbconf_set_global_parameter(ctx
, "workgroup",
2072 r
->out
.netbios_domain_name
);
2073 if (!SBC_ERROR_IS_OK(err
)) {
2074 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2078 if (r
->out
.domain_is_ad
) {
2079 err
= smbconf_set_global_parameter(ctx
, "security", "ads");
2080 if (!SBC_ERROR_IS_OK(err
)) {
2081 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2085 err
= smbconf_set_global_parameter(ctx
, "realm",
2086 r
->out
.dns_domain_name
);
2087 if (!SBC_ERROR_IS_OK(err
)) {
2088 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2094 smbconf_shutdown(ctx
);
2098 /****************************************************************
2099 ****************************************************************/
2101 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
2103 WERROR werr
= WERR_OK
;
2105 struct smbconf_ctx
*ctx
;
2107 err
= smbconf_init_reg(r
, &ctx
, NULL
);
2108 if (!SBC_ERROR_IS_OK(err
)) {
2109 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2113 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2115 err
= smbconf_set_global_parameter(ctx
, "security", "user");
2116 if (!SBC_ERROR_IS_OK(err
)) {
2117 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2121 err
= smbconf_delete_global_parameter(ctx
, "workgroup");
2122 if (!SBC_ERROR_IS_OK(err
)) {
2123 werr
= WERR_SERVICE_DOES_NOT_EXIST
;
2127 smbconf_delete_global_parameter(ctx
, "realm");
2131 smbconf_shutdown(ctx
);
2135 /****************************************************************
2136 ****************************************************************/
2138 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
2142 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2143 return r
->out
.result
;
2146 if (!r
->in
.modify_config
) {
2150 werr
= do_join_modify_vals_config(r
);
2151 if (!W_ERROR_IS_OK(werr
)) {
2155 lp_load_global(get_dyn_CONFIGFILE());
2157 r
->out
.modified_config
= true;
2158 r
->out
.result
= werr
;
2163 /****************************************************************
2164 ****************************************************************/
2166 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
2170 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2171 return r
->out
.result
;
2174 if (!r
->in
.modify_config
) {
2178 werr
= do_unjoin_modify_vals_config(r
);
2179 if (!W_ERROR_IS_OK(werr
)) {
2183 lp_load_global(get_dyn_CONFIGFILE());
2185 r
->out
.modified_config
= true;
2186 r
->out
.result
= werr
;
2191 /****************************************************************
2192 ****************************************************************/
2194 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
2195 const char *domain_str
,
2196 const char **domain_p
,
2199 char *domain
= NULL
;
2201 const char *p
= NULL
;
2203 if (!domain_str
|| !domain_p
|| !dc_p
) {
2207 p
= strchr_m(domain_str
, '\\');
2210 domain
= talloc_strndup(mem_ctx
, domain_str
,
2211 PTR_DIFF(p
, domain_str
));
2212 dc
= talloc_strdup(mem_ctx
, p
+1);
2217 domain
= talloc_strdup(mem_ctx
, domain_str
);
2233 /****************************************************************
2234 ****************************************************************/
2236 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
2237 struct libnet_JoinCtx
*r
)
2239 if (!r
->in
.domain_name
) {
2240 libnet_join_set_error_string(mem_ctx
, r
,
2241 "No domain name defined");
2242 return WERR_INVALID_PARAMETER
;
2245 if (strlen(r
->in
.machine_name
) > 15) {
2246 libnet_join_set_error_string(mem_ctx
, r
,
2247 "Our netbios name can be at most 15 chars long, "
2248 "\"%s\" is %u chars long\n",
2250 (unsigned int)strlen(r
->in
.machine_name
));
2251 return WERR_INVALID_PARAMETER
;
2254 r
->out
.account_name
= talloc_asprintf(mem_ctx
, "%s$",
2255 r
->in
.machine_name
);
2256 if (r
->out
.account_name
== NULL
) {
2257 libnet_join_set_error_string(mem_ctx
, r
,
2258 "Unable to construct r->out.account_name");
2259 return WERR_NOT_ENOUGH_MEMORY
;
2262 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2265 libnet_join_set_error_string(mem_ctx
, r
,
2266 "Failed to parse domain name");
2267 return WERR_INVALID_PARAMETER
;
2270 if (!r
->in
.admin_domain
) {
2271 char *admin_domain
= NULL
;
2272 char *admin_account
= NULL
;
2275 ok
= split_domain_user(mem_ctx
,
2276 r
->in
.admin_account
,
2280 return WERR_NOT_ENOUGH_MEMORY
;
2283 if (admin_domain
!= NULL
) {
2284 r
->in
.admin_domain
= admin_domain
;
2286 r
->in
.admin_domain
= r
->in
.domain_name
;
2288 r
->in
.admin_account
= admin_account
;
2291 if (!secrets_init()) {
2292 libnet_join_set_error_string(mem_ctx
, r
,
2293 "Unable to open secrets database");
2294 return WERR_CAN_NOT_COMPLETE
;
2300 /****************************************************************
2301 ****************************************************************/
2303 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
2307 /* Try adding dom admins to builtin\admins. Only log failures. */
2308 status
= create_builtin_administrators(domain_sid
);
2309 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2310 DEBUG(10,("Unable to auto-add domain administrators to "
2311 "BUILTIN\\Administrators during join because "
2312 "winbindd must be running.\n"));
2313 } else if (!NT_STATUS_IS_OK(status
)) {
2314 DEBUG(5, ("Failed to auto-add domain administrators to "
2315 "BUILTIN\\Administrators during join: %s\n",
2316 nt_errstr(status
)));
2319 /* Try adding dom users to builtin\users. Only log failures. */
2320 status
= create_builtin_users(domain_sid
);
2321 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2322 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2323 "during join because winbindd must be running.\n"));
2324 } else if (!NT_STATUS_IS_OK(status
)) {
2325 DEBUG(5, ("Failed to auto-add domain administrators to "
2326 "BUILTIN\\Administrators during join: %s\n",
2327 nt_errstr(status
)));
2330 /* Try adding dom guests to builtin\guests. Only log failures. */
2331 status
= create_builtin_guests(domain_sid
);
2332 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
2333 DEBUG(10,("Unable to auto-add domain guests to "
2334 "BUILTIN\\Guests during join because "
2335 "winbindd must be running.\n"));
2336 } else if (!NT_STATUS_IS_OK(status
)) {
2337 DEBUG(5, ("Failed to auto-add domain guests to "
2338 "BUILTIN\\Guests during join: %s\n",
2339 nt_errstr(status
)));
2343 /****************************************************************
2344 ****************************************************************/
2346 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
2347 struct libnet_JoinCtx
*r
)
2351 if (!W_ERROR_IS_OK(r
->out
.result
)) {
2352 return r
->out
.result
;
2355 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
2356 werr
= do_JoinConfig(r
);
2357 if (!W_ERROR_IS_OK(werr
)) {
2365 if (r
->out
.domain_is_ad
&&
2366 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2367 ADS_STATUS ads_status
;
2369 ads_status
= libnet_join_post_processing_ads_modify(mem_ctx
, r
);
2370 if (!ADS_ERR_OK(ads_status
)) {
2371 return WERR_GEN_FAILURE
;
2374 #endif /* HAVE_ADS */
2376 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
2377 if (r
->out
.dns_domain_name
) {
2378 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
2381 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2382 return WERR_NERR_SETUPNOTJOINED
;
2385 werr
= do_JoinConfig(r
);
2386 if (!W_ERROR_IS_OK(werr
)) {
2391 if (r
->out
.domain_is_ad
&&
2392 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2393 ADS_STATUS ads_status
;
2395 ads_status
= libnet_join_post_processing_ads_sync(mem_ctx
, r
);
2396 if (!ADS_ERR_OK(ads_status
)) {
2397 return WERR_GEN_FAILURE
;
2400 #endif /* HAVE_ADS */
2402 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
2407 /****************************************************************
2408 ****************************************************************/
2410 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
2413 ads_destroy(&r
->in
.ads
);
2419 /****************************************************************
2420 ****************************************************************/
2422 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
2425 ads_destroy(&r
->in
.ads
);
2431 /****************************************************************
2432 ****************************************************************/
2434 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
2435 struct libnet_JoinCtx
**r
)
2437 struct libnet_JoinCtx
*ctx
;
2439 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
2441 return WERR_NOT_ENOUGH_MEMORY
;
2444 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
2446 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
2447 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
2449 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
2451 ctx
->in
.desired_encryption_types
= ENC_CRC32
|
2454 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2455 ctx
->in
.desired_encryption_types
|= ENC_HMAC_SHA1_96_AES128
;
2457 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2458 ctx
->in
.desired_encryption_types
|= ENC_HMAC_SHA1_96_AES256
;
2466 /****************************************************************
2467 ****************************************************************/
2469 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
2470 struct libnet_UnjoinCtx
**r
)
2472 struct libnet_UnjoinCtx
*ctx
;
2474 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
2476 return WERR_NOT_ENOUGH_MEMORY
;
2479 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
2481 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
2482 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
2489 /****************************************************************
2490 ****************************************************************/
2492 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
2493 struct libnet_JoinCtx
*r
)
2495 bool valid_security
= false;
2496 bool valid_workgroup
= false;
2497 bool valid_realm
= false;
2498 bool ignored_realm
= false;
2500 /* check if configuration is already set correctly */
2502 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
2504 switch (r
->out
.domain_is_ad
) {
2506 valid_security
= (lp_security() == SEC_DOMAIN
)
2507 || (lp_server_role() == ROLE_DOMAIN_PDC
)
2508 || (lp_server_role() == ROLE_DOMAIN_BDC
);
2509 if (valid_workgroup
&& valid_security
) {
2510 /* nothing to be done */
2515 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
2516 switch (lp_security()) {
2518 if (!valid_realm
&& lp_winbind_rpc_only()) {
2520 ignored_realm
= true;
2525 valid_security
= true;
2528 if (valid_workgroup
&& valid_realm
&& valid_security
) {
2529 if (ignored_realm
&& !r
->in
.modify_config
)
2531 libnet_join_set_error_string(mem_ctx
, r
,
2532 "Warning: ignoring realm when "
2533 "joining AD domain with "
2534 "'security=domain' and "
2535 "'winbind rpc only = yes'. "
2536 "(realm set to '%s', "
2537 "should be '%s').", lp_realm(),
2538 r
->out
.dns_domain_name
);
2540 /* nothing to be done */
2546 /* check if we are supposed to manipulate configuration */
2548 if (!r
->in
.modify_config
) {
2550 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
2552 if (!valid_workgroup
) {
2553 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2554 "\"workgroup\" set to '%s', should be '%s'",
2555 lp_workgroup(), r
->out
.netbios_domain_name
);
2556 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2560 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2561 "\"realm\" set to '%s', should be '%s'",
2562 lp_realm(), r
->out
.dns_domain_name
);
2563 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2566 if (!valid_security
) {
2567 const char *sec
= NULL
;
2568 switch (lp_security()) {
2569 case SEC_USER
: sec
= "user"; break;
2570 case SEC_DOMAIN
: sec
= "domain"; break;
2571 case SEC_ADS
: sec
= "ads"; break;
2573 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2574 "\"security\" set to '%s', should be %s",
2575 sec
, r
->out
.domain_is_ad
?
2576 "either 'domain' or 'ads'" : "'domain'");
2577 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2580 libnet_join_set_error_string(mem_ctx
, r
,
2581 "Invalid configuration (%s) and configuration modification "
2582 "was not requested", wrong_conf
);
2583 return WERR_CAN_NOT_COMPLETE
;
2586 /* check if we are able to manipulate configuration */
2588 if (!lp_config_backend_is_registry()) {
2589 libnet_join_set_error_string(mem_ctx
, r
,
2590 "Configuration manipulation requested but not "
2591 "supported by backend");
2592 return WERR_NOT_SUPPORTED
;
2598 /****************************************************************
2599 ****************************************************************/
2601 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
2602 struct libnet_JoinCtx
*r
)
2606 struct cli_state
*cli
= NULL
;
2608 ADS_STATUS ads_status
;
2609 #endif /* HAVE_ADS */
2610 const char *pre_connect_realm
= NULL
;
2611 const char *numeric_dcip
= NULL
;
2612 const char *sitename
= NULL
;
2614 /* Before contacting a DC, we can securely know
2615 * the realm only if the user specifies it.
2617 if (r
->in
.use_kerberos
&&
2618 r
->in
.domain_name_type
== JoinDomNameTypeDNS
) {
2619 pre_connect_realm
= r
->in
.domain_name
;
2622 if (!r
->in
.dc_name
) {
2623 struct netr_DsRGetDCNameInfo
*info
;
2625 uint32_t name_type_flags
= 0;
2626 if (r
->in
.domain_name_type
== JoinDomNameTypeDNS
) {
2627 name_type_flags
= DS_IS_DNS_NAME
;
2628 } else if (r
->in
.domain_name_type
== JoinDomNameTypeNBT
) {
2629 name_type_flags
= DS_IS_FLAT_NAME
;
2631 status
= dsgetdcname(mem_ctx
,
2636 DS_FORCE_REDISCOVERY
|
2637 DS_DIRECTORY_SERVICE_REQUIRED
|
2638 DS_WRITABLE_REQUIRED
|
2639 DS_RETURN_DNS_NAME
|
2642 if (!NT_STATUS_IS_OK(status
)) {
2643 libnet_join_set_error_string(mem_ctx
, r
,
2644 "failed to find DC for domain %s - %s",
2646 get_friendly_nt_error_msg(status
));
2647 return WERR_NERR_DCNOTFOUND
;
2650 dc
= strip_hostname(info
->dc_unc
);
2651 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2652 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2654 if (info
->dc_address
== NULL
|| info
->dc_address
[0] != '\\' ||
2655 info
->dc_address
[1] != '\\') {
2656 DBG_ERR("ill-formed DC address '%s'\n",
2658 return WERR_NERR_DCNOTFOUND
;
2661 numeric_dcip
= info
->dc_address
+ 2;
2662 sitename
= info
->dc_site_name
;
2663 /* info goes out of scope but the memory stays
2664 allocated on the talloc context */
2667 if (pre_connect_realm
!= NULL
) {
2668 struct sockaddr_storage ss
= {0};
2670 if (numeric_dcip
!= NULL
) {
2671 if (!interpret_string_addr(&ss
, numeric_dcip
,
2674 "cannot parse IP address '%s' of DC '%s'\n",
2675 numeric_dcip
, r
->in
.dc_name
);
2676 return WERR_NERR_DCNOTFOUND
;
2679 if (!interpret_string_addr(&ss
, r
->in
.dc_name
, 0)) {
2681 "cannot resolve IP address of DC '%s'\n",
2683 return WERR_NERR_DCNOTFOUND
;
2687 /* The domain parameter is only used as modifier
2688 * to krb5.conf file name. _JOIN_ is is not a valid
2689 * NetBIOS name so it cannot clash with another domain
2692 create_local_private_krb5_conf_for_domain(pre_connect_realm
,
2698 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
2699 if (!NT_STATUS_IS_OK(status
)) {
2700 libnet_join_set_error_string(mem_ctx
, r
,
2701 "failed to lookup DC info for domain '%s' over rpc: %s",
2702 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2703 return ntstatus_to_werror(status
);
2706 werr
= libnet_join_check_config(mem_ctx
, r
);
2707 if (!W_ERROR_IS_OK(werr
)) {
2713 if (r
->out
.domain_is_ad
) {
2714 create_local_private_krb5_conf_for_domain(
2715 r
->out
.dns_domain_name
, r
->out
.netbios_domain_name
,
2716 sitename
, smbXcli_conn_remote_sockaddr(cli
->conn
));
2719 if (r
->out
.domain_is_ad
&&
2720 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2722 const char *initial_account_ou
= r
->in
.account_ou
;
2725 * we want to create the msDS-SupportedEncryptionTypes attribute
2726 * as early as possible so always try an LDAP create as the user
2727 * first. We copy r->in.account_ou because it may be changed
2728 * during the machine pre-creation.
2731 ads_status
= libnet_join_connect_ads_user(mem_ctx
, r
);
2732 if (!ADS_ERR_OK(ads_status
)) {
2733 libnet_join_set_error_string(mem_ctx
, r
,
2734 "failed to connect to AD: %s",
2735 ads_errstr(ads_status
));
2736 return WERR_NERR_DEFAULTJOINREQUIRED
;
2739 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
2740 if (ADS_ERR_OK(ads_status
)) {
2743 * LDAP object creation succeeded.
2745 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
2750 if (initial_account_ou
!= NULL
) {
2751 libnet_join_set_error_string(mem_ctx
, r
,
2752 "failed to precreate account in ou %s: %s",
2754 ads_errstr(ads_status
));
2755 return WERR_NERR_DEFAULTJOINREQUIRED
;
2758 DBG_INFO("Failed to pre-create account in OU %s: %s\n",
2759 r
->in
.account_ou
, ads_errstr(ads_status
));
2761 #endif /* HAVE_ADS */
2763 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
2764 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
2765 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2767 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2769 if (!NT_STATUS_IS_OK(status
)) {
2770 libnet_join_set_error_string(mem_ctx
, r
,
2771 "failed to join domain '%s' over rpc: %s",
2772 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2773 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2774 return WERR_NERR_SETUPALREADYJOINED
;
2776 werr
= ntstatus_to_werror(status
);
2790 /****************************************************************
2791 ****************************************************************/
2793 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2794 struct libnet_JoinCtx
*r
)
2797 struct libnet_UnjoinCtx
*u
= NULL
;
2799 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2800 if (!W_ERROR_IS_OK(werr
)) {
2804 u
->in
.debug
= r
->in
.debug
;
2805 u
->in
.dc_name
= r
->in
.dc_name
;
2806 u
->in
.domain_name
= r
->in
.domain_name
;
2807 u
->in
.admin_account
= r
->in
.admin_account
;
2808 u
->in
.admin_password
= r
->in
.admin_password
;
2809 u
->in
.modify_config
= r
->in
.modify_config
;
2810 u
->in
.use_kerberos
= r
->in
.use_kerberos
;
2811 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2812 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2814 werr
= libnet_Unjoin(mem_ctx
, u
);
2820 /****************************************************************
2821 ****************************************************************/
2823 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2824 struct libnet_JoinCtx
*r
)
2829 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2832 ZERO_STRUCT(r
->out
);
2834 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2835 if (!W_ERROR_IS_OK(werr
)) {
2839 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2840 werr
= libnet_DomainJoin(mem_ctx
, r
);
2841 if (!W_ERROR_IS_OK(werr
)) {
2846 werr
= libnet_join_post_processing(mem_ctx
, r
);
2847 if (!W_ERROR_IS_OK(werr
)) {
2851 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2852 werr
= libnet_join_post_verify(mem_ctx
, r
);
2853 if (!W_ERROR_IS_OK(werr
)) {
2854 libnet_join_rollback(mem_ctx
, r
);
2859 r
->out
.result
= werr
;
2862 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2867 /****************************************************************
2868 ****************************************************************/
2870 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2871 struct libnet_UnjoinCtx
*r
)
2875 if (!r
->in
.domain_sid
) {
2877 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2878 libnet_unjoin_set_error_string(mem_ctx
, r
,
2879 "Unable to fetch domain sid: are we joined?");
2880 return WERR_NERR_SETUPNOTJOINED
;
2882 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2883 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2886 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2887 !r
->in
.delete_machine_account
) {
2888 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2892 if (!r
->in
.dc_name
) {
2893 struct netr_DsRGetDCNameInfo
*info
;
2895 status
= dsgetdcname(mem_ctx
,
2900 DS_DIRECTORY_SERVICE_REQUIRED
|
2901 DS_WRITABLE_REQUIRED
|
2904 if (!NT_STATUS_IS_OK(status
)) {
2905 libnet_unjoin_set_error_string(mem_ctx
, r
,
2906 "failed to find DC for domain %s - %s",
2908 get_friendly_nt_error_msg(status
));
2909 return WERR_NERR_DCNOTFOUND
;
2912 dc
= strip_hostname(info
->dc_unc
);
2913 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2914 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2918 /* for net ads leave, try to delete the account. If it works,
2919 no sense in disabling. If it fails, we can still try to
2922 if (r
->in
.delete_machine_account
) {
2923 ADS_STATUS ads_status
;
2924 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2925 if (ADS_ERR_OK(ads_status
)) {
2927 r
->out
.dns_domain_name
=
2928 talloc_strdup(mem_ctx
,
2929 r
->in
.ads
->server
.realm
);
2931 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2933 if (!ADS_ERR_OK(ads_status
)) {
2934 libnet_unjoin_set_error_string(mem_ctx
, r
,
2935 "failed to remove machine account from AD: %s",
2936 ads_errstr(ads_status
));
2938 r
->out
.deleted_machine_account
= true;
2939 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2940 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2944 #endif /* HAVE_ADS */
2946 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2948 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2949 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2950 if (!NT_STATUS_IS_OK(status
)) {
2951 libnet_unjoin_set_error_string(mem_ctx
, r
,
2952 "failed to disable machine account via rpc: %s",
2953 get_friendly_nt_error_msg(status
));
2954 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2955 return WERR_NERR_SETUPNOTJOINED
;
2957 return ntstatus_to_werror(status
);
2960 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
2962 r
->out
.disabled_machine_account
= true;
2965 /* If disable succeeded or was not requested at all, we
2966 should be getting rid of our end of things */
2968 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2973 /****************************************************************
2974 ****************************************************************/
2976 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2977 struct libnet_UnjoinCtx
*r
)
2979 if (!r
->in
.domain_name
) {
2980 libnet_unjoin_set_error_string(mem_ctx
, r
,
2981 "No domain name defined");
2982 return WERR_INVALID_PARAMETER
;
2985 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2988 libnet_unjoin_set_error_string(mem_ctx
, r
,
2989 "Failed to parse domain name");
2990 return WERR_INVALID_PARAMETER
;
2994 return WERR_NERR_SETUPDOMAINCONTROLLER
;
2997 if (!r
->in
.admin_domain
) {
2998 char *admin_domain
= NULL
;
2999 char *admin_account
= NULL
;
3002 ok
= split_domain_user(mem_ctx
,
3003 r
->in
.admin_account
,
3007 return WERR_NOT_ENOUGH_MEMORY
;
3010 if (admin_domain
!= NULL
) {
3011 r
->in
.admin_domain
= admin_domain
;
3013 r
->in
.admin_domain
= r
->in
.domain_name
;
3015 r
->in
.admin_account
= admin_account
;
3018 if (!secrets_init()) {
3019 libnet_unjoin_set_error_string(mem_ctx
, r
,
3020 "Unable to open secrets database");
3021 return WERR_CAN_NOT_COMPLETE
;
3027 /****************************************************************
3028 ****************************************************************/
3030 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
3031 struct libnet_UnjoinCtx
*r
)
3033 saf_delete(r
->out
.netbios_domain_name
);
3034 saf_delete(r
->out
.dns_domain_name
);
3036 return libnet_unjoin_config(r
);
3039 /****************************************************************
3040 ****************************************************************/
3042 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
3043 struct libnet_UnjoinCtx
*r
)
3048 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
3051 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
3052 if (!W_ERROR_IS_OK(werr
)) {
3056 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
3057 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
3058 if (!W_ERROR_IS_OK(werr
)) {
3059 libnet_unjoin_config(r
);
3064 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
3065 if (!W_ERROR_IS_OK(werr
)) {
3070 r
->out
.result
= werr
;
3073 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);