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"
24 /****************************************************************
25 ****************************************************************/
27 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
30 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
31 DEBUG(1,("libnet_Join:\n%s", str)); \
35 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
36 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
37 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
38 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
40 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
43 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
44 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
48 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
49 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
50 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
51 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
53 #define W_ERROR_NOT_OK_GOTO_DONE(x) do { \
54 if (!W_ERROR_IS_OK(x)) {\
59 /****************************************************************
60 ****************************************************************/
62 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
63 struct libnet_JoinCtx
*r
,
64 const char *format
, ...)
68 if (r
->out
.error_string
) {
72 va_start(args
, format
);
73 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
77 /****************************************************************
78 ****************************************************************/
80 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
81 struct libnet_UnjoinCtx
*r
,
82 const char *format
, ...)
86 if (r
->out
.error_string
) {
90 va_start(args
, format
);
91 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
97 /****************************************************************
98 ****************************************************************/
100 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
101 const char *netbios_domain_name
,
103 const char *user_name
,
104 const char *password
,
108 ADS_STRUCT
*my_ads
= NULL
;
110 my_ads
= ads_init(dns_domain_name
,
114 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
118 SAFE_FREE(my_ads
->auth
.user_name
);
119 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
123 SAFE_FREE(my_ads
->auth
.password
);
124 my_ads
->auth
.password
= SMB_STRDUP(password
);
127 status
= ads_connect_user_creds(my_ads
);
128 if (!ADS_ERR_OK(status
)) {
129 ads_destroy(&my_ads
);
137 /****************************************************************
138 ****************************************************************/
140 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
141 struct libnet_JoinCtx
*r
)
145 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
146 r
->out
.netbios_domain_name
,
149 r
->in
.admin_password
,
151 if (!ADS_ERR_OK(status
)) {
152 libnet_join_set_error_string(mem_ctx
, r
,
153 "failed to connect to AD: %s",
158 if (!r
->out
.netbios_domain_name
) {
159 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
160 r
->in
.ads
->server
.workgroup
);
161 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
164 if (!r
->out
.dns_domain_name
) {
165 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
166 r
->in
.ads
->config
.realm
);
167 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
170 r
->out
.domain_is_ad
= true;
175 /****************************************************************
176 ****************************************************************/
178 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
179 struct libnet_UnjoinCtx
*r
)
183 status
= libnet_connect_ads(r
->in
.domain_name
,
187 r
->in
.admin_password
,
189 if (!ADS_ERR_OK(status
)) {
190 libnet_unjoin_set_error_string(mem_ctx
, r
,
191 "failed to connect to AD: %s",
198 /****************************************************************
199 join a domain using ADS (LDAP mods)
200 ****************************************************************/
202 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
203 struct libnet_JoinCtx
*r
)
206 LDAPMessage
*res
= NULL
;
207 const char *attrs
[] = { "dn", NULL
};
210 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
211 if (!ADS_ERR_OK(status
)) {
215 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
216 if (!ADS_ERR_OK(status
)) {
220 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
221 ads_msgfree(r
->in
.ads
, res
);
222 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
225 ads_msgfree(r
->in
.ads
, res
);
227 /* Attempt to create the machine account and bail if this fails.
228 Assume that the admin wants exactly what they requested */
230 status
= ads_create_machine_acct(r
->in
.ads
,
234 if (ADS_ERR_OK(status
)) {
235 DEBUG(1,("machine account creation created\n"));
237 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
238 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
239 status
= ADS_SUCCESS
;
242 if (!ADS_ERR_OK(status
)) {
243 DEBUG(1,("machine account creation failed\n"));
247 status
= ads_move_machine_acct(r
->in
.ads
,
251 if (!ADS_ERR_OK(status
)) {
252 DEBUG(1,("failure to locate/move pre-existing "
253 "machine account\n"));
257 DEBUG(1,("The machine account %s the specified OU.\n",
258 moved
? "was moved into" : "already exists in"));
263 /****************************************************************
264 ****************************************************************/
266 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
267 struct libnet_UnjoinCtx
*r
)
272 return libnet_unjoin_connect_ads(mem_ctx
, r
);
275 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
276 if (!ADS_ERR_OK(status
)) {
277 libnet_unjoin_set_error_string(mem_ctx
, r
,
278 "failed to leave realm: %s",
286 /****************************************************************
287 ****************************************************************/
289 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
290 struct libnet_JoinCtx
*r
)
293 LDAPMessage
*res
= NULL
;
296 if (!r
->in
.machine_name
) {
297 return ADS_ERROR(LDAP_NO_MEMORY
);
300 status
= ads_find_machine_acct(r
->in
.ads
,
303 if (!ADS_ERR_OK(status
)) {
307 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
308 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
312 dn
= ads_get_dn(r
->in
.ads
, res
);
314 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
318 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
320 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
325 ads_msgfree(r
->in
.ads
, res
);
326 ads_memfree(r
->in
.ads
, dn
);
331 /****************************************************************
332 Set a machines dNSHostName and servicePrincipalName attributes
333 ****************************************************************/
335 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
336 struct libnet_JoinCtx
*r
)
341 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
346 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
347 if (!ADS_ERR_OK(status
)) {
351 /* Windows only creates HOST/shortname & HOST/fqdn. */
353 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
355 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
360 if (name_to_fqdn(my_fqdn
, r
->in
.machine_name
) &&
361 !strequal(my_fqdn
, r
->in
.machine_name
)) {
364 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
366 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
371 mods
= ads_init_mods(mem_ctx
);
373 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
376 /* fields of primary importance */
378 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
379 if (!ADS_ERR_OK(status
)) {
380 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
383 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
385 if (!ADS_ERR_OK(status
)) {
386 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
389 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
392 /****************************************************************
393 ****************************************************************/
395 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
396 struct libnet_JoinCtx
*r
)
401 if (!r
->in
.create_upn
) {
407 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
408 if (!ADS_ERR_OK(status
)) {
413 r
->in
.upn
= talloc_asprintf(mem_ctx
,
416 r
->out
.dns_domain_name
);
418 return ADS_ERROR(LDAP_NO_MEMORY
);
422 /* now do the mods */
424 mods
= ads_init_mods(mem_ctx
);
426 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
429 /* fields of primary importance */
431 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
432 if (!ADS_ERR_OK(status
)) {
433 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
436 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
440 /****************************************************************
441 ****************************************************************/
443 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
444 struct libnet_JoinCtx
*r
)
450 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
456 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
457 if (!ADS_ERR_OK(status
)) {
461 /* now do the mods */
463 mods
= ads_init_mods(mem_ctx
);
465 return ADS_ERROR(LDAP_NO_MEMORY
);
468 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", SAMBA_VERSION_STRING
);
470 return ADS_ERROR(LDAP_NO_MEMORY
);
473 /* fields of primary importance */
475 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
477 if (!ADS_ERR_OK(status
)) {
481 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
483 if (!ADS_ERR_OK(status
)) {
487 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
489 if (!ADS_ERR_OK(status
)) {
493 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
496 /****************************************************************
497 ****************************************************************/
499 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
500 struct libnet_JoinCtx
*r
)
502 if (!lp_use_kerberos_keytab()) {
506 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
513 /****************************************************************
514 ****************************************************************/
516 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
517 struct libnet_JoinCtx
*r
)
519 uint32_t domain_func
;
521 const char *salt
= NULL
;
522 char *std_salt
= NULL
;
524 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
525 if (!ADS_ERR_OK(status
)) {
526 libnet_join_set_error_string(mem_ctx
, r
,
527 "failed to determine domain functional level: %s",
532 /* go ahead and setup the default salt */
534 std_salt
= kerberos_standard_des_salt();
536 libnet_join_set_error_string(mem_ctx
, r
,
537 "failed to obtain standard DES salt");
541 salt
= talloc_strdup(mem_ctx
, std_salt
);
548 /* if it's a Windows functional domain, we have to look for the UPN */
550 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
553 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
556 salt
= talloc_strdup(mem_ctx
, upn
);
563 return kerberos_secrets_store_des_salt(salt
);
566 /****************************************************************
567 ****************************************************************/
569 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
570 struct libnet_JoinCtx
*r
)
575 status
= libnet_join_connect_ads(mem_ctx
, r
);
576 if (!ADS_ERR_OK(status
)) {
581 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
582 if (!ADS_ERR_OK(status
)) {
583 libnet_join_set_error_string(mem_ctx
, r
,
584 "failed to set machine spn: %s",
589 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
590 if (!ADS_ERR_OK(status
)) {
591 libnet_join_set_error_string(mem_ctx
, r
,
592 "failed to set machine os attributes: %s",
597 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
598 if (!ADS_ERR_OK(status
)) {
599 libnet_join_set_error_string(mem_ctx
, r
,
600 "failed to set machine upn: %s",
605 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
606 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
609 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
610 libnet_join_set_error_string(mem_ctx
, r
,
611 "failed to create kerberos keytab");
612 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
617 #endif /* WITH_ADS */
619 /****************************************************************
620 Store the machine password and domain SID
621 ****************************************************************/
623 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
624 struct libnet_JoinCtx
*r
)
626 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
629 DEBUG(1,("Failed to save domain sid\n"));
633 if (!secrets_store_machine_password(r
->in
.machine_password
,
634 r
->out
.netbios_domain_name
,
635 r
->in
.secure_channel_type
))
637 DEBUG(1,("Failed to save machine password\n"));
644 /****************************************************************
645 Connect dc's IPC$ share
646 ****************************************************************/
648 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
652 struct cli_state
**cli
)
657 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
660 if (use_kerberos
&& pass
) {
661 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
664 return cli_full_connection(cli
, NULL
,
675 /****************************************************************
676 Lookup domain dc's info
677 ****************************************************************/
679 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
680 struct libnet_JoinCtx
*r
,
681 struct cli_state
**cli
)
683 struct rpc_pipe_client
*pipe_hnd
= NULL
;
685 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
686 union lsa_PolicyInformation
*info
= NULL
;
688 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
690 r
->in
.admin_password
,
693 if (!NT_STATUS_IS_OK(status
)) {
697 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
699 if (!NT_STATUS_IS_OK(status
)) {
700 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
705 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
706 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
707 if (!NT_STATUS_IS_OK(status
)) {
711 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
715 if (NT_STATUS_IS_OK(status
)) {
716 r
->out
.domain_is_ad
= true;
717 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
718 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
719 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
720 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->dns
.sid
);
721 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
724 if (!NT_STATUS_IS_OK(status
)) {
725 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
727 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
729 if (!NT_STATUS_IS_OK(status
)) {
733 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
734 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->account_domain
.sid
);
735 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
738 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
739 TALLOC_FREE(pipe_hnd
);
745 /****************************************************************
747 ****************************************************************/
749 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
750 struct libnet_JoinCtx
*r
,
751 struct cli_state
*cli
)
753 struct rpc_pipe_client
*pipe_hnd
= NULL
;
754 POLICY_HND sam_pol
, domain_pol
, user_pol
;
755 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
757 struct lsa_String lsa_acct_name
;
759 uint32_t acct_flags
= ACB_WSTRUST
;
760 uchar md4_trust_password
[16];
761 struct samr_Ids user_rids
;
762 struct samr_Ids name_types
;
763 union samr_UserInfo user_info
;
765 struct samr_CryptPassword crypt_pwd
;
766 struct samr_CryptPasswordEx crypt_pwd_ex
;
768 ZERO_STRUCT(sam_pol
);
769 ZERO_STRUCT(domain_pol
);
770 ZERO_STRUCT(user_pol
);
772 if (!r
->in
.machine_password
) {
773 r
->in
.machine_password
= talloc_strdup(mem_ctx
, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
));
774 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
777 /* Open the domain */
779 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
781 if (!NT_STATUS_IS_OK(status
)) {
782 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
787 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
789 SEC_RIGHTS_MAXIMUM_ALLOWED
,
791 if (!NT_STATUS_IS_OK(status
)) {
795 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
797 SEC_RIGHTS_MAXIMUM_ALLOWED
,
800 if (!NT_STATUS_IS_OK(status
)) {
804 /* Create domain user */
806 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
807 strlower_m(acct_name
);
809 init_lsa_String(&lsa_acct_name
, acct_name
);
811 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
812 uint32_t access_desired
=
813 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
814 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
815 SAMR_USER_ACCESS_SET_PASSWORD
|
816 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
817 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
818 uint32_t access_granted
= 0;
820 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
822 DEBUG(10,("Creating account with desired access mask: %d\n",
825 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
833 if (!NT_STATUS_IS_OK(status
) &&
834 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
836 DEBUG(10,("Creation of workstation account failed: %s\n",
839 /* If NT_STATUS_ACCESS_DENIED then we have a valid
840 username/password combo but the user does not have
841 administrator access. */
843 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
844 libnet_join_set_error_string(mem_ctx
, r
,
845 "User specified does not have "
846 "administrator privileges");
852 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
853 if (!(r
->in
.join_flags
&
854 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
859 /* We *must* do this.... don't ask... */
861 if (NT_STATUS_IS_OK(status
)) {
862 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
866 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
872 if (!NT_STATUS_IS_OK(status
)) {
876 if (name_types
.ids
[0] != SID_NAME_USER
) {
877 DEBUG(0,("%s is not a user account (type=%d)\n",
878 acct_name
, name_types
.ids
[0]));
879 status
= NT_STATUS_INVALID_WORKSTATION
;
883 user_rid
= user_rids
.ids
[0];
885 /* Open handle on user */
887 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
889 SEC_RIGHTS_MAXIMUM_ALLOWED
,
892 if (!NT_STATUS_IS_OK(status
)) {
896 /* Create a random machine account password and generate the hash */
898 E_md4hash(r
->in
.machine_password
, md4_trust_password
);
900 init_samr_CryptPasswordEx(r
->in
.machine_password
,
901 &cli
->user_session_key
,
904 /* Fill in the additional account flags now */
906 acct_flags
|= ACB_PWNOEXP
;
907 if (r
->out
.domain_is_ad
) {
908 #if !defined(ENCTYPE_ARCFOUR_HMAC)
909 acct_flags
|= ACB_USE_DES_KEY_ONLY
;
914 /* Set password and account flags on machine account */
916 ZERO_STRUCT(user_info
.info25
);
918 user_info
.info25
.info
.fields_present
= ACCT_NT_PWD_SET
|
920 SAMR_FIELD_ACCT_FLAGS
;
922 user_info
.info25
.info
.acct_flags
= acct_flags
;
923 memcpy(&user_info
.info25
.password
.data
, crypt_pwd_ex
.data
,
924 sizeof(crypt_pwd_ex
.data
));
926 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
931 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
933 /* retry with level 24 */
935 init_samr_CryptPassword(r
->in
.machine_password
,
936 &cli
->user_session_key
,
939 init_samr_user_info24(&user_info
.info24
, crypt_pwd
.data
, 24);
941 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
947 if (!NT_STATUS_IS_OK(status
)) {
949 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
952 libnet_join_set_error_string(mem_ctx
, r
,
953 "Failed to set password for machine account (%s)\n",
958 status
= NT_STATUS_OK
;
965 if (is_valid_policy_hnd(&sam_pol
)) {
966 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
968 if (is_valid_policy_hnd(&domain_pol
)) {
969 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
971 if (is_valid_policy_hnd(&user_pol
)) {
972 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
974 TALLOC_FREE(pipe_hnd
);
979 /****************************************************************
980 ****************************************************************/
982 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
983 const char *machine_name
,
986 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
987 struct cli_state
*cli
= NULL
;
988 struct rpc_pipe_client
*pipe_hnd
= NULL
;
989 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
991 char *machine_password
= NULL
;
992 char *machine_account
= NULL
;
995 return NT_STATUS_INVALID_PARAMETER
;
998 if (!secrets_init()) {
999 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1002 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1004 if (!machine_password
) {
1005 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1008 asprintf(&machine_account
, "%s$", machine_name
);
1009 if (!machine_account
) {
1010 SAFE_FREE(machine_password
);
1011 return NT_STATUS_NO_MEMORY
;
1014 status
= cli_full_connection(&cli
, NULL
,
1023 free(machine_account
);
1024 free(machine_password
);
1026 if (!NT_STATUS_IS_OK(status
)) {
1027 status
= cli_full_connection(&cli
, NULL
,
1038 if (!NT_STATUS_IS_OK(status
)) {
1042 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1043 &neg_flags
, &netlogon_pipe
);
1044 if (!NT_STATUS_IS_OK(status
)) {
1045 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1047 return NT_STATUS_OK
;
1050 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1051 "key from server %s for domain %s. Error was %s\n",
1052 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1057 if (!lp_client_schannel()) {
1059 return NT_STATUS_OK
;
1062 status
= cli_rpc_pipe_open_schannel_with_key(
1063 cli
, &ndr_table_netlogon
.syntax_id
, PIPE_AUTH_LEVEL_PRIVACY
,
1064 netbios_domain_name
, netlogon_pipe
->dc
, &pipe_hnd
);
1068 if (!NT_STATUS_IS_OK(status
)) {
1069 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1070 "on netlogon pipe to server %s for domain %s. "
1072 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1076 return NT_STATUS_OK
;
1079 /****************************************************************
1080 ****************************************************************/
1082 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1083 struct libnet_JoinCtx
*r
)
1087 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1090 if (!NT_STATUS_IS_OK(status
)) {
1091 libnet_join_set_error_string(mem_ctx
, r
,
1092 "failed to verify domain membership after joining: %s",
1093 get_friendly_nt_error_msg(status
));
1094 return WERR_SETUP_NOT_JOINED
;
1100 /****************************************************************
1101 ****************************************************************/
1103 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1104 struct libnet_UnjoinCtx
*r
)
1106 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1110 if (!secrets_delete_domain_sid(lp_workgroup())) {
1117 /****************************************************************
1118 ****************************************************************/
1120 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1121 struct libnet_UnjoinCtx
*r
)
1123 struct cli_state
*cli
= NULL
;
1124 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1125 POLICY_HND sam_pol
, domain_pol
, user_pol
;
1126 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1129 struct lsa_String lsa_acct_name
;
1130 struct samr_Ids user_rids
;
1131 struct samr_Ids name_types
;
1132 union samr_UserInfo
*info
= NULL
;
1134 ZERO_STRUCT(sam_pol
);
1135 ZERO_STRUCT(domain_pol
);
1136 ZERO_STRUCT(user_pol
);
1138 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1139 r
->in
.admin_account
,
1140 r
->in
.admin_password
,
1143 if (!NT_STATUS_IS_OK(status
)) {
1147 /* Open the domain */
1149 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1151 if (!NT_STATUS_IS_OK(status
)) {
1152 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1153 nt_errstr(status
)));
1157 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1159 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1161 if (!NT_STATUS_IS_OK(status
)) {
1165 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1167 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1170 if (!NT_STATUS_IS_OK(status
)) {
1174 /* Create domain user */
1176 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1177 strlower_m(acct_name
);
1179 init_lsa_String(&lsa_acct_name
, acct_name
);
1181 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1188 if (!NT_STATUS_IS_OK(status
)) {
1192 if (name_types
.ids
[0] != SID_NAME_USER
) {
1193 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1194 name_types
.ids
[0]));
1195 status
= NT_STATUS_INVALID_WORKSTATION
;
1199 user_rid
= user_rids
.ids
[0];
1201 /* Open handle on user */
1203 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1205 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1208 if (!NT_STATUS_IS_OK(status
)) {
1214 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1218 if (!NT_STATUS_IS_OK(status
)) {
1219 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1223 /* now disable and setuser info */
1225 info
->info16
.acct_flags
|= ACB_DISABLED
;
1227 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1232 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1236 if (is_valid_policy_hnd(&domain_pol
)) {
1237 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1239 if (is_valid_policy_hnd(&sam_pol
)) {
1240 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1242 TALLOC_FREE(pipe_hnd
);
1252 /****************************************************************
1253 ****************************************************************/
1255 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1258 struct smbconf_ctx
*ctx
;
1260 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1261 if (!W_ERROR_IS_OK(werr
)) {
1265 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1267 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1268 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1270 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1273 smbconf_delete_global_parameter(ctx
, "realm");
1277 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1278 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1280 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1281 r
->out
.netbios_domain_name
);
1282 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1284 if (r
->out
.domain_is_ad
) {
1285 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1286 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1288 werr
= smbconf_set_global_parameter(ctx
, "realm",
1289 r
->out
.dns_domain_name
);
1290 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1294 smbconf_shutdown(ctx
);
1298 /****************************************************************
1299 ****************************************************************/
1301 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1303 WERROR werr
= WERR_OK
;
1304 struct smbconf_ctx
*ctx
;
1306 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1307 if (!W_ERROR_IS_OK(werr
)) {
1311 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1313 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1314 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1316 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1317 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1319 smbconf_delete_global_parameter(ctx
, "realm");
1323 smbconf_shutdown(ctx
);
1327 /****************************************************************
1328 ****************************************************************/
1330 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1334 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1335 return r
->out
.result
;
1338 if (!r
->in
.modify_config
) {
1342 werr
= do_join_modify_vals_config(r
);
1343 if (!W_ERROR_IS_OK(werr
)) {
1347 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1349 r
->out
.modified_config
= true;
1350 r
->out
.result
= werr
;
1355 /****************************************************************
1356 ****************************************************************/
1358 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1362 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1363 return r
->out
.result
;
1366 if (!r
->in
.modify_config
) {
1370 werr
= do_unjoin_modify_vals_config(r
);
1371 if (!W_ERROR_IS_OK(werr
)) {
1375 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1377 r
->out
.modified_config
= true;
1378 r
->out
.result
= werr
;
1383 /****************************************************************
1384 ****************************************************************/
1386 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1387 const char *domain_str
,
1388 const char **domain_p
,
1391 char *domain
= NULL
;
1393 const char *p
= NULL
;
1395 if (!domain_str
|| !domain_p
|| !dc_p
) {
1399 p
= strchr_m(domain_str
, '\\');
1402 domain
= talloc_strndup(mem_ctx
, domain_str
,
1403 PTR_DIFF(p
, domain_str
));
1404 dc
= talloc_strdup(mem_ctx
, p
+1);
1409 domain
= talloc_strdup(mem_ctx
, domain_str
);
1425 /****************************************************************
1426 ****************************************************************/
1428 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1429 struct libnet_JoinCtx
*r
)
1431 if (!r
->in
.domain_name
) {
1432 libnet_join_set_error_string(mem_ctx
, r
,
1433 "No domain name defined");
1434 return WERR_INVALID_PARAM
;
1437 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1440 libnet_join_set_error_string(mem_ctx
, r
,
1441 "Failed to parse domain name");
1442 return WERR_INVALID_PARAM
;
1446 return WERR_SETUP_DOMAIN_CONTROLLER
;
1449 if (!secrets_init()) {
1450 libnet_join_set_error_string(mem_ctx
, r
,
1451 "Unable to open secrets database");
1452 return WERR_CAN_NOT_COMPLETE
;
1458 /****************************************************************
1459 ****************************************************************/
1461 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1465 /* Try adding dom admins to builtin\admins. Only log failures. */
1466 status
= create_builtin_administrators(domain_sid
);
1467 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1468 DEBUG(10,("Unable to auto-add domain administrators to "
1469 "BUILTIN\\Administrators during join because "
1470 "winbindd must be running."));
1471 } else if (!NT_STATUS_IS_OK(status
)) {
1472 DEBUG(5, ("Failed to auto-add domain administrators to "
1473 "BUILTIN\\Administrators during join: %s\n",
1474 nt_errstr(status
)));
1477 /* Try adding dom users to builtin\users. Only log failures. */
1478 status
= create_builtin_users(domain_sid
);
1479 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1480 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1481 "during join because winbindd must be running."));
1482 } else if (!NT_STATUS_IS_OK(status
)) {
1483 DEBUG(5, ("Failed to auto-add domain administrators to "
1484 "BUILTIN\\Administrators during join: %s\n",
1485 nt_errstr(status
)));
1489 /****************************************************************
1490 ****************************************************************/
1492 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1493 struct libnet_JoinCtx
*r
)
1497 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1498 return r
->out
.result
;
1501 werr
= do_JoinConfig(r
);
1502 if (!W_ERROR_IS_OK(werr
)) {
1506 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1507 saf_store(r
->in
.domain_name
, r
->in
.dc_name
);
1510 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1515 /****************************************************************
1516 ****************************************************************/
1518 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1520 const char *krb5_cc_env
= NULL
;
1523 ads_destroy(&r
->in
.ads
);
1526 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1527 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1528 unsetenv(KRB5_ENV_CCNAME
);
1534 /****************************************************************
1535 ****************************************************************/
1537 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1539 const char *krb5_cc_env
= NULL
;
1542 ads_destroy(&r
->in
.ads
);
1545 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1546 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1547 unsetenv(KRB5_ENV_CCNAME
);
1553 /****************************************************************
1554 ****************************************************************/
1556 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1557 struct libnet_JoinCtx
**r
)
1559 struct libnet_JoinCtx
*ctx
;
1560 const char *krb5_cc_env
= NULL
;
1562 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1567 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1569 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1570 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1572 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1573 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1574 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1575 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1576 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1579 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1586 /****************************************************************
1587 ****************************************************************/
1589 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1590 struct libnet_UnjoinCtx
**r
)
1592 struct libnet_UnjoinCtx
*ctx
;
1593 const char *krb5_cc_env
= NULL
;
1595 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1600 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1602 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1603 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1605 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1606 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1607 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1608 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1609 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1617 /****************************************************************
1618 ****************************************************************/
1620 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1621 struct libnet_JoinCtx
*r
)
1623 /* check if configuration is already set correctly */
1625 switch (r
->out
.domain_is_ad
) {
1627 if ((strequal(lp_workgroup(),
1628 r
->out
.netbios_domain_name
)) &&
1629 (lp_security() == SEC_DOMAIN
)) {
1630 /* nothing to be done */
1635 if ((strequal(lp_workgroup(),
1636 r
->out
.netbios_domain_name
)) &&
1637 (strequal(lp_realm(),
1638 r
->out
.dns_domain_name
)) &&
1639 ((lp_security() == SEC_ADS
) ||
1640 (lp_security() == SEC_DOMAIN
))) {
1641 /* nothing to be done */
1647 /* check if we are supposed to manipulate configuration */
1649 if (!r
->in
.modify_config
) {
1650 libnet_join_set_error_string(mem_ctx
, r
,
1651 "Invalid configuration and configuration modification "
1652 "was not requested");
1653 return WERR_CAN_NOT_COMPLETE
;
1656 /* check if we are able to manipulate configuration */
1658 if (!lp_config_backend_is_registry()) {
1659 libnet_join_set_error_string(mem_ctx
, r
,
1660 "Configuration manipulation requested but not "
1661 "supported by backend");
1662 return WERR_NOT_SUPPORTED
;
1668 /****************************************************************
1669 ****************************************************************/
1671 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1672 struct libnet_JoinCtx
*r
)
1676 struct cli_state
*cli
= NULL
;
1678 ADS_STATUS ads_status
;
1679 #endif /* WITH_ADS */
1681 if (!r
->in
.dc_name
) {
1682 struct netr_DsRGetDCNameInfo
*info
;
1684 status
= dsgetdcname(mem_ctx
,
1689 DS_DIRECTORY_SERVICE_REQUIRED
|
1690 DS_WRITABLE_REQUIRED
|
1693 if (!NT_STATUS_IS_OK(status
)) {
1694 libnet_join_set_error_string(mem_ctx
, r
,
1695 "failed to find DC for domain %s",
1697 get_friendly_nt_error_msg(status
));
1698 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1701 dc
= strip_hostname(info
->dc_unc
);
1702 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1703 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1706 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1707 if (!NT_STATUS_IS_OK(status
)) {
1708 libnet_join_set_error_string(mem_ctx
, r
,
1709 "failed to lookup DC info for domain '%s' over rpc: %s",
1710 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1711 return ntstatus_to_werror(status
);
1714 werr
= libnet_join_check_config(mem_ctx
, r
);
1715 if (!W_ERROR_IS_OK(werr
)) {
1720 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
) {
1722 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1723 if (!ADS_ERR_OK(ads_status
)) {
1724 return WERR_DEFAULT_JOIN_REQUIRED
;
1727 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1728 if (!ADS_ERR_OK(ads_status
)) {
1729 libnet_join_set_error_string(mem_ctx
, r
,
1730 "failed to precreate account in ou %s: %s",
1732 ads_errstr(ads_status
));
1733 return WERR_DEFAULT_JOIN_REQUIRED
;
1736 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1738 #endif /* WITH_ADS */
1740 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1741 if (!NT_STATUS_IS_OK(status
)) {
1742 libnet_join_set_error_string(mem_ctx
, r
,
1743 "failed to join domain '%s' over rpc: %s",
1744 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1745 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1746 return WERR_SETUP_ALREADY_JOINED
;
1748 werr
= ntstatus_to_werror(status
);
1752 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1753 werr
= WERR_SETUP_NOT_JOINED
;
1758 if (r
->out
.domain_is_ad
) {
1759 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1760 if (!ADS_ERR_OK(ads_status
)) {
1761 werr
= WERR_GENERAL_FAILURE
;
1765 #endif /* WITH_ADS */
1777 /****************************************************************
1778 ****************************************************************/
1780 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
1781 struct libnet_JoinCtx
*r
)
1784 struct libnet_UnjoinCtx
*u
= NULL
;
1786 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
1787 if (!W_ERROR_IS_OK(werr
)) {
1791 u
->in
.debug
= r
->in
.debug
;
1792 u
->in
.dc_name
= r
->in
.dc_name
;
1793 u
->in
.domain_name
= r
->in
.domain_name
;
1794 u
->in
.admin_account
= r
->in
.admin_account
;
1795 u
->in
.admin_password
= r
->in
.admin_password
;
1796 u
->in
.modify_config
= r
->in
.modify_config
;
1797 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1798 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1800 werr
= libnet_Unjoin(mem_ctx
, u
);
1806 /****************************************************************
1807 ****************************************************************/
1809 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1810 struct libnet_JoinCtx
*r
)
1815 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1818 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1819 if (!W_ERROR_IS_OK(werr
)) {
1823 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1824 werr
= libnet_DomainJoin(mem_ctx
, r
);
1825 if (!W_ERROR_IS_OK(werr
)) {
1830 werr
= libnet_join_post_processing(mem_ctx
, r
);
1831 if (!W_ERROR_IS_OK(werr
)) {
1835 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1836 werr
= libnet_join_post_verify(mem_ctx
, r
);
1837 if (!W_ERROR_IS_OK(werr
)) {
1838 libnet_join_rollback(mem_ctx
, r
);
1843 r
->out
.result
= werr
;
1846 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1851 /****************************************************************
1852 ****************************************************************/
1854 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
1855 struct libnet_UnjoinCtx
*r
)
1859 if (!r
->in
.domain_sid
) {
1861 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
1862 libnet_unjoin_set_error_string(mem_ctx
, r
,
1863 "Unable to fetch domain sid: are we joined?");
1864 return WERR_SETUP_NOT_JOINED
;
1866 r
->in
.domain_sid
= sid_dup_talloc(mem_ctx
, &sid
);
1867 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
1870 if (!r
->in
.dc_name
) {
1871 struct netr_DsRGetDCNameInfo
*info
;
1873 status
= dsgetdcname(mem_ctx
,
1878 DS_DIRECTORY_SERVICE_REQUIRED
|
1879 DS_WRITABLE_REQUIRED
|
1882 if (!NT_STATUS_IS_OK(status
)) {
1883 libnet_unjoin_set_error_string(mem_ctx
, r
,
1884 "failed to find DC for domain %s",
1886 get_friendly_nt_error_msg(status
));
1887 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1890 dc
= strip_hostname(info
->dc_unc
);
1891 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1892 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1895 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
1896 if (!NT_STATUS_IS_OK(status
)) {
1897 libnet_unjoin_set_error_string(mem_ctx
, r
,
1898 "failed to disable machine account via rpc: %s",
1899 get_friendly_nt_error_msg(status
));
1900 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
1901 return WERR_SETUP_NOT_JOINED
;
1903 return ntstatus_to_werror(status
);
1906 r
->out
.disabled_machine_account
= true;
1909 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
1910 ADS_STATUS ads_status
;
1911 libnet_unjoin_connect_ads(mem_ctx
, r
);
1912 ads_status
= libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
1913 if (!ADS_ERR_OK(ads_status
)) {
1914 libnet_unjoin_set_error_string(mem_ctx
, r
,
1915 "failed to remove machine account from AD: %s",
1916 ads_errstr(ads_status
));
1918 r
->out
.deleted_machine_account
= true;
1920 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
1921 r
->in
.ads
->server
.realm
);
1922 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
1925 #endif /* WITH_ADS */
1927 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
1932 /****************************************************************
1933 ****************************************************************/
1935 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
1936 struct libnet_UnjoinCtx
*r
)
1938 if (!r
->in
.domain_name
) {
1939 libnet_unjoin_set_error_string(mem_ctx
, r
,
1940 "No domain name defined");
1941 return WERR_INVALID_PARAM
;
1944 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1947 libnet_unjoin_set_error_string(mem_ctx
, r
,
1948 "Failed to parse domain name");
1949 return WERR_INVALID_PARAM
;
1953 return WERR_SETUP_DOMAIN_CONTROLLER
;
1956 if (!secrets_init()) {
1957 libnet_unjoin_set_error_string(mem_ctx
, r
,
1958 "Unable to open secrets database");
1959 return WERR_CAN_NOT_COMPLETE
;
1965 /****************************************************************
1966 ****************************************************************/
1968 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
1969 struct libnet_UnjoinCtx
*r
)
1971 saf_delete(r
->out
.netbios_domain_name
);
1972 saf_delete(r
->out
.dns_domain_name
);
1974 return libnet_unjoin_config(r
);
1977 /****************************************************************
1978 ****************************************************************/
1980 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
1981 struct libnet_UnjoinCtx
*r
)
1986 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
1989 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
1990 if (!W_ERROR_IS_OK(werr
)) {
1994 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1995 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
1996 if (!W_ERROR_IS_OK(werr
)) {
1997 libnet_unjoin_config(r
);
2002 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2003 if (!W_ERROR_IS_OK(werr
)) {
2008 r
->out
.result
= werr
;
2011 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);