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"
37 /****************************************************************
38 ****************************************************************/
40 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
43 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
44 DEBUG(1,("libnet_Join:\n%s", str)); \
48 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
49 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
50 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
51 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
53 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
56 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
57 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
61 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
62 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
63 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
64 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
66 /****************************************************************
67 ****************************************************************/
69 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
70 struct libnet_JoinCtx
*r
,
71 const char *format
, ...)
75 if (r
->out
.error_string
) {
79 va_start(args
, format
);
80 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
84 /****************************************************************
85 ****************************************************************/
87 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
88 struct libnet_UnjoinCtx
*r
,
89 const char *format
, ...)
93 if (r
->out
.error_string
) {
97 va_start(args
, format
);
98 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
104 /****************************************************************
105 ****************************************************************/
107 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
108 const char *netbios_domain_name
,
110 const char *user_name
,
111 const char *password
,
115 ADS_STRUCT
*my_ads
= NULL
;
117 my_ads
= ads_init(dns_domain_name
,
121 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
125 SAFE_FREE(my_ads
->auth
.user_name
);
126 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
130 SAFE_FREE(my_ads
->auth
.password
);
131 my_ads
->auth
.password
= SMB_STRDUP(password
);
134 status
= ads_connect_user_creds(my_ads
);
135 if (!ADS_ERR_OK(status
)) {
136 ads_destroy(&my_ads
);
144 /****************************************************************
145 ****************************************************************/
147 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
148 struct libnet_JoinCtx
*r
)
152 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
153 r
->out
.netbios_domain_name
,
156 r
->in
.admin_password
,
158 if (!ADS_ERR_OK(status
)) {
159 libnet_join_set_error_string(mem_ctx
, r
,
160 "failed to connect to AD: %s",
165 if (!r
->out
.netbios_domain_name
) {
166 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
167 r
->in
.ads
->server
.workgroup
);
168 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
171 if (!r
->out
.dns_domain_name
) {
172 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
173 r
->in
.ads
->config
.realm
);
174 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
177 r
->out
.domain_is_ad
= true;
182 /****************************************************************
183 ****************************************************************/
185 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
186 struct libnet_UnjoinCtx
*r
)
190 status
= libnet_connect_ads(r
->in
.domain_name
,
194 r
->in
.admin_password
,
196 if (!ADS_ERR_OK(status
)) {
197 libnet_unjoin_set_error_string(mem_ctx
, r
,
198 "failed to connect to AD: %s",
205 /****************************************************************
206 join a domain using ADS (LDAP mods)
207 ****************************************************************/
209 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
210 struct libnet_JoinCtx
*r
)
213 LDAPMessage
*res
= NULL
;
214 const char *attrs
[] = { "dn", NULL
};
217 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
218 if (!ADS_ERR_OK(status
)) {
222 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
223 if (!ADS_ERR_OK(status
)) {
227 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
228 ads_msgfree(r
->in
.ads
, res
);
229 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
232 ads_msgfree(r
->in
.ads
, res
);
234 /* Attempt to create the machine account and bail if this fails.
235 Assume that the admin wants exactly what they requested */
237 status
= ads_create_machine_acct(r
->in
.ads
,
241 if (ADS_ERR_OK(status
)) {
242 DEBUG(1,("machine account creation created\n"));
244 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
245 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
246 status
= ADS_SUCCESS
;
249 if (!ADS_ERR_OK(status
)) {
250 DEBUG(1,("machine account creation failed\n"));
254 status
= ads_move_machine_acct(r
->in
.ads
,
258 if (!ADS_ERR_OK(status
)) {
259 DEBUG(1,("failure to locate/move pre-existing "
260 "machine account\n"));
264 DEBUG(1,("The machine account %s the specified OU.\n",
265 moved
? "was moved into" : "already exists in"));
270 /****************************************************************
271 ****************************************************************/
273 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
274 struct libnet_UnjoinCtx
*r
)
279 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
280 if (!ADS_ERR_OK(status
)) {
281 libnet_unjoin_set_error_string(mem_ctx
, r
,
282 "failed to connect to AD: %s",
288 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
289 if (!ADS_ERR_OK(status
)) {
290 libnet_unjoin_set_error_string(mem_ctx
, r
,
291 "failed to leave realm: %s",
299 /****************************************************************
300 ****************************************************************/
302 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
303 struct libnet_JoinCtx
*r
)
306 LDAPMessage
*res
= NULL
;
309 if (!r
->in
.machine_name
) {
310 return ADS_ERROR(LDAP_NO_MEMORY
);
313 status
= ads_find_machine_acct(r
->in
.ads
,
316 if (!ADS_ERR_OK(status
)) {
320 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
321 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
325 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
327 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
331 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
333 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
338 ads_msgfree(r
->in
.ads
, res
);
344 /****************************************************************
345 Set a machines dNSHostName and servicePrincipalName attributes
346 ****************************************************************/
348 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
349 struct libnet_JoinCtx
*r
)
354 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
359 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
360 if (!ADS_ERR_OK(status
)) {
364 /* Windows only creates HOST/shortname & HOST/fqdn. */
366 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
368 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
373 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
374 || (strchr(my_fqdn
, '.') == NULL
)) {
375 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
376 r
->out
.dns_domain_name
);
381 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
382 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
384 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
389 mods
= ads_init_mods(mem_ctx
);
391 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
394 /* fields of primary importance */
396 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
397 if (!ADS_ERR_OK(status
)) {
398 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
401 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
403 if (!ADS_ERR_OK(status
)) {
404 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
407 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
410 /****************************************************************
411 ****************************************************************/
413 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
414 struct libnet_JoinCtx
*r
)
419 if (!r
->in
.create_upn
) {
425 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
426 if (!ADS_ERR_OK(status
)) {
431 r
->in
.upn
= talloc_asprintf(mem_ctx
,
434 r
->out
.dns_domain_name
);
436 return ADS_ERROR(LDAP_NO_MEMORY
);
440 /* now do the mods */
442 mods
= ads_init_mods(mem_ctx
);
444 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
447 /* fields of primary importance */
449 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
450 if (!ADS_ERR_OK(status
)) {
451 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
454 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
458 /****************************************************************
459 ****************************************************************/
461 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
462 struct libnet_JoinCtx
*r
)
468 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
474 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
475 if (!ADS_ERR_OK(status
)) {
479 /* now do the mods */
481 mods
= ads_init_mods(mem_ctx
);
483 return ADS_ERROR(LDAP_NO_MEMORY
);
486 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
488 return ADS_ERROR(LDAP_NO_MEMORY
);
491 /* fields of primary importance */
493 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
495 if (!ADS_ERR_OK(status
)) {
499 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
501 if (!ADS_ERR_OK(status
)) {
505 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
507 if (!ADS_ERR_OK(status
)) {
511 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
514 /****************************************************************
515 ****************************************************************/
517 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
518 struct libnet_JoinCtx
*r
)
520 if (!USE_SYSTEM_KEYTAB
) {
524 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
531 /****************************************************************
532 ****************************************************************/
534 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
535 struct libnet_JoinCtx
*r
)
537 uint32_t domain_func
;
539 const char *salt
= NULL
;
540 char *std_salt
= NULL
;
542 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
543 if (!ADS_ERR_OK(status
)) {
544 libnet_join_set_error_string(mem_ctx
, r
,
545 "failed to determine domain functional level: %s",
550 /* go ahead and setup the default salt */
552 std_salt
= kerberos_standard_des_salt();
554 libnet_join_set_error_string(mem_ctx
, r
,
555 "failed to obtain standard DES salt");
559 salt
= talloc_strdup(mem_ctx
, std_salt
);
566 /* if it's a Windows functional domain, we have to look for the UPN */
568 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
571 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
574 salt
= talloc_strdup(mem_ctx
, upn
);
581 return kerberos_secrets_store_des_salt(salt
);
584 /****************************************************************
585 ****************************************************************/
587 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
588 struct libnet_JoinCtx
*r
)
593 status
= libnet_join_connect_ads(mem_ctx
, r
);
594 if (!ADS_ERR_OK(status
)) {
599 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
600 if (!ADS_ERR_OK(status
)) {
601 libnet_join_set_error_string(mem_ctx
, r
,
602 "failed to set machine spn: %s",
607 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
608 if (!ADS_ERR_OK(status
)) {
609 libnet_join_set_error_string(mem_ctx
, r
,
610 "failed to set machine os attributes: %s",
615 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
616 if (!ADS_ERR_OK(status
)) {
617 libnet_join_set_error_string(mem_ctx
, r
,
618 "failed to set machine upn: %s",
623 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
624 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
627 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
628 libnet_join_set_error_string(mem_ctx
, r
,
629 "failed to create kerberos keytab");
630 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
635 #endif /* WITH_ADS */
637 /****************************************************************
638 Store the machine password and domain SID
639 ****************************************************************/
641 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
642 struct libnet_JoinCtx
*r
)
644 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
647 DEBUG(1,("Failed to save domain sid\n"));
651 if (!secrets_store_machine_password(r
->in
.machine_password
,
652 r
->out
.netbios_domain_name
,
653 r
->in
.secure_channel_type
))
655 DEBUG(1,("Failed to save machine password\n"));
662 /****************************************************************
663 Connect dc's IPC$ share
664 ****************************************************************/
666 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
670 struct cli_state
**cli
)
675 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
678 if (use_kerberos
&& pass
) {
679 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
682 return cli_full_connection(cli
, NULL
,
693 /****************************************************************
694 Lookup domain dc's info
695 ****************************************************************/
697 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
698 struct libnet_JoinCtx
*r
,
699 struct cli_state
**cli
)
701 struct rpc_pipe_client
*pipe_hnd
= NULL
;
702 struct policy_handle lsa_pol
;
703 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
704 union lsa_PolicyInformation
*info
= NULL
;
706 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
708 r
->in
.admin_password
,
711 if (!NT_STATUS_IS_OK(status
)) {
715 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
717 if (!NT_STATUS_IS_OK(status
)) {
718 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
723 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
724 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
725 if (!NT_STATUS_IS_OK(status
)) {
729 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
733 if (NT_STATUS_IS_OK(status
)) {
734 r
->out
.domain_is_ad
= true;
735 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
736 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
737 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
738 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->dns
.sid
);
739 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
742 if (!NT_STATUS_IS_OK(status
)) {
743 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
745 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
747 if (!NT_STATUS_IS_OK(status
)) {
751 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
752 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->account_domain
.sid
);
753 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
756 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
757 TALLOC_FREE(pipe_hnd
);
763 /****************************************************************
764 Do the domain join unsecure
765 ****************************************************************/
767 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
768 struct libnet_JoinCtx
*r
,
769 struct cli_state
*cli
)
771 struct rpc_pipe_client
*pipe_hnd
= NULL
;
772 unsigned char orig_trust_passwd_hash
[16];
773 unsigned char new_trust_passwd_hash
[16];
774 fstring trust_passwd
;
777 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
779 if (!NT_STATUS_IS_OK(status
)) {
783 if (!r
->in
.machine_password
) {
784 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
785 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
788 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
790 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
791 fstrcpy(trust_passwd
, r
->in
.admin_password
);
792 strlower_m(trust_passwd
);
795 * Machine names can be 15 characters, but the max length on
796 * a password is 14. --jerry
799 trust_passwd
[14] = '\0';
801 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
803 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
805 orig_trust_passwd_hash
,
806 r
->in
.machine_password
,
807 new_trust_passwd_hash
,
808 r
->in
.secure_channel_type
);
813 /****************************************************************
815 ****************************************************************/
817 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
818 struct libnet_JoinCtx
*r
,
819 struct cli_state
*cli
)
821 struct rpc_pipe_client
*pipe_hnd
= NULL
;
822 struct policy_handle sam_pol
, domain_pol
, user_pol
;
823 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
825 struct lsa_String lsa_acct_name
;
827 uint32_t acct_flags
= ACB_WSTRUST
;
828 struct samr_Ids user_rids
;
829 struct samr_Ids name_types
;
830 union samr_UserInfo user_info
;
832 struct samr_CryptPassword crypt_pwd
;
833 struct samr_CryptPasswordEx crypt_pwd_ex
;
835 ZERO_STRUCT(sam_pol
);
836 ZERO_STRUCT(domain_pol
);
837 ZERO_STRUCT(user_pol
);
839 switch (r
->in
.secure_channel_type
) {
841 acct_flags
= ACB_WSTRUST
;
844 acct_flags
= ACB_SVRTRUST
;
847 return NT_STATUS_INVALID_PARAMETER
;
850 if (!r
->in
.machine_password
) {
851 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
852 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
855 /* Open the domain */
857 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
859 if (!NT_STATUS_IS_OK(status
)) {
860 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
865 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
867 SAMR_ACCESS_ENUM_DOMAINS
868 | SAMR_ACCESS_LOOKUP_DOMAIN
,
870 if (!NT_STATUS_IS_OK(status
)) {
874 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
876 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
877 | SAMR_DOMAIN_ACCESS_CREATE_USER
878 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
881 if (!NT_STATUS_IS_OK(status
)) {
885 /* Create domain user */
887 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
888 strlower_m(acct_name
);
890 init_lsa_String(&lsa_acct_name
, acct_name
);
892 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
893 uint32_t access_desired
=
894 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
895 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
896 SAMR_USER_ACCESS_SET_PASSWORD
|
897 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
898 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
899 uint32_t access_granted
= 0;
901 DEBUG(10,("Creating account with desired access mask: %d\n",
904 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
912 if (!NT_STATUS_IS_OK(status
) &&
913 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
915 DEBUG(10,("Creation of workstation account failed: %s\n",
918 /* If NT_STATUS_ACCESS_DENIED then we have a valid
919 username/password combo but the user does not have
920 administrator access. */
922 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
923 libnet_join_set_error_string(mem_ctx
, r
,
924 "User specified does not have "
925 "administrator privileges");
931 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
932 if (!(r
->in
.join_flags
&
933 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
938 /* We *must* do this.... don't ask... */
940 if (NT_STATUS_IS_OK(status
)) {
941 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
945 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
951 if (!NT_STATUS_IS_OK(status
)) {
955 if (name_types
.ids
[0] != SID_NAME_USER
) {
956 DEBUG(0,("%s is not a user account (type=%d)\n",
957 acct_name
, name_types
.ids
[0]));
958 status
= NT_STATUS_INVALID_WORKSTATION
;
962 user_rid
= user_rids
.ids
[0];
964 /* Open handle on user */
966 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
968 SEC_FLAG_MAXIMUM_ALLOWED
,
971 if (!NT_STATUS_IS_OK(status
)) {
975 /* Fill in the additional account flags now */
977 acct_flags
|= ACB_PWNOEXP
;
978 if (r
->out
.domain_is_ad
) {
979 #if !defined(ENCTYPE_ARCFOUR_HMAC)
980 acct_flags
|= ACB_USE_DES_KEY_ONLY
;
985 /* Set account flags on machine account */
986 ZERO_STRUCT(user_info
.info16
);
987 user_info
.info16
.acct_flags
= acct_flags
;
989 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
994 if (!NT_STATUS_IS_OK(status
)) {
996 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
999 libnet_join_set_error_string(mem_ctx
, r
,
1000 "Failed to set account flags for machine account (%s)\n",
1005 /* Set password on machine account - first try level 26 */
1007 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1008 &cli
->user_session_key
,
1011 user_info
.info26
.password
= crypt_pwd_ex
;
1012 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1014 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1019 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
1021 /* retry with level 24 */
1023 init_samr_CryptPassword(r
->in
.machine_password
,
1024 &cli
->user_session_key
,
1027 user_info
.info24
.password
= crypt_pwd
;
1028 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1030 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1036 if (!NT_STATUS_IS_OK(status
)) {
1038 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
1041 libnet_join_set_error_string(mem_ctx
, r
,
1042 "Failed to set password for machine account (%s)\n",
1047 status
= NT_STATUS_OK
;
1054 if (is_valid_policy_hnd(&sam_pol
)) {
1055 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1057 if (is_valid_policy_hnd(&domain_pol
)) {
1058 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1060 if (is_valid_policy_hnd(&user_pol
)) {
1061 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1063 TALLOC_FREE(pipe_hnd
);
1068 /****************************************************************
1069 ****************************************************************/
1071 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1072 const char *machine_name
,
1073 const char *dc_name
)
1075 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1076 struct cli_state
*cli
= NULL
;
1077 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1078 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1080 char *machine_password
= NULL
;
1081 char *machine_account
= NULL
;
1084 return NT_STATUS_INVALID_PARAMETER
;
1087 if (!secrets_init()) {
1088 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1091 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1093 if (!machine_password
) {
1094 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1097 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1098 SAFE_FREE(machine_password
);
1099 return NT_STATUS_NO_MEMORY
;
1102 status
= cli_full_connection(&cli
, NULL
,
1111 free(machine_account
);
1112 free(machine_password
);
1114 if (!NT_STATUS_IS_OK(status
)) {
1115 status
= cli_full_connection(&cli
, NULL
,
1126 if (!NT_STATUS_IS_OK(status
)) {
1130 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1131 &neg_flags
, &netlogon_pipe
);
1132 if (!NT_STATUS_IS_OK(status
)) {
1133 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1135 return NT_STATUS_OK
;
1138 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1139 "key from server %s for domain %s. Error was %s\n",
1140 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1145 if (!lp_client_schannel()) {
1147 return NT_STATUS_OK
;
1150 status
= cli_rpc_pipe_open_schannel_with_key(
1151 cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
1152 DCERPC_AUTH_LEVEL_PRIVACY
,
1153 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1157 if (!NT_STATUS_IS_OK(status
)) {
1158 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1159 "on netlogon pipe to server %s for domain %s. "
1161 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1165 return NT_STATUS_OK
;
1168 /****************************************************************
1169 ****************************************************************/
1171 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1172 struct libnet_JoinCtx
*r
)
1176 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1179 if (!NT_STATUS_IS_OK(status
)) {
1180 libnet_join_set_error_string(mem_ctx
, r
,
1181 "failed to verify domain membership after joining: %s",
1182 get_friendly_nt_error_msg(status
));
1183 return WERR_SETUP_NOT_JOINED
;
1189 /****************************************************************
1190 ****************************************************************/
1192 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1193 struct libnet_UnjoinCtx
*r
)
1195 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1199 if (!secrets_delete_domain_sid(lp_workgroup())) {
1206 /****************************************************************
1207 ****************************************************************/
1209 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1210 struct libnet_UnjoinCtx
*r
)
1212 struct cli_state
*cli
= NULL
;
1213 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1214 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1215 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1218 struct lsa_String lsa_acct_name
;
1219 struct samr_Ids user_rids
;
1220 struct samr_Ids name_types
;
1221 union samr_UserInfo
*info
= NULL
;
1223 ZERO_STRUCT(sam_pol
);
1224 ZERO_STRUCT(domain_pol
);
1225 ZERO_STRUCT(user_pol
);
1227 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1228 r
->in
.admin_account
,
1229 r
->in
.admin_password
,
1232 if (!NT_STATUS_IS_OK(status
)) {
1236 /* Open the domain */
1238 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1240 if (!NT_STATUS_IS_OK(status
)) {
1241 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1242 nt_errstr(status
)));
1246 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1248 SEC_FLAG_MAXIMUM_ALLOWED
,
1250 if (!NT_STATUS_IS_OK(status
)) {
1254 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1256 SEC_FLAG_MAXIMUM_ALLOWED
,
1259 if (!NT_STATUS_IS_OK(status
)) {
1263 /* Create domain user */
1265 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1266 strlower_m(acct_name
);
1268 init_lsa_String(&lsa_acct_name
, acct_name
);
1270 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1277 if (!NT_STATUS_IS_OK(status
)) {
1281 if (name_types
.ids
[0] != SID_NAME_USER
) {
1282 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1283 name_types
.ids
[0]));
1284 status
= NT_STATUS_INVALID_WORKSTATION
;
1288 user_rid
= user_rids
.ids
[0];
1290 /* Open handle on user */
1292 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1294 SEC_FLAG_MAXIMUM_ALLOWED
,
1297 if (!NT_STATUS_IS_OK(status
)) {
1303 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1307 if (!NT_STATUS_IS_OK(status
)) {
1308 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1312 /* now disable and setuser info */
1314 info
->info16
.acct_flags
|= ACB_DISABLED
;
1316 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1321 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1325 if (is_valid_policy_hnd(&domain_pol
)) {
1326 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1328 if (is_valid_policy_hnd(&sam_pol
)) {
1329 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1331 TALLOC_FREE(pipe_hnd
);
1341 /****************************************************************
1342 ****************************************************************/
1344 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1347 struct smbconf_ctx
*ctx
;
1349 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1350 if (!W_ERROR_IS_OK(werr
)) {
1354 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1356 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1357 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1359 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1362 smbconf_delete_global_parameter(ctx
, "realm");
1366 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1367 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1369 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1370 r
->out
.netbios_domain_name
);
1371 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1373 if (r
->out
.domain_is_ad
) {
1374 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1375 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1377 werr
= smbconf_set_global_parameter(ctx
, "realm",
1378 r
->out
.dns_domain_name
);
1379 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1383 smbconf_shutdown(ctx
);
1387 /****************************************************************
1388 ****************************************************************/
1390 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1392 WERROR werr
= WERR_OK
;
1393 struct smbconf_ctx
*ctx
;
1395 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1396 if (!W_ERROR_IS_OK(werr
)) {
1400 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1402 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1403 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1405 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1406 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1408 smbconf_delete_global_parameter(ctx
, "realm");
1412 smbconf_shutdown(ctx
);
1416 /****************************************************************
1417 ****************************************************************/
1419 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1423 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1424 return r
->out
.result
;
1427 if (!r
->in
.modify_config
) {
1431 werr
= do_join_modify_vals_config(r
);
1432 if (!W_ERROR_IS_OK(werr
)) {
1436 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1438 r
->out
.modified_config
= true;
1439 r
->out
.result
= werr
;
1444 /****************************************************************
1445 ****************************************************************/
1447 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1451 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1452 return r
->out
.result
;
1455 if (!r
->in
.modify_config
) {
1459 werr
= do_unjoin_modify_vals_config(r
);
1460 if (!W_ERROR_IS_OK(werr
)) {
1464 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1466 r
->out
.modified_config
= true;
1467 r
->out
.result
= werr
;
1472 /****************************************************************
1473 ****************************************************************/
1475 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1476 const char *domain_str
,
1477 const char **domain_p
,
1480 char *domain
= NULL
;
1482 const char *p
= NULL
;
1484 if (!domain_str
|| !domain_p
|| !dc_p
) {
1488 p
= strchr_m(domain_str
, '\\');
1491 domain
= talloc_strndup(mem_ctx
, domain_str
,
1492 PTR_DIFF(p
, domain_str
));
1493 dc
= talloc_strdup(mem_ctx
, p
+1);
1498 domain
= talloc_strdup(mem_ctx
, domain_str
);
1514 /****************************************************************
1515 ****************************************************************/
1517 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1518 struct libnet_JoinCtx
*r
)
1520 if (!r
->in
.domain_name
) {
1521 libnet_join_set_error_string(mem_ctx
, r
,
1522 "No domain name defined");
1523 return WERR_INVALID_PARAM
;
1526 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1529 libnet_join_set_error_string(mem_ctx
, r
,
1530 "Failed to parse domain name");
1531 return WERR_INVALID_PARAM
;
1535 return WERR_SETUP_DOMAIN_CONTROLLER
;
1538 if (!secrets_init()) {
1539 libnet_join_set_error_string(mem_ctx
, r
,
1540 "Unable to open secrets database");
1541 return WERR_CAN_NOT_COMPLETE
;
1547 /****************************************************************
1548 ****************************************************************/
1550 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1554 /* Try adding dom admins to builtin\admins. Only log failures. */
1555 status
= create_builtin_administrators(domain_sid
);
1556 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1557 DEBUG(10,("Unable to auto-add domain administrators to "
1558 "BUILTIN\\Administrators during join because "
1559 "winbindd must be running."));
1560 } else if (!NT_STATUS_IS_OK(status
)) {
1561 DEBUG(5, ("Failed to auto-add domain administrators to "
1562 "BUILTIN\\Administrators during join: %s\n",
1563 nt_errstr(status
)));
1566 /* Try adding dom users to builtin\users. Only log failures. */
1567 status
= create_builtin_users(domain_sid
);
1568 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1569 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1570 "during join because winbindd must be running."));
1571 } else if (!NT_STATUS_IS_OK(status
)) {
1572 DEBUG(5, ("Failed to auto-add domain administrators to "
1573 "BUILTIN\\Administrators during join: %s\n",
1574 nt_errstr(status
)));
1578 /****************************************************************
1579 ****************************************************************/
1581 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1582 struct libnet_JoinCtx
*r
)
1586 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1587 return r
->out
.result
;
1590 werr
= do_JoinConfig(r
);
1591 if (!W_ERROR_IS_OK(werr
)) {
1595 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1599 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1600 if (r
->out
.dns_domain_name
) {
1601 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1605 if (r
->out
.domain_is_ad
&&
1606 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1607 ADS_STATUS ads_status
;
1609 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1610 if (!ADS_ERR_OK(ads_status
)) {
1611 return WERR_GENERAL_FAILURE
;
1614 #endif /* WITH_ADS */
1616 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1621 /****************************************************************
1622 ****************************************************************/
1624 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1626 const char *krb5_cc_env
= NULL
;
1629 ads_destroy(&r
->in
.ads
);
1632 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1633 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1634 unsetenv(KRB5_ENV_CCNAME
);
1640 /****************************************************************
1641 ****************************************************************/
1643 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1645 const char *krb5_cc_env
= NULL
;
1648 ads_destroy(&r
->in
.ads
);
1651 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1652 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1653 unsetenv(KRB5_ENV_CCNAME
);
1659 /****************************************************************
1660 ****************************************************************/
1662 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1663 struct libnet_JoinCtx
**r
)
1665 struct libnet_JoinCtx
*ctx
;
1666 const char *krb5_cc_env
= NULL
;
1668 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1673 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1675 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1676 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1678 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1679 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1680 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1681 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1682 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1685 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1692 /****************************************************************
1693 ****************************************************************/
1695 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1696 struct libnet_UnjoinCtx
**r
)
1698 struct libnet_UnjoinCtx
*ctx
;
1699 const char *krb5_cc_env
= NULL
;
1701 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1706 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1708 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1709 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1711 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1712 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1713 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1714 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1715 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1723 /****************************************************************
1724 ****************************************************************/
1726 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1727 struct libnet_JoinCtx
*r
)
1729 bool valid_security
= false;
1730 bool valid_workgroup
= false;
1731 bool valid_realm
= false;
1733 /* check if configuration is already set correctly */
1735 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1737 switch (r
->out
.domain_is_ad
) {
1739 valid_security
= (lp_security() == SEC_DOMAIN
);
1740 if (valid_workgroup
&& valid_security
) {
1741 /* nothing to be done */
1746 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1747 switch (lp_security()) {
1750 valid_security
= true;
1753 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1754 /* nothing to be done */
1760 /* check if we are supposed to manipulate configuration */
1762 if (!r
->in
.modify_config
) {
1764 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1766 if (!valid_workgroup
) {
1767 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1768 "\"workgroup\" set to '%s', should be '%s'",
1769 lp_workgroup(), r
->out
.netbios_domain_name
);
1770 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1774 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1775 "\"realm\" set to '%s', should be '%s'",
1776 lp_realm(), r
->out
.dns_domain_name
);
1777 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1780 if (!valid_security
) {
1781 const char *sec
= NULL
;
1782 switch (lp_security()) {
1783 case SEC_SHARE
: sec
= "share"; break;
1784 case SEC_USER
: sec
= "user"; break;
1785 case SEC_DOMAIN
: sec
= "domain"; break;
1786 case SEC_ADS
: sec
= "ads"; break;
1788 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1789 "\"security\" set to '%s', should be %s",
1790 sec
, r
->out
.domain_is_ad
?
1791 "either 'domain' or 'ads'" : "'domain'");
1792 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1795 libnet_join_set_error_string(mem_ctx
, r
,
1796 "Invalid configuration (%s) and configuration modification "
1797 "was not requested", wrong_conf
);
1798 return WERR_CAN_NOT_COMPLETE
;
1801 /* check if we are able to manipulate configuration */
1803 if (!lp_config_backend_is_registry()) {
1804 libnet_join_set_error_string(mem_ctx
, r
,
1805 "Configuration manipulation requested but not "
1806 "supported by backend");
1807 return WERR_NOT_SUPPORTED
;
1813 /****************************************************************
1814 ****************************************************************/
1816 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1817 struct libnet_JoinCtx
*r
)
1821 struct cli_state
*cli
= NULL
;
1823 ADS_STATUS ads_status
;
1824 #endif /* WITH_ADS */
1826 if (!r
->in
.dc_name
) {
1827 struct netr_DsRGetDCNameInfo
*info
;
1829 status
= dsgetdcname(mem_ctx
,
1834 DS_FORCE_REDISCOVERY
|
1835 DS_DIRECTORY_SERVICE_REQUIRED
|
1836 DS_WRITABLE_REQUIRED
|
1839 if (!NT_STATUS_IS_OK(status
)) {
1840 libnet_join_set_error_string(mem_ctx
, r
,
1841 "failed to find DC for domain %s",
1843 get_friendly_nt_error_msg(status
));
1844 return WERR_DCNOTFOUND
;
1847 dc
= strip_hostname(info
->dc_unc
);
1848 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1849 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1852 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1853 if (!NT_STATUS_IS_OK(status
)) {
1854 libnet_join_set_error_string(mem_ctx
, r
,
1855 "failed to lookup DC info for domain '%s' over rpc: %s",
1856 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1857 return ntstatus_to_werror(status
);
1860 werr
= libnet_join_check_config(mem_ctx
, r
);
1861 if (!W_ERROR_IS_OK(werr
)) {
1866 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
1867 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1869 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1870 if (!ADS_ERR_OK(ads_status
)) {
1871 return WERR_DEFAULT_JOIN_REQUIRED
;
1874 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1875 if (!ADS_ERR_OK(ads_status
)) {
1876 libnet_join_set_error_string(mem_ctx
, r
,
1877 "failed to precreate account in ou %s: %s",
1879 ads_errstr(ads_status
));
1880 return WERR_DEFAULT_JOIN_REQUIRED
;
1883 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1885 #endif /* WITH_ADS */
1887 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
1888 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
1889 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
1891 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1893 if (!NT_STATUS_IS_OK(status
)) {
1894 libnet_join_set_error_string(mem_ctx
, r
,
1895 "failed to join domain '%s' over rpc: %s",
1896 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1897 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1898 return WERR_SETUP_ALREADY_JOINED
;
1900 werr
= ntstatus_to_werror(status
);
1904 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1905 werr
= WERR_SETUP_NOT_JOINED
;
1919 /****************************************************************
1920 ****************************************************************/
1922 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
1923 struct libnet_JoinCtx
*r
)
1926 struct libnet_UnjoinCtx
*u
= NULL
;
1928 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
1929 if (!W_ERROR_IS_OK(werr
)) {
1933 u
->in
.debug
= r
->in
.debug
;
1934 u
->in
.dc_name
= r
->in
.dc_name
;
1935 u
->in
.domain_name
= r
->in
.domain_name
;
1936 u
->in
.admin_account
= r
->in
.admin_account
;
1937 u
->in
.admin_password
= r
->in
.admin_password
;
1938 u
->in
.modify_config
= r
->in
.modify_config
;
1939 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1940 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1942 werr
= libnet_Unjoin(mem_ctx
, u
);
1948 /****************************************************************
1949 ****************************************************************/
1951 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1952 struct libnet_JoinCtx
*r
)
1957 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1960 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1961 if (!W_ERROR_IS_OK(werr
)) {
1965 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1966 werr
= libnet_DomainJoin(mem_ctx
, r
);
1967 if (!W_ERROR_IS_OK(werr
)) {
1972 werr
= libnet_join_post_processing(mem_ctx
, r
);
1973 if (!W_ERROR_IS_OK(werr
)) {
1977 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1978 werr
= libnet_join_post_verify(mem_ctx
, r
);
1979 if (!W_ERROR_IS_OK(werr
)) {
1980 libnet_join_rollback(mem_ctx
, r
);
1985 r
->out
.result
= werr
;
1988 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1993 /****************************************************************
1994 ****************************************************************/
1996 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
1997 struct libnet_UnjoinCtx
*r
)
2001 if (!r
->in
.domain_sid
) {
2003 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2004 libnet_unjoin_set_error_string(mem_ctx
, r
,
2005 "Unable to fetch domain sid: are we joined?");
2006 return WERR_SETUP_NOT_JOINED
;
2008 r
->in
.domain_sid
= sid_dup_talloc(mem_ctx
, &sid
);
2009 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2012 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2013 !r
->in
.delete_machine_account
) {
2014 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2018 if (!r
->in
.dc_name
) {
2019 struct netr_DsRGetDCNameInfo
*info
;
2021 status
= dsgetdcname(mem_ctx
,
2026 DS_DIRECTORY_SERVICE_REQUIRED
|
2027 DS_WRITABLE_REQUIRED
|
2030 if (!NT_STATUS_IS_OK(status
)) {
2031 libnet_unjoin_set_error_string(mem_ctx
, r
,
2032 "failed to find DC for domain %s",
2034 get_friendly_nt_error_msg(status
));
2035 return WERR_DCNOTFOUND
;
2038 dc
= strip_hostname(info
->dc_unc
);
2039 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2040 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2044 /* for net ads leave, try to delete the account. If it works,
2045 no sense in disabling. If it fails, we can still try to
2048 if (r
->in
.delete_machine_account
) {
2049 ADS_STATUS ads_status
;
2050 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2051 if (ADS_ERR_OK(ads_status
)) {
2053 r
->out
.dns_domain_name
=
2054 talloc_strdup(mem_ctx
,
2055 r
->in
.ads
->server
.realm
);
2057 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2059 if (!ADS_ERR_OK(ads_status
)) {
2060 libnet_unjoin_set_error_string(mem_ctx
, r
,
2061 "failed to remove machine account from AD: %s",
2062 ads_errstr(ads_status
));
2064 r
->out
.deleted_machine_account
= true;
2065 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2066 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2070 #endif /* WITH_ADS */
2072 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2074 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2075 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2076 if (!NT_STATUS_IS_OK(status
)) {
2077 libnet_unjoin_set_error_string(mem_ctx
, r
,
2078 "failed to disable machine account via rpc: %s",
2079 get_friendly_nt_error_msg(status
));
2080 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2081 return WERR_SETUP_NOT_JOINED
;
2083 return ntstatus_to_werror(status
);
2086 r
->out
.disabled_machine_account
= true;
2089 /* If disable succeeded or was not requested at all, we
2090 should be getting rid of our end of things */
2092 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2097 /****************************************************************
2098 ****************************************************************/
2100 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2101 struct libnet_UnjoinCtx
*r
)
2103 if (!r
->in
.domain_name
) {
2104 libnet_unjoin_set_error_string(mem_ctx
, r
,
2105 "No domain name defined");
2106 return WERR_INVALID_PARAM
;
2109 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2112 libnet_unjoin_set_error_string(mem_ctx
, r
,
2113 "Failed to parse domain name");
2114 return WERR_INVALID_PARAM
;
2118 return WERR_SETUP_DOMAIN_CONTROLLER
;
2121 if (!secrets_init()) {
2122 libnet_unjoin_set_error_string(mem_ctx
, r
,
2123 "Unable to open secrets database");
2124 return WERR_CAN_NOT_COMPLETE
;
2130 /****************************************************************
2131 ****************************************************************/
2133 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2134 struct libnet_UnjoinCtx
*r
)
2136 saf_delete(r
->out
.netbios_domain_name
);
2137 saf_delete(r
->out
.dns_domain_name
);
2139 return libnet_unjoin_config(r
);
2142 /****************************************************************
2143 ****************************************************************/
2145 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2146 struct libnet_UnjoinCtx
*r
)
2151 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2154 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2155 if (!W_ERROR_IS_OK(werr
)) {
2159 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2160 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2161 if (!W_ERROR_IS_OK(werr
)) {
2162 libnet_unjoin_config(r
);
2167 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2168 if (!W_ERROR_IS_OK(werr
)) {
2173 r
->out
.result
= werr
;
2176 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);