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(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
)) {
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 Lookup domain dc's info
646 ****************************************************************/
648 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
649 struct libnet_JoinCtx
*r
,
650 struct cli_state
**cli
)
652 struct rpc_pipe_client
*pipe_hnd
= NULL
;
654 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
655 union lsa_PolicyInformation
*info
= NULL
;
657 status
= cli_full_connection(cli
, NULL
,
663 r
->in
.admin_password
,
667 if (!NT_STATUS_IS_OK(status
)) {
671 pipe_hnd
= cli_rpc_pipe_open_noauth(*cli
, PI_LSARPC
, &status
);
673 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
678 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
679 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
680 if (!NT_STATUS_IS_OK(status
)) {
684 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
688 if (NT_STATUS_IS_OK(status
)) {
689 r
->out
.domain_is_ad
= true;
690 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
691 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
692 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
693 r
->out
.domain_sid
= info
->dns
.sid
;
696 if (!NT_STATUS_IS_OK(status
)) {
697 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
699 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
701 if (!NT_STATUS_IS_OK(status
)) {
705 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
706 r
->out
.domain_sid
= info
->account_domain
.sid
;
709 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
710 TALLOC_FREE(pipe_hnd
);
716 /****************************************************************
718 ****************************************************************/
720 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
721 struct libnet_JoinCtx
*r
,
722 struct cli_state
*cli
)
724 struct rpc_pipe_client
*pipe_hnd
= NULL
;
725 POLICY_HND sam_pol
, domain_pol
, user_pol
;
726 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
728 struct lsa_String lsa_acct_name
;
730 uint32_t acct_flags
= ACB_WSTRUST
;
732 struct MD5Context md5ctx
;
734 DATA_BLOB digested_session_key
;
735 uchar md4_trust_password
[16];
736 struct samr_Ids user_rids
;
737 struct samr_Ids name_types
;
738 union samr_UserInfo user_info
;
740 ZERO_STRUCT(sam_pol
);
741 ZERO_STRUCT(domain_pol
);
742 ZERO_STRUCT(user_pol
);
744 if (!r
->in
.machine_password
) {
745 r
->in
.machine_password
= talloc_strdup(mem_ctx
, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
));
746 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
749 /* Open the domain */
751 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &status
);
753 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
758 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
760 SEC_RIGHTS_MAXIMUM_ALLOWED
,
762 if (!NT_STATUS_IS_OK(status
)) {
766 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
768 SEC_RIGHTS_MAXIMUM_ALLOWED
,
771 if (!NT_STATUS_IS_OK(status
)) {
775 /* Create domain user */
777 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
778 strlower_m(acct_name
);
780 init_lsa_String(&lsa_acct_name
, acct_name
);
782 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
783 uint32_t access_desired
=
784 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
785 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
786 SAMR_USER_ACCESS_SET_PASSWORD
|
787 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
788 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
789 uint32_t access_granted
= 0;
791 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
793 DEBUG(10,("Creating account with desired access mask: %d\n",
796 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
804 if (!NT_STATUS_IS_OK(status
) &&
805 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
807 DEBUG(10,("Creation of workstation account failed: %s\n",
810 /* If NT_STATUS_ACCESS_DENIED then we have a valid
811 username/password combo but the user does not have
812 administrator access. */
814 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
815 libnet_join_set_error_string(mem_ctx
, r
,
816 "User specified does not have "
817 "administrator privileges");
823 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
824 if (!(r
->in
.join_flags
&
825 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
830 /* We *must* do this.... don't ask... */
832 if (NT_STATUS_IS_OK(status
)) {
833 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
837 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
843 if (!NT_STATUS_IS_OK(status
)) {
847 if (name_types
.ids
[0] != SID_NAME_USER
) {
848 DEBUG(0,("%s is not a user account (type=%d)\n",
849 acct_name
, name_types
.ids
[0]));
850 status
= NT_STATUS_INVALID_WORKSTATION
;
854 user_rid
= user_rids
.ids
[0];
856 /* Open handle on user */
858 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
860 SEC_RIGHTS_MAXIMUM_ALLOWED
,
863 if (!NT_STATUS_IS_OK(status
)) {
867 /* Create a random machine account password and generate the hash */
869 E_md4hash(r
->in
.machine_password
, md4_trust_password
);
870 encode_pw_buffer(pwbuf
, r
->in
.machine_password
, STR_UNICODE
);
872 generate_random_buffer((uint8_t*)md5buffer
, sizeof(md5buffer
));
873 digested_session_key
= data_blob_talloc(mem_ctx
, 0, 16);
876 MD5Update(&md5ctx
, md5buffer
, sizeof(md5buffer
));
877 MD5Update(&md5ctx
, cli
->user_session_key
.data
,
878 cli
->user_session_key
.length
);
879 MD5Final(digested_session_key
.data
, &md5ctx
);
881 SamOEMhashBlob(pwbuf
, sizeof(pwbuf
), &digested_session_key
);
882 memcpy(&pwbuf
[516], md5buffer
, sizeof(md5buffer
));
884 /* Fill in the additional account flags now */
886 acct_flags
|= ACB_PWNOEXP
;
887 if (r
->out
.domain_is_ad
) {
888 #if !defined(ENCTYPE_ARCFOUR_HMAC)
889 acct_flags
|= ACB_USE_DES_KEY_ONLY
;
894 /* Set password and account flags on machine account */
896 ZERO_STRUCT(user_info
.info25
);
898 user_info
.info25
.info
.fields_present
= ACCT_NT_PWD_SET
|
900 SAMR_FIELD_ACCT_FLAGS
;
902 user_info
.info25
.info
.acct_flags
= acct_flags
;
903 memcpy(&user_info
.info25
.password
.data
, pwbuf
, sizeof(pwbuf
));
905 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
910 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
914 encode_pw_buffer(pwbuf2
, r
->in
.machine_password
, STR_UNICODE
);
916 /* retry with level 24 */
917 init_samr_user_info24(&user_info
.info24
, pwbuf2
, 24);
919 SamOEMhashBlob(user_info
.info24
.password
.data
, 516,
920 &cli
->user_session_key
);
922 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
928 if (!NT_STATUS_IS_OK(status
)) {
930 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
933 libnet_join_set_error_string(mem_ctx
, r
,
934 "Failed to set password for machine account (%s)\n",
939 status
= NT_STATUS_OK
;
946 if (is_valid_policy_hnd(&sam_pol
)) {
947 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
949 if (is_valid_policy_hnd(&domain_pol
)) {
950 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
952 if (is_valid_policy_hnd(&user_pol
)) {
953 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
955 TALLOC_FREE(pipe_hnd
);
960 /****************************************************************
961 ****************************************************************/
963 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
964 const char *machine_name
,
967 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
968 struct cli_state
*cli
= NULL
;
969 struct rpc_pipe_client
*pipe_hnd
= NULL
;
970 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
972 char *machine_password
= NULL
;
973 char *machine_account
= NULL
;
976 return NT_STATUS_INVALID_PARAMETER
;
979 if (!secrets_init()) {
980 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
983 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
985 if (!machine_password
) {
986 return NT_STATUS_NO_TRUST_LSA_SECRET
;
989 asprintf(&machine_account
, "%s$", machine_name
);
990 if (!machine_account
) {
991 SAFE_FREE(machine_password
);
992 return NT_STATUS_NO_MEMORY
;
995 status
= cli_full_connection(&cli
, NULL
,
1004 free(machine_account
);
1005 free(machine_password
);
1007 if (!NT_STATUS_IS_OK(status
)) {
1008 status
= cli_full_connection(&cli
, NULL
,
1019 if (!NT_STATUS_IS_OK(status
)) {
1023 netlogon_pipe
= get_schannel_session_key(cli
,
1024 netbios_domain_name
,
1025 &neg_flags
, &status
);
1026 if (!netlogon_pipe
) {
1027 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1029 return NT_STATUS_OK
;
1032 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1033 "key from server %s for domain %s. Error was %s\n",
1034 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1039 if (!lp_client_schannel()) {
1041 return NT_STATUS_OK
;
1044 pipe_hnd
= cli_rpc_pipe_open_schannel_with_key(cli
, PI_NETLOGON
,
1045 PIPE_AUTH_LEVEL_PRIVACY
,
1046 netbios_domain_name
,
1053 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1054 "on netlogon pipe to server %s for domain %s. "
1056 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1060 return NT_STATUS_OK
;
1063 /****************************************************************
1064 ****************************************************************/
1066 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1067 struct libnet_JoinCtx
*r
)
1071 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1074 if (!NT_STATUS_IS_OK(status
)) {
1075 libnet_join_set_error_string(mem_ctx
, r
,
1076 "failed to verify domain membership after joining: %s",
1077 get_friendly_nt_error_msg(status
));
1078 return WERR_SETUP_NOT_JOINED
;
1084 /****************************************************************
1085 ****************************************************************/
1087 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1088 struct libnet_UnjoinCtx
*r
)
1090 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1094 if (!secrets_delete_domain_sid(lp_workgroup())) {
1101 /****************************************************************
1102 ****************************************************************/
1104 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1105 struct libnet_UnjoinCtx
*r
)
1107 struct cli_state
*cli
= NULL
;
1108 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1109 POLICY_HND sam_pol
, domain_pol
, user_pol
;
1110 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1113 struct lsa_String lsa_acct_name
;
1114 struct samr_Ids user_rids
;
1115 struct samr_Ids name_types
;
1116 union samr_UserInfo
*info
= NULL
;
1118 ZERO_STRUCT(sam_pol
);
1119 ZERO_STRUCT(domain_pol
);
1120 ZERO_STRUCT(user_pol
);
1122 status
= cli_full_connection(&cli
, NULL
,
1126 r
->in
.admin_account
,
1128 r
->in
.admin_password
,
1129 0, Undefined
, NULL
);
1131 if (!NT_STATUS_IS_OK(status
)) {
1135 /* Open the domain */
1137 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &status
);
1139 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1140 nt_errstr(status
)));
1144 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1146 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1148 if (!NT_STATUS_IS_OK(status
)) {
1152 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1154 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1157 if (!NT_STATUS_IS_OK(status
)) {
1161 /* Create domain user */
1163 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1164 strlower_m(acct_name
);
1166 init_lsa_String(&lsa_acct_name
, acct_name
);
1168 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1175 if (!NT_STATUS_IS_OK(status
)) {
1179 if (name_types
.ids
[0] != SID_NAME_USER
) {
1180 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1181 name_types
.ids
[0]));
1182 status
= NT_STATUS_INVALID_WORKSTATION
;
1186 user_rid
= user_rids
.ids
[0];
1188 /* Open handle on user */
1190 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1192 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1195 if (!NT_STATUS_IS_OK(status
)) {
1201 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1205 if (!NT_STATUS_IS_OK(status
)) {
1206 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1210 /* now disable and setuser info */
1212 info
->info16
.acct_flags
|= ACB_DISABLED
;
1214 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1219 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1223 if (is_valid_policy_hnd(&domain_pol
)) {
1224 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1226 if (is_valid_policy_hnd(&sam_pol
)) {
1227 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1229 TALLOC_FREE(pipe_hnd
);
1239 /****************************************************************
1240 ****************************************************************/
1242 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1245 struct smbconf_ctx
*ctx
;
1247 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1248 if (!W_ERROR_IS_OK(werr
)) {
1252 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1254 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1255 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1257 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1260 smbconf_delete_global_parameter(ctx
, "realm");
1264 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1265 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1267 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1268 r
->out
.netbios_domain_name
);
1269 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1271 if (r
->out
.domain_is_ad
) {
1272 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1273 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1275 werr
= smbconf_set_global_parameter(ctx
, "realm",
1276 r
->out
.dns_domain_name
);
1277 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1281 smbconf_shutdown(ctx
);
1285 /****************************************************************
1286 ****************************************************************/
1288 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1290 WERROR werr
= WERR_OK
;
1291 struct smbconf_ctx
*ctx
;
1293 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1294 if (!W_ERROR_IS_OK(werr
)) {
1298 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1300 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1301 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1303 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1304 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1306 smbconf_delete_global_parameter(ctx
, "realm");
1310 smbconf_shutdown(ctx
);
1314 /****************************************************************
1315 ****************************************************************/
1317 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1321 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1322 return r
->out
.result
;
1325 if (!r
->in
.modify_config
) {
1329 werr
= do_join_modify_vals_config(r
);
1330 if (!W_ERROR_IS_OK(werr
)) {
1334 r
->out
.modified_config
= true;
1335 r
->out
.result
= werr
;
1340 /****************************************************************
1341 ****************************************************************/
1343 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1347 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1348 return r
->out
.result
;
1351 if (!r
->in
.modify_config
) {
1355 werr
= do_unjoin_modify_vals_config(r
);
1356 if (!W_ERROR_IS_OK(werr
)) {
1360 r
->out
.modified_config
= true;
1361 r
->out
.result
= werr
;
1366 /****************************************************************
1367 ****************************************************************/
1369 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1370 const char *domain_str
,
1371 const char **domain_p
,
1374 char *domain
= NULL
;
1376 const char *p
= NULL
;
1378 if (!domain_str
|| !domain_p
|| !dc_p
) {
1382 p
= strchr_m(domain_str
, '\\');
1385 domain
= talloc_strndup(mem_ctx
, domain_str
,
1386 PTR_DIFF(p
, domain_str
));
1387 dc
= talloc_strdup(mem_ctx
, p
+1);
1392 domain
= talloc_strdup(mem_ctx
, domain_str
);
1408 /****************************************************************
1409 ****************************************************************/
1411 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1412 struct libnet_JoinCtx
*r
)
1414 if (!r
->in
.domain_name
) {
1415 libnet_join_set_error_string(mem_ctx
, r
,
1416 "No domain name defined");
1417 return WERR_INVALID_PARAM
;
1420 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1423 libnet_join_set_error_string(mem_ctx
, r
,
1424 "Failed to parse domain name");
1425 return WERR_INVALID_PARAM
;
1429 return WERR_SETUP_DOMAIN_CONTROLLER
;
1432 if (!secrets_init()) {
1433 libnet_join_set_error_string(mem_ctx
, r
,
1434 "Unable to open secrets database");
1435 return WERR_CAN_NOT_COMPLETE
;
1441 /****************************************************************
1442 ****************************************************************/
1444 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1445 struct libnet_JoinCtx
*r
)
1449 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1450 return r
->out
.result
;
1453 werr
= do_JoinConfig(r
);
1454 if (!W_ERROR_IS_OK(werr
)) {
1458 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1459 saf_store(r
->in
.domain_name
, r
->in
.dc_name
);
1465 /****************************************************************
1466 ****************************************************************/
1468 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1470 const char *krb5_cc_env
= NULL
;
1473 ads_destroy(&r
->in
.ads
);
1476 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1477 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1478 unsetenv(KRB5_ENV_CCNAME
);
1484 /****************************************************************
1485 ****************************************************************/
1487 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1489 const char *krb5_cc_env
= NULL
;
1492 ads_destroy(&r
->in
.ads
);
1495 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1496 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1497 unsetenv(KRB5_ENV_CCNAME
);
1503 /****************************************************************
1504 ****************************************************************/
1506 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1507 struct libnet_JoinCtx
**r
)
1509 struct libnet_JoinCtx
*ctx
;
1510 const char *krb5_cc_env
= NULL
;
1512 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1517 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1519 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1520 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1522 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1523 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1524 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1525 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1526 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1529 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1536 /****************************************************************
1537 ****************************************************************/
1539 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1540 struct libnet_UnjoinCtx
**r
)
1542 struct libnet_UnjoinCtx
*ctx
;
1543 const char *krb5_cc_env
= NULL
;
1545 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1550 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1552 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1553 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1555 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1556 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1557 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1558 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1559 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1567 /****************************************************************
1568 ****************************************************************/
1570 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1571 struct libnet_JoinCtx
*r
)
1573 /* check if configuration is already set correctly */
1575 switch (r
->out
.domain_is_ad
) {
1577 if ((strequal(lp_workgroup(),
1578 r
->out
.netbios_domain_name
)) &&
1579 (lp_security() == SEC_DOMAIN
)) {
1580 /* nothing to be done */
1585 if ((strequal(lp_workgroup(),
1586 r
->out
.netbios_domain_name
)) &&
1587 (strequal(lp_realm(),
1588 r
->out
.dns_domain_name
)) &&
1589 ((lp_security() == SEC_ADS
) ||
1590 (lp_security() == SEC_DOMAIN
))) {
1591 /* nothing to be done */
1597 /* check if we are supposed to manipulate configuration */
1599 if (!r
->in
.modify_config
) {
1600 libnet_join_set_error_string(mem_ctx
, r
,
1601 "Invalid configuration and configuration modification "
1602 "was not requested");
1603 return WERR_CAN_NOT_COMPLETE
;
1606 /* check if we are able to manipulate configuration */
1608 if (!lp_config_backend_is_registry()) {
1609 libnet_join_set_error_string(mem_ctx
, r
,
1610 "Configuration manipulation requested but not "
1611 "supported by backend");
1612 return WERR_NOT_SUPPORTED
;
1618 /****************************************************************
1619 ****************************************************************/
1621 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1622 struct libnet_JoinCtx
*r
)
1626 struct cli_state
*cli
= NULL
;
1628 ADS_STATUS ads_status
;
1629 #endif /* WITH_ADS */
1631 if (!r
->in
.dc_name
) {
1632 struct netr_DsRGetDCNameInfo
*info
;
1634 status
= dsgetdcname(mem_ctx
,
1639 DS_DIRECTORY_SERVICE_REQUIRED
|
1640 DS_WRITABLE_REQUIRED
|
1643 if (!NT_STATUS_IS_OK(status
)) {
1644 libnet_join_set_error_string(mem_ctx
, r
,
1645 "failed to find DC for domain %s",
1647 get_friendly_nt_error_msg(status
));
1648 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1651 dc
= strip_hostname(info
->dc_unc
);
1652 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1653 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1656 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1657 if (!NT_STATUS_IS_OK(status
)) {
1658 libnet_join_set_error_string(mem_ctx
, r
,
1659 "failed to lookup DC info for domain '%s' over rpc: %s",
1660 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1661 return ntstatus_to_werror(status
);
1664 werr
= libnet_join_check_config(mem_ctx
, r
);
1665 if (!W_ERROR_IS_OK(werr
)) {
1670 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
) {
1672 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1673 if (!ADS_ERR_OK(ads_status
)) {
1674 return WERR_DEFAULT_JOIN_REQUIRED
;
1677 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1678 if (!ADS_ERR_OK(ads_status
)) {
1679 libnet_join_set_error_string(mem_ctx
, r
,
1680 "failed to precreate account in ou %s: %s",
1682 ads_errstr(ads_status
));
1683 return WERR_DEFAULT_JOIN_REQUIRED
;
1686 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1688 #endif /* WITH_ADS */
1690 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1691 if (!NT_STATUS_IS_OK(status
)) {
1692 libnet_join_set_error_string(mem_ctx
, r
,
1693 "failed to join domain '%s' over rpc: %s",
1694 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1695 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1696 return WERR_SETUP_ALREADY_JOINED
;
1698 werr
= ntstatus_to_werror(status
);
1702 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1703 werr
= WERR_SETUP_NOT_JOINED
;
1708 if (r
->out
.domain_is_ad
) {
1709 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1710 if (!ADS_ERR_OK(ads_status
)) {
1711 werr
= WERR_GENERAL_FAILURE
;
1715 #endif /* WITH_ADS */
1727 /****************************************************************
1728 ****************************************************************/
1730 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1731 struct libnet_JoinCtx
*r
)
1736 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1739 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1740 if (!W_ERROR_IS_OK(werr
)) {
1744 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1745 werr
= libnet_DomainJoin(mem_ctx
, r
);
1746 if (!W_ERROR_IS_OK(werr
)) {
1750 werr
= libnet_join_post_verify(mem_ctx
, r
);
1751 if (!W_ERROR_IS_OK(werr
)) {
1756 werr
= libnet_join_post_processing(mem_ctx
, r
);
1757 if (!W_ERROR_IS_OK(werr
)) {
1761 r
->out
.result
= werr
;
1764 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1769 /****************************************************************
1770 ****************************************************************/
1772 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
1773 struct libnet_UnjoinCtx
*r
)
1777 if (!r
->in
.domain_sid
) {
1779 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
1780 libnet_unjoin_set_error_string(mem_ctx
, r
,
1781 "Unable to fetch domain sid: are we joined?");
1782 return WERR_SETUP_NOT_JOINED
;
1784 r
->in
.domain_sid
= sid_dup_talloc(mem_ctx
, &sid
);
1785 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
1788 if (!r
->in
.dc_name
) {
1789 struct netr_DsRGetDCNameInfo
*info
;
1791 status
= dsgetdcname(mem_ctx
,
1796 DS_DIRECTORY_SERVICE_REQUIRED
|
1797 DS_WRITABLE_REQUIRED
|
1800 if (!NT_STATUS_IS_OK(status
)) {
1801 libnet_unjoin_set_error_string(mem_ctx
, r
,
1802 "failed to find DC for domain %s",
1804 get_friendly_nt_error_msg(status
));
1805 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1808 dc
= strip_hostname(info
->dc_unc
);
1809 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1810 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1813 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
1814 if (!NT_STATUS_IS_OK(status
)) {
1815 libnet_unjoin_set_error_string(mem_ctx
, r
,
1816 "failed to disable machine account via rpc: %s",
1817 get_friendly_nt_error_msg(status
));
1818 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
1819 return WERR_SETUP_NOT_JOINED
;
1821 return ntstatus_to_werror(status
);
1824 r
->out
.disabled_machine_account
= true;
1827 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
1828 ADS_STATUS ads_status
;
1829 libnet_unjoin_connect_ads(mem_ctx
, r
);
1830 ads_status
= libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
1831 if (!ADS_ERR_OK(ads_status
)) {
1832 libnet_unjoin_set_error_string(mem_ctx
, r
,
1833 "failed to remove machine account from AD: %s",
1834 ads_errstr(ads_status
));
1836 r
->out
.deleted_machine_account
= true;
1838 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
1839 r
->in
.ads
->server
.realm
);
1840 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
1843 #endif /* WITH_ADS */
1845 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
1850 /****************************************************************
1851 ****************************************************************/
1853 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
1854 struct libnet_UnjoinCtx
*r
)
1856 if (!r
->in
.domain_name
) {
1857 libnet_unjoin_set_error_string(mem_ctx
, r
,
1858 "No domain name defined");
1859 return WERR_INVALID_PARAM
;
1862 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1865 libnet_unjoin_set_error_string(mem_ctx
, r
,
1866 "Failed to parse domain name");
1867 return WERR_INVALID_PARAM
;
1871 return WERR_SETUP_DOMAIN_CONTROLLER
;
1874 if (!secrets_init()) {
1875 libnet_unjoin_set_error_string(mem_ctx
, r
,
1876 "Unable to open secrets database");
1877 return WERR_CAN_NOT_COMPLETE
;
1883 /****************************************************************
1884 ****************************************************************/
1886 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
1887 struct libnet_UnjoinCtx
*r
)
1889 saf_delete(r
->out
.netbios_domain_name
);
1890 saf_delete(r
->out
.dns_domain_name
);
1892 return libnet_unjoin_config(r
);
1895 /****************************************************************
1896 ****************************************************************/
1898 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
1899 struct libnet_UnjoinCtx
*r
)
1904 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
1907 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
1908 if (!W_ERROR_IS_OK(werr
)) {
1912 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1913 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
1914 if (!W_ERROR_IS_OK(werr
)) {
1915 libnet_unjoin_config(r
);
1920 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
1921 if (!W_ERROR_IS_OK(werr
)) {
1926 r
->out
.result
= werr
;
1929 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);