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"
39 #include "../libcli/security/security.h"
41 /****************************************************************
42 ****************************************************************/
44 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
47 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
48 DEBUG(1,("libnet_Join:\n%s", str)); \
52 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
53 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
54 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
55 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
57 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
60 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
61 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
65 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
66 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
67 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
68 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
70 /****************************************************************
71 ****************************************************************/
73 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
74 struct libnet_JoinCtx
*r
,
75 const char *format
, ...)
79 if (r
->out
.error_string
) {
83 va_start(args
, format
);
84 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
88 /****************************************************************
89 ****************************************************************/
91 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
92 struct libnet_UnjoinCtx
*r
,
93 const char *format
, ...)
97 if (r
->out
.error_string
) {
101 va_start(args
, format
);
102 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
108 /****************************************************************
109 ****************************************************************/
111 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
112 const char *netbios_domain_name
,
114 const char *user_name
,
115 const char *password
,
119 ADS_STRUCT
*my_ads
= NULL
;
122 my_ads
= ads_init(dns_domain_name
,
126 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
130 SAFE_FREE(my_ads
->auth
.user_name
);
131 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
132 if ((cp
= strchr_m(my_ads
->auth
.user_name
, '@'))!=0) {
134 SAFE_FREE(my_ads
->auth
.realm
);
135 my_ads
->auth
.realm
= smb_xstrdup(cp
);
136 strupper_m(my_ads
->auth
.realm
);
141 SAFE_FREE(my_ads
->auth
.password
);
142 my_ads
->auth
.password
= SMB_STRDUP(password
);
145 status
= ads_connect_user_creds(my_ads
);
146 if (!ADS_ERR_OK(status
)) {
147 ads_destroy(&my_ads
);
155 /****************************************************************
156 ****************************************************************/
158 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
159 struct libnet_JoinCtx
*r
)
163 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
164 r
->out
.netbios_domain_name
,
167 r
->in
.admin_password
,
169 if (!ADS_ERR_OK(status
)) {
170 libnet_join_set_error_string(mem_ctx
, r
,
171 "failed to connect to AD: %s",
176 if (!r
->out
.netbios_domain_name
) {
177 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
178 r
->in
.ads
->server
.workgroup
);
179 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
182 if (!r
->out
.dns_domain_name
) {
183 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
184 r
->in
.ads
->config
.realm
);
185 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
188 r
->out
.domain_is_ad
= true;
193 /****************************************************************
194 ****************************************************************/
196 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
197 struct libnet_UnjoinCtx
*r
)
201 status
= libnet_connect_ads(r
->in
.domain_name
,
205 r
->in
.admin_password
,
207 if (!ADS_ERR_OK(status
)) {
208 libnet_unjoin_set_error_string(mem_ctx
, r
,
209 "failed to connect to AD: %s",
216 /****************************************************************
217 join a domain using ADS (LDAP mods)
218 ****************************************************************/
220 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
221 struct libnet_JoinCtx
*r
)
224 LDAPMessage
*res
= NULL
;
225 const char *attrs
[] = { "dn", NULL
};
228 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
229 if (!ADS_ERR_OK(status
)) {
233 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
234 if (!ADS_ERR_OK(status
)) {
238 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
239 ads_msgfree(r
->in
.ads
, res
);
240 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
243 ads_msgfree(r
->in
.ads
, res
);
245 /* Attempt to create the machine account and bail if this fails.
246 Assume that the admin wants exactly what they requested */
248 status
= ads_create_machine_acct(r
->in
.ads
,
252 if (ADS_ERR_OK(status
)) {
253 DEBUG(1,("machine account creation created\n"));
255 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
256 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
257 status
= ADS_SUCCESS
;
260 if (!ADS_ERR_OK(status
)) {
261 DEBUG(1,("machine account creation failed\n"));
265 status
= ads_move_machine_acct(r
->in
.ads
,
269 if (!ADS_ERR_OK(status
)) {
270 DEBUG(1,("failure to locate/move pre-existing "
271 "machine account\n"));
275 DEBUG(1,("The machine account %s the specified OU.\n",
276 moved
? "was moved into" : "already exists in"));
281 /****************************************************************
282 ****************************************************************/
284 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
285 struct libnet_UnjoinCtx
*r
)
290 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
291 if (!ADS_ERR_OK(status
)) {
292 libnet_unjoin_set_error_string(mem_ctx
, r
,
293 "failed to connect to AD: %s",
299 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
300 if (!ADS_ERR_OK(status
)) {
301 libnet_unjoin_set_error_string(mem_ctx
, r
,
302 "failed to leave realm: %s",
310 /****************************************************************
311 ****************************************************************/
313 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
314 struct libnet_JoinCtx
*r
)
317 LDAPMessage
*res
= NULL
;
320 if (!r
->in
.machine_name
) {
321 return ADS_ERROR(LDAP_NO_MEMORY
);
324 status
= ads_find_machine_acct(r
->in
.ads
,
327 if (!ADS_ERR_OK(status
)) {
331 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
332 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
336 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
338 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
342 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
344 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
349 ads_msgfree(r
->in
.ads
, res
);
355 /****************************************************************
356 Set a machines dNSHostName and servicePrincipalName attributes
357 ****************************************************************/
359 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
360 struct libnet_JoinCtx
*r
)
365 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
370 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
371 if (!ADS_ERR_OK(status
)) {
375 /* Windows only creates HOST/shortname & HOST/fqdn. */
377 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
379 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
384 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
385 || (strchr(my_fqdn
, '.') == NULL
)) {
386 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
387 r
->out
.dns_domain_name
);
392 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
393 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
395 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
400 mods
= ads_init_mods(mem_ctx
);
402 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
405 /* fields of primary importance */
407 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
408 if (!ADS_ERR_OK(status
)) {
409 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
412 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
414 if (!ADS_ERR_OK(status
)) {
415 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
418 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
421 /****************************************************************
422 ****************************************************************/
424 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
425 struct libnet_JoinCtx
*r
)
430 if (!r
->in
.create_upn
) {
436 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
437 if (!ADS_ERR_OK(status
)) {
442 r
->in
.upn
= talloc_asprintf(mem_ctx
,
445 r
->out
.dns_domain_name
);
447 return ADS_ERROR(LDAP_NO_MEMORY
);
451 /* now do the mods */
453 mods
= ads_init_mods(mem_ctx
);
455 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
458 /* fields of primary importance */
460 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
461 if (!ADS_ERR_OK(status
)) {
462 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
465 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
469 /****************************************************************
470 ****************************************************************/
472 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
473 struct libnet_JoinCtx
*r
)
479 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
485 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
486 if (!ADS_ERR_OK(status
)) {
490 /* now do the mods */
492 mods
= ads_init_mods(mem_ctx
);
494 return ADS_ERROR(LDAP_NO_MEMORY
);
497 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
499 return ADS_ERROR(LDAP_NO_MEMORY
);
502 /* fields of primary importance */
504 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
506 if (!ADS_ERR_OK(status
)) {
510 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
512 if (!ADS_ERR_OK(status
)) {
516 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
518 if (!ADS_ERR_OK(status
)) {
522 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
525 /****************************************************************
526 ****************************************************************/
528 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
529 struct libnet_JoinCtx
*r
)
531 if (!USE_SYSTEM_KEYTAB
) {
535 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
542 /****************************************************************
543 ****************************************************************/
545 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
546 struct libnet_JoinCtx
*r
)
548 uint32_t domain_func
;
550 const char *salt
= NULL
;
551 char *std_salt
= NULL
;
553 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
554 if (!ADS_ERR_OK(status
)) {
555 libnet_join_set_error_string(mem_ctx
, r
,
556 "failed to determine domain functional level: %s",
561 /* go ahead and setup the default salt */
563 std_salt
= kerberos_standard_des_salt();
565 libnet_join_set_error_string(mem_ctx
, r
,
566 "failed to obtain standard DES salt");
570 salt
= talloc_strdup(mem_ctx
, std_salt
);
577 /* if it's a Windows functional domain, we have to look for the UPN */
579 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
582 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
585 salt
= talloc_strdup(mem_ctx
, upn
);
592 return kerberos_secrets_store_des_salt(salt
);
595 /****************************************************************
596 ****************************************************************/
598 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
599 struct libnet_JoinCtx
*r
)
604 status
= libnet_join_connect_ads(mem_ctx
, r
);
605 if (!ADS_ERR_OK(status
)) {
610 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
611 if (!ADS_ERR_OK(status
)) {
612 libnet_join_set_error_string(mem_ctx
, r
,
613 "failed to set machine spn: %s",
618 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
619 if (!ADS_ERR_OK(status
)) {
620 libnet_join_set_error_string(mem_ctx
, r
,
621 "failed to set machine os attributes: %s",
626 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
627 if (!ADS_ERR_OK(status
)) {
628 libnet_join_set_error_string(mem_ctx
, r
,
629 "failed to set machine upn: %s",
634 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
635 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
638 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
639 libnet_join_set_error_string(mem_ctx
, r
,
640 "failed to create kerberos keytab");
641 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
646 #endif /* HAVE_ADS */
648 /****************************************************************
649 Store the machine password and domain SID
650 ****************************************************************/
652 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
653 struct libnet_JoinCtx
*r
)
655 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
658 DEBUG(1,("Failed to save domain sid\n"));
662 if (!secrets_store_machine_password(r
->in
.machine_password
,
663 r
->out
.netbios_domain_name
,
664 r
->in
.secure_channel_type
))
666 DEBUG(1,("Failed to save machine password\n"));
673 /****************************************************************
674 Connect dc's IPC$ share
675 ****************************************************************/
677 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
681 struct cli_state
**cli
)
686 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
689 if (use_kerberos
&& pass
) {
690 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
693 return cli_full_connection(cli
, NULL
,
704 /****************************************************************
705 Lookup domain dc's info
706 ****************************************************************/
708 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
709 struct libnet_JoinCtx
*r
,
710 struct cli_state
**cli
)
712 struct rpc_pipe_client
*pipe_hnd
= NULL
;
713 struct policy_handle lsa_pol
;
714 NTSTATUS status
, result
;
715 union lsa_PolicyInformation
*info
= NULL
;
716 struct dcerpc_binding_handle
*b
;
718 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
720 r
->in
.admin_password
,
723 if (!NT_STATUS_IS_OK(status
)) {
727 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
729 if (!NT_STATUS_IS_OK(status
)) {
730 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
735 b
= pipe_hnd
->binding_handle
;
737 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
738 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
739 if (!NT_STATUS_IS_OK(status
)) {
743 status
= dcerpc_lsa_QueryInfoPolicy2(b
, mem_ctx
,
748 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
749 r
->out
.domain_is_ad
= true;
750 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
751 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
752 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
753 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
754 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
757 if (!NT_STATUS_IS_OK(status
)) {
758 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
760 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
763 if (!NT_STATUS_IS_OK(status
)) {
766 if (!NT_STATUS_IS_OK(result
)) {
771 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
772 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
773 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
776 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
777 TALLOC_FREE(pipe_hnd
);
783 /****************************************************************
784 Do the domain join unsecure
785 ****************************************************************/
787 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
788 struct libnet_JoinCtx
*r
,
789 struct cli_state
*cli
)
791 struct rpc_pipe_client
*pipe_hnd
= NULL
;
792 unsigned char orig_trust_passwd_hash
[16];
793 unsigned char new_trust_passwd_hash
[16];
794 fstring trust_passwd
;
797 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
799 if (!NT_STATUS_IS_OK(status
)) {
803 if (!r
->in
.machine_password
) {
804 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
805 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
808 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
810 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
811 fstrcpy(trust_passwd
, r
->in
.admin_password
);
812 strlower_m(trust_passwd
);
815 * Machine names can be 15 characters, but the max length on
816 * a password is 14. --jerry
819 trust_passwd
[14] = '\0';
821 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
823 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
825 orig_trust_passwd_hash
,
826 r
->in
.machine_password
,
827 new_trust_passwd_hash
,
828 r
->in
.secure_channel_type
);
833 /****************************************************************
835 ****************************************************************/
837 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
838 struct libnet_JoinCtx
*r
,
839 struct cli_state
*cli
)
841 struct rpc_pipe_client
*pipe_hnd
= NULL
;
842 struct policy_handle sam_pol
, domain_pol
, user_pol
;
843 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
845 struct lsa_String lsa_acct_name
;
847 uint32_t acct_flags
= ACB_WSTRUST
;
848 struct samr_Ids user_rids
;
849 struct samr_Ids name_types
;
850 union samr_UserInfo user_info
;
851 struct dcerpc_binding_handle
*b
= NULL
;
853 struct samr_CryptPassword crypt_pwd
;
854 struct samr_CryptPasswordEx crypt_pwd_ex
;
856 ZERO_STRUCT(sam_pol
);
857 ZERO_STRUCT(domain_pol
);
858 ZERO_STRUCT(user_pol
);
860 switch (r
->in
.secure_channel_type
) {
862 acct_flags
= ACB_WSTRUST
;
865 acct_flags
= ACB_SVRTRUST
;
868 return NT_STATUS_INVALID_PARAMETER
;
871 if (!r
->in
.machine_password
) {
872 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
873 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
876 /* Open the domain */
878 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
880 if (!NT_STATUS_IS_OK(status
)) {
881 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
886 b
= pipe_hnd
->binding_handle
;
888 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
890 SAMR_ACCESS_ENUM_DOMAINS
891 | SAMR_ACCESS_LOOKUP_DOMAIN
,
894 if (!NT_STATUS_IS_OK(status
)) {
897 if (!NT_STATUS_IS_OK(result
)) {
902 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
904 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
905 | SAMR_DOMAIN_ACCESS_CREATE_USER
906 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
910 if (!NT_STATUS_IS_OK(status
)) {
913 if (!NT_STATUS_IS_OK(result
)) {
918 /* Create domain user */
920 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
921 strlower_m(acct_name
);
923 init_lsa_String(&lsa_acct_name
, acct_name
);
925 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
926 uint32_t access_desired
=
927 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
928 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
929 SAMR_USER_ACCESS_SET_PASSWORD
|
930 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
931 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
932 uint32_t access_granted
= 0;
934 DEBUG(10,("Creating account with desired access mask: %d\n",
937 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
946 if (!NT_STATUS_IS_OK(status
)) {
951 if (!NT_STATUS_IS_OK(status
) &&
952 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
954 DEBUG(10,("Creation of workstation account failed: %s\n",
957 /* If NT_STATUS_ACCESS_DENIED then we have a valid
958 username/password combo but the user does not have
959 administrator access. */
961 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
962 libnet_join_set_error_string(mem_ctx
, r
,
963 "User specified does not have "
964 "administrator privileges");
970 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
971 if (!(r
->in
.join_flags
&
972 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
977 /* We *must* do this.... don't ask... */
979 if (NT_STATUS_IS_OK(status
)) {
980 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
984 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
991 if (!NT_STATUS_IS_OK(status
)) {
994 if (!NT_STATUS_IS_OK(result
)) {
999 if (name_types
.ids
[0] != SID_NAME_USER
) {
1000 DEBUG(0,("%s is not a user account (type=%d)\n",
1001 acct_name
, name_types
.ids
[0]));
1002 status
= NT_STATUS_INVALID_WORKSTATION
;
1006 user_rid
= user_rids
.ids
[0];
1008 /* Open handle on user */
1010 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1012 SEC_FLAG_MAXIMUM_ALLOWED
,
1016 if (!NT_STATUS_IS_OK(status
)) {
1019 if (!NT_STATUS_IS_OK(result
)) {
1024 /* Fill in the additional account flags now */
1026 acct_flags
|= ACB_PWNOEXP
;
1028 /* Set account flags on machine account */
1029 ZERO_STRUCT(user_info
.info16
);
1030 user_info
.info16
.acct_flags
= acct_flags
;
1032 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1037 if (!NT_STATUS_IS_OK(status
)) {
1038 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1042 libnet_join_set_error_string(mem_ctx
, r
,
1043 "Failed to set account flags for machine account (%s)\n",
1048 if (!NT_STATUS_IS_OK(result
)) {
1051 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1055 libnet_join_set_error_string(mem_ctx
, r
,
1056 "Failed to set account flags for machine account (%s)\n",
1061 /* Set password on machine account - first try level 26 */
1063 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1064 &cli
->user_session_key
,
1067 user_info
.info26
.password
= crypt_pwd_ex
;
1068 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1070 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1076 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
1078 /* retry with level 24 */
1080 init_samr_CryptPassword(r
->in
.machine_password
,
1081 &cli
->user_session_key
,
1084 user_info
.info24
.password
= crypt_pwd
;
1085 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1087 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1094 if (!NT_STATUS_IS_OK(status
)) {
1096 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1100 libnet_join_set_error_string(mem_ctx
, r
,
1101 "Failed to set password for machine account (%s)\n",
1105 if (!NT_STATUS_IS_OK(result
)) {
1108 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1112 libnet_join_set_error_string(mem_ctx
, r
,
1113 "Failed to set password for machine account (%s)\n",
1118 status
= NT_STATUS_OK
;
1125 if (is_valid_policy_hnd(&sam_pol
)) {
1126 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1128 if (is_valid_policy_hnd(&domain_pol
)) {
1129 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1131 if (is_valid_policy_hnd(&user_pol
)) {
1132 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1134 TALLOC_FREE(pipe_hnd
);
1139 /****************************************************************
1140 ****************************************************************/
1142 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1143 const char *machine_name
,
1144 const char *dc_name
)
1146 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1147 struct cli_state
*cli
= NULL
;
1148 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1149 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1151 char *machine_password
= NULL
;
1152 char *machine_account
= NULL
;
1155 return NT_STATUS_INVALID_PARAMETER
;
1158 if (!secrets_init()) {
1159 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1162 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1164 if (!machine_password
) {
1165 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1168 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1169 SAFE_FREE(machine_password
);
1170 return NT_STATUS_NO_MEMORY
;
1173 status
= cli_full_connection(&cli
, NULL
,
1182 free(machine_account
);
1183 free(machine_password
);
1185 if (!NT_STATUS_IS_OK(status
)) {
1186 status
= cli_full_connection(&cli
, NULL
,
1197 if (!NT_STATUS_IS_OK(status
)) {
1201 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1202 &neg_flags
, &netlogon_pipe
);
1203 if (!NT_STATUS_IS_OK(status
)) {
1204 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1206 return NT_STATUS_OK
;
1209 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1210 "key from server %s for domain %s. Error was %s\n",
1211 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1216 if (!lp_client_schannel()) {
1218 return NT_STATUS_OK
;
1221 status
= cli_rpc_pipe_open_schannel_with_key(
1222 cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
1223 DCERPC_AUTH_LEVEL_PRIVACY
,
1224 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1228 if (!NT_STATUS_IS_OK(status
)) {
1229 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1230 "on netlogon pipe to server %s for domain %s. "
1232 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1236 return NT_STATUS_OK
;
1239 /****************************************************************
1240 ****************************************************************/
1242 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1243 struct libnet_JoinCtx
*r
)
1247 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1250 if (!NT_STATUS_IS_OK(status
)) {
1251 libnet_join_set_error_string(mem_ctx
, r
,
1252 "failed to verify domain membership after joining: %s",
1253 get_friendly_nt_error_msg(status
));
1254 return WERR_SETUP_NOT_JOINED
;
1260 /****************************************************************
1261 ****************************************************************/
1263 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1264 struct libnet_UnjoinCtx
*r
)
1266 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1270 if (!secrets_delete_domain_sid(lp_workgroup())) {
1277 /****************************************************************
1278 ****************************************************************/
1280 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1281 struct libnet_UnjoinCtx
*r
)
1283 struct cli_state
*cli
= NULL
;
1284 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1285 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1286 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1289 struct lsa_String lsa_acct_name
;
1290 struct samr_Ids user_rids
;
1291 struct samr_Ids name_types
;
1292 union samr_UserInfo
*info
= NULL
;
1293 struct dcerpc_binding_handle
*b
;
1295 ZERO_STRUCT(sam_pol
);
1296 ZERO_STRUCT(domain_pol
);
1297 ZERO_STRUCT(user_pol
);
1299 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1300 r
->in
.admin_account
,
1301 r
->in
.admin_password
,
1304 if (!NT_STATUS_IS_OK(status
)) {
1308 /* Open the domain */
1310 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1312 if (!NT_STATUS_IS_OK(status
)) {
1313 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1314 nt_errstr(status
)));
1318 b
= pipe_hnd
->binding_handle
;
1320 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1322 SEC_FLAG_MAXIMUM_ALLOWED
,
1325 if (!NT_STATUS_IS_OK(status
)) {
1328 if (!NT_STATUS_IS_OK(result
)) {
1333 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1335 SEC_FLAG_MAXIMUM_ALLOWED
,
1339 if (!NT_STATUS_IS_OK(status
)) {
1342 if (!NT_STATUS_IS_OK(result
)) {
1347 /* Create domain user */
1349 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1350 strlower_m(acct_name
);
1352 init_lsa_String(&lsa_acct_name
, acct_name
);
1354 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1362 if (!NT_STATUS_IS_OK(status
)) {
1365 if (!NT_STATUS_IS_OK(result
)) {
1370 if (name_types
.ids
[0] != SID_NAME_USER
) {
1371 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1372 name_types
.ids
[0]));
1373 status
= NT_STATUS_INVALID_WORKSTATION
;
1377 user_rid
= user_rids
.ids
[0];
1379 /* Open handle on user */
1381 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1383 SEC_FLAG_MAXIMUM_ALLOWED
,
1387 if (!NT_STATUS_IS_OK(status
)) {
1390 if (!NT_STATUS_IS_OK(result
)) {
1397 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1402 if (!NT_STATUS_IS_OK(status
)) {
1403 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1406 if (!NT_STATUS_IS_OK(result
)) {
1408 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1412 /* now disable and setuser info */
1414 info
->info16
.acct_flags
|= ACB_DISABLED
;
1416 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1421 if (!NT_STATUS_IS_OK(status
)) {
1422 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1425 if (!NT_STATUS_IS_OK(result
)) {
1427 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1431 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1435 if (is_valid_policy_hnd(&domain_pol
)) {
1436 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1438 if (is_valid_policy_hnd(&sam_pol
)) {
1439 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1441 TALLOC_FREE(pipe_hnd
);
1451 /****************************************************************
1452 ****************************************************************/
1454 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1457 struct smbconf_ctx
*ctx
;
1459 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1460 if (!W_ERROR_IS_OK(werr
)) {
1464 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1466 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1467 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1469 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1472 smbconf_delete_global_parameter(ctx
, "realm");
1476 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1477 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1479 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1480 r
->out
.netbios_domain_name
);
1481 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1483 if (r
->out
.domain_is_ad
) {
1484 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1485 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1487 werr
= smbconf_set_global_parameter(ctx
, "realm",
1488 r
->out
.dns_domain_name
);
1489 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1493 smbconf_shutdown(ctx
);
1497 /****************************************************************
1498 ****************************************************************/
1500 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1502 WERROR werr
= WERR_OK
;
1503 struct smbconf_ctx
*ctx
;
1505 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1506 if (!W_ERROR_IS_OK(werr
)) {
1510 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1512 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1513 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1515 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1516 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1518 smbconf_delete_global_parameter(ctx
, "realm");
1522 smbconf_shutdown(ctx
);
1526 /****************************************************************
1527 ****************************************************************/
1529 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1533 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1534 return r
->out
.result
;
1537 if (!r
->in
.modify_config
) {
1541 werr
= do_join_modify_vals_config(r
);
1542 if (!W_ERROR_IS_OK(werr
)) {
1546 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1548 r
->out
.modified_config
= true;
1549 r
->out
.result
= werr
;
1554 /****************************************************************
1555 ****************************************************************/
1557 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1561 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1562 return r
->out
.result
;
1565 if (!r
->in
.modify_config
) {
1569 werr
= do_unjoin_modify_vals_config(r
);
1570 if (!W_ERROR_IS_OK(werr
)) {
1574 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1576 r
->out
.modified_config
= true;
1577 r
->out
.result
= werr
;
1582 /****************************************************************
1583 ****************************************************************/
1585 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1586 const char *domain_str
,
1587 const char **domain_p
,
1590 char *domain
= NULL
;
1592 const char *p
= NULL
;
1594 if (!domain_str
|| !domain_p
|| !dc_p
) {
1598 p
= strchr_m(domain_str
, '\\');
1601 domain
= talloc_strndup(mem_ctx
, domain_str
,
1602 PTR_DIFF(p
, domain_str
));
1603 dc
= talloc_strdup(mem_ctx
, p
+1);
1608 domain
= talloc_strdup(mem_ctx
, domain_str
);
1624 /****************************************************************
1625 ****************************************************************/
1627 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1628 struct libnet_JoinCtx
*r
)
1630 if (!r
->in
.domain_name
) {
1631 libnet_join_set_error_string(mem_ctx
, r
,
1632 "No domain name defined");
1633 return WERR_INVALID_PARAM
;
1636 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1639 libnet_join_set_error_string(mem_ctx
, r
,
1640 "Failed to parse domain name");
1641 return WERR_INVALID_PARAM
;
1645 return WERR_SETUP_DOMAIN_CONTROLLER
;
1648 if (!secrets_init()) {
1649 libnet_join_set_error_string(mem_ctx
, r
,
1650 "Unable to open secrets database");
1651 return WERR_CAN_NOT_COMPLETE
;
1657 /****************************************************************
1658 ****************************************************************/
1660 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1664 /* Try adding dom admins to builtin\admins. Only log failures. */
1665 status
= create_builtin_administrators(domain_sid
);
1666 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1667 DEBUG(10,("Unable to auto-add domain administrators to "
1668 "BUILTIN\\Administrators during join because "
1669 "winbindd must be running."));
1670 } else if (!NT_STATUS_IS_OK(status
)) {
1671 DEBUG(5, ("Failed to auto-add domain administrators to "
1672 "BUILTIN\\Administrators during join: %s\n",
1673 nt_errstr(status
)));
1676 /* Try adding dom users to builtin\users. Only log failures. */
1677 status
= create_builtin_users(domain_sid
);
1678 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1679 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1680 "during join because winbindd must be running."));
1681 } else if (!NT_STATUS_IS_OK(status
)) {
1682 DEBUG(5, ("Failed to auto-add domain administrators to "
1683 "BUILTIN\\Administrators during join: %s\n",
1684 nt_errstr(status
)));
1688 /****************************************************************
1689 ****************************************************************/
1691 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1692 struct libnet_JoinCtx
*r
)
1696 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1697 return r
->out
.result
;
1700 werr
= do_JoinConfig(r
);
1701 if (!W_ERROR_IS_OK(werr
)) {
1705 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1709 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1710 if (r
->out
.dns_domain_name
) {
1711 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1715 if (r
->out
.domain_is_ad
&&
1716 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1717 ADS_STATUS ads_status
;
1719 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1720 if (!ADS_ERR_OK(ads_status
)) {
1721 return WERR_GENERAL_FAILURE
;
1724 #endif /* HAVE_ADS */
1726 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1731 /****************************************************************
1732 ****************************************************************/
1734 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1736 const char *krb5_cc_env
= NULL
;
1739 ads_destroy(&r
->in
.ads
);
1742 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1743 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1744 unsetenv(KRB5_ENV_CCNAME
);
1750 /****************************************************************
1751 ****************************************************************/
1753 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1755 const char *krb5_cc_env
= NULL
;
1758 ads_destroy(&r
->in
.ads
);
1761 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1762 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1763 unsetenv(KRB5_ENV_CCNAME
);
1769 /****************************************************************
1770 ****************************************************************/
1772 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1773 struct libnet_JoinCtx
**r
)
1775 struct libnet_JoinCtx
*ctx
;
1776 const char *krb5_cc_env
= NULL
;
1778 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1783 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1785 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1786 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1788 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1789 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1790 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1791 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1792 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1795 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1802 /****************************************************************
1803 ****************************************************************/
1805 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1806 struct libnet_UnjoinCtx
**r
)
1808 struct libnet_UnjoinCtx
*ctx
;
1809 const char *krb5_cc_env
= NULL
;
1811 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1816 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1818 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1819 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1821 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1822 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1823 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1824 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1825 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1833 /****************************************************************
1834 ****************************************************************/
1836 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1837 struct libnet_JoinCtx
*r
)
1839 bool valid_security
= false;
1840 bool valid_workgroup
= false;
1841 bool valid_realm
= false;
1843 /* check if configuration is already set correctly */
1845 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1847 switch (r
->out
.domain_is_ad
) {
1849 valid_security
= (lp_security() == SEC_DOMAIN
);
1850 if (valid_workgroup
&& valid_security
) {
1851 /* nothing to be done */
1856 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1857 switch (lp_security()) {
1860 valid_security
= true;
1863 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1864 /* nothing to be done */
1870 /* check if we are supposed to manipulate configuration */
1872 if (!r
->in
.modify_config
) {
1874 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1876 if (!valid_workgroup
) {
1877 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1878 "\"workgroup\" set to '%s', should be '%s'",
1879 lp_workgroup(), r
->out
.netbios_domain_name
);
1880 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1884 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1885 "\"realm\" set to '%s', should be '%s'",
1886 lp_realm(), r
->out
.dns_domain_name
);
1887 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1890 if (!valid_security
) {
1891 const char *sec
= NULL
;
1892 switch (lp_security()) {
1893 case SEC_SHARE
: sec
= "share"; break;
1894 case SEC_USER
: sec
= "user"; break;
1895 case SEC_DOMAIN
: sec
= "domain"; break;
1896 case SEC_ADS
: sec
= "ads"; break;
1898 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1899 "\"security\" set to '%s', should be %s",
1900 sec
, r
->out
.domain_is_ad
?
1901 "either 'domain' or 'ads'" : "'domain'");
1902 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1905 libnet_join_set_error_string(mem_ctx
, r
,
1906 "Invalid configuration (%s) and configuration modification "
1907 "was not requested", wrong_conf
);
1908 return WERR_CAN_NOT_COMPLETE
;
1911 /* check if we are able to manipulate configuration */
1913 if (!lp_config_backend_is_registry()) {
1914 libnet_join_set_error_string(mem_ctx
, r
,
1915 "Configuration manipulation requested but not "
1916 "supported by backend");
1917 return WERR_NOT_SUPPORTED
;
1923 /****************************************************************
1924 ****************************************************************/
1926 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1927 struct libnet_JoinCtx
*r
)
1931 struct cli_state
*cli
= NULL
;
1933 ADS_STATUS ads_status
;
1934 #endif /* HAVE_ADS */
1936 if (!r
->in
.dc_name
) {
1937 struct netr_DsRGetDCNameInfo
*info
;
1939 status
= dsgetdcname(mem_ctx
,
1944 DS_FORCE_REDISCOVERY
|
1945 DS_DIRECTORY_SERVICE_REQUIRED
|
1946 DS_WRITABLE_REQUIRED
|
1949 if (!NT_STATUS_IS_OK(status
)) {
1950 libnet_join_set_error_string(mem_ctx
, r
,
1951 "failed to find DC for domain %s",
1953 get_friendly_nt_error_msg(status
));
1954 return WERR_DCNOTFOUND
;
1957 dc
= strip_hostname(info
->dc_unc
);
1958 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1959 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1962 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1963 if (!NT_STATUS_IS_OK(status
)) {
1964 libnet_join_set_error_string(mem_ctx
, r
,
1965 "failed to lookup DC info for domain '%s' over rpc: %s",
1966 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1967 return ntstatus_to_werror(status
);
1970 werr
= libnet_join_check_config(mem_ctx
, r
);
1971 if (!W_ERROR_IS_OK(werr
)) {
1976 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
1977 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1979 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1980 if (!ADS_ERR_OK(ads_status
)) {
1981 return WERR_DEFAULT_JOIN_REQUIRED
;
1984 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1985 if (!ADS_ERR_OK(ads_status
)) {
1986 libnet_join_set_error_string(mem_ctx
, r
,
1987 "failed to precreate account in ou %s: %s",
1989 ads_errstr(ads_status
));
1990 return WERR_DEFAULT_JOIN_REQUIRED
;
1993 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1995 #endif /* HAVE_ADS */
1997 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
1998 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
1999 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2001 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2003 if (!NT_STATUS_IS_OK(status
)) {
2004 libnet_join_set_error_string(mem_ctx
, r
,
2005 "failed to join domain '%s' over rpc: %s",
2006 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2007 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2008 return WERR_SETUP_ALREADY_JOINED
;
2010 werr
= ntstatus_to_werror(status
);
2014 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2015 werr
= WERR_SETUP_NOT_JOINED
;
2029 /****************************************************************
2030 ****************************************************************/
2032 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2033 struct libnet_JoinCtx
*r
)
2036 struct libnet_UnjoinCtx
*u
= NULL
;
2038 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2039 if (!W_ERROR_IS_OK(werr
)) {
2043 u
->in
.debug
= r
->in
.debug
;
2044 u
->in
.dc_name
= r
->in
.dc_name
;
2045 u
->in
.domain_name
= r
->in
.domain_name
;
2046 u
->in
.admin_account
= r
->in
.admin_account
;
2047 u
->in
.admin_password
= r
->in
.admin_password
;
2048 u
->in
.modify_config
= r
->in
.modify_config
;
2049 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2050 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2052 werr
= libnet_Unjoin(mem_ctx
, u
);
2058 /****************************************************************
2059 ****************************************************************/
2061 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2062 struct libnet_JoinCtx
*r
)
2067 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2070 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2071 if (!W_ERROR_IS_OK(werr
)) {
2075 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2076 werr
= libnet_DomainJoin(mem_ctx
, r
);
2077 if (!W_ERROR_IS_OK(werr
)) {
2082 werr
= libnet_join_post_processing(mem_ctx
, r
);
2083 if (!W_ERROR_IS_OK(werr
)) {
2087 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2088 werr
= libnet_join_post_verify(mem_ctx
, r
);
2089 if (!W_ERROR_IS_OK(werr
)) {
2090 libnet_join_rollback(mem_ctx
, r
);
2095 r
->out
.result
= werr
;
2098 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2103 /****************************************************************
2104 ****************************************************************/
2106 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2107 struct libnet_UnjoinCtx
*r
)
2111 if (!r
->in
.domain_sid
) {
2113 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2114 libnet_unjoin_set_error_string(mem_ctx
, r
,
2115 "Unable to fetch domain sid: are we joined?");
2116 return WERR_SETUP_NOT_JOINED
;
2118 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2119 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2122 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2123 !r
->in
.delete_machine_account
) {
2124 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2128 if (!r
->in
.dc_name
) {
2129 struct netr_DsRGetDCNameInfo
*info
;
2131 status
= dsgetdcname(mem_ctx
,
2136 DS_DIRECTORY_SERVICE_REQUIRED
|
2137 DS_WRITABLE_REQUIRED
|
2140 if (!NT_STATUS_IS_OK(status
)) {
2141 libnet_unjoin_set_error_string(mem_ctx
, r
,
2142 "failed to find DC for domain %s",
2144 get_friendly_nt_error_msg(status
));
2145 return WERR_DCNOTFOUND
;
2148 dc
= strip_hostname(info
->dc_unc
);
2149 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2150 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2154 /* for net ads leave, try to delete the account. If it works,
2155 no sense in disabling. If it fails, we can still try to
2158 if (r
->in
.delete_machine_account
) {
2159 ADS_STATUS ads_status
;
2160 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2161 if (ADS_ERR_OK(ads_status
)) {
2163 r
->out
.dns_domain_name
=
2164 talloc_strdup(mem_ctx
,
2165 r
->in
.ads
->server
.realm
);
2167 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2169 if (!ADS_ERR_OK(ads_status
)) {
2170 libnet_unjoin_set_error_string(mem_ctx
, r
,
2171 "failed to remove machine account from AD: %s",
2172 ads_errstr(ads_status
));
2174 r
->out
.deleted_machine_account
= true;
2175 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2176 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2180 #endif /* HAVE_ADS */
2182 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2184 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2185 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2186 if (!NT_STATUS_IS_OK(status
)) {
2187 libnet_unjoin_set_error_string(mem_ctx
, r
,
2188 "failed to disable machine account via rpc: %s",
2189 get_friendly_nt_error_msg(status
));
2190 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2191 return WERR_SETUP_NOT_JOINED
;
2193 return ntstatus_to_werror(status
);
2196 r
->out
.disabled_machine_account
= true;
2199 /* If disable succeeded or was not requested at all, we
2200 should be getting rid of our end of things */
2202 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2207 /****************************************************************
2208 ****************************************************************/
2210 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2211 struct libnet_UnjoinCtx
*r
)
2213 if (!r
->in
.domain_name
) {
2214 libnet_unjoin_set_error_string(mem_ctx
, r
,
2215 "No domain name defined");
2216 return WERR_INVALID_PARAM
;
2219 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2222 libnet_unjoin_set_error_string(mem_ctx
, r
,
2223 "Failed to parse domain name");
2224 return WERR_INVALID_PARAM
;
2228 return WERR_SETUP_DOMAIN_CONTROLLER
;
2231 if (!secrets_init()) {
2232 libnet_unjoin_set_error_string(mem_ctx
, r
,
2233 "Unable to open secrets database");
2234 return WERR_CAN_NOT_COMPLETE
;
2240 /****************************************************************
2241 ****************************************************************/
2243 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2244 struct libnet_UnjoinCtx
*r
)
2246 saf_delete(r
->out
.netbios_domain_name
);
2247 saf_delete(r
->out
.dns_domain_name
);
2249 return libnet_unjoin_config(r
);
2252 /****************************************************************
2253 ****************************************************************/
2255 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2256 struct libnet_UnjoinCtx
*r
)
2261 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2264 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2265 if (!W_ERROR_IS_OK(werr
)) {
2269 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2270 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2271 if (!W_ERROR_IS_OK(werr
)) {
2272 libnet_unjoin_config(r
);
2277 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2278 if (!W_ERROR_IS_OK(werr
)) {
2283 r
->out
.result
= werr
;
2286 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);