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/>.
22 #include "libnet/libnet.h"
23 #include "libcli/auth/libcli_auth.h"
24 #include "../librpc/gen_ndr/cli_samr.h"
25 #include "../librpc/gen_ndr/cli_lsa.h"
26 #include "../librpc/gen_ndr/cli_netlogon.h"
28 /****************************************************************
29 ****************************************************************/
31 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
34 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
35 DEBUG(1,("libnet_Join:\n%s", str)); \
39 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
40 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
41 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
42 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
44 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
47 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
48 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
52 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
53 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
54 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
55 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
57 /****************************************************************
58 ****************************************************************/
60 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
61 struct libnet_JoinCtx
*r
,
62 const char *format
, ...)
66 if (r
->out
.error_string
) {
70 va_start(args
, format
);
71 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
75 /****************************************************************
76 ****************************************************************/
78 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
79 struct libnet_UnjoinCtx
*r
,
80 const char *format
, ...)
84 if (r
->out
.error_string
) {
88 va_start(args
, format
);
89 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
95 /****************************************************************
96 ****************************************************************/
98 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
99 const char *netbios_domain_name
,
101 const char *user_name
,
102 const char *password
,
106 ADS_STRUCT
*my_ads
= NULL
;
108 my_ads
= ads_init(dns_domain_name
,
112 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
116 SAFE_FREE(my_ads
->auth
.user_name
);
117 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
121 SAFE_FREE(my_ads
->auth
.password
);
122 my_ads
->auth
.password
= SMB_STRDUP(password
);
125 status
= ads_connect_user_creds(my_ads
);
126 if (!ADS_ERR_OK(status
)) {
127 ads_destroy(&my_ads
);
135 /****************************************************************
136 ****************************************************************/
138 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
139 struct libnet_JoinCtx
*r
)
143 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
144 r
->out
.netbios_domain_name
,
147 r
->in
.admin_password
,
149 if (!ADS_ERR_OK(status
)) {
150 libnet_join_set_error_string(mem_ctx
, r
,
151 "failed to connect to AD: %s",
156 if (!r
->out
.netbios_domain_name
) {
157 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
158 r
->in
.ads
->server
.workgroup
);
159 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
162 if (!r
->out
.dns_domain_name
) {
163 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
164 r
->in
.ads
->config
.realm
);
165 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
168 r
->out
.domain_is_ad
= true;
173 /****************************************************************
174 ****************************************************************/
176 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
177 struct libnet_UnjoinCtx
*r
)
181 status
= libnet_connect_ads(r
->in
.domain_name
,
185 r
->in
.admin_password
,
187 if (!ADS_ERR_OK(status
)) {
188 libnet_unjoin_set_error_string(mem_ctx
, r
,
189 "failed to connect to AD: %s",
196 /****************************************************************
197 join a domain using ADS (LDAP mods)
198 ****************************************************************/
200 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
201 struct libnet_JoinCtx
*r
)
204 LDAPMessage
*res
= NULL
;
205 const char *attrs
[] = { "dn", NULL
};
208 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
209 if (!ADS_ERR_OK(status
)) {
213 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
214 if (!ADS_ERR_OK(status
)) {
218 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
219 ads_msgfree(r
->in
.ads
, res
);
220 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
223 ads_msgfree(r
->in
.ads
, res
);
225 /* Attempt to create the machine account and bail if this fails.
226 Assume that the admin wants exactly what they requested */
228 status
= ads_create_machine_acct(r
->in
.ads
,
232 if (ADS_ERR_OK(status
)) {
233 DEBUG(1,("machine account creation created\n"));
235 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
236 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
237 status
= ADS_SUCCESS
;
240 if (!ADS_ERR_OK(status
)) {
241 DEBUG(1,("machine account creation failed\n"));
245 status
= ads_move_machine_acct(r
->in
.ads
,
249 if (!ADS_ERR_OK(status
)) {
250 DEBUG(1,("failure to locate/move pre-existing "
251 "machine account\n"));
255 DEBUG(1,("The machine account %s the specified OU.\n",
256 moved
? "was moved into" : "already exists in"));
261 /****************************************************************
262 ****************************************************************/
264 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
265 struct libnet_UnjoinCtx
*r
)
270 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
271 if (!ADS_ERR_OK(status
)) {
272 libnet_unjoin_set_error_string(mem_ctx
, r
,
273 "failed to connect to AD: %s",
279 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
280 if (!ADS_ERR_OK(status
)) {
281 libnet_unjoin_set_error_string(mem_ctx
, r
,
282 "failed to leave realm: %s",
290 /****************************************************************
291 ****************************************************************/
293 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
294 struct libnet_JoinCtx
*r
)
297 LDAPMessage
*res
= NULL
;
300 if (!r
->in
.machine_name
) {
301 return ADS_ERROR(LDAP_NO_MEMORY
);
304 status
= ads_find_machine_acct(r
->in
.ads
,
307 if (!ADS_ERR_OK(status
)) {
311 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
312 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
316 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
318 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
322 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
324 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
329 ads_msgfree(r
->in
.ads
, res
);
335 /****************************************************************
336 Set a machines dNSHostName and servicePrincipalName attributes
337 ****************************************************************/
339 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
340 struct libnet_JoinCtx
*r
)
345 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
350 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
351 if (!ADS_ERR_OK(status
)) {
355 /* Windows only creates HOST/shortname & HOST/fqdn. */
357 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
359 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
364 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
365 || (strchr(my_fqdn
, '.') == NULL
)) {
366 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
367 r
->out
.dns_domain_name
);
372 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
373 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
375 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
380 mods
= ads_init_mods(mem_ctx
);
382 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
385 /* fields of primary importance */
387 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
388 if (!ADS_ERR_OK(status
)) {
389 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
392 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
394 if (!ADS_ERR_OK(status
)) {
395 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
398 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
401 /****************************************************************
402 ****************************************************************/
404 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
405 struct libnet_JoinCtx
*r
)
410 if (!r
->in
.create_upn
) {
416 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
417 if (!ADS_ERR_OK(status
)) {
422 r
->in
.upn
= talloc_asprintf(mem_ctx
,
425 r
->out
.dns_domain_name
);
427 return ADS_ERROR(LDAP_NO_MEMORY
);
431 /* now do the mods */
433 mods
= ads_init_mods(mem_ctx
);
435 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
438 /* fields of primary importance */
440 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
441 if (!ADS_ERR_OK(status
)) {
442 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
445 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
449 /****************************************************************
450 ****************************************************************/
452 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
453 struct libnet_JoinCtx
*r
)
459 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
465 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
466 if (!ADS_ERR_OK(status
)) {
470 /* now do the mods */
472 mods
= ads_init_mods(mem_ctx
);
474 return ADS_ERROR(LDAP_NO_MEMORY
);
477 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
479 return ADS_ERROR(LDAP_NO_MEMORY
);
482 /* fields of primary importance */
484 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
486 if (!ADS_ERR_OK(status
)) {
490 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
492 if (!ADS_ERR_OK(status
)) {
496 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
498 if (!ADS_ERR_OK(status
)) {
502 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
505 /****************************************************************
506 ****************************************************************/
508 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
509 struct libnet_JoinCtx
*r
)
511 if (!USE_SYSTEM_KEYTAB
) {
515 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
522 /****************************************************************
523 ****************************************************************/
525 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
526 struct libnet_JoinCtx
*r
)
528 uint32_t domain_func
;
530 const char *salt
= NULL
;
531 char *std_salt
= NULL
;
533 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
534 if (!ADS_ERR_OK(status
)) {
535 libnet_join_set_error_string(mem_ctx
, r
,
536 "failed to determine domain functional level: %s",
541 /* go ahead and setup the default salt */
543 std_salt
= kerberos_standard_des_salt();
545 libnet_join_set_error_string(mem_ctx
, r
,
546 "failed to obtain standard DES salt");
550 salt
= talloc_strdup(mem_ctx
, std_salt
);
557 /* if it's a Windows functional domain, we have to look for the UPN */
559 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
562 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
565 salt
= talloc_strdup(mem_ctx
, upn
);
572 return kerberos_secrets_store_des_salt(salt
);
575 /****************************************************************
576 ****************************************************************/
578 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
579 struct libnet_JoinCtx
*r
)
584 status
= libnet_join_connect_ads(mem_ctx
, r
);
585 if (!ADS_ERR_OK(status
)) {
590 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
591 if (!ADS_ERR_OK(status
)) {
592 libnet_join_set_error_string(mem_ctx
, r
,
593 "failed to set machine spn: %s",
598 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
599 if (!ADS_ERR_OK(status
)) {
600 libnet_join_set_error_string(mem_ctx
, r
,
601 "failed to set machine os attributes: %s",
606 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
607 if (!ADS_ERR_OK(status
)) {
608 libnet_join_set_error_string(mem_ctx
, r
,
609 "failed to set machine upn: %s",
614 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
615 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
618 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
619 libnet_join_set_error_string(mem_ctx
, r
,
620 "failed to create kerberos keytab");
621 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
626 #endif /* WITH_ADS */
628 /****************************************************************
629 Store the machine password and domain SID
630 ****************************************************************/
632 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
633 struct libnet_JoinCtx
*r
)
635 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
638 DEBUG(1,("Failed to save domain sid\n"));
642 if (!secrets_store_machine_password(r
->in
.machine_password
,
643 r
->out
.netbios_domain_name
,
644 r
->in
.secure_channel_type
))
646 DEBUG(1,("Failed to save machine password\n"));
653 /****************************************************************
654 Connect dc's IPC$ share
655 ****************************************************************/
657 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
661 struct cli_state
**cli
)
666 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
669 if (use_kerberos
&& pass
) {
670 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
673 return cli_full_connection(cli
, NULL
,
684 /****************************************************************
685 Lookup domain dc's info
686 ****************************************************************/
688 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
689 struct libnet_JoinCtx
*r
,
690 struct cli_state
**cli
)
692 struct rpc_pipe_client
*pipe_hnd
= NULL
;
693 struct policy_handle lsa_pol
;
694 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
695 union lsa_PolicyInformation
*info
= NULL
;
697 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
699 r
->in
.admin_password
,
702 if (!NT_STATUS_IS_OK(status
)) {
706 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
708 if (!NT_STATUS_IS_OK(status
)) {
709 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
714 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
715 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
716 if (!NT_STATUS_IS_OK(status
)) {
720 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
724 if (NT_STATUS_IS_OK(status
)) {
725 r
->out
.domain_is_ad
= true;
726 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
727 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
728 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
729 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->dns
.sid
);
730 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
733 if (!NT_STATUS_IS_OK(status
)) {
734 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
736 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
738 if (!NT_STATUS_IS_OK(status
)) {
742 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
743 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->account_domain
.sid
);
744 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
747 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
748 TALLOC_FREE(pipe_hnd
);
754 /****************************************************************
755 Do the domain join unsecure
756 ****************************************************************/
758 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
759 struct libnet_JoinCtx
*r
,
760 struct cli_state
*cli
)
762 struct rpc_pipe_client
*pipe_hnd
= NULL
;
763 unsigned char orig_trust_passwd_hash
[16];
764 unsigned char new_trust_passwd_hash
[16];
765 fstring trust_passwd
;
768 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
770 if (!NT_STATUS_IS_OK(status
)) {
774 if (!r
->in
.machine_password
) {
775 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
776 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
779 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
781 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
782 fstrcpy(trust_passwd
, r
->in
.admin_password
);
783 strlower_m(trust_passwd
);
786 * Machine names can be 15 characters, but the max length on
787 * a password is 14. --jerry
790 trust_passwd
[14] = '\0';
792 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
794 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
796 orig_trust_passwd_hash
,
797 r
->in
.machine_password
,
798 new_trust_passwd_hash
,
799 r
->in
.secure_channel_type
);
804 /****************************************************************
806 ****************************************************************/
808 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
809 struct libnet_JoinCtx
*r
,
810 struct cli_state
*cli
)
812 struct rpc_pipe_client
*pipe_hnd
= NULL
;
813 struct policy_handle sam_pol
, domain_pol
, user_pol
;
814 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
816 struct lsa_String lsa_acct_name
;
818 uint32_t acct_flags
= ACB_WSTRUST
;
819 struct samr_Ids user_rids
;
820 struct samr_Ids name_types
;
821 union samr_UserInfo user_info
;
823 struct samr_CryptPassword crypt_pwd
;
824 struct samr_CryptPasswordEx crypt_pwd_ex
;
826 ZERO_STRUCT(sam_pol
);
827 ZERO_STRUCT(domain_pol
);
828 ZERO_STRUCT(user_pol
);
830 switch (r
->in
.secure_channel_type
) {
832 acct_flags
= ACB_WSTRUST
;
835 acct_flags
= ACB_SVRTRUST
;
838 return NT_STATUS_INVALID_PARAMETER
;
841 if (!r
->in
.machine_password
) {
842 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
843 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
846 /* Open the domain */
848 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
850 if (!NT_STATUS_IS_OK(status
)) {
851 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
856 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
858 SAMR_ACCESS_ENUM_DOMAINS
859 | SAMR_ACCESS_LOOKUP_DOMAIN
,
861 if (!NT_STATUS_IS_OK(status
)) {
865 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
867 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
868 | SAMR_DOMAIN_ACCESS_CREATE_USER
869 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
872 if (!NT_STATUS_IS_OK(status
)) {
876 /* Create domain user */
878 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
879 strlower_m(acct_name
);
881 init_lsa_String(&lsa_acct_name
, acct_name
);
883 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
884 uint32_t access_desired
=
885 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
886 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
887 SAMR_USER_ACCESS_SET_PASSWORD
|
888 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
889 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
890 uint32_t access_granted
= 0;
892 DEBUG(10,("Creating account with desired access mask: %d\n",
895 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
903 if (!NT_STATUS_IS_OK(status
) &&
904 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
906 DEBUG(10,("Creation of workstation account failed: %s\n",
909 /* If NT_STATUS_ACCESS_DENIED then we have a valid
910 username/password combo but the user does not have
911 administrator access. */
913 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
914 libnet_join_set_error_string(mem_ctx
, r
,
915 "User specified does not have "
916 "administrator privileges");
922 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
923 if (!(r
->in
.join_flags
&
924 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
929 /* We *must* do this.... don't ask... */
931 if (NT_STATUS_IS_OK(status
)) {
932 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
936 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
942 if (!NT_STATUS_IS_OK(status
)) {
946 if (name_types
.ids
[0] != SID_NAME_USER
) {
947 DEBUG(0,("%s is not a user account (type=%d)\n",
948 acct_name
, name_types
.ids
[0]));
949 status
= NT_STATUS_INVALID_WORKSTATION
;
953 user_rid
= user_rids
.ids
[0];
955 /* Open handle on user */
957 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
959 SEC_FLAG_MAXIMUM_ALLOWED
,
962 if (!NT_STATUS_IS_OK(status
)) {
966 /* Fill in the additional account flags now */
968 acct_flags
|= ACB_PWNOEXP
;
969 if (r
->out
.domain_is_ad
) {
970 #if !defined(ENCTYPE_ARCFOUR_HMAC)
971 acct_flags
|= ACB_USE_DES_KEY_ONLY
;
976 /* Set account flags on machine account */
977 ZERO_STRUCT(user_info
.info16
);
978 user_info
.info16
.acct_flags
= acct_flags
;
980 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
985 if (!NT_STATUS_IS_OK(status
)) {
987 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
990 libnet_join_set_error_string(mem_ctx
, r
,
991 "Failed to set account flags for machine account (%s)\n",
996 /* Set password on machine account - first try level 26 */
998 init_samr_CryptPasswordEx(r
->in
.machine_password
,
999 &cli
->user_session_key
,
1002 user_info
.info26
.password
= crypt_pwd_ex
;
1003 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1005 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1010 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
1012 /* retry with level 24 */
1014 init_samr_CryptPassword(r
->in
.machine_password
,
1015 &cli
->user_session_key
,
1018 user_info
.info24
.password
= crypt_pwd
;
1019 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1021 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1027 if (!NT_STATUS_IS_OK(status
)) {
1029 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
1032 libnet_join_set_error_string(mem_ctx
, r
,
1033 "Failed to set password for machine account (%s)\n",
1038 status
= NT_STATUS_OK
;
1045 if (is_valid_policy_hnd(&sam_pol
)) {
1046 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1048 if (is_valid_policy_hnd(&domain_pol
)) {
1049 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1051 if (is_valid_policy_hnd(&user_pol
)) {
1052 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1054 TALLOC_FREE(pipe_hnd
);
1059 /****************************************************************
1060 ****************************************************************/
1062 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1063 const char *machine_name
,
1064 const char *dc_name
)
1066 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1067 struct cli_state
*cli
= NULL
;
1068 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1069 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1071 char *machine_password
= NULL
;
1072 char *machine_account
= NULL
;
1075 return NT_STATUS_INVALID_PARAMETER
;
1078 if (!secrets_init()) {
1079 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1082 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1084 if (!machine_password
) {
1085 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1088 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1089 SAFE_FREE(machine_password
);
1090 return NT_STATUS_NO_MEMORY
;
1093 status
= cli_full_connection(&cli
, NULL
,
1102 free(machine_account
);
1103 free(machine_password
);
1105 if (!NT_STATUS_IS_OK(status
)) {
1106 status
= cli_full_connection(&cli
, NULL
,
1117 if (!NT_STATUS_IS_OK(status
)) {
1121 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1122 &neg_flags
, &netlogon_pipe
);
1123 if (!NT_STATUS_IS_OK(status
)) {
1124 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1126 return NT_STATUS_OK
;
1129 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1130 "key from server %s for domain %s. Error was %s\n",
1131 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1136 if (!lp_client_schannel()) {
1138 return NT_STATUS_OK
;
1141 status
= cli_rpc_pipe_open_schannel_with_key(
1142 cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
1143 DCERPC_AUTH_LEVEL_PRIVACY
,
1144 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1148 if (!NT_STATUS_IS_OK(status
)) {
1149 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1150 "on netlogon pipe to server %s for domain %s. "
1152 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1156 return NT_STATUS_OK
;
1159 /****************************************************************
1160 ****************************************************************/
1162 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1163 struct libnet_JoinCtx
*r
)
1167 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1170 if (!NT_STATUS_IS_OK(status
)) {
1171 libnet_join_set_error_string(mem_ctx
, r
,
1172 "failed to verify domain membership after joining: %s",
1173 get_friendly_nt_error_msg(status
));
1174 return WERR_SETUP_NOT_JOINED
;
1180 /****************************************************************
1181 ****************************************************************/
1183 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1184 struct libnet_UnjoinCtx
*r
)
1186 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1190 if (!secrets_delete_domain_sid(lp_workgroup())) {
1197 /****************************************************************
1198 ****************************************************************/
1200 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1201 struct libnet_UnjoinCtx
*r
)
1203 struct cli_state
*cli
= NULL
;
1204 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1205 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1206 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1209 struct lsa_String lsa_acct_name
;
1210 struct samr_Ids user_rids
;
1211 struct samr_Ids name_types
;
1212 union samr_UserInfo
*info
= NULL
;
1214 ZERO_STRUCT(sam_pol
);
1215 ZERO_STRUCT(domain_pol
);
1216 ZERO_STRUCT(user_pol
);
1218 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1219 r
->in
.admin_account
,
1220 r
->in
.admin_password
,
1223 if (!NT_STATUS_IS_OK(status
)) {
1227 /* Open the domain */
1229 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1231 if (!NT_STATUS_IS_OK(status
)) {
1232 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1233 nt_errstr(status
)));
1237 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1239 SEC_FLAG_MAXIMUM_ALLOWED
,
1241 if (!NT_STATUS_IS_OK(status
)) {
1245 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1247 SEC_FLAG_MAXIMUM_ALLOWED
,
1250 if (!NT_STATUS_IS_OK(status
)) {
1254 /* Create domain user */
1256 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1257 strlower_m(acct_name
);
1259 init_lsa_String(&lsa_acct_name
, acct_name
);
1261 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1268 if (!NT_STATUS_IS_OK(status
)) {
1272 if (name_types
.ids
[0] != SID_NAME_USER
) {
1273 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1274 name_types
.ids
[0]));
1275 status
= NT_STATUS_INVALID_WORKSTATION
;
1279 user_rid
= user_rids
.ids
[0];
1281 /* Open handle on user */
1283 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1285 SEC_FLAG_MAXIMUM_ALLOWED
,
1288 if (!NT_STATUS_IS_OK(status
)) {
1294 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1298 if (!NT_STATUS_IS_OK(status
)) {
1299 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1303 /* now disable and setuser info */
1305 info
->info16
.acct_flags
|= ACB_DISABLED
;
1307 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1312 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1316 if (is_valid_policy_hnd(&domain_pol
)) {
1317 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1319 if (is_valid_policy_hnd(&sam_pol
)) {
1320 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1322 TALLOC_FREE(pipe_hnd
);
1332 /****************************************************************
1333 ****************************************************************/
1335 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1338 struct smbconf_ctx
*ctx
;
1340 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1341 if (!W_ERROR_IS_OK(werr
)) {
1345 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1347 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1348 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1350 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1353 smbconf_delete_global_parameter(ctx
, "realm");
1357 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1358 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1360 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1361 r
->out
.netbios_domain_name
);
1362 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1364 if (r
->out
.domain_is_ad
) {
1365 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1366 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1368 werr
= smbconf_set_global_parameter(ctx
, "realm",
1369 r
->out
.dns_domain_name
);
1370 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1374 smbconf_shutdown(ctx
);
1378 /****************************************************************
1379 ****************************************************************/
1381 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1383 WERROR werr
= WERR_OK
;
1384 struct smbconf_ctx
*ctx
;
1386 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1387 if (!W_ERROR_IS_OK(werr
)) {
1391 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1393 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1394 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1396 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1397 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1399 smbconf_delete_global_parameter(ctx
, "realm");
1403 smbconf_shutdown(ctx
);
1407 /****************************************************************
1408 ****************************************************************/
1410 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1414 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1415 return r
->out
.result
;
1418 if (!r
->in
.modify_config
) {
1422 werr
= do_join_modify_vals_config(r
);
1423 if (!W_ERROR_IS_OK(werr
)) {
1427 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1429 r
->out
.modified_config
= true;
1430 r
->out
.result
= werr
;
1435 /****************************************************************
1436 ****************************************************************/
1438 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1442 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1443 return r
->out
.result
;
1446 if (!r
->in
.modify_config
) {
1450 werr
= do_unjoin_modify_vals_config(r
);
1451 if (!W_ERROR_IS_OK(werr
)) {
1455 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1457 r
->out
.modified_config
= true;
1458 r
->out
.result
= werr
;
1463 /****************************************************************
1464 ****************************************************************/
1466 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1467 const char *domain_str
,
1468 const char **domain_p
,
1471 char *domain
= NULL
;
1473 const char *p
= NULL
;
1475 if (!domain_str
|| !domain_p
|| !dc_p
) {
1479 p
= strchr_m(domain_str
, '\\');
1482 domain
= talloc_strndup(mem_ctx
, domain_str
,
1483 PTR_DIFF(p
, domain_str
));
1484 dc
= talloc_strdup(mem_ctx
, p
+1);
1489 domain
= talloc_strdup(mem_ctx
, domain_str
);
1505 /****************************************************************
1506 ****************************************************************/
1508 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1509 struct libnet_JoinCtx
*r
)
1511 if (!r
->in
.domain_name
) {
1512 libnet_join_set_error_string(mem_ctx
, r
,
1513 "No domain name defined");
1514 return WERR_INVALID_PARAM
;
1517 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1520 libnet_join_set_error_string(mem_ctx
, r
,
1521 "Failed to parse domain name");
1522 return WERR_INVALID_PARAM
;
1526 return WERR_SETUP_DOMAIN_CONTROLLER
;
1529 if (!secrets_init()) {
1530 libnet_join_set_error_string(mem_ctx
, r
,
1531 "Unable to open secrets database");
1532 return WERR_CAN_NOT_COMPLETE
;
1538 /****************************************************************
1539 ****************************************************************/
1541 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1545 /* Try adding dom admins to builtin\admins. Only log failures. */
1546 status
= create_builtin_administrators(domain_sid
);
1547 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1548 DEBUG(10,("Unable to auto-add domain administrators to "
1549 "BUILTIN\\Administrators during join because "
1550 "winbindd must be running."));
1551 } else if (!NT_STATUS_IS_OK(status
)) {
1552 DEBUG(5, ("Failed to auto-add domain administrators to "
1553 "BUILTIN\\Administrators during join: %s\n",
1554 nt_errstr(status
)));
1557 /* Try adding dom users to builtin\users. Only log failures. */
1558 status
= create_builtin_users(domain_sid
);
1559 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1560 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1561 "during join because winbindd must be running."));
1562 } else if (!NT_STATUS_IS_OK(status
)) {
1563 DEBUG(5, ("Failed to auto-add domain administrators to "
1564 "BUILTIN\\Administrators during join: %s\n",
1565 nt_errstr(status
)));
1569 /****************************************************************
1570 ****************************************************************/
1572 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1573 struct libnet_JoinCtx
*r
)
1577 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1578 return r
->out
.result
;
1581 werr
= do_JoinConfig(r
);
1582 if (!W_ERROR_IS_OK(werr
)) {
1586 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1590 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1591 if (r
->out
.dns_domain_name
) {
1592 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1596 if (r
->out
.domain_is_ad
&&
1597 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1598 ADS_STATUS ads_status
;
1600 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1601 if (!ADS_ERR_OK(ads_status
)) {
1602 return WERR_GENERAL_FAILURE
;
1605 #endif /* WITH_ADS */
1607 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1612 /****************************************************************
1613 ****************************************************************/
1615 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1617 const char *krb5_cc_env
= NULL
;
1620 ads_destroy(&r
->in
.ads
);
1623 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1624 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1625 unsetenv(KRB5_ENV_CCNAME
);
1631 /****************************************************************
1632 ****************************************************************/
1634 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1636 const char *krb5_cc_env
= NULL
;
1639 ads_destroy(&r
->in
.ads
);
1642 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1643 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1644 unsetenv(KRB5_ENV_CCNAME
);
1650 /****************************************************************
1651 ****************************************************************/
1653 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1654 struct libnet_JoinCtx
**r
)
1656 struct libnet_JoinCtx
*ctx
;
1657 const char *krb5_cc_env
= NULL
;
1659 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1664 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1666 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1667 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1669 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1670 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1671 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1672 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1673 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1676 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1683 /****************************************************************
1684 ****************************************************************/
1686 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1687 struct libnet_UnjoinCtx
**r
)
1689 struct libnet_UnjoinCtx
*ctx
;
1690 const char *krb5_cc_env
= NULL
;
1692 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1697 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1699 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1700 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1702 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1703 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1704 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1705 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1706 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1714 /****************************************************************
1715 ****************************************************************/
1717 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1718 struct libnet_JoinCtx
*r
)
1720 bool valid_security
= false;
1721 bool valid_workgroup
= false;
1722 bool valid_realm
= false;
1724 /* check if configuration is already set correctly */
1726 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1728 switch (r
->out
.domain_is_ad
) {
1730 valid_security
= (lp_security() == SEC_DOMAIN
);
1731 if (valid_workgroup
&& valid_security
) {
1732 /* nothing to be done */
1737 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1738 switch (lp_security()) {
1741 valid_security
= true;
1744 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1745 /* nothing to be done */
1751 /* check if we are supposed to manipulate configuration */
1753 if (!r
->in
.modify_config
) {
1755 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1757 if (!valid_workgroup
) {
1758 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1759 "\"workgroup\" set to '%s', should be '%s'",
1760 lp_workgroup(), r
->out
.netbios_domain_name
);
1761 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1765 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1766 "\"realm\" set to '%s', should be '%s'",
1767 lp_realm(), r
->out
.dns_domain_name
);
1768 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1771 if (!valid_security
) {
1772 const char *sec
= NULL
;
1773 switch (lp_security()) {
1774 case SEC_SHARE
: sec
= "share"; break;
1775 case SEC_USER
: sec
= "user"; break;
1776 case SEC_DOMAIN
: sec
= "domain"; break;
1777 case SEC_ADS
: sec
= "ads"; break;
1779 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1780 "\"security\" set to '%s', should be %s",
1781 sec
, r
->out
.domain_is_ad
?
1782 "either 'domain' or 'ads'" : "'domain'");
1783 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1786 libnet_join_set_error_string(mem_ctx
, r
,
1787 "Invalid configuration (%s) and configuration modification "
1788 "was not requested", wrong_conf
);
1789 return WERR_CAN_NOT_COMPLETE
;
1792 /* check if we are able to manipulate configuration */
1794 if (!lp_config_backend_is_registry()) {
1795 libnet_join_set_error_string(mem_ctx
, r
,
1796 "Configuration manipulation requested but not "
1797 "supported by backend");
1798 return WERR_NOT_SUPPORTED
;
1804 /****************************************************************
1805 ****************************************************************/
1807 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1808 struct libnet_JoinCtx
*r
)
1812 struct cli_state
*cli
= NULL
;
1814 ADS_STATUS ads_status
;
1815 #endif /* WITH_ADS */
1817 if (!r
->in
.dc_name
) {
1818 struct netr_DsRGetDCNameInfo
*info
;
1820 status
= dsgetdcname(mem_ctx
,
1825 DS_FORCE_REDISCOVERY
|
1826 DS_DIRECTORY_SERVICE_REQUIRED
|
1827 DS_WRITABLE_REQUIRED
|
1830 if (!NT_STATUS_IS_OK(status
)) {
1831 libnet_join_set_error_string(mem_ctx
, r
,
1832 "failed to find DC for domain %s",
1834 get_friendly_nt_error_msg(status
));
1835 return WERR_DCNOTFOUND
;
1838 dc
= strip_hostname(info
->dc_unc
);
1839 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1840 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1843 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1844 if (!NT_STATUS_IS_OK(status
)) {
1845 libnet_join_set_error_string(mem_ctx
, r
,
1846 "failed to lookup DC info for domain '%s' over rpc: %s",
1847 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1848 return ntstatus_to_werror(status
);
1851 werr
= libnet_join_check_config(mem_ctx
, r
);
1852 if (!W_ERROR_IS_OK(werr
)) {
1857 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
1858 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1860 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1861 if (!ADS_ERR_OK(ads_status
)) {
1862 return WERR_DEFAULT_JOIN_REQUIRED
;
1865 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1866 if (!ADS_ERR_OK(ads_status
)) {
1867 libnet_join_set_error_string(mem_ctx
, r
,
1868 "failed to precreate account in ou %s: %s",
1870 ads_errstr(ads_status
));
1871 return WERR_DEFAULT_JOIN_REQUIRED
;
1874 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1876 #endif /* WITH_ADS */
1878 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
1879 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
1880 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
1882 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1884 if (!NT_STATUS_IS_OK(status
)) {
1885 libnet_join_set_error_string(mem_ctx
, r
,
1886 "failed to join domain '%s' over rpc: %s",
1887 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1888 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1889 return WERR_SETUP_ALREADY_JOINED
;
1891 werr
= ntstatus_to_werror(status
);
1895 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1896 werr
= WERR_SETUP_NOT_JOINED
;
1910 /****************************************************************
1911 ****************************************************************/
1913 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
1914 struct libnet_JoinCtx
*r
)
1917 struct libnet_UnjoinCtx
*u
= NULL
;
1919 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
1920 if (!W_ERROR_IS_OK(werr
)) {
1924 u
->in
.debug
= r
->in
.debug
;
1925 u
->in
.dc_name
= r
->in
.dc_name
;
1926 u
->in
.domain_name
= r
->in
.domain_name
;
1927 u
->in
.admin_account
= r
->in
.admin_account
;
1928 u
->in
.admin_password
= r
->in
.admin_password
;
1929 u
->in
.modify_config
= r
->in
.modify_config
;
1930 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1931 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1933 werr
= libnet_Unjoin(mem_ctx
, u
);
1939 /****************************************************************
1940 ****************************************************************/
1942 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1943 struct libnet_JoinCtx
*r
)
1948 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1951 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1952 if (!W_ERROR_IS_OK(werr
)) {
1956 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1957 werr
= libnet_DomainJoin(mem_ctx
, r
);
1958 if (!W_ERROR_IS_OK(werr
)) {
1963 werr
= libnet_join_post_processing(mem_ctx
, r
);
1964 if (!W_ERROR_IS_OK(werr
)) {
1968 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1969 werr
= libnet_join_post_verify(mem_ctx
, r
);
1970 if (!W_ERROR_IS_OK(werr
)) {
1971 libnet_join_rollback(mem_ctx
, r
);
1976 r
->out
.result
= werr
;
1979 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1984 /****************************************************************
1985 ****************************************************************/
1987 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
1988 struct libnet_UnjoinCtx
*r
)
1992 if (!r
->in
.domain_sid
) {
1994 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
1995 libnet_unjoin_set_error_string(mem_ctx
, r
,
1996 "Unable to fetch domain sid: are we joined?");
1997 return WERR_SETUP_NOT_JOINED
;
1999 r
->in
.domain_sid
= sid_dup_talloc(mem_ctx
, &sid
);
2000 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2003 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2004 !r
->in
.delete_machine_account
) {
2005 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2009 if (!r
->in
.dc_name
) {
2010 struct netr_DsRGetDCNameInfo
*info
;
2012 status
= dsgetdcname(mem_ctx
,
2017 DS_DIRECTORY_SERVICE_REQUIRED
|
2018 DS_WRITABLE_REQUIRED
|
2021 if (!NT_STATUS_IS_OK(status
)) {
2022 libnet_unjoin_set_error_string(mem_ctx
, r
,
2023 "failed to find DC for domain %s",
2025 get_friendly_nt_error_msg(status
));
2026 return WERR_DCNOTFOUND
;
2029 dc
= strip_hostname(info
->dc_unc
);
2030 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2031 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2035 /* for net ads leave, try to delete the account. If it works,
2036 no sense in disabling. If it fails, we can still try to
2039 if (r
->in
.delete_machine_account
) {
2040 ADS_STATUS ads_status
;
2041 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2042 if (ADS_ERR_OK(ads_status
)) {
2044 r
->out
.dns_domain_name
=
2045 talloc_strdup(mem_ctx
,
2046 r
->in
.ads
->server
.realm
);
2048 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2050 if (!ADS_ERR_OK(ads_status
)) {
2051 libnet_unjoin_set_error_string(mem_ctx
, r
,
2052 "failed to remove machine account from AD: %s",
2053 ads_errstr(ads_status
));
2055 r
->out
.deleted_machine_account
= true;
2056 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2057 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2061 #endif /* WITH_ADS */
2063 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2065 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2066 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2067 if (!NT_STATUS_IS_OK(status
)) {
2068 libnet_unjoin_set_error_string(mem_ctx
, r
,
2069 "failed to disable machine account via rpc: %s",
2070 get_friendly_nt_error_msg(status
));
2071 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2072 return WERR_SETUP_NOT_JOINED
;
2074 return ntstatus_to_werror(status
);
2077 r
->out
.disabled_machine_account
= true;
2080 /* If disable succeeded or was not requested at all, we
2081 should be getting rid of our end of things */
2083 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2088 /****************************************************************
2089 ****************************************************************/
2091 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2092 struct libnet_UnjoinCtx
*r
)
2094 if (!r
->in
.domain_name
) {
2095 libnet_unjoin_set_error_string(mem_ctx
, r
,
2096 "No domain name defined");
2097 return WERR_INVALID_PARAM
;
2100 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2103 libnet_unjoin_set_error_string(mem_ctx
, r
,
2104 "Failed to parse domain name");
2105 return WERR_INVALID_PARAM
;
2109 return WERR_SETUP_DOMAIN_CONTROLLER
;
2112 if (!secrets_init()) {
2113 libnet_unjoin_set_error_string(mem_ctx
, r
,
2114 "Unable to open secrets database");
2115 return WERR_CAN_NOT_COMPLETE
;
2121 /****************************************************************
2122 ****************************************************************/
2124 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2125 struct libnet_UnjoinCtx
*r
)
2127 saf_delete(r
->out
.netbios_domain_name
);
2128 saf_delete(r
->out
.dns_domain_name
);
2130 return libnet_unjoin_config(r
);
2133 /****************************************************************
2134 ****************************************************************/
2136 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2137 struct libnet_UnjoinCtx
*r
)
2142 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2145 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2146 if (!W_ERROR_IS_OK(werr
)) {
2150 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2151 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2152 if (!W_ERROR_IS_OK(werr
)) {
2153 libnet_unjoin_config(r
);
2158 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2159 if (!W_ERROR_IS_OK(werr
)) {
2164 r
->out
.result
= werr
;
2167 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);