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"
43 #include "libcli/auth/netlogon_creds_cli.h"
44 #include "auth/credentials/credentials.h"
46 /****************************************************************
47 ****************************************************************/
49 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
52 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
53 DEBUG(1,("libnet_Join:\n%s", str)); \
57 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
58 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
59 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
60 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
62 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
65 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
66 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
70 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
71 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
72 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
73 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
75 /****************************************************************
76 ****************************************************************/
78 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
79 struct libnet_JoinCtx
*r
,
80 const char *format
, ...)
84 if (r
->out
.error_string
) {
88 va_start(args
, format
);
89 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
93 /****************************************************************
94 ****************************************************************/
96 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
97 struct libnet_UnjoinCtx
*r
,
98 const char *format
, ...)
102 if (r
->out
.error_string
) {
106 va_start(args
, format
);
107 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
113 /****************************************************************
114 ****************************************************************/
116 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
117 const char *netbios_domain_name
,
119 const char *user_name
,
120 const char *password
,
124 ADS_STRUCT
*my_ads
= NULL
;
127 my_ads
= ads_init(dns_domain_name
,
131 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
135 SAFE_FREE(my_ads
->auth
.user_name
);
136 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
137 if ((cp
= strchr_m(my_ads
->auth
.user_name
, '@'))!=0) {
139 SAFE_FREE(my_ads
->auth
.realm
);
140 my_ads
->auth
.realm
= smb_xstrdup(cp
);
141 if (!strupper_m(my_ads
->auth
.realm
)) {
142 ads_destroy(&my_ads
);
143 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
149 SAFE_FREE(my_ads
->auth
.password
);
150 my_ads
->auth
.password
= SMB_STRDUP(password
);
153 status
= ads_connect_user_creds(my_ads
);
154 if (!ADS_ERR_OK(status
)) {
155 ads_destroy(&my_ads
);
163 /****************************************************************
164 ****************************************************************/
166 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
167 struct libnet_JoinCtx
*r
)
171 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
172 r
->out
.netbios_domain_name
,
175 r
->in
.admin_password
,
177 if (!ADS_ERR_OK(status
)) {
178 libnet_join_set_error_string(mem_ctx
, r
,
179 "failed to connect to AD: %s",
184 if (!r
->out
.netbios_domain_name
) {
185 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
186 r
->in
.ads
->server
.workgroup
);
187 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
190 if (!r
->out
.dns_domain_name
) {
191 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
192 r
->in
.ads
->config
.realm
);
193 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
196 r
->out
.domain_is_ad
= true;
201 /****************************************************************
202 ****************************************************************/
204 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
205 struct libnet_UnjoinCtx
*r
)
209 status
= libnet_connect_ads(r
->in
.domain_name
,
213 r
->in
.admin_password
,
215 if (!ADS_ERR_OK(status
)) {
216 libnet_unjoin_set_error_string(mem_ctx
, r
,
217 "failed to connect to AD: %s",
224 /****************************************************************
225 join a domain using ADS (LDAP mods)
226 ****************************************************************/
228 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
229 struct libnet_JoinCtx
*r
)
232 LDAPMessage
*res
= NULL
;
233 const char *attrs
[] = { "dn", NULL
};
236 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
237 if (!ADS_ERR_OK(status
)) {
241 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
242 if (!ADS_ERR_OK(status
)) {
246 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
247 ads_msgfree(r
->in
.ads
, res
);
248 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
251 ads_msgfree(r
->in
.ads
, res
);
253 /* Attempt to create the machine account and bail if this fails.
254 Assume that the admin wants exactly what they requested */
256 status
= ads_create_machine_acct(r
->in
.ads
,
260 if (ADS_ERR_OK(status
)) {
261 DEBUG(1,("machine account creation created\n"));
263 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
264 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
265 status
= ADS_SUCCESS
;
268 if (!ADS_ERR_OK(status
)) {
269 DEBUG(1,("machine account creation failed\n"));
273 status
= ads_move_machine_acct(r
->in
.ads
,
277 if (!ADS_ERR_OK(status
)) {
278 DEBUG(1,("failure to locate/move pre-existing "
279 "machine account\n"));
283 DEBUG(1,("The machine account %s the specified OU.\n",
284 moved
? "was moved into" : "already exists in"));
289 /****************************************************************
290 ****************************************************************/
292 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
293 struct libnet_UnjoinCtx
*r
)
298 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
299 if (!ADS_ERR_OK(status
)) {
300 libnet_unjoin_set_error_string(mem_ctx
, r
,
301 "failed to connect to AD: %s",
307 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
308 if (!ADS_ERR_OK(status
)) {
309 libnet_unjoin_set_error_string(mem_ctx
, r
,
310 "failed to leave realm: %s",
318 /****************************************************************
319 ****************************************************************/
321 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
322 struct libnet_JoinCtx
*r
)
325 LDAPMessage
*res
= NULL
;
328 if (!r
->in
.machine_name
) {
329 return ADS_ERROR(LDAP_NO_MEMORY
);
332 status
= ads_find_machine_acct(r
->in
.ads
,
335 if (!ADS_ERR_OK(status
)) {
339 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
340 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
344 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
346 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
350 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
352 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
357 ads_msgfree(r
->in
.ads
, res
);
363 /****************************************************************
364 Set a machines dNSHostName and servicePrincipalName attributes
365 ****************************************************************/
367 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
368 struct libnet_JoinCtx
*r
)
373 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
378 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
379 if (!ADS_ERR_OK(status
)) {
383 /* Windows only creates HOST/shortname & HOST/fqdn. */
385 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
387 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
389 if (!strupper_m(spn
)) {
390 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
394 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
395 || (strchr(my_fqdn
, '.') == NULL
)) {
396 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
397 r
->out
.dns_domain_name
);
400 if (!strlower_m(my_fqdn
)) {
401 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
404 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
405 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
407 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
412 mods
= ads_init_mods(mem_ctx
);
414 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
417 /* fields of primary importance */
419 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
420 if (!ADS_ERR_OK(status
)) {
421 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
424 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
426 if (!ADS_ERR_OK(status
)) {
427 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
430 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
433 /****************************************************************
434 ****************************************************************/
436 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
437 struct libnet_JoinCtx
*r
)
442 if (!r
->in
.create_upn
) {
448 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
449 if (!ADS_ERR_OK(status
)) {
454 const char *realm
= r
->out
.dns_domain_name
;
456 /* in case we are about to generate a keytab during the join
457 * make sure the default upn we create is usable with kinit -k.
460 if (USE_KERBEROS_KEYTAB
) {
461 realm
= talloc_strdup_upper(mem_ctx
,
462 r
->out
.dns_domain_name
);
466 return ADS_ERROR(LDAP_NO_MEMORY
);
469 r
->in
.upn
= talloc_asprintf(mem_ctx
,
474 return ADS_ERROR(LDAP_NO_MEMORY
);
478 /* now do the mods */
480 mods
= ads_init_mods(mem_ctx
);
482 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
485 /* fields of primary importance */
487 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
488 if (!ADS_ERR_OK(status
)) {
489 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
492 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
496 /****************************************************************
497 ****************************************************************/
499 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
500 struct libnet_JoinCtx
*r
)
506 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
512 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
513 if (!ADS_ERR_OK(status
)) {
517 /* now do the mods */
519 mods
= ads_init_mods(mem_ctx
);
521 return ADS_ERROR(LDAP_NO_MEMORY
);
524 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
526 return ADS_ERROR(LDAP_NO_MEMORY
);
529 /* fields of primary importance */
531 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
533 if (!ADS_ERR_OK(status
)) {
537 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
539 if (!ADS_ERR_OK(status
)) {
543 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
545 if (!ADS_ERR_OK(status
)) {
549 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
552 /****************************************************************
553 ****************************************************************/
555 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
556 struct libnet_JoinCtx
*r
)
558 if (!USE_SYSTEM_KEYTAB
) {
562 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
569 /****************************************************************
570 ****************************************************************/
572 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
573 struct libnet_JoinCtx
*r
)
575 uint32_t domain_func
;
577 const char *salt
= NULL
;
578 char *std_salt
= NULL
;
580 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
581 if (!ADS_ERR_OK(status
)) {
582 libnet_join_set_error_string(mem_ctx
, r
,
583 "failed to determine domain functional level: %s",
588 /* go ahead and setup the default salt */
590 std_salt
= kerberos_standard_des_salt();
592 libnet_join_set_error_string(mem_ctx
, r
,
593 "failed to obtain standard DES salt");
597 salt
= talloc_strdup(mem_ctx
, std_salt
);
604 /* if it's a Windows functional domain, we have to look for the UPN */
606 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
609 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
612 salt
= talloc_strdup(mem_ctx
, upn
);
619 return kerberos_secrets_store_des_salt(salt
);
622 /****************************************************************
623 ****************************************************************/
625 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
626 struct libnet_JoinCtx
*r
)
631 status
= libnet_join_connect_ads(mem_ctx
, r
);
632 if (!ADS_ERR_OK(status
)) {
637 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
638 if (!ADS_ERR_OK(status
)) {
639 libnet_join_set_error_string(mem_ctx
, r
,
640 "failed to set machine spn: %s",
645 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
646 if (!ADS_ERR_OK(status
)) {
647 libnet_join_set_error_string(mem_ctx
, r
,
648 "failed to set machine os attributes: %s",
653 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
654 if (!ADS_ERR_OK(status
)) {
655 libnet_join_set_error_string(mem_ctx
, r
,
656 "failed to set machine upn: %s",
661 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
662 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
665 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
666 libnet_join_set_error_string(mem_ctx
, r
,
667 "failed to create kerberos keytab");
668 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
673 #endif /* HAVE_ADS */
675 /****************************************************************
676 Store the machine password and domain SID
677 ****************************************************************/
679 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
680 struct libnet_JoinCtx
*r
)
682 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
685 DEBUG(1,("Failed to save domain sid\n"));
689 if (!secrets_store_machine_password(r
->in
.machine_password
,
690 r
->out
.netbios_domain_name
,
691 r
->in
.secure_channel_type
))
693 DEBUG(1,("Failed to save machine password\n"));
700 /****************************************************************
701 Connect dc's IPC$ share
702 ****************************************************************/
704 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
709 struct cli_state
**cli
)
714 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
717 if (use_kerberos
&& pass
) {
718 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
721 return cli_full_connection(cli
, NULL
,
729 SMB_SIGNING_DEFAULT
);
732 /****************************************************************
733 Lookup domain dc's info
734 ****************************************************************/
736 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
737 struct libnet_JoinCtx
*r
,
738 struct cli_state
**cli
)
740 struct rpc_pipe_client
*pipe_hnd
= NULL
;
741 struct policy_handle lsa_pol
;
742 NTSTATUS status
, result
;
743 union lsa_PolicyInformation
*info
= NULL
;
744 struct dcerpc_binding_handle
*b
;
746 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
749 r
->in
.admin_password
,
752 if (!NT_STATUS_IS_OK(status
)) {
756 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
,
758 if (!NT_STATUS_IS_OK(status
)) {
759 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
764 b
= pipe_hnd
->binding_handle
;
766 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
767 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
768 if (!NT_STATUS_IS_OK(status
)) {
772 status
= dcerpc_lsa_QueryInfoPolicy2(b
, mem_ctx
,
777 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
778 r
->out
.domain_is_ad
= true;
779 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
780 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
781 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
782 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
783 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
786 if (!NT_STATUS_IS_OK(status
)) {
787 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
789 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
792 if (!NT_STATUS_IS_OK(status
)) {
795 if (!NT_STATUS_IS_OK(result
)) {
800 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
801 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
802 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
805 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
806 TALLOC_FREE(pipe_hnd
);
812 /****************************************************************
813 Do the domain join unsecure
814 ****************************************************************/
816 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
817 struct libnet_JoinCtx
*r
,
818 struct cli_state
*cli
)
820 TALLOC_CTX
*frame
= talloc_stackframe();
821 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
822 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
823 struct samr_Password current_nt_hash
;
824 const char *account_name
= NULL
;
827 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
,
829 if (!NT_STATUS_IS_OK(status
)) {
834 if (!r
->in
.machine_password
) {
835 r
->in
.machine_password
= generate_random_password(mem_ctx
,
836 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
,
837 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
838 if (r
->in
.machine_password
== NULL
) {
840 return NT_STATUS_NO_MEMORY
;
844 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
845 E_md4hash(r
->in
.admin_password
, current_nt_hash
.hash
);
847 account_name
= talloc_asprintf(frame
, "%s$",
849 if (account_name
== NULL
) {
851 return NT_STATUS_NO_MEMORY
;
854 status
= rpccli_create_netlogon_creds(netlogon_pipe
->desthost
,
857 r
->in
.secure_channel_type
,
861 if (!NT_STATUS_IS_OK(status
)) {
866 status
= rpccli_setup_netlogon_creds(cli
,
868 true, /* force_reauth */
870 NULL
); /* previous_nt_hash */
871 if (!NT_STATUS_IS_OK(status
)) {
876 status
= netlogon_creds_cli_ServerPasswordSet(netlogon_creds
,
877 netlogon_pipe
->binding_handle
,
878 r
->in
.machine_password
,
879 NULL
); /* new_version */
880 if (!NT_STATUS_IS_OK(status
)) {
889 /****************************************************************
891 ****************************************************************/
893 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
894 struct libnet_JoinCtx
*r
,
895 struct cli_state
*cli
)
897 struct rpc_pipe_client
*pipe_hnd
= NULL
;
898 struct policy_handle sam_pol
, domain_pol
, user_pol
;
899 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
901 struct lsa_String lsa_acct_name
;
903 uint32_t acct_flags
= ACB_WSTRUST
;
904 struct samr_Ids user_rids
;
905 struct samr_Ids name_types
;
906 union samr_UserInfo user_info
;
907 struct dcerpc_binding_handle
*b
= NULL
;
908 unsigned int old_timeout
= 0;
910 DATA_BLOB session_key
= data_blob_null
;
911 struct samr_CryptPassword crypt_pwd
;
912 struct samr_CryptPasswordEx crypt_pwd_ex
;
914 ZERO_STRUCT(sam_pol
);
915 ZERO_STRUCT(domain_pol
);
916 ZERO_STRUCT(user_pol
);
918 switch (r
->in
.secure_channel_type
) {
920 acct_flags
= ACB_WSTRUST
;
923 acct_flags
= ACB_SVRTRUST
;
926 return NT_STATUS_INVALID_PARAMETER
;
929 if (!r
->in
.machine_password
) {
930 r
->in
.machine_password
= generate_random_password(mem_ctx
,
931 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
,
932 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
933 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
936 /* Open the domain */
938 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
940 if (!NT_STATUS_IS_OK(status
)) {
941 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
946 b
= pipe_hnd
->binding_handle
;
948 status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
949 if (!NT_STATUS_IS_OK(status
)) {
950 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
955 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
957 SAMR_ACCESS_ENUM_DOMAINS
958 | SAMR_ACCESS_LOOKUP_DOMAIN
,
961 if (!NT_STATUS_IS_OK(status
)) {
964 if (!NT_STATUS_IS_OK(result
)) {
969 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
971 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
972 | SAMR_DOMAIN_ACCESS_CREATE_USER
973 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
977 if (!NT_STATUS_IS_OK(status
)) {
980 if (!NT_STATUS_IS_OK(result
)) {
985 /* Create domain user */
987 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
988 if (!strlower_m(acct_name
)) {
989 status
= NT_STATUS_INVALID_PARAMETER
;
993 init_lsa_String(&lsa_acct_name
, acct_name
);
995 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
996 uint32_t access_desired
=
997 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
998 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
999 SAMR_USER_ACCESS_SET_PASSWORD
|
1000 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
1001 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
1002 uint32_t access_granted
= 0;
1004 DEBUG(10,("Creating account with desired access mask: %d\n",
1007 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
1016 if (!NT_STATUS_IS_OK(status
)) {
1021 if (!NT_STATUS_IS_OK(status
) &&
1022 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1024 DEBUG(10,("Creation of workstation account failed: %s\n",
1025 nt_errstr(status
)));
1027 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1028 username/password combo but the user does not have
1029 administrator access. */
1031 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
1032 libnet_join_set_error_string(mem_ctx
, r
,
1033 "User specified does not have "
1034 "administrator privileges");
1040 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1041 if (!(r
->in
.join_flags
&
1042 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
1047 /* We *must* do this.... don't ask... */
1049 if (NT_STATUS_IS_OK(status
)) {
1050 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1054 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1061 if (!NT_STATUS_IS_OK(status
)) {
1064 if (!NT_STATUS_IS_OK(result
)) {
1068 if (user_rids
.count
!= 1) {
1069 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1072 if (name_types
.count
!= 1) {
1073 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1077 if (name_types
.ids
[0] != SID_NAME_USER
) {
1078 DEBUG(0,("%s is not a user account (type=%d)\n",
1079 acct_name
, name_types
.ids
[0]));
1080 status
= NT_STATUS_INVALID_WORKSTATION
;
1084 user_rid
= user_rids
.ids
[0];
1086 /* Open handle on user */
1088 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1090 SEC_FLAG_MAXIMUM_ALLOWED
,
1094 if (!NT_STATUS_IS_OK(status
)) {
1097 if (!NT_STATUS_IS_OK(result
)) {
1102 /* Fill in the additional account flags now */
1104 acct_flags
|= ACB_PWNOEXP
;
1106 /* Set account flags on machine account */
1107 ZERO_STRUCT(user_info
.info16
);
1108 user_info
.info16
.acct_flags
= acct_flags
;
1110 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1115 if (!NT_STATUS_IS_OK(status
)) {
1116 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1120 libnet_join_set_error_string(mem_ctx
, r
,
1121 "Failed to set account flags for machine account (%s)\n",
1126 if (!NT_STATUS_IS_OK(result
)) {
1129 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1133 libnet_join_set_error_string(mem_ctx
, r
,
1134 "Failed to set account flags for machine account (%s)\n",
1139 /* Set password on machine account - first try level 26 */
1142 * increase the timeout as password filter modules on the DC
1143 * might delay the operation for a significant amount of time
1145 old_timeout
= rpccli_set_timeout(pipe_hnd
, 600000);
1147 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1151 user_info
.info26
.password
= crypt_pwd_ex
;
1152 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1154 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1160 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
1162 /* retry with level 24 */
1164 init_samr_CryptPassword(r
->in
.machine_password
,
1168 user_info
.info24
.password
= crypt_pwd
;
1169 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1171 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1178 old_timeout
= rpccli_set_timeout(pipe_hnd
, old_timeout
);
1180 if (!NT_STATUS_IS_OK(status
)) {
1182 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1186 libnet_join_set_error_string(mem_ctx
, r
,
1187 "Failed to set password for machine account (%s)\n",
1191 if (!NT_STATUS_IS_OK(result
)) {
1194 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1198 libnet_join_set_error_string(mem_ctx
, r
,
1199 "Failed to set password for machine account (%s)\n",
1204 status
= NT_STATUS_OK
;
1211 data_blob_clear_free(&session_key
);
1213 if (is_valid_policy_hnd(&sam_pol
)) {
1214 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1216 if (is_valid_policy_hnd(&domain_pol
)) {
1217 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1219 if (is_valid_policy_hnd(&user_pol
)) {
1220 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1222 TALLOC_FREE(pipe_hnd
);
1227 /****************************************************************
1228 ****************************************************************/
1230 NTSTATUS
libnet_join_ok(struct messaging_context
*msg_ctx
,
1231 const char *netbios_domain_name
,
1232 const char *dc_name
,
1233 const bool use_kerberos
)
1235 TALLOC_CTX
*frame
= talloc_stackframe();
1236 struct cli_state
*cli
= NULL
;
1237 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1238 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
1239 struct netlogon_creds_CredentialState
*creds
= NULL
;
1240 uint32_t netlogon_flags
= 0;
1241 enum netr_SchannelType sec_chan_type
= 0;
1243 char *machine_password
= NULL
;
1244 const char *machine_name
= NULL
;
1245 const char *machine_account
= NULL
;
1247 struct samr_Password current_nt_hash
;
1248 struct samr_Password
*previous_nt_hash
= NULL
;
1253 return NT_STATUS_INVALID_PARAMETER
;
1256 if (!secrets_init()) {
1258 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1261 ok
= get_trust_pw_clear(netbios_domain_name
,
1267 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1270 machine_account
= talloc_asprintf(frame
, "%s$", machine_name
);
1271 if (machine_account
== NULL
) {
1272 SAFE_FREE(machine_password
);
1273 SAFE_FREE(previous_nt_hash
);
1275 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1279 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
1282 status
= cli_full_connection(&cli
, NULL
,
1287 netbios_domain_name
,
1290 SMB_SIGNING_DEFAULT
);
1292 E_md4hash(machine_password
, current_nt_hash
.hash
);
1293 SAFE_FREE(machine_password
);
1295 if (!NT_STATUS_IS_OK(status
)) {
1296 status
= cli_full_connection(&cli
, NULL
,
1304 SMB_SIGNING_DEFAULT
);
1307 if (!NT_STATUS_IS_OK(status
)) {
1308 SAFE_FREE(previous_nt_hash
);
1313 status
= rpccli_create_netlogon_creds(dc_name
,
1314 netbios_domain_name
,
1320 if (!NT_STATUS_IS_OK(status
)) {
1321 SAFE_FREE(previous_nt_hash
);
1327 status
= rpccli_setup_netlogon_creds(cli
,
1329 true, /* force_reauth */
1332 SAFE_FREE(previous_nt_hash
);
1333 if (!NT_STATUS_IS_OK(status
)) {
1334 DEBUG(0,("connect_to_domain_password_server: "
1335 "unable to open the domain client session to "
1336 "machine %s. Flags[0x%08X] Error was : %s.\n",
1337 dc_name
, (unsigned)netlogon_flags
,
1338 nt_errstr(status
)));
1344 status
= netlogon_creds_cli_get(netlogon_creds
,
1347 if (!NT_STATUS_IS_OK(status
)) {
1352 netlogon_flags
= creds
->negotiate_flags
;
1355 if (!(netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
)) {
1358 return NT_STATUS_OK
;
1361 status
= cli_rpc_pipe_open_schannel_with_key(
1362 cli
, &ndr_table_netlogon
, NCACN_NP
,
1363 netbios_domain_name
,
1364 netlogon_creds
, &netlogon_pipe
);
1366 TALLOC_FREE(netlogon_pipe
);
1368 if (!NT_STATUS_IS_OK(status
)) {
1369 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1370 "on netlogon pipe to server %s for domain %s. "
1372 smbXcli_conn_remote_name(cli
->conn
),
1373 netbios_domain_name
, nt_errstr(status
)));
1381 return NT_STATUS_OK
;
1384 /****************************************************************
1385 ****************************************************************/
1387 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1388 struct libnet_JoinCtx
*r
)
1392 status
= libnet_join_ok(r
->in
.msg_ctx
,
1393 r
->out
.netbios_domain_name
,
1395 r
->in
.use_kerberos
);
1396 if (!NT_STATUS_IS_OK(status
)) {
1397 libnet_join_set_error_string(mem_ctx
, r
,
1398 "failed to verify domain membership after joining: %s",
1399 get_friendly_nt_error_msg(status
));
1400 return WERR_SETUP_NOT_JOINED
;
1406 /****************************************************************
1407 ****************************************************************/
1409 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1410 struct libnet_UnjoinCtx
*r
)
1412 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1416 if (!secrets_delete_domain_sid(lp_workgroup())) {
1423 /****************************************************************
1424 ****************************************************************/
1426 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1427 struct libnet_UnjoinCtx
*r
)
1429 struct cli_state
*cli
= NULL
;
1430 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1431 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1432 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1435 struct lsa_String lsa_acct_name
;
1436 struct samr_Ids user_rids
;
1437 struct samr_Ids name_types
;
1438 union samr_UserInfo
*info
= NULL
;
1439 struct dcerpc_binding_handle
*b
= NULL
;
1441 ZERO_STRUCT(sam_pol
);
1442 ZERO_STRUCT(domain_pol
);
1443 ZERO_STRUCT(user_pol
);
1445 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1446 r
->in
.admin_account
,
1448 r
->in
.admin_password
,
1451 if (!NT_STATUS_IS_OK(status
)) {
1455 /* Open the domain */
1457 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
1459 if (!NT_STATUS_IS_OK(status
)) {
1460 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1461 nt_errstr(status
)));
1465 b
= pipe_hnd
->binding_handle
;
1467 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1469 SEC_FLAG_MAXIMUM_ALLOWED
,
1472 if (!NT_STATUS_IS_OK(status
)) {
1475 if (!NT_STATUS_IS_OK(result
)) {
1480 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1482 SEC_FLAG_MAXIMUM_ALLOWED
,
1486 if (!NT_STATUS_IS_OK(status
)) {
1489 if (!NT_STATUS_IS_OK(result
)) {
1494 /* Create domain user */
1496 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1497 if (!strlower_m(acct_name
)) {
1498 status
= NT_STATUS_INVALID_PARAMETER
;
1502 init_lsa_String(&lsa_acct_name
, acct_name
);
1504 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1512 if (!NT_STATUS_IS_OK(status
)) {
1515 if (!NT_STATUS_IS_OK(result
)) {
1519 if (user_rids
.count
!= 1) {
1520 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1523 if (name_types
.count
!= 1) {
1524 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1528 if (name_types
.ids
[0] != SID_NAME_USER
) {
1529 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1530 name_types
.ids
[0]));
1531 status
= NT_STATUS_INVALID_WORKSTATION
;
1535 user_rid
= user_rids
.ids
[0];
1537 /* Open handle on user */
1539 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1541 SEC_FLAG_MAXIMUM_ALLOWED
,
1545 if (!NT_STATUS_IS_OK(status
)) {
1548 if (!NT_STATUS_IS_OK(result
)) {
1555 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1560 if (!NT_STATUS_IS_OK(status
)) {
1561 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1564 if (!NT_STATUS_IS_OK(result
)) {
1566 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1570 /* now disable and setuser info */
1572 info
->info16
.acct_flags
|= ACB_DISABLED
;
1574 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1579 if (!NT_STATUS_IS_OK(status
)) {
1580 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1583 if (!NT_STATUS_IS_OK(result
)) {
1585 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1589 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1592 if (pipe_hnd
&& b
) {
1593 if (is_valid_policy_hnd(&domain_pol
)) {
1594 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1596 if (is_valid_policy_hnd(&sam_pol
)) {
1597 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1599 TALLOC_FREE(pipe_hnd
);
1609 /****************************************************************
1610 ****************************************************************/
1612 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1614 WERROR werr
= WERR_OK
;
1616 struct smbconf_ctx
*ctx
;
1618 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1619 if (!SBC_ERROR_IS_OK(err
)) {
1620 werr
= WERR_NO_SUCH_SERVICE
;
1624 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1626 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1627 if (!SBC_ERROR_IS_OK(err
)) {
1628 werr
= WERR_NO_SUCH_SERVICE
;
1632 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1634 if (!SBC_ERROR_IS_OK(err
)) {
1635 werr
= WERR_NO_SUCH_SERVICE
;
1639 smbconf_delete_global_parameter(ctx
, "realm");
1643 err
= smbconf_set_global_parameter(ctx
, "security", "domain");
1644 if (!SBC_ERROR_IS_OK(err
)) {
1645 werr
= WERR_NO_SUCH_SERVICE
;
1649 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1650 r
->out
.netbios_domain_name
);
1651 if (!SBC_ERROR_IS_OK(err
)) {
1652 werr
= WERR_NO_SUCH_SERVICE
;
1656 if (r
->out
.domain_is_ad
) {
1657 err
= smbconf_set_global_parameter(ctx
, "security", "ads");
1658 if (!SBC_ERROR_IS_OK(err
)) {
1659 werr
= WERR_NO_SUCH_SERVICE
;
1663 err
= smbconf_set_global_parameter(ctx
, "realm",
1664 r
->out
.dns_domain_name
);
1665 if (!SBC_ERROR_IS_OK(err
)) {
1666 werr
= WERR_NO_SUCH_SERVICE
;
1672 smbconf_shutdown(ctx
);
1676 /****************************************************************
1677 ****************************************************************/
1679 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1681 WERROR werr
= WERR_OK
;
1683 struct smbconf_ctx
*ctx
;
1685 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1686 if (!SBC_ERROR_IS_OK(err
)) {
1687 werr
= WERR_NO_SUCH_SERVICE
;
1691 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1693 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1694 if (!SBC_ERROR_IS_OK(err
)) {
1695 werr
= WERR_NO_SUCH_SERVICE
;
1699 err
= smbconf_delete_global_parameter(ctx
, "workgroup");
1700 if (!SBC_ERROR_IS_OK(err
)) {
1701 werr
= WERR_NO_SUCH_SERVICE
;
1705 smbconf_delete_global_parameter(ctx
, "realm");
1709 smbconf_shutdown(ctx
);
1713 /****************************************************************
1714 ****************************************************************/
1716 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1720 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1721 return r
->out
.result
;
1724 if (!r
->in
.modify_config
) {
1728 werr
= do_join_modify_vals_config(r
);
1729 if (!W_ERROR_IS_OK(werr
)) {
1733 lp_load_global(get_dyn_CONFIGFILE());
1735 r
->out
.modified_config
= true;
1736 r
->out
.result
= werr
;
1741 /****************************************************************
1742 ****************************************************************/
1744 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1748 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1749 return r
->out
.result
;
1752 if (!r
->in
.modify_config
) {
1756 werr
= do_unjoin_modify_vals_config(r
);
1757 if (!W_ERROR_IS_OK(werr
)) {
1761 lp_load_global(get_dyn_CONFIGFILE());
1763 r
->out
.modified_config
= true;
1764 r
->out
.result
= werr
;
1769 /****************************************************************
1770 ****************************************************************/
1772 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1773 const char *domain_str
,
1774 const char **domain_p
,
1777 char *domain
= NULL
;
1779 const char *p
= NULL
;
1781 if (!domain_str
|| !domain_p
|| !dc_p
) {
1785 p
= strchr_m(domain_str
, '\\');
1788 domain
= talloc_strndup(mem_ctx
, domain_str
,
1789 PTR_DIFF(p
, domain_str
));
1790 dc
= talloc_strdup(mem_ctx
, p
+1);
1795 domain
= talloc_strdup(mem_ctx
, domain_str
);
1811 /****************************************************************
1812 ****************************************************************/
1814 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1815 struct libnet_JoinCtx
*r
)
1817 if (!r
->in
.domain_name
) {
1818 libnet_join_set_error_string(mem_ctx
, r
,
1819 "No domain name defined");
1820 return WERR_INVALID_PARAM
;
1823 if (strlen(r
->in
.machine_name
) > 15) {
1824 libnet_join_set_error_string(mem_ctx
, r
,
1825 "Our netbios name can be at most 15 chars long, "
1826 "\"%s\" is %u chars long\n",
1828 (unsigned int)strlen(r
->in
.machine_name
));
1829 return WERR_INVALID_PARAM
;
1832 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1835 libnet_join_set_error_string(mem_ctx
, r
,
1836 "Failed to parse domain name");
1837 return WERR_INVALID_PARAM
;
1841 return WERR_SETUP_DOMAIN_CONTROLLER
;
1844 if (!r
->in
.admin_domain
) {
1845 char *admin_domain
= NULL
;
1846 char *admin_account
= NULL
;
1847 split_domain_user(mem_ctx
,
1848 r
->in
.admin_account
,
1851 r
->in
.admin_domain
= admin_domain
;
1852 r
->in
.admin_account
= admin_account
;
1855 if (!secrets_init()) {
1856 libnet_join_set_error_string(mem_ctx
, r
,
1857 "Unable to open secrets database");
1858 return WERR_CAN_NOT_COMPLETE
;
1864 /****************************************************************
1865 ****************************************************************/
1867 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1871 /* Try adding dom admins to builtin\admins. Only log failures. */
1872 status
= create_builtin_administrators(domain_sid
);
1873 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1874 DEBUG(10,("Unable to auto-add domain administrators to "
1875 "BUILTIN\\Administrators during join because "
1876 "winbindd must be running.\n"));
1877 } else if (!NT_STATUS_IS_OK(status
)) {
1878 DEBUG(5, ("Failed to auto-add domain administrators to "
1879 "BUILTIN\\Administrators during join: %s\n",
1880 nt_errstr(status
)));
1883 /* Try adding dom users to builtin\users. Only log failures. */
1884 status
= create_builtin_users(domain_sid
);
1885 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1886 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1887 "during join because winbindd must be running.\n"));
1888 } else if (!NT_STATUS_IS_OK(status
)) {
1889 DEBUG(5, ("Failed to auto-add domain administrators to "
1890 "BUILTIN\\Administrators during join: %s\n",
1891 nt_errstr(status
)));
1895 /****************************************************************
1896 ****************************************************************/
1898 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1899 struct libnet_JoinCtx
*r
)
1903 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1904 return r
->out
.result
;
1907 werr
= do_JoinConfig(r
);
1908 if (!W_ERROR_IS_OK(werr
)) {
1912 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1916 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1917 if (r
->out
.dns_domain_name
) {
1918 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1922 if (r
->out
.domain_is_ad
&&
1923 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1924 ADS_STATUS ads_status
;
1926 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1927 if (!ADS_ERR_OK(ads_status
)) {
1928 return WERR_GENERAL_FAILURE
;
1931 #endif /* HAVE_ADS */
1933 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1938 /****************************************************************
1939 ****************************************************************/
1941 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1944 ads_destroy(&r
->in
.ads
);
1950 /****************************************************************
1951 ****************************************************************/
1953 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1956 ads_destroy(&r
->in
.ads
);
1962 /****************************************************************
1963 ****************************************************************/
1965 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1966 struct libnet_JoinCtx
**r
)
1968 struct libnet_JoinCtx
*ctx
;
1970 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1975 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1977 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
1978 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1980 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1987 /****************************************************************
1988 ****************************************************************/
1990 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1991 struct libnet_UnjoinCtx
**r
)
1993 struct libnet_UnjoinCtx
*ctx
;
1995 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
2000 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
2002 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
2003 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
2010 /****************************************************************
2011 ****************************************************************/
2013 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
2014 struct libnet_JoinCtx
*r
)
2016 bool valid_security
= false;
2017 bool valid_workgroup
= false;
2018 bool valid_realm
= false;
2020 /* check if configuration is already set correctly */
2022 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
2024 switch (r
->out
.domain_is_ad
) {
2026 valid_security
= (lp_security() == SEC_DOMAIN
);
2027 if (valid_workgroup
&& valid_security
) {
2028 /* nothing to be done */
2033 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
2034 switch (lp_security()) {
2037 valid_security
= true;
2040 if (valid_workgroup
&& valid_realm
&& valid_security
) {
2041 /* nothing to be done */
2047 /* check if we are supposed to manipulate configuration */
2049 if (!r
->in
.modify_config
) {
2051 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
2053 if (!valid_workgroup
) {
2054 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2055 "\"workgroup\" set to '%s', should be '%s'",
2056 lp_workgroup(), r
->out
.netbios_domain_name
);
2057 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2061 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2062 "\"realm\" set to '%s', should be '%s'",
2063 lp_realm(), r
->out
.dns_domain_name
);
2064 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2067 if (!valid_security
) {
2068 const char *sec
= NULL
;
2069 switch (lp_security()) {
2070 case SEC_USER
: sec
= "user"; break;
2071 case SEC_DOMAIN
: sec
= "domain"; break;
2072 case SEC_ADS
: sec
= "ads"; break;
2074 wrong_conf
= talloc_asprintf_append(wrong_conf
,
2075 "\"security\" set to '%s', should be %s",
2076 sec
, r
->out
.domain_is_ad
?
2077 "either 'domain' or 'ads'" : "'domain'");
2078 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
2081 libnet_join_set_error_string(mem_ctx
, r
,
2082 "Invalid configuration (%s) and configuration modification "
2083 "was not requested", wrong_conf
);
2084 return WERR_CAN_NOT_COMPLETE
;
2087 /* check if we are able to manipulate configuration */
2089 if (!lp_config_backend_is_registry()) {
2090 libnet_join_set_error_string(mem_ctx
, r
,
2091 "Configuration manipulation requested but not "
2092 "supported by backend");
2093 return WERR_NOT_SUPPORTED
;
2099 /****************************************************************
2100 ****************************************************************/
2102 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
2103 struct libnet_JoinCtx
*r
)
2107 struct cli_state
*cli
= NULL
;
2109 ADS_STATUS ads_status
;
2110 #endif /* HAVE_ADS */
2112 if (!r
->in
.dc_name
) {
2113 struct netr_DsRGetDCNameInfo
*info
;
2115 status
= dsgetdcname(mem_ctx
,
2120 DS_FORCE_REDISCOVERY
|
2121 DS_DIRECTORY_SERVICE_REQUIRED
|
2122 DS_WRITABLE_REQUIRED
|
2125 if (!NT_STATUS_IS_OK(status
)) {
2126 libnet_join_set_error_string(mem_ctx
, r
,
2127 "failed to find DC for domain %s",
2129 get_friendly_nt_error_msg(status
));
2130 return WERR_DCNOTFOUND
;
2133 dc
= strip_hostname(info
->dc_unc
);
2134 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2135 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2138 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
2139 if (!NT_STATUS_IS_OK(status
)) {
2140 libnet_join_set_error_string(mem_ctx
, r
,
2141 "failed to lookup DC info for domain '%s' over rpc: %s",
2142 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2143 return ntstatus_to_werror(status
);
2146 werr
= libnet_join_check_config(mem_ctx
, r
);
2147 if (!W_ERROR_IS_OK(werr
)) {
2153 create_local_private_krb5_conf_for_domain(
2154 r
->out
.dns_domain_name
, r
->out
.netbios_domain_name
,
2155 NULL
, smbXcli_conn_remote_sockaddr(cli
->conn
));
2157 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
2158 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2160 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
2161 if (!ADS_ERR_OK(ads_status
)) {
2162 return WERR_DEFAULT_JOIN_REQUIRED
;
2165 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
2166 if (!ADS_ERR_OK(ads_status
)) {
2167 libnet_join_set_error_string(mem_ctx
, r
,
2168 "failed to precreate account in ou %s: %s",
2170 ads_errstr(ads_status
));
2171 return WERR_DEFAULT_JOIN_REQUIRED
;
2174 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
2176 #endif /* HAVE_ADS */
2178 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
2179 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
2180 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2182 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2184 if (!NT_STATUS_IS_OK(status
)) {
2185 libnet_join_set_error_string(mem_ctx
, r
,
2186 "failed to join domain '%s' over rpc: %s",
2187 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2188 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2189 return WERR_SETUP_ALREADY_JOINED
;
2191 werr
= ntstatus_to_werror(status
);
2195 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2196 werr
= WERR_SETUP_NOT_JOINED
;
2210 /****************************************************************
2211 ****************************************************************/
2213 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2214 struct libnet_JoinCtx
*r
)
2217 struct libnet_UnjoinCtx
*u
= NULL
;
2219 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2220 if (!W_ERROR_IS_OK(werr
)) {
2224 u
->in
.debug
= r
->in
.debug
;
2225 u
->in
.dc_name
= r
->in
.dc_name
;
2226 u
->in
.domain_name
= r
->in
.domain_name
;
2227 u
->in
.admin_account
= r
->in
.admin_account
;
2228 u
->in
.admin_password
= r
->in
.admin_password
;
2229 u
->in
.modify_config
= r
->in
.modify_config
;
2230 u
->in
.use_kerberos
= r
->in
.use_kerberos
;
2231 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2232 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2234 werr
= libnet_Unjoin(mem_ctx
, u
);
2240 /****************************************************************
2241 ****************************************************************/
2243 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2244 struct libnet_JoinCtx
*r
)
2249 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2252 ZERO_STRUCT(r
->out
);
2254 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2255 if (!W_ERROR_IS_OK(werr
)) {
2259 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2260 werr
= libnet_DomainJoin(mem_ctx
, r
);
2261 if (!W_ERROR_IS_OK(werr
)) {
2266 werr
= libnet_join_post_processing(mem_ctx
, r
);
2267 if (!W_ERROR_IS_OK(werr
)) {
2271 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2272 werr
= libnet_join_post_verify(mem_ctx
, r
);
2273 if (!W_ERROR_IS_OK(werr
)) {
2274 libnet_join_rollback(mem_ctx
, r
);
2279 r
->out
.result
= werr
;
2282 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2287 /****************************************************************
2288 ****************************************************************/
2290 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2291 struct libnet_UnjoinCtx
*r
)
2295 if (!r
->in
.domain_sid
) {
2297 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2298 libnet_unjoin_set_error_string(mem_ctx
, r
,
2299 "Unable to fetch domain sid: are we joined?");
2300 return WERR_SETUP_NOT_JOINED
;
2302 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2303 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2306 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2307 !r
->in
.delete_machine_account
) {
2308 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2312 if (!r
->in
.dc_name
) {
2313 struct netr_DsRGetDCNameInfo
*info
;
2315 status
= dsgetdcname(mem_ctx
,
2320 DS_DIRECTORY_SERVICE_REQUIRED
|
2321 DS_WRITABLE_REQUIRED
|
2324 if (!NT_STATUS_IS_OK(status
)) {
2325 libnet_unjoin_set_error_string(mem_ctx
, r
,
2326 "failed to find DC for domain %s",
2328 get_friendly_nt_error_msg(status
));
2329 return WERR_DCNOTFOUND
;
2332 dc
= strip_hostname(info
->dc_unc
);
2333 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2334 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2338 /* for net ads leave, try to delete the account. If it works,
2339 no sense in disabling. If it fails, we can still try to
2342 if (r
->in
.delete_machine_account
) {
2343 ADS_STATUS ads_status
;
2344 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2345 if (ADS_ERR_OK(ads_status
)) {
2347 r
->out
.dns_domain_name
=
2348 talloc_strdup(mem_ctx
,
2349 r
->in
.ads
->server
.realm
);
2351 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2353 if (!ADS_ERR_OK(ads_status
)) {
2354 libnet_unjoin_set_error_string(mem_ctx
, r
,
2355 "failed to remove machine account from AD: %s",
2356 ads_errstr(ads_status
));
2358 r
->out
.deleted_machine_account
= true;
2359 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2360 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2364 #endif /* HAVE_ADS */
2366 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2368 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2369 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2370 if (!NT_STATUS_IS_OK(status
)) {
2371 libnet_unjoin_set_error_string(mem_ctx
, r
,
2372 "failed to disable machine account via rpc: %s",
2373 get_friendly_nt_error_msg(status
));
2374 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2375 return WERR_SETUP_NOT_JOINED
;
2377 return ntstatus_to_werror(status
);
2380 r
->out
.disabled_machine_account
= true;
2383 /* If disable succeeded or was not requested at all, we
2384 should be getting rid of our end of things */
2386 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2391 /****************************************************************
2392 ****************************************************************/
2394 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2395 struct libnet_UnjoinCtx
*r
)
2397 if (!r
->in
.domain_name
) {
2398 libnet_unjoin_set_error_string(mem_ctx
, r
,
2399 "No domain name defined");
2400 return WERR_INVALID_PARAM
;
2403 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2406 libnet_unjoin_set_error_string(mem_ctx
, r
,
2407 "Failed to parse domain name");
2408 return WERR_INVALID_PARAM
;
2412 return WERR_SETUP_DOMAIN_CONTROLLER
;
2415 if (!r
->in
.admin_domain
) {
2416 char *admin_domain
= NULL
;
2417 char *admin_account
= NULL
;
2418 split_domain_user(mem_ctx
,
2419 r
->in
.admin_account
,
2422 r
->in
.admin_domain
= admin_domain
;
2423 r
->in
.admin_account
= admin_account
;
2426 if (!secrets_init()) {
2427 libnet_unjoin_set_error_string(mem_ctx
, r
,
2428 "Unable to open secrets database");
2429 return WERR_CAN_NOT_COMPLETE
;
2435 /****************************************************************
2436 ****************************************************************/
2438 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2439 struct libnet_UnjoinCtx
*r
)
2441 saf_delete(r
->out
.netbios_domain_name
);
2442 saf_delete(r
->out
.dns_domain_name
);
2444 return libnet_unjoin_config(r
);
2447 /****************************************************************
2448 ****************************************************************/
2450 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2451 struct libnet_UnjoinCtx
*r
)
2456 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2459 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2460 if (!W_ERROR_IS_OK(werr
)) {
2464 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2465 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2466 if (!W_ERROR_IS_OK(werr
)) {
2467 libnet_unjoin_config(r
);
2472 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2473 if (!W_ERROR_IS_OK(werr
)) {
2478 r
->out
.result
= werr
;
2481 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);