2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Gerald (Jerry) Carter 2006
5 * Copyright (C) Guenther Deschner 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "librpc/gen_ndr/ndr_libnet_join.h"
24 #include "libnet/libnet_join.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_lsa_c.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/ndr_netlogon.h"
31 #include "rpc_client/cli_netlogon.h"
32 #include "lib/smbconf/smbconf.h"
33 #include "lib/smbconf/smbconf_reg.h"
34 #include "../libds/common/flags.h"
36 #include "rpc_client/init_lsa.h"
37 #include "rpc_client/cli_pipe.h"
38 #include "../libcli/security/security.h"
40 #include "libsmb/libsmb.h"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/param/loadparm.h"
44 /****************************************************************
45 ****************************************************************/
47 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
50 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
51 DEBUG(1,("libnet_Join:\n%s", str)); \
55 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
56 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
57 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
58 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
60 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
63 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
64 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
68 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
69 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
70 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
71 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
73 /****************************************************************
74 ****************************************************************/
76 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
77 struct libnet_JoinCtx
*r
,
78 const char *format
, ...)
82 if (r
->out
.error_string
) {
86 va_start(args
, format
);
87 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
91 /****************************************************************
92 ****************************************************************/
94 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
95 struct libnet_UnjoinCtx
*r
,
96 const char *format
, ...)
100 if (r
->out
.error_string
) {
104 va_start(args
, format
);
105 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
111 /****************************************************************
112 ****************************************************************/
114 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
115 const char *netbios_domain_name
,
117 const char *user_name
,
118 const char *password
,
122 ADS_STRUCT
*my_ads
= NULL
;
125 my_ads
= ads_init(dns_domain_name
,
129 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
133 SAFE_FREE(my_ads
->auth
.user_name
);
134 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
135 if ((cp
= strchr_m(my_ads
->auth
.user_name
, '@'))!=0) {
137 SAFE_FREE(my_ads
->auth
.realm
);
138 my_ads
->auth
.realm
= smb_xstrdup(cp
);
139 if (!strupper_m(my_ads
->auth
.realm
)) {
140 ads_destroy(&my_ads
);
141 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
147 SAFE_FREE(my_ads
->auth
.password
);
148 my_ads
->auth
.password
= SMB_STRDUP(password
);
151 status
= ads_connect_user_creds(my_ads
);
152 if (!ADS_ERR_OK(status
)) {
153 ads_destroy(&my_ads
);
161 /****************************************************************
162 ****************************************************************/
164 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
165 struct libnet_JoinCtx
*r
)
169 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
170 r
->out
.netbios_domain_name
,
173 r
->in
.admin_password
,
175 if (!ADS_ERR_OK(status
)) {
176 libnet_join_set_error_string(mem_ctx
, r
,
177 "failed to connect to AD: %s",
182 if (!r
->out
.netbios_domain_name
) {
183 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
184 r
->in
.ads
->server
.workgroup
);
185 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
188 if (!r
->out
.dns_domain_name
) {
189 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
190 r
->in
.ads
->config
.realm
);
191 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
194 r
->out
.domain_is_ad
= true;
199 /****************************************************************
200 ****************************************************************/
202 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
203 struct libnet_UnjoinCtx
*r
)
207 status
= libnet_connect_ads(r
->in
.domain_name
,
211 r
->in
.admin_password
,
213 if (!ADS_ERR_OK(status
)) {
214 libnet_unjoin_set_error_string(mem_ctx
, r
,
215 "failed to connect to AD: %s",
222 /****************************************************************
223 join a domain using ADS (LDAP mods)
224 ****************************************************************/
226 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
227 struct libnet_JoinCtx
*r
)
230 LDAPMessage
*res
= NULL
;
231 const char *attrs
[] = { "dn", NULL
};
234 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
235 if (!ADS_ERR_OK(status
)) {
239 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
240 if (!ADS_ERR_OK(status
)) {
244 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
245 ads_msgfree(r
->in
.ads
, res
);
246 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
249 ads_msgfree(r
->in
.ads
, res
);
251 /* Attempt to create the machine account and bail if this fails.
252 Assume that the admin wants exactly what they requested */
254 status
= ads_create_machine_acct(r
->in
.ads
,
258 if (ADS_ERR_OK(status
)) {
259 DEBUG(1,("machine account creation created\n"));
261 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
262 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
263 status
= ADS_SUCCESS
;
266 if (!ADS_ERR_OK(status
)) {
267 DEBUG(1,("machine account creation failed\n"));
271 status
= ads_move_machine_acct(r
->in
.ads
,
275 if (!ADS_ERR_OK(status
)) {
276 DEBUG(1,("failure to locate/move pre-existing "
277 "machine account\n"));
281 DEBUG(1,("The machine account %s the specified OU.\n",
282 moved
? "was moved into" : "already exists in"));
287 /****************************************************************
288 ****************************************************************/
290 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
291 struct libnet_UnjoinCtx
*r
)
296 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
297 if (!ADS_ERR_OK(status
)) {
298 libnet_unjoin_set_error_string(mem_ctx
, r
,
299 "failed to connect to AD: %s",
305 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
306 if (!ADS_ERR_OK(status
)) {
307 libnet_unjoin_set_error_string(mem_ctx
, r
,
308 "failed to leave realm: %s",
316 /****************************************************************
317 ****************************************************************/
319 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
320 struct libnet_JoinCtx
*r
)
323 LDAPMessage
*res
= NULL
;
326 if (!r
->in
.machine_name
) {
327 return ADS_ERROR(LDAP_NO_MEMORY
);
330 status
= ads_find_machine_acct(r
->in
.ads
,
333 if (!ADS_ERR_OK(status
)) {
337 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
338 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
342 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
344 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
348 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
350 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
355 ads_msgfree(r
->in
.ads
, res
);
361 static ADS_STATUS
libnet_join_get_machine_spns(TALLOC_CTX
*mem_ctx
,
362 struct libnet_JoinCtx
*r
,
368 if (r
->in
.machine_name
== NULL
) {
369 return ADS_ERROR_SYSTEM(EINVAL
);
372 status
= ads_get_service_principal_names(mem_ctx
,
381 /****************************************************************
382 Set a machines dNSHostName and servicePrincipalName attributes
383 ****************************************************************/
385 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
386 struct libnet_JoinCtx
*r
)
391 const char **spn_array
= NULL
;
398 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
399 if (!ADS_ERR_OK(status
)) {
403 status
= libnet_join_get_machine_spns(mem_ctx
,
405 discard_const_p(char **, &spn_array
),
407 if (!ADS_ERR_OK(status
)) {
408 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
411 /* Windows only creates HOST/shortname & HOST/fqdn. */
413 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
415 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
417 if (!strupper_m(spn
)) {
418 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
421 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
423 ok
= add_string_to_array(spn_array
, spn
,
424 &spn_array
, (int *)&num_spns
);
426 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
430 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
431 || (strchr(my_fqdn
, '.') == NULL
)) {
432 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
433 r
->out
.dns_domain_name
);
436 if (!strlower_m(my_fqdn
)) {
437 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
440 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
441 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
443 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
446 ok
= ads_element_in_array(spn_array
, num_spns
, spn
);
448 ok
= add_string_to_array(spn_array
, spn
,
449 &spn_array
, (int *)&num_spns
);
451 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
456 /* make sure to NULL terminate the array */
457 spn_array
= talloc_realloc(mem_ctx
, spn_array
, const char *, num_spns
+ 1);
458 if (spn_array
== NULL
) {
459 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
461 spn_array
[num_spns
] = NULL
;
463 mods
= ads_init_mods(mem_ctx
);
465 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
468 /* fields of primary importance */
470 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
471 if (!ADS_ERR_OK(status
)) {
472 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
475 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
477 if (!ADS_ERR_OK(status
)) {
478 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
481 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
484 /****************************************************************
485 ****************************************************************/
487 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
488 struct libnet_JoinCtx
*r
)
493 if (!r
->in
.create_upn
) {
499 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
500 if (!ADS_ERR_OK(status
)) {
505 r
->in
.upn
= talloc_asprintf(mem_ctx
,
508 r
->out
.dns_domain_name
);
510 return ADS_ERROR(LDAP_NO_MEMORY
);
514 /* now do the mods */
516 mods
= ads_init_mods(mem_ctx
);
518 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
521 /* fields of primary importance */
523 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
524 if (!ADS_ERR_OK(status
)) {
525 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
528 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
532 /****************************************************************
533 ****************************************************************/
535 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
536 struct libnet_JoinCtx
*r
)
542 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
548 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
549 if (!ADS_ERR_OK(status
)) {
553 /* now do the mods */
555 mods
= ads_init_mods(mem_ctx
);
557 return ADS_ERROR(LDAP_NO_MEMORY
);
560 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
562 return ADS_ERROR(LDAP_NO_MEMORY
);
565 /* fields of primary importance */
567 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
569 if (!ADS_ERR_OK(status
)) {
573 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
575 if (!ADS_ERR_OK(status
)) {
579 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
581 if (!ADS_ERR_OK(status
)) {
585 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
588 /****************************************************************
589 ****************************************************************/
591 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
592 struct libnet_JoinCtx
*r
)
594 if (!USE_SYSTEM_KEYTAB
) {
598 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
605 /****************************************************************
606 ****************************************************************/
608 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
609 struct libnet_JoinCtx
*r
)
611 uint32_t domain_func
;
613 const char *salt
= NULL
;
614 char *std_salt
= NULL
;
616 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
617 if (!ADS_ERR_OK(status
)) {
618 libnet_join_set_error_string(mem_ctx
, r
,
619 "failed to determine domain functional level: %s",
624 /* go ahead and setup the default salt */
626 std_salt
= kerberos_standard_des_salt();
628 libnet_join_set_error_string(mem_ctx
, r
,
629 "failed to obtain standard DES salt");
633 salt
= talloc_strdup(mem_ctx
, std_salt
);
640 /* if it's a Windows functional domain, we have to look for the UPN */
642 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
645 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
648 salt
= talloc_strdup(mem_ctx
, upn
);
655 return kerberos_secrets_store_des_salt(salt
);
658 /****************************************************************
659 ****************************************************************/
661 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
662 struct libnet_JoinCtx
*r
)
667 status
= libnet_join_connect_ads(mem_ctx
, r
);
668 if (!ADS_ERR_OK(status
)) {
673 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
674 if (!ADS_ERR_OK(status
)) {
675 libnet_join_set_error_string(mem_ctx
, r
,
676 "failed to set machine spn: %s",
681 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
682 if (!ADS_ERR_OK(status
)) {
683 libnet_join_set_error_string(mem_ctx
, r
,
684 "failed to set machine os attributes: %s",
689 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
690 if (!ADS_ERR_OK(status
)) {
691 libnet_join_set_error_string(mem_ctx
, r
,
692 "failed to set machine upn: %s",
697 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
698 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
701 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
702 libnet_join_set_error_string(mem_ctx
, r
,
703 "failed to create kerberos keytab");
704 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
709 #endif /* HAVE_ADS */
711 /****************************************************************
712 Store the machine password and domain SID
713 ****************************************************************/
715 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
716 struct libnet_JoinCtx
*r
)
718 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
721 DEBUG(1,("Failed to save domain sid\n"));
725 if (!secrets_store_machine_password(r
->in
.machine_password
,
726 r
->out
.netbios_domain_name
,
727 r
->in
.secure_channel_type
))
729 DEBUG(1,("Failed to save machine password\n"));
736 /****************************************************************
737 Connect dc's IPC$ share
738 ****************************************************************/
740 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
744 struct cli_state
**cli
)
749 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
752 if (use_kerberos
&& pass
) {
753 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
756 return cli_full_connection(cli
, NULL
,
764 SMB_SIGNING_DEFAULT
);
767 /****************************************************************
768 Lookup domain dc's info
769 ****************************************************************/
771 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
772 struct libnet_JoinCtx
*r
,
773 struct cli_state
**cli
)
775 struct rpc_pipe_client
*pipe_hnd
= NULL
;
776 struct policy_handle lsa_pol
;
777 NTSTATUS status
, result
;
778 union lsa_PolicyInformation
*info
= NULL
;
779 struct dcerpc_binding_handle
*b
;
781 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
783 r
->in
.admin_password
,
786 if (!NT_STATUS_IS_OK(status
)) {
790 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
792 if (!NT_STATUS_IS_OK(status
)) {
793 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
798 b
= pipe_hnd
->binding_handle
;
800 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
801 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
802 if (!NT_STATUS_IS_OK(status
)) {
806 status
= dcerpc_lsa_QueryInfoPolicy2(b
, mem_ctx
,
811 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
812 r
->out
.domain_is_ad
= true;
813 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
814 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
815 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
816 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
817 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
820 if (!NT_STATUS_IS_OK(status
)) {
821 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
823 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
826 if (!NT_STATUS_IS_OK(status
)) {
829 if (!NT_STATUS_IS_OK(result
)) {
834 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
835 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
836 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
839 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
840 TALLOC_FREE(pipe_hnd
);
846 /****************************************************************
847 Do the domain join unsecure
848 ****************************************************************/
850 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
851 struct libnet_JoinCtx
*r
,
852 struct cli_state
*cli
)
854 struct rpc_pipe_client
*pipe_hnd
= NULL
;
855 unsigned char orig_trust_passwd_hash
[16];
856 unsigned char new_trust_passwd_hash
[16];
857 fstring trust_passwd
;
860 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
862 if (!NT_STATUS_IS_OK(status
)) {
866 if (!r
->in
.machine_password
) {
867 r
->in
.machine_password
= generate_random_password(mem_ctx
,
868 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
,
869 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
870 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
873 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
875 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
876 fstrcpy(trust_passwd
, r
->in
.admin_password
);
877 if (!strlower_m(trust_passwd
)) {
878 return NT_STATUS_INVALID_PARAMETER
;
882 * Machine names can be 15 characters, but the max length on
883 * a password is 14. --jerry
886 trust_passwd
[14] = '\0';
888 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
890 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
892 orig_trust_passwd_hash
,
893 r
->in
.machine_password
,
894 new_trust_passwd_hash
,
895 r
->in
.secure_channel_type
);
900 /****************************************************************
902 ****************************************************************/
904 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
905 struct libnet_JoinCtx
*r
,
906 struct cli_state
*cli
)
908 struct rpc_pipe_client
*pipe_hnd
= NULL
;
909 struct policy_handle sam_pol
, domain_pol
, user_pol
;
910 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
912 struct lsa_String lsa_acct_name
;
914 uint32_t acct_flags
= ACB_WSTRUST
;
915 struct samr_Ids user_rids
;
916 struct samr_Ids name_types
;
917 union samr_UserInfo user_info
;
918 struct dcerpc_binding_handle
*b
= NULL
;
920 DATA_BLOB session_key
= data_blob_null
;
921 struct samr_CryptPassword crypt_pwd
;
922 struct samr_CryptPasswordEx crypt_pwd_ex
;
924 ZERO_STRUCT(sam_pol
);
925 ZERO_STRUCT(domain_pol
);
926 ZERO_STRUCT(user_pol
);
928 switch (r
->in
.secure_channel_type
) {
930 acct_flags
= ACB_WSTRUST
;
933 acct_flags
= ACB_SVRTRUST
;
936 return NT_STATUS_INVALID_PARAMETER
;
939 if (!r
->in
.machine_password
) {
940 r
->in
.machine_password
= generate_random_password(mem_ctx
,
941 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
,
942 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
943 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
946 /* Open the domain */
948 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
950 if (!NT_STATUS_IS_OK(status
)) {
951 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
956 b
= pipe_hnd
->binding_handle
;
958 status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
959 if (!NT_STATUS_IS_OK(status
)) {
960 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
965 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
967 SAMR_ACCESS_ENUM_DOMAINS
968 | SAMR_ACCESS_LOOKUP_DOMAIN
,
971 if (!NT_STATUS_IS_OK(status
)) {
974 if (!NT_STATUS_IS_OK(result
)) {
979 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
981 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
982 | SAMR_DOMAIN_ACCESS_CREATE_USER
983 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
987 if (!NT_STATUS_IS_OK(status
)) {
990 if (!NT_STATUS_IS_OK(result
)) {
995 /* Create domain user */
997 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
998 if (!strlower_m(acct_name
)) {
999 status
= NT_STATUS_INVALID_PARAMETER
;
1003 init_lsa_String(&lsa_acct_name
, acct_name
);
1005 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
1006 uint32_t access_desired
=
1007 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
1008 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
1009 SAMR_USER_ACCESS_SET_PASSWORD
|
1010 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
1011 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
1012 uint32_t access_granted
= 0;
1014 DEBUG(10,("Creating account with desired access mask: %d\n",
1017 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
1026 if (!NT_STATUS_IS_OK(status
)) {
1031 if (!NT_STATUS_IS_OK(status
) &&
1032 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1034 DEBUG(10,("Creation of workstation account failed: %s\n",
1035 nt_errstr(status
)));
1037 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1038 username/password combo but the user does not have
1039 administrator access. */
1041 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
1042 libnet_join_set_error_string(mem_ctx
, r
,
1043 "User specified does not have "
1044 "administrator privileges");
1050 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1051 if (!(r
->in
.join_flags
&
1052 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
1057 /* We *must* do this.... don't ask... */
1059 if (NT_STATUS_IS_OK(status
)) {
1060 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1064 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1071 if (!NT_STATUS_IS_OK(status
)) {
1074 if (!NT_STATUS_IS_OK(result
)) {
1078 if (user_rids
.count
!= 1) {
1079 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1082 if (name_types
.count
!= 1) {
1083 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1087 if (name_types
.ids
[0] != SID_NAME_USER
) {
1088 DEBUG(0,("%s is not a user account (type=%d)\n",
1089 acct_name
, name_types
.ids
[0]));
1090 status
= NT_STATUS_INVALID_WORKSTATION
;
1094 user_rid
= user_rids
.ids
[0];
1096 /* Open handle on user */
1098 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1100 SEC_FLAG_MAXIMUM_ALLOWED
,
1104 if (!NT_STATUS_IS_OK(status
)) {
1107 if (!NT_STATUS_IS_OK(result
)) {
1112 /* Fill in the additional account flags now */
1114 acct_flags
|= ACB_PWNOEXP
;
1116 /* Set account flags on machine account */
1117 ZERO_STRUCT(user_info
.info16
);
1118 user_info
.info16
.acct_flags
= acct_flags
;
1120 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1125 if (!NT_STATUS_IS_OK(status
)) {
1126 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1130 libnet_join_set_error_string(mem_ctx
, r
,
1131 "Failed to set account flags for machine account (%s)\n",
1136 if (!NT_STATUS_IS_OK(result
)) {
1139 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1143 libnet_join_set_error_string(mem_ctx
, r
,
1144 "Failed to set account flags for machine account (%s)\n",
1149 /* Set password on machine account - first try level 26 */
1151 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1155 user_info
.info26
.password
= crypt_pwd_ex
;
1156 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1158 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1164 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
1166 /* retry with level 24 */
1168 init_samr_CryptPassword(r
->in
.machine_password
,
1172 user_info
.info24
.password
= crypt_pwd
;
1173 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1175 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1182 if (!NT_STATUS_IS_OK(status
)) {
1184 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1188 libnet_join_set_error_string(mem_ctx
, r
,
1189 "Failed to set password for machine account (%s)\n",
1193 if (!NT_STATUS_IS_OK(result
)) {
1196 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1200 libnet_join_set_error_string(mem_ctx
, r
,
1201 "Failed to set password for machine account (%s)\n",
1206 status
= NT_STATUS_OK
;
1213 data_blob_clear_free(&session_key
);
1215 if (is_valid_policy_hnd(&sam_pol
)) {
1216 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1218 if (is_valid_policy_hnd(&domain_pol
)) {
1219 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1221 if (is_valid_policy_hnd(&user_pol
)) {
1222 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1224 TALLOC_FREE(pipe_hnd
);
1229 /****************************************************************
1230 ****************************************************************/
1232 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1233 const char *machine_name
,
1234 const char *dc_name
,
1235 const bool use_kerberos
)
1237 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1238 struct cli_state
*cli
= NULL
;
1239 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1240 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1242 char *machine_password
= NULL
;
1243 char *machine_account
= NULL
;
1247 return NT_STATUS_INVALID_PARAMETER
;
1250 if (!secrets_init()) {
1251 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1254 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1256 if (!machine_password
) {
1257 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1260 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1261 SAFE_FREE(machine_password
);
1262 return NT_STATUS_NO_MEMORY
;
1266 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
1269 status
= cli_full_connection(&cli
, NULL
,
1277 SMB_SIGNING_DEFAULT
);
1278 free(machine_account
);
1279 free(machine_password
);
1281 if (!NT_STATUS_IS_OK(status
)) {
1282 status
= cli_full_connection(&cli
, NULL
,
1290 SMB_SIGNING_DEFAULT
);
1293 if (!NT_STATUS_IS_OK(status
)) {
1297 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1298 &neg_flags
, &netlogon_pipe
);
1299 if (!NT_STATUS_IS_OK(status
)) {
1300 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1302 return NT_STATUS_OK
;
1305 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1306 "key from server %s for domain %s. Error was %s\n",
1307 smbXcli_conn_remote_name(cli
->conn
),
1308 netbios_domain_name
, nt_errstr(status
)));
1313 if (!lp_client_schannel()) {
1315 return NT_STATUS_OK
;
1318 status
= cli_rpc_pipe_open_schannel_with_key(
1319 cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
1320 DCERPC_AUTH_LEVEL_PRIVACY
,
1321 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1325 if (!NT_STATUS_IS_OK(status
)) {
1326 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1327 "on netlogon pipe to server %s for domain %s. "
1329 smbXcli_conn_remote_name(cli
->conn
),
1330 netbios_domain_name
, nt_errstr(status
)));
1334 return NT_STATUS_OK
;
1337 /****************************************************************
1338 ****************************************************************/
1340 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1341 struct libnet_JoinCtx
*r
)
1345 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1348 r
->in
.use_kerberos
);
1349 if (!NT_STATUS_IS_OK(status
)) {
1350 libnet_join_set_error_string(mem_ctx
, r
,
1351 "failed to verify domain membership after joining: %s",
1352 get_friendly_nt_error_msg(status
));
1353 return WERR_SETUP_NOT_JOINED
;
1359 /****************************************************************
1360 ****************************************************************/
1362 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1363 struct libnet_UnjoinCtx
*r
)
1365 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1369 if (!secrets_delete_domain_sid(lp_workgroup())) {
1376 /****************************************************************
1377 ****************************************************************/
1379 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1380 struct libnet_UnjoinCtx
*r
)
1382 struct cli_state
*cli
= NULL
;
1383 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1384 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1385 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1388 struct lsa_String lsa_acct_name
;
1389 struct samr_Ids user_rids
;
1390 struct samr_Ids name_types
;
1391 union samr_UserInfo
*info
= NULL
;
1392 struct dcerpc_binding_handle
*b
= NULL
;
1394 ZERO_STRUCT(sam_pol
);
1395 ZERO_STRUCT(domain_pol
);
1396 ZERO_STRUCT(user_pol
);
1398 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1399 r
->in
.admin_account
,
1400 r
->in
.admin_password
,
1403 if (!NT_STATUS_IS_OK(status
)) {
1407 /* Open the domain */
1409 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1411 if (!NT_STATUS_IS_OK(status
)) {
1412 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1413 nt_errstr(status
)));
1417 b
= pipe_hnd
->binding_handle
;
1419 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1421 SEC_FLAG_MAXIMUM_ALLOWED
,
1424 if (!NT_STATUS_IS_OK(status
)) {
1427 if (!NT_STATUS_IS_OK(result
)) {
1432 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1434 SEC_FLAG_MAXIMUM_ALLOWED
,
1438 if (!NT_STATUS_IS_OK(status
)) {
1441 if (!NT_STATUS_IS_OK(result
)) {
1446 /* Create domain user */
1448 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1449 if (!strlower_m(acct_name
)) {
1450 status
= NT_STATUS_INVALID_PARAMETER
;
1454 init_lsa_String(&lsa_acct_name
, acct_name
);
1456 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1464 if (!NT_STATUS_IS_OK(status
)) {
1467 if (!NT_STATUS_IS_OK(result
)) {
1471 if (user_rids
.count
!= 1) {
1472 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1475 if (name_types
.count
!= 1) {
1476 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1480 if (name_types
.ids
[0] != SID_NAME_USER
) {
1481 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1482 name_types
.ids
[0]));
1483 status
= NT_STATUS_INVALID_WORKSTATION
;
1487 user_rid
= user_rids
.ids
[0];
1489 /* Open handle on user */
1491 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1493 SEC_FLAG_MAXIMUM_ALLOWED
,
1497 if (!NT_STATUS_IS_OK(status
)) {
1500 if (!NT_STATUS_IS_OK(result
)) {
1507 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1512 if (!NT_STATUS_IS_OK(status
)) {
1513 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1516 if (!NT_STATUS_IS_OK(result
)) {
1518 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1522 /* now disable and setuser info */
1524 info
->info16
.acct_flags
|= ACB_DISABLED
;
1526 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1531 if (!NT_STATUS_IS_OK(status
)) {
1532 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1535 if (!NT_STATUS_IS_OK(result
)) {
1537 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1541 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1544 if (pipe_hnd
&& b
) {
1545 if (is_valid_policy_hnd(&domain_pol
)) {
1546 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1548 if (is_valid_policy_hnd(&sam_pol
)) {
1549 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1551 TALLOC_FREE(pipe_hnd
);
1561 /****************************************************************
1562 ****************************************************************/
1564 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1566 WERROR werr
= WERR_OK
;
1568 struct smbconf_ctx
*ctx
;
1570 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1571 if (!SBC_ERROR_IS_OK(err
)) {
1572 werr
= WERR_NO_SUCH_SERVICE
;
1576 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1578 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1579 if (!SBC_ERROR_IS_OK(err
)) {
1580 werr
= WERR_NO_SUCH_SERVICE
;
1584 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1586 if (!SBC_ERROR_IS_OK(err
)) {
1587 werr
= WERR_NO_SUCH_SERVICE
;
1591 smbconf_delete_global_parameter(ctx
, "realm");
1595 err
= smbconf_set_global_parameter(ctx
, "security", "domain");
1596 if (!SBC_ERROR_IS_OK(err
)) {
1597 werr
= WERR_NO_SUCH_SERVICE
;
1601 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1602 r
->out
.netbios_domain_name
);
1603 if (!SBC_ERROR_IS_OK(err
)) {
1604 werr
= WERR_NO_SUCH_SERVICE
;
1608 if (r
->out
.domain_is_ad
) {
1609 err
= smbconf_set_global_parameter(ctx
, "security", "ads");
1610 if (!SBC_ERROR_IS_OK(err
)) {
1611 werr
= WERR_NO_SUCH_SERVICE
;
1615 err
= smbconf_set_global_parameter(ctx
, "realm",
1616 r
->out
.dns_domain_name
);
1617 if (!SBC_ERROR_IS_OK(err
)) {
1618 werr
= WERR_NO_SUCH_SERVICE
;
1624 smbconf_shutdown(ctx
);
1628 /****************************************************************
1629 ****************************************************************/
1631 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1633 WERROR werr
= WERR_OK
;
1635 struct smbconf_ctx
*ctx
;
1637 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1638 if (!SBC_ERROR_IS_OK(err
)) {
1639 werr
= WERR_NO_SUCH_SERVICE
;
1643 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1645 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1646 if (!SBC_ERROR_IS_OK(err
)) {
1647 werr
= WERR_NO_SUCH_SERVICE
;
1651 err
= smbconf_delete_global_parameter(ctx
, "workgroup");
1652 if (!SBC_ERROR_IS_OK(err
)) {
1653 werr
= WERR_NO_SUCH_SERVICE
;
1657 smbconf_delete_global_parameter(ctx
, "realm");
1661 smbconf_shutdown(ctx
);
1665 /****************************************************************
1666 ****************************************************************/
1668 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1672 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1673 return r
->out
.result
;
1676 if (!r
->in
.modify_config
) {
1680 werr
= do_join_modify_vals_config(r
);
1681 if (!W_ERROR_IS_OK(werr
)) {
1685 lp_load_global(get_dyn_CONFIGFILE());
1687 r
->out
.modified_config
= true;
1688 r
->out
.result
= werr
;
1693 /****************************************************************
1694 ****************************************************************/
1696 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1700 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1701 return r
->out
.result
;
1704 if (!r
->in
.modify_config
) {
1708 werr
= do_unjoin_modify_vals_config(r
);
1709 if (!W_ERROR_IS_OK(werr
)) {
1713 lp_load_global(get_dyn_CONFIGFILE());
1715 r
->out
.modified_config
= true;
1716 r
->out
.result
= werr
;
1721 /****************************************************************
1722 ****************************************************************/
1724 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1725 const char *domain_str
,
1726 const char **domain_p
,
1729 char *domain
= NULL
;
1731 const char *p
= NULL
;
1733 if (!domain_str
|| !domain_p
|| !dc_p
) {
1737 p
= strchr_m(domain_str
, '\\');
1740 domain
= talloc_strndup(mem_ctx
, domain_str
,
1741 PTR_DIFF(p
, domain_str
));
1742 dc
= talloc_strdup(mem_ctx
, p
+1);
1747 domain
= talloc_strdup(mem_ctx
, domain_str
);
1763 /****************************************************************
1764 ****************************************************************/
1766 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1767 struct libnet_JoinCtx
*r
)
1769 if (!r
->in
.domain_name
) {
1770 libnet_join_set_error_string(mem_ctx
, r
,
1771 "No domain name defined");
1772 return WERR_INVALID_PARAM
;
1775 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1778 libnet_join_set_error_string(mem_ctx
, r
,
1779 "Failed to parse domain name");
1780 return WERR_INVALID_PARAM
;
1784 return WERR_SETUP_DOMAIN_CONTROLLER
;
1787 if (!secrets_init()) {
1788 libnet_join_set_error_string(mem_ctx
, r
,
1789 "Unable to open secrets database");
1790 return WERR_CAN_NOT_COMPLETE
;
1796 /****************************************************************
1797 ****************************************************************/
1799 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1803 /* Try adding dom admins to builtin\admins. Only log failures. */
1804 status
= create_builtin_administrators(domain_sid
);
1805 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1806 DEBUG(10,("Unable to auto-add domain administrators to "
1807 "BUILTIN\\Administrators during join because "
1808 "winbindd must be running.\n"));
1809 } else if (!NT_STATUS_IS_OK(status
)) {
1810 DEBUG(5, ("Failed to auto-add domain administrators to "
1811 "BUILTIN\\Administrators during join: %s\n",
1812 nt_errstr(status
)));
1815 /* Try adding dom users to builtin\users. Only log failures. */
1816 status
= create_builtin_users(domain_sid
);
1817 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1818 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1819 "during join because winbindd must be running.\n"));
1820 } else if (!NT_STATUS_IS_OK(status
)) {
1821 DEBUG(5, ("Failed to auto-add domain administrators to "
1822 "BUILTIN\\Administrators during join: %s\n",
1823 nt_errstr(status
)));
1827 /****************************************************************
1828 ****************************************************************/
1830 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1831 struct libnet_JoinCtx
*r
)
1835 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1836 return r
->out
.result
;
1839 werr
= do_JoinConfig(r
);
1840 if (!W_ERROR_IS_OK(werr
)) {
1844 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1848 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1849 if (r
->out
.dns_domain_name
) {
1850 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1854 if (r
->out
.domain_is_ad
&&
1855 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1856 ADS_STATUS ads_status
;
1858 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1859 if (!ADS_ERR_OK(ads_status
)) {
1860 return WERR_GENERAL_FAILURE
;
1863 #endif /* HAVE_ADS */
1865 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1870 /****************************************************************
1871 ****************************************************************/
1873 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1876 ads_destroy(&r
->in
.ads
);
1882 /****************************************************************
1883 ****************************************************************/
1885 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1888 ads_destroy(&r
->in
.ads
);
1894 /****************************************************************
1895 ****************************************************************/
1897 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1898 struct libnet_JoinCtx
**r
)
1900 struct libnet_JoinCtx
*ctx
;
1902 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1907 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1909 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
1910 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1912 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1919 /****************************************************************
1920 ****************************************************************/
1922 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1923 struct libnet_UnjoinCtx
**r
)
1925 struct libnet_UnjoinCtx
*ctx
;
1927 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1932 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1934 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
1935 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1942 /****************************************************************
1943 ****************************************************************/
1945 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1946 struct libnet_JoinCtx
*r
)
1948 bool valid_security
= false;
1949 bool valid_workgroup
= false;
1950 bool valid_realm
= false;
1952 /* check if configuration is already set correctly */
1954 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1956 switch (r
->out
.domain_is_ad
) {
1958 valid_security
= (lp_security() == SEC_DOMAIN
);
1959 if (valid_workgroup
&& valid_security
) {
1960 /* nothing to be done */
1965 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1966 switch (lp_security()) {
1969 valid_security
= true;
1972 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1973 /* nothing to be done */
1979 /* check if we are supposed to manipulate configuration */
1981 if (!r
->in
.modify_config
) {
1983 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1985 if (!valid_workgroup
) {
1986 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1987 "\"workgroup\" set to '%s', should be '%s'",
1988 lp_workgroup(), r
->out
.netbios_domain_name
);
1989 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1993 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1994 "\"realm\" set to '%s', should be '%s'",
1995 lp_realm(), r
->out
.dns_domain_name
);
1996 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1999 if (!valid_security
) {
2000 const char *sec
= NULL
;
2001 switch (lp_security()) {
2002 case SEC_USER
: sec
= "user"; break;
2003 case SEC_DOMAIN
: sec
= "domain"; break;
2004 case SEC_ADS
: sec
= "ads"; break;
2006 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2007 "\"security\" set to '%s', should be %s",
2008 sec
, r
->out
.domain_is_ad
?
2009 "either 'domain' or 'ads'" : "'domain'");
2010 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2013 libnet_join_set_error_string(mem_ctx
, r
,
2014 "Invalid configuration (%s) and configuration modification "
2015 "was not requested", wrong_conf
);
2016 return WERR_CAN_NOT_COMPLETE
;
2019 /* check if we are able to manipulate configuration */
2021 if (!lp_config_backend_is_registry()) {
2022 libnet_join_set_error_string(mem_ctx
, r
,
2023 "Configuration manipulation requested but not "
2024 "supported by backend");
2025 return WERR_NOT_SUPPORTED
;
2031 /****************************************************************
2032 ****************************************************************/
2034 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
2035 struct libnet_JoinCtx
*r
)
2039 struct cli_state
*cli
= NULL
;
2041 ADS_STATUS ads_status
;
2042 #endif /* HAVE_ADS */
2044 if (!r
->in
.dc_name
) {
2045 struct netr_DsRGetDCNameInfo
*info
;
2047 status
= dsgetdcname(mem_ctx
,
2052 DS_FORCE_REDISCOVERY
|
2053 DS_DIRECTORY_SERVICE_REQUIRED
|
2054 DS_WRITABLE_REQUIRED
|
2057 if (!NT_STATUS_IS_OK(status
)) {
2058 libnet_join_set_error_string(mem_ctx
, r
,
2059 "failed to find DC for domain %s",
2061 get_friendly_nt_error_msg(status
));
2062 return WERR_DCNOTFOUND
;
2065 dc
= strip_hostname(info
->dc_unc
);
2066 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2067 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2070 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
2071 if (!NT_STATUS_IS_OK(status
)) {
2072 libnet_join_set_error_string(mem_ctx
, r
,
2073 "failed to lookup DC info for domain '%s' over rpc: %s",
2074 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2075 return ntstatus_to_werror(status
);
2078 werr
= libnet_join_check_config(mem_ctx
, r
);
2079 if (!W_ERROR_IS_OK(werr
)) {
2085 create_local_private_krb5_conf_for_domain(
2086 r
->out
.dns_domain_name
, r
->out
.netbios_domain_name
,
2087 NULL
, smbXcli_conn_remote_sockaddr(cli
->conn
),
2088 smbXcli_conn_remote_name(cli
->conn
));
2090 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
2091 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2093 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
2094 if (!ADS_ERR_OK(ads_status
)) {
2095 return WERR_DEFAULT_JOIN_REQUIRED
;
2098 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
2099 if (!ADS_ERR_OK(ads_status
)) {
2100 libnet_join_set_error_string(mem_ctx
, r
,
2101 "failed to precreate account in ou %s: %s",
2103 ads_errstr(ads_status
));
2104 return WERR_DEFAULT_JOIN_REQUIRED
;
2107 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
2109 #endif /* HAVE_ADS */
2111 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
2112 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
2113 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2115 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2117 if (!NT_STATUS_IS_OK(status
)) {
2118 libnet_join_set_error_string(mem_ctx
, r
,
2119 "failed to join domain '%s' over rpc: %s",
2120 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2121 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2122 return WERR_SETUP_ALREADY_JOINED
;
2124 werr
= ntstatus_to_werror(status
);
2128 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2129 werr
= WERR_SETUP_NOT_JOINED
;
2143 /****************************************************************
2144 ****************************************************************/
2146 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2147 struct libnet_JoinCtx
*r
)
2150 struct libnet_UnjoinCtx
*u
= NULL
;
2152 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2153 if (!W_ERROR_IS_OK(werr
)) {
2157 u
->in
.debug
= r
->in
.debug
;
2158 u
->in
.dc_name
= r
->in
.dc_name
;
2159 u
->in
.domain_name
= r
->in
.domain_name
;
2160 u
->in
.admin_account
= r
->in
.admin_account
;
2161 u
->in
.admin_password
= r
->in
.admin_password
;
2162 u
->in
.modify_config
= r
->in
.modify_config
;
2163 u
->in
.use_kerberos
= r
->in
.use_kerberos
;
2164 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2165 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2167 werr
= libnet_Unjoin(mem_ctx
, u
);
2173 /****************************************************************
2174 ****************************************************************/
2176 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2177 struct libnet_JoinCtx
*r
)
2182 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2185 ZERO_STRUCT(r
->out
);
2187 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2188 if (!W_ERROR_IS_OK(werr
)) {
2192 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2193 werr
= libnet_DomainJoin(mem_ctx
, r
);
2194 if (!W_ERROR_IS_OK(werr
)) {
2199 werr
= libnet_join_post_processing(mem_ctx
, r
);
2200 if (!W_ERROR_IS_OK(werr
)) {
2204 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2205 werr
= libnet_join_post_verify(mem_ctx
, r
);
2206 if (!W_ERROR_IS_OK(werr
)) {
2207 libnet_join_rollback(mem_ctx
, r
);
2212 r
->out
.result
= werr
;
2215 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2220 /****************************************************************
2221 ****************************************************************/
2223 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2224 struct libnet_UnjoinCtx
*r
)
2228 if (!r
->in
.domain_sid
) {
2230 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2231 libnet_unjoin_set_error_string(mem_ctx
, r
,
2232 "Unable to fetch domain sid: are we joined?");
2233 return WERR_SETUP_NOT_JOINED
;
2235 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2236 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2239 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2240 !r
->in
.delete_machine_account
) {
2241 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2245 if (!r
->in
.dc_name
) {
2246 struct netr_DsRGetDCNameInfo
*info
;
2248 status
= dsgetdcname(mem_ctx
,
2253 DS_DIRECTORY_SERVICE_REQUIRED
|
2254 DS_WRITABLE_REQUIRED
|
2257 if (!NT_STATUS_IS_OK(status
)) {
2258 libnet_unjoin_set_error_string(mem_ctx
, r
,
2259 "failed to find DC for domain %s",
2261 get_friendly_nt_error_msg(status
));
2262 return WERR_DCNOTFOUND
;
2265 dc
= strip_hostname(info
->dc_unc
);
2266 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2267 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2271 /* for net ads leave, try to delete the account. If it works,
2272 no sense in disabling. If it fails, we can still try to
2275 if (r
->in
.delete_machine_account
) {
2276 ADS_STATUS ads_status
;
2277 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2278 if (ADS_ERR_OK(ads_status
)) {
2280 r
->out
.dns_domain_name
=
2281 talloc_strdup(mem_ctx
,
2282 r
->in
.ads
->server
.realm
);
2284 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2286 if (!ADS_ERR_OK(ads_status
)) {
2287 libnet_unjoin_set_error_string(mem_ctx
, r
,
2288 "failed to remove machine account from AD: %s",
2289 ads_errstr(ads_status
));
2291 r
->out
.deleted_machine_account
= true;
2292 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2293 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2297 #endif /* HAVE_ADS */
2299 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2301 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2302 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2303 if (!NT_STATUS_IS_OK(status
)) {
2304 libnet_unjoin_set_error_string(mem_ctx
, r
,
2305 "failed to disable machine account via rpc: %s",
2306 get_friendly_nt_error_msg(status
));
2307 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2308 return WERR_SETUP_NOT_JOINED
;
2310 return ntstatus_to_werror(status
);
2313 r
->out
.disabled_machine_account
= true;
2316 /* If disable succeeded or was not requested at all, we
2317 should be getting rid of our end of things */
2319 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2324 /****************************************************************
2325 ****************************************************************/
2327 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2328 struct libnet_UnjoinCtx
*r
)
2330 if (!r
->in
.domain_name
) {
2331 libnet_unjoin_set_error_string(mem_ctx
, r
,
2332 "No domain name defined");
2333 return WERR_INVALID_PARAM
;
2336 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2339 libnet_unjoin_set_error_string(mem_ctx
, r
,
2340 "Failed to parse domain name");
2341 return WERR_INVALID_PARAM
;
2345 return WERR_SETUP_DOMAIN_CONTROLLER
;
2348 if (!secrets_init()) {
2349 libnet_unjoin_set_error_string(mem_ctx
, r
,
2350 "Unable to open secrets database");
2351 return WERR_CAN_NOT_COMPLETE
;
2357 /****************************************************************
2358 ****************************************************************/
2360 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2361 struct libnet_UnjoinCtx
*r
)
2363 saf_delete(r
->out
.netbios_domain_name
);
2364 saf_delete(r
->out
.dns_domain_name
);
2366 return libnet_unjoin_config(r
);
2369 /****************************************************************
2370 ****************************************************************/
2372 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2373 struct libnet_UnjoinCtx
*r
)
2378 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2381 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2382 if (!W_ERROR_IS_OK(werr
)) {
2386 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2387 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2388 if (!W_ERROR_IS_OK(werr
)) {
2389 libnet_unjoin_config(r
);
2394 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2395 if (!W_ERROR_IS_OK(werr
)) {
2400 r
->out
.result
= werr
;
2403 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);