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/cli_samr.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/cli_lsa.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/cli_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"
38 #include "../libcli/security/dom_sid.h"
40 /****************************************************************
41 ****************************************************************/
43 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
46 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
47 DEBUG(1,("libnet_Join:\n%s", str)); \
51 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
52 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
53 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
54 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
56 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
59 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
60 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
64 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
65 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
66 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
67 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
69 /****************************************************************
70 ****************************************************************/
72 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
73 struct libnet_JoinCtx
*r
,
74 const char *format
, ...)
78 if (r
->out
.error_string
) {
82 va_start(args
, format
);
83 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
87 /****************************************************************
88 ****************************************************************/
90 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
91 struct libnet_UnjoinCtx
*r
,
92 const char *format
, ...)
96 if (r
->out
.error_string
) {
100 va_start(args
, format
);
101 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
107 /****************************************************************
108 ****************************************************************/
110 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
111 const char *netbios_domain_name
,
113 const char *user_name
,
114 const char *password
,
118 ADS_STRUCT
*my_ads
= NULL
;
121 my_ads
= ads_init(dns_domain_name
,
125 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
129 SAFE_FREE(my_ads
->auth
.user_name
);
130 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
131 if ((cp
= strchr_m(my_ads
->auth
.user_name
, '@'))!=0) {
133 SAFE_FREE(my_ads
->auth
.realm
);
134 my_ads
->auth
.realm
= smb_xstrdup(cp
);
135 strupper_m(my_ads
->auth
.realm
);
140 SAFE_FREE(my_ads
->auth
.password
);
141 my_ads
->auth
.password
= SMB_STRDUP(password
);
144 status
= ads_connect_user_creds(my_ads
);
145 if (!ADS_ERR_OK(status
)) {
146 ads_destroy(&my_ads
);
154 /****************************************************************
155 ****************************************************************/
157 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
158 struct libnet_JoinCtx
*r
)
162 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
163 r
->out
.netbios_domain_name
,
166 r
->in
.admin_password
,
168 if (!ADS_ERR_OK(status
)) {
169 libnet_join_set_error_string(mem_ctx
, r
,
170 "failed to connect to AD: %s",
175 if (!r
->out
.netbios_domain_name
) {
176 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
177 r
->in
.ads
->server
.workgroup
);
178 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
181 if (!r
->out
.dns_domain_name
) {
182 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
183 r
->in
.ads
->config
.realm
);
184 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
187 r
->out
.domain_is_ad
= true;
192 /****************************************************************
193 ****************************************************************/
195 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
196 struct libnet_UnjoinCtx
*r
)
200 status
= libnet_connect_ads(r
->in
.domain_name
,
204 r
->in
.admin_password
,
206 if (!ADS_ERR_OK(status
)) {
207 libnet_unjoin_set_error_string(mem_ctx
, r
,
208 "failed to connect to AD: %s",
215 /****************************************************************
216 join a domain using ADS (LDAP mods)
217 ****************************************************************/
219 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
220 struct libnet_JoinCtx
*r
)
223 LDAPMessage
*res
= NULL
;
224 const char *attrs
[] = { "dn", NULL
};
227 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
228 if (!ADS_ERR_OK(status
)) {
232 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
233 if (!ADS_ERR_OK(status
)) {
237 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
238 ads_msgfree(r
->in
.ads
, res
);
239 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
242 ads_msgfree(r
->in
.ads
, res
);
244 /* Attempt to create the machine account and bail if this fails.
245 Assume that the admin wants exactly what they requested */
247 status
= ads_create_machine_acct(r
->in
.ads
,
251 if (ADS_ERR_OK(status
)) {
252 DEBUG(1,("machine account creation created\n"));
254 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
255 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
256 status
= ADS_SUCCESS
;
259 if (!ADS_ERR_OK(status
)) {
260 DEBUG(1,("machine account creation failed\n"));
264 status
= ads_move_machine_acct(r
->in
.ads
,
268 if (!ADS_ERR_OK(status
)) {
269 DEBUG(1,("failure to locate/move pre-existing "
270 "machine account\n"));
274 DEBUG(1,("The machine account %s the specified OU.\n",
275 moved
? "was moved into" : "already exists in"));
280 /****************************************************************
281 ****************************************************************/
283 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
284 struct libnet_UnjoinCtx
*r
)
289 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
290 if (!ADS_ERR_OK(status
)) {
291 libnet_unjoin_set_error_string(mem_ctx
, r
,
292 "failed to connect to AD: %s",
298 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
299 if (!ADS_ERR_OK(status
)) {
300 libnet_unjoin_set_error_string(mem_ctx
, r
,
301 "failed to leave realm: %s",
309 /****************************************************************
310 ****************************************************************/
312 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
313 struct libnet_JoinCtx
*r
)
316 LDAPMessage
*res
= NULL
;
319 if (!r
->in
.machine_name
) {
320 return ADS_ERROR(LDAP_NO_MEMORY
);
323 status
= ads_find_machine_acct(r
->in
.ads
,
326 if (!ADS_ERR_OK(status
)) {
330 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
331 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
335 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
337 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
341 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
343 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
348 ads_msgfree(r
->in
.ads
, res
);
354 /****************************************************************
355 Set a machines dNSHostName and servicePrincipalName attributes
356 ****************************************************************/
358 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
359 struct libnet_JoinCtx
*r
)
364 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
369 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
370 if (!ADS_ERR_OK(status
)) {
374 /* Windows only creates HOST/shortname & HOST/fqdn. */
376 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
378 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
383 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
384 || (strchr(my_fqdn
, '.') == NULL
)) {
385 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
386 r
->out
.dns_domain_name
);
391 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
392 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
394 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
399 mods
= ads_init_mods(mem_ctx
);
401 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
404 /* fields of primary importance */
406 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
407 if (!ADS_ERR_OK(status
)) {
408 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
411 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
413 if (!ADS_ERR_OK(status
)) {
414 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
417 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
420 /****************************************************************
421 ****************************************************************/
423 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
424 struct libnet_JoinCtx
*r
)
429 if (!r
->in
.create_upn
) {
435 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
436 if (!ADS_ERR_OK(status
)) {
441 r
->in
.upn
= talloc_asprintf(mem_ctx
,
444 r
->out
.dns_domain_name
);
446 return ADS_ERROR(LDAP_NO_MEMORY
);
450 /* now do the mods */
452 mods
= ads_init_mods(mem_ctx
);
454 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
457 /* fields of primary importance */
459 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
460 if (!ADS_ERR_OK(status
)) {
461 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
464 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
468 /****************************************************************
469 ****************************************************************/
471 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
472 struct libnet_JoinCtx
*r
)
478 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
484 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
485 if (!ADS_ERR_OK(status
)) {
489 /* now do the mods */
491 mods
= ads_init_mods(mem_ctx
);
493 return ADS_ERROR(LDAP_NO_MEMORY
);
496 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
498 return ADS_ERROR(LDAP_NO_MEMORY
);
501 /* fields of primary importance */
503 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
505 if (!ADS_ERR_OK(status
)) {
509 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
511 if (!ADS_ERR_OK(status
)) {
515 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
517 if (!ADS_ERR_OK(status
)) {
521 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
524 /****************************************************************
525 ****************************************************************/
527 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
528 struct libnet_JoinCtx
*r
)
530 if (!USE_SYSTEM_KEYTAB
) {
534 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
541 /****************************************************************
542 ****************************************************************/
544 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
545 struct libnet_JoinCtx
*r
)
547 uint32_t domain_func
;
549 const char *salt
= NULL
;
550 char *std_salt
= NULL
;
552 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
553 if (!ADS_ERR_OK(status
)) {
554 libnet_join_set_error_string(mem_ctx
, r
,
555 "failed to determine domain functional level: %s",
560 /* go ahead and setup the default salt */
562 std_salt
= kerberos_standard_des_salt();
564 libnet_join_set_error_string(mem_ctx
, r
,
565 "failed to obtain standard DES salt");
569 salt
= talloc_strdup(mem_ctx
, std_salt
);
576 /* if it's a Windows functional domain, we have to look for the UPN */
578 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
581 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
584 salt
= talloc_strdup(mem_ctx
, upn
);
591 return kerberos_secrets_store_des_salt(salt
);
594 /****************************************************************
595 ****************************************************************/
597 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
598 struct libnet_JoinCtx
*r
)
603 status
= libnet_join_connect_ads(mem_ctx
, r
);
604 if (!ADS_ERR_OK(status
)) {
609 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
610 if (!ADS_ERR_OK(status
)) {
611 libnet_join_set_error_string(mem_ctx
, r
,
612 "failed to set machine spn: %s",
617 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
618 if (!ADS_ERR_OK(status
)) {
619 libnet_join_set_error_string(mem_ctx
, r
,
620 "failed to set machine os attributes: %s",
625 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
626 if (!ADS_ERR_OK(status
)) {
627 libnet_join_set_error_string(mem_ctx
, r
,
628 "failed to set machine upn: %s",
633 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
634 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
637 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
638 libnet_join_set_error_string(mem_ctx
, r
,
639 "failed to create kerberos keytab");
640 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
645 #endif /* WITH_ADS */
647 /****************************************************************
648 Store the machine password and domain SID
649 ****************************************************************/
651 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
652 struct libnet_JoinCtx
*r
)
654 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
657 DEBUG(1,("Failed to save domain sid\n"));
661 if (!secrets_store_machine_password(r
->in
.machine_password
,
662 r
->out
.netbios_domain_name
,
663 r
->in
.secure_channel_type
))
665 DEBUG(1,("Failed to save machine password\n"));
672 /****************************************************************
673 Connect dc's IPC$ share
674 ****************************************************************/
676 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
680 struct cli_state
**cli
)
685 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
688 if (use_kerberos
&& pass
) {
689 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
692 return cli_full_connection(cli
, NULL
,
703 /****************************************************************
704 Lookup domain dc's info
705 ****************************************************************/
707 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
708 struct libnet_JoinCtx
*r
,
709 struct cli_state
**cli
)
711 struct rpc_pipe_client
*pipe_hnd
= NULL
;
712 struct policy_handle lsa_pol
;
713 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
714 union lsa_PolicyInformation
*info
= NULL
;
716 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
718 r
->in
.admin_password
,
721 if (!NT_STATUS_IS_OK(status
)) {
725 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
727 if (!NT_STATUS_IS_OK(status
)) {
728 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
733 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
734 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
735 if (!NT_STATUS_IS_OK(status
)) {
739 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
743 if (NT_STATUS_IS_OK(status
)) {
744 r
->out
.domain_is_ad
= true;
745 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
746 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
747 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
748 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
749 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
752 if (!NT_STATUS_IS_OK(status
)) {
753 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
755 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
757 if (!NT_STATUS_IS_OK(status
)) {
761 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
762 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
763 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
766 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
767 TALLOC_FREE(pipe_hnd
);
773 /****************************************************************
774 Do the domain join unsecure
775 ****************************************************************/
777 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
778 struct libnet_JoinCtx
*r
,
779 struct cli_state
*cli
)
781 struct rpc_pipe_client
*pipe_hnd
= NULL
;
782 unsigned char orig_trust_passwd_hash
[16];
783 unsigned char new_trust_passwd_hash
[16];
784 fstring trust_passwd
;
787 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
789 if (!NT_STATUS_IS_OK(status
)) {
793 if (!r
->in
.machine_password
) {
794 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
795 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
798 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
800 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
801 fstrcpy(trust_passwd
, r
->in
.admin_password
);
802 strlower_m(trust_passwd
);
805 * Machine names can be 15 characters, but the max length on
806 * a password is 14. --jerry
809 trust_passwd
[14] = '\0';
811 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
813 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
815 orig_trust_passwd_hash
,
816 r
->in
.machine_password
,
817 new_trust_passwd_hash
,
818 r
->in
.secure_channel_type
);
823 /****************************************************************
825 ****************************************************************/
827 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
828 struct libnet_JoinCtx
*r
,
829 struct cli_state
*cli
)
831 struct rpc_pipe_client
*pipe_hnd
= NULL
;
832 struct policy_handle sam_pol
, domain_pol
, user_pol
;
833 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
835 struct lsa_String lsa_acct_name
;
837 uint32_t acct_flags
= ACB_WSTRUST
;
838 struct samr_Ids user_rids
;
839 struct samr_Ids name_types
;
840 union samr_UserInfo user_info
;
842 struct samr_CryptPassword crypt_pwd
;
843 struct samr_CryptPasswordEx crypt_pwd_ex
;
845 ZERO_STRUCT(sam_pol
);
846 ZERO_STRUCT(domain_pol
);
847 ZERO_STRUCT(user_pol
);
849 switch (r
->in
.secure_channel_type
) {
851 acct_flags
= ACB_WSTRUST
;
854 acct_flags
= ACB_SVRTRUST
;
857 return NT_STATUS_INVALID_PARAMETER
;
860 if (!r
->in
.machine_password
) {
861 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
862 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
865 /* Open the domain */
867 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
869 if (!NT_STATUS_IS_OK(status
)) {
870 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
875 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
877 SAMR_ACCESS_ENUM_DOMAINS
878 | SAMR_ACCESS_LOOKUP_DOMAIN
,
880 if (!NT_STATUS_IS_OK(status
)) {
884 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
886 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
887 | SAMR_DOMAIN_ACCESS_CREATE_USER
888 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
891 if (!NT_STATUS_IS_OK(status
)) {
895 /* Create domain user */
897 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
898 strlower_m(acct_name
);
900 init_lsa_String(&lsa_acct_name
, acct_name
);
902 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
903 uint32_t access_desired
=
904 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
905 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
906 SAMR_USER_ACCESS_SET_PASSWORD
|
907 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
908 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
909 uint32_t access_granted
= 0;
911 DEBUG(10,("Creating account with desired access mask: %d\n",
914 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
922 if (!NT_STATUS_IS_OK(status
) &&
923 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
925 DEBUG(10,("Creation of workstation account failed: %s\n",
928 /* If NT_STATUS_ACCESS_DENIED then we have a valid
929 username/password combo but the user does not have
930 administrator access. */
932 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
933 libnet_join_set_error_string(mem_ctx
, r
,
934 "User specified does not have "
935 "administrator privileges");
941 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
942 if (!(r
->in
.join_flags
&
943 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
948 /* We *must* do this.... don't ask... */
950 if (NT_STATUS_IS_OK(status
)) {
951 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
955 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
961 if (!NT_STATUS_IS_OK(status
)) {
965 if (name_types
.ids
[0] != SID_NAME_USER
) {
966 DEBUG(0,("%s is not a user account (type=%d)\n",
967 acct_name
, name_types
.ids
[0]));
968 status
= NT_STATUS_INVALID_WORKSTATION
;
972 user_rid
= user_rids
.ids
[0];
974 /* Open handle on user */
976 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
978 SEC_FLAG_MAXIMUM_ALLOWED
,
981 if (!NT_STATUS_IS_OK(status
)) {
985 /* Fill in the additional account flags now */
987 acct_flags
|= ACB_PWNOEXP
;
989 /* Set account flags on machine account */
990 ZERO_STRUCT(user_info
.info16
);
991 user_info
.info16
.acct_flags
= acct_flags
;
993 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
998 if (!NT_STATUS_IS_OK(status
)) {
1000 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
1003 libnet_join_set_error_string(mem_ctx
, r
,
1004 "Failed to set account flags for machine account (%s)\n",
1009 /* Set password on machine account - first try level 26 */
1011 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1012 &cli
->user_session_key
,
1015 user_info
.info26
.password
= crypt_pwd_ex
;
1016 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1018 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1023 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
1025 /* retry with level 24 */
1027 init_samr_CryptPassword(r
->in
.machine_password
,
1028 &cli
->user_session_key
,
1031 user_info
.info24
.password
= crypt_pwd
;
1032 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1034 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1040 if (!NT_STATUS_IS_OK(status
)) {
1042 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
1045 libnet_join_set_error_string(mem_ctx
, r
,
1046 "Failed to set password for machine account (%s)\n",
1051 status
= NT_STATUS_OK
;
1058 if (is_valid_policy_hnd(&sam_pol
)) {
1059 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1061 if (is_valid_policy_hnd(&domain_pol
)) {
1062 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1064 if (is_valid_policy_hnd(&user_pol
)) {
1065 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1067 TALLOC_FREE(pipe_hnd
);
1072 /****************************************************************
1073 ****************************************************************/
1075 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1076 const char *machine_name
,
1077 const char *dc_name
)
1079 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1080 struct cli_state
*cli
= NULL
;
1081 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1082 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1084 char *machine_password
= NULL
;
1085 char *machine_account
= NULL
;
1088 return NT_STATUS_INVALID_PARAMETER
;
1091 if (!secrets_init()) {
1092 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1095 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1097 if (!machine_password
) {
1098 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1101 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1102 SAFE_FREE(machine_password
);
1103 return NT_STATUS_NO_MEMORY
;
1106 status
= cli_full_connection(&cli
, NULL
,
1115 free(machine_account
);
1116 free(machine_password
);
1118 if (!NT_STATUS_IS_OK(status
)) {
1119 status
= cli_full_connection(&cli
, NULL
,
1130 if (!NT_STATUS_IS_OK(status
)) {
1134 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1135 &neg_flags
, &netlogon_pipe
);
1136 if (!NT_STATUS_IS_OK(status
)) {
1137 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1139 return NT_STATUS_OK
;
1142 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1143 "key from server %s for domain %s. Error was %s\n",
1144 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1149 if (!lp_client_schannel()) {
1151 return NT_STATUS_OK
;
1154 status
= cli_rpc_pipe_open_schannel_with_key(
1155 cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
1156 DCERPC_AUTH_LEVEL_PRIVACY
,
1157 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1161 if (!NT_STATUS_IS_OK(status
)) {
1162 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1163 "on netlogon pipe to server %s for domain %s. "
1165 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1169 return NT_STATUS_OK
;
1172 /****************************************************************
1173 ****************************************************************/
1175 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1176 struct libnet_JoinCtx
*r
)
1180 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1183 if (!NT_STATUS_IS_OK(status
)) {
1184 libnet_join_set_error_string(mem_ctx
, r
,
1185 "failed to verify domain membership after joining: %s",
1186 get_friendly_nt_error_msg(status
));
1187 return WERR_SETUP_NOT_JOINED
;
1193 /****************************************************************
1194 ****************************************************************/
1196 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1197 struct libnet_UnjoinCtx
*r
)
1199 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1203 if (!secrets_delete_domain_sid(lp_workgroup())) {
1210 /****************************************************************
1211 ****************************************************************/
1213 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1214 struct libnet_UnjoinCtx
*r
)
1216 struct cli_state
*cli
= NULL
;
1217 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1218 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1219 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1222 struct lsa_String lsa_acct_name
;
1223 struct samr_Ids user_rids
;
1224 struct samr_Ids name_types
;
1225 union samr_UserInfo
*info
= NULL
;
1227 ZERO_STRUCT(sam_pol
);
1228 ZERO_STRUCT(domain_pol
);
1229 ZERO_STRUCT(user_pol
);
1231 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1232 r
->in
.admin_account
,
1233 r
->in
.admin_password
,
1236 if (!NT_STATUS_IS_OK(status
)) {
1240 /* Open the domain */
1242 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1244 if (!NT_STATUS_IS_OK(status
)) {
1245 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1246 nt_errstr(status
)));
1250 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1252 SEC_FLAG_MAXIMUM_ALLOWED
,
1254 if (!NT_STATUS_IS_OK(status
)) {
1258 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1260 SEC_FLAG_MAXIMUM_ALLOWED
,
1263 if (!NT_STATUS_IS_OK(status
)) {
1267 /* Create domain user */
1269 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1270 strlower_m(acct_name
);
1272 init_lsa_String(&lsa_acct_name
, acct_name
);
1274 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1281 if (!NT_STATUS_IS_OK(status
)) {
1285 if (name_types
.ids
[0] != SID_NAME_USER
) {
1286 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1287 name_types
.ids
[0]));
1288 status
= NT_STATUS_INVALID_WORKSTATION
;
1292 user_rid
= user_rids
.ids
[0];
1294 /* Open handle on user */
1296 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1298 SEC_FLAG_MAXIMUM_ALLOWED
,
1301 if (!NT_STATUS_IS_OK(status
)) {
1307 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1311 if (!NT_STATUS_IS_OK(status
)) {
1312 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1316 /* now disable and setuser info */
1318 info
->info16
.acct_flags
|= ACB_DISABLED
;
1320 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1325 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1329 if (is_valid_policy_hnd(&domain_pol
)) {
1330 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1332 if (is_valid_policy_hnd(&sam_pol
)) {
1333 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1335 TALLOC_FREE(pipe_hnd
);
1345 /****************************************************************
1346 ****************************************************************/
1348 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1351 struct smbconf_ctx
*ctx
;
1353 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1354 if (!W_ERROR_IS_OK(werr
)) {
1358 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1360 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1361 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1363 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1366 smbconf_delete_global_parameter(ctx
, "realm");
1370 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1371 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1373 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1374 r
->out
.netbios_domain_name
);
1375 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1377 if (r
->out
.domain_is_ad
) {
1378 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1379 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1381 werr
= smbconf_set_global_parameter(ctx
, "realm",
1382 r
->out
.dns_domain_name
);
1383 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1387 smbconf_shutdown(ctx
);
1391 /****************************************************************
1392 ****************************************************************/
1394 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1396 WERROR werr
= WERR_OK
;
1397 struct smbconf_ctx
*ctx
;
1399 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1400 if (!W_ERROR_IS_OK(werr
)) {
1404 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1406 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1407 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1409 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1410 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1412 smbconf_delete_global_parameter(ctx
, "realm");
1416 smbconf_shutdown(ctx
);
1420 /****************************************************************
1421 ****************************************************************/
1423 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1427 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1428 return r
->out
.result
;
1431 if (!r
->in
.modify_config
) {
1435 werr
= do_join_modify_vals_config(r
);
1436 if (!W_ERROR_IS_OK(werr
)) {
1440 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1442 r
->out
.modified_config
= true;
1443 r
->out
.result
= werr
;
1448 /****************************************************************
1449 ****************************************************************/
1451 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1455 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1456 return r
->out
.result
;
1459 if (!r
->in
.modify_config
) {
1463 werr
= do_unjoin_modify_vals_config(r
);
1464 if (!W_ERROR_IS_OK(werr
)) {
1468 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1470 r
->out
.modified_config
= true;
1471 r
->out
.result
= werr
;
1476 /****************************************************************
1477 ****************************************************************/
1479 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1480 const char *domain_str
,
1481 const char **domain_p
,
1484 char *domain
= NULL
;
1486 const char *p
= NULL
;
1488 if (!domain_str
|| !domain_p
|| !dc_p
) {
1492 p
= strchr_m(domain_str
, '\\');
1495 domain
= talloc_strndup(mem_ctx
, domain_str
,
1496 PTR_DIFF(p
, domain_str
));
1497 dc
= talloc_strdup(mem_ctx
, p
+1);
1502 domain
= talloc_strdup(mem_ctx
, domain_str
);
1518 /****************************************************************
1519 ****************************************************************/
1521 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1522 struct libnet_JoinCtx
*r
)
1524 if (!r
->in
.domain_name
) {
1525 libnet_join_set_error_string(mem_ctx
, r
,
1526 "No domain name defined");
1527 return WERR_INVALID_PARAM
;
1530 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1533 libnet_join_set_error_string(mem_ctx
, r
,
1534 "Failed to parse domain name");
1535 return WERR_INVALID_PARAM
;
1539 return WERR_SETUP_DOMAIN_CONTROLLER
;
1542 if (!secrets_init()) {
1543 libnet_join_set_error_string(mem_ctx
, r
,
1544 "Unable to open secrets database");
1545 return WERR_CAN_NOT_COMPLETE
;
1551 /****************************************************************
1552 ****************************************************************/
1554 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1558 /* Try adding dom admins to builtin\admins. Only log failures. */
1559 status
= create_builtin_administrators(domain_sid
);
1560 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1561 DEBUG(10,("Unable to auto-add domain administrators to "
1562 "BUILTIN\\Administrators during join because "
1563 "winbindd must be running."));
1564 } else if (!NT_STATUS_IS_OK(status
)) {
1565 DEBUG(5, ("Failed to auto-add domain administrators to "
1566 "BUILTIN\\Administrators during join: %s\n",
1567 nt_errstr(status
)));
1570 /* Try adding dom users to builtin\users. Only log failures. */
1571 status
= create_builtin_users(domain_sid
);
1572 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1573 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1574 "during join because winbindd must be running."));
1575 } else if (!NT_STATUS_IS_OK(status
)) {
1576 DEBUG(5, ("Failed to auto-add domain administrators to "
1577 "BUILTIN\\Administrators during join: %s\n",
1578 nt_errstr(status
)));
1582 /****************************************************************
1583 ****************************************************************/
1585 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1586 struct libnet_JoinCtx
*r
)
1590 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1591 return r
->out
.result
;
1594 werr
= do_JoinConfig(r
);
1595 if (!W_ERROR_IS_OK(werr
)) {
1599 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1603 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1604 if (r
->out
.dns_domain_name
) {
1605 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1609 if (r
->out
.domain_is_ad
&&
1610 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1611 ADS_STATUS ads_status
;
1613 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1614 if (!ADS_ERR_OK(ads_status
)) {
1615 return WERR_GENERAL_FAILURE
;
1618 #endif /* WITH_ADS */
1620 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1625 /****************************************************************
1626 ****************************************************************/
1628 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1630 const char *krb5_cc_env
= NULL
;
1633 ads_destroy(&r
->in
.ads
);
1636 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1637 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1638 unsetenv(KRB5_ENV_CCNAME
);
1644 /****************************************************************
1645 ****************************************************************/
1647 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1649 const char *krb5_cc_env
= NULL
;
1652 ads_destroy(&r
->in
.ads
);
1655 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1656 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1657 unsetenv(KRB5_ENV_CCNAME
);
1663 /****************************************************************
1664 ****************************************************************/
1666 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1667 struct libnet_JoinCtx
**r
)
1669 struct libnet_JoinCtx
*ctx
;
1670 const char *krb5_cc_env
= NULL
;
1672 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1677 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1679 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1680 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1682 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1683 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1684 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1685 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1686 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1689 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1696 /****************************************************************
1697 ****************************************************************/
1699 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1700 struct libnet_UnjoinCtx
**r
)
1702 struct libnet_UnjoinCtx
*ctx
;
1703 const char *krb5_cc_env
= NULL
;
1705 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1710 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1712 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1713 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1715 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1716 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1717 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1718 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1719 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1727 /****************************************************************
1728 ****************************************************************/
1730 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1731 struct libnet_JoinCtx
*r
)
1733 bool valid_security
= false;
1734 bool valid_workgroup
= false;
1735 bool valid_realm
= false;
1737 /* check if configuration is already set correctly */
1739 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1741 switch (r
->out
.domain_is_ad
) {
1743 valid_security
= (lp_security() == SEC_DOMAIN
);
1744 if (valid_workgroup
&& valid_security
) {
1745 /* nothing to be done */
1750 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1751 switch (lp_security()) {
1754 valid_security
= true;
1757 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1758 /* nothing to be done */
1764 /* check if we are supposed to manipulate configuration */
1766 if (!r
->in
.modify_config
) {
1768 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1770 if (!valid_workgroup
) {
1771 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1772 "\"workgroup\" set to '%s', should be '%s'",
1773 lp_workgroup(), r
->out
.netbios_domain_name
);
1774 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1778 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1779 "\"realm\" set to '%s', should be '%s'",
1780 lp_realm(), r
->out
.dns_domain_name
);
1781 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1784 if (!valid_security
) {
1785 const char *sec
= NULL
;
1786 switch (lp_security()) {
1787 case SEC_SHARE
: sec
= "share"; break;
1788 case SEC_USER
: sec
= "user"; break;
1789 case SEC_DOMAIN
: sec
= "domain"; break;
1790 case SEC_ADS
: sec
= "ads"; break;
1792 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1793 "\"security\" set to '%s', should be %s",
1794 sec
, r
->out
.domain_is_ad
?
1795 "either 'domain' or 'ads'" : "'domain'");
1796 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1799 libnet_join_set_error_string(mem_ctx
, r
,
1800 "Invalid configuration (%s) and configuration modification "
1801 "was not requested", wrong_conf
);
1802 return WERR_CAN_NOT_COMPLETE
;
1805 /* check if we are able to manipulate configuration */
1807 if (!lp_config_backend_is_registry()) {
1808 libnet_join_set_error_string(mem_ctx
, r
,
1809 "Configuration manipulation requested but not "
1810 "supported by backend");
1811 return WERR_NOT_SUPPORTED
;
1817 /****************************************************************
1818 ****************************************************************/
1820 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1821 struct libnet_JoinCtx
*r
)
1825 struct cli_state
*cli
= NULL
;
1827 ADS_STATUS ads_status
;
1828 #endif /* WITH_ADS */
1830 if (!r
->in
.dc_name
) {
1831 struct netr_DsRGetDCNameInfo
*info
;
1833 status
= dsgetdcname(mem_ctx
,
1838 DS_FORCE_REDISCOVERY
|
1839 DS_DIRECTORY_SERVICE_REQUIRED
|
1840 DS_WRITABLE_REQUIRED
|
1843 if (!NT_STATUS_IS_OK(status
)) {
1844 libnet_join_set_error_string(mem_ctx
, r
,
1845 "failed to find DC for domain %s",
1847 get_friendly_nt_error_msg(status
));
1848 return WERR_DCNOTFOUND
;
1851 dc
= strip_hostname(info
->dc_unc
);
1852 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1853 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1856 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1857 if (!NT_STATUS_IS_OK(status
)) {
1858 libnet_join_set_error_string(mem_ctx
, r
,
1859 "failed to lookup DC info for domain '%s' over rpc: %s",
1860 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1861 return ntstatus_to_werror(status
);
1864 werr
= libnet_join_check_config(mem_ctx
, r
);
1865 if (!W_ERROR_IS_OK(werr
)) {
1870 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
1871 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1873 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1874 if (!ADS_ERR_OK(ads_status
)) {
1875 return WERR_DEFAULT_JOIN_REQUIRED
;
1878 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1879 if (!ADS_ERR_OK(ads_status
)) {
1880 libnet_join_set_error_string(mem_ctx
, r
,
1881 "failed to precreate account in ou %s: %s",
1883 ads_errstr(ads_status
));
1884 return WERR_DEFAULT_JOIN_REQUIRED
;
1887 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1889 #endif /* WITH_ADS */
1891 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
1892 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
1893 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
1895 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1897 if (!NT_STATUS_IS_OK(status
)) {
1898 libnet_join_set_error_string(mem_ctx
, r
,
1899 "failed to join domain '%s' over rpc: %s",
1900 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1901 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1902 return WERR_SETUP_ALREADY_JOINED
;
1904 werr
= ntstatus_to_werror(status
);
1908 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1909 werr
= WERR_SETUP_NOT_JOINED
;
1923 /****************************************************************
1924 ****************************************************************/
1926 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
1927 struct libnet_JoinCtx
*r
)
1930 struct libnet_UnjoinCtx
*u
= NULL
;
1932 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
1933 if (!W_ERROR_IS_OK(werr
)) {
1937 u
->in
.debug
= r
->in
.debug
;
1938 u
->in
.dc_name
= r
->in
.dc_name
;
1939 u
->in
.domain_name
= r
->in
.domain_name
;
1940 u
->in
.admin_account
= r
->in
.admin_account
;
1941 u
->in
.admin_password
= r
->in
.admin_password
;
1942 u
->in
.modify_config
= r
->in
.modify_config
;
1943 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1944 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1946 werr
= libnet_Unjoin(mem_ctx
, u
);
1952 /****************************************************************
1953 ****************************************************************/
1955 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1956 struct libnet_JoinCtx
*r
)
1961 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1964 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1965 if (!W_ERROR_IS_OK(werr
)) {
1969 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1970 werr
= libnet_DomainJoin(mem_ctx
, r
);
1971 if (!W_ERROR_IS_OK(werr
)) {
1976 werr
= libnet_join_post_processing(mem_ctx
, r
);
1977 if (!W_ERROR_IS_OK(werr
)) {
1981 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1982 werr
= libnet_join_post_verify(mem_ctx
, r
);
1983 if (!W_ERROR_IS_OK(werr
)) {
1984 libnet_join_rollback(mem_ctx
, r
);
1989 r
->out
.result
= werr
;
1992 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1997 /****************************************************************
1998 ****************************************************************/
2000 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2001 struct libnet_UnjoinCtx
*r
)
2005 if (!r
->in
.domain_sid
) {
2007 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2008 libnet_unjoin_set_error_string(mem_ctx
, r
,
2009 "Unable to fetch domain sid: are we joined?");
2010 return WERR_SETUP_NOT_JOINED
;
2012 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2013 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2016 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2017 !r
->in
.delete_machine_account
) {
2018 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2022 if (!r
->in
.dc_name
) {
2023 struct netr_DsRGetDCNameInfo
*info
;
2025 status
= dsgetdcname(mem_ctx
,
2030 DS_DIRECTORY_SERVICE_REQUIRED
|
2031 DS_WRITABLE_REQUIRED
|
2034 if (!NT_STATUS_IS_OK(status
)) {
2035 libnet_unjoin_set_error_string(mem_ctx
, r
,
2036 "failed to find DC for domain %s",
2038 get_friendly_nt_error_msg(status
));
2039 return WERR_DCNOTFOUND
;
2042 dc
= strip_hostname(info
->dc_unc
);
2043 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2044 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2048 /* for net ads leave, try to delete the account. If it works,
2049 no sense in disabling. If it fails, we can still try to
2052 if (r
->in
.delete_machine_account
) {
2053 ADS_STATUS ads_status
;
2054 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2055 if (ADS_ERR_OK(ads_status
)) {
2057 r
->out
.dns_domain_name
=
2058 talloc_strdup(mem_ctx
,
2059 r
->in
.ads
->server
.realm
);
2061 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2063 if (!ADS_ERR_OK(ads_status
)) {
2064 libnet_unjoin_set_error_string(mem_ctx
, r
,
2065 "failed to remove machine account from AD: %s",
2066 ads_errstr(ads_status
));
2068 r
->out
.deleted_machine_account
= true;
2069 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2070 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2074 #endif /* WITH_ADS */
2076 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2078 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2079 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2080 if (!NT_STATUS_IS_OK(status
)) {
2081 libnet_unjoin_set_error_string(mem_ctx
, r
,
2082 "failed to disable machine account via rpc: %s",
2083 get_friendly_nt_error_msg(status
));
2084 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2085 return WERR_SETUP_NOT_JOINED
;
2087 return ntstatus_to_werror(status
);
2090 r
->out
.disabled_machine_account
= true;
2093 /* If disable succeeded or was not requested at all, we
2094 should be getting rid of our end of things */
2096 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2101 /****************************************************************
2102 ****************************************************************/
2104 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2105 struct libnet_UnjoinCtx
*r
)
2107 if (!r
->in
.domain_name
) {
2108 libnet_unjoin_set_error_string(mem_ctx
, r
,
2109 "No domain name defined");
2110 return WERR_INVALID_PARAM
;
2113 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2116 libnet_unjoin_set_error_string(mem_ctx
, r
,
2117 "Failed to parse domain name");
2118 return WERR_INVALID_PARAM
;
2122 return WERR_SETUP_DOMAIN_CONTROLLER
;
2125 if (!secrets_init()) {
2126 libnet_unjoin_set_error_string(mem_ctx
, r
,
2127 "Unable to open secrets database");
2128 return WERR_CAN_NOT_COMPLETE
;
2134 /****************************************************************
2135 ****************************************************************/
2137 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2138 struct libnet_UnjoinCtx
*r
)
2140 saf_delete(r
->out
.netbios_domain_name
);
2141 saf_delete(r
->out
.dns_domain_name
);
2143 return libnet_unjoin_config(r
);
2146 /****************************************************************
2147 ****************************************************************/
2149 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2150 struct libnet_UnjoinCtx
*r
)
2155 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2158 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2159 if (!W_ERROR_IS_OK(werr
)) {
2163 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2164 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2165 if (!W_ERROR_IS_OK(werr
)) {
2166 libnet_unjoin_config(r
);
2171 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2172 if (!W_ERROR_IS_OK(werr
)) {
2177 r
->out
.result
= werr
;
2180 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);