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 || (strchr(my_fqdn
, '.') == NULL
)) {
362 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
363 r
->out
.dns_domain_name
);
368 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
369 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
371 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
376 mods
= ads_init_mods(mem_ctx
);
378 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
381 /* fields of primary importance */
383 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
384 if (!ADS_ERR_OK(status
)) {
385 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
388 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
390 if (!ADS_ERR_OK(status
)) {
391 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
394 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
397 /****************************************************************
398 ****************************************************************/
400 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
401 struct libnet_JoinCtx
*r
)
406 if (!r
->in
.create_upn
) {
412 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
413 if (!ADS_ERR_OK(status
)) {
418 r
->in
.upn
= talloc_asprintf(mem_ctx
,
421 r
->out
.dns_domain_name
);
423 return ADS_ERROR(LDAP_NO_MEMORY
);
427 /* now do the mods */
429 mods
= ads_init_mods(mem_ctx
);
431 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
434 /* fields of primary importance */
436 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
437 if (!ADS_ERR_OK(status
)) {
438 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
441 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
445 /****************************************************************
446 ****************************************************************/
448 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
449 struct libnet_JoinCtx
*r
)
455 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
461 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
462 if (!ADS_ERR_OK(status
)) {
466 /* now do the mods */
468 mods
= ads_init_mods(mem_ctx
);
470 return ADS_ERROR(LDAP_NO_MEMORY
);
473 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", SAMBA_VERSION_STRING
);
475 return ADS_ERROR(LDAP_NO_MEMORY
);
478 /* fields of primary importance */
480 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
482 if (!ADS_ERR_OK(status
)) {
486 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
488 if (!ADS_ERR_OK(status
)) {
492 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
494 if (!ADS_ERR_OK(status
)) {
498 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
501 /****************************************************************
502 ****************************************************************/
504 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
505 struct libnet_JoinCtx
*r
)
507 if (!lp_use_kerberos_keytab()) {
511 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
518 /****************************************************************
519 ****************************************************************/
521 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
522 struct libnet_JoinCtx
*r
)
524 uint32_t domain_func
;
526 const char *salt
= NULL
;
527 char *std_salt
= NULL
;
529 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
530 if (!ADS_ERR_OK(status
)) {
531 libnet_join_set_error_string(mem_ctx
, r
,
532 "failed to determine domain functional level: %s",
537 /* go ahead and setup the default salt */
539 std_salt
= kerberos_standard_des_salt();
541 libnet_join_set_error_string(mem_ctx
, r
,
542 "failed to obtain standard DES salt");
546 salt
= talloc_strdup(mem_ctx
, std_salt
);
553 /* if it's a Windows functional domain, we have to look for the UPN */
555 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
558 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
561 salt
= talloc_strdup(mem_ctx
, upn
);
568 return kerberos_secrets_store_des_salt(salt
);
571 /****************************************************************
572 ****************************************************************/
574 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
575 struct libnet_JoinCtx
*r
)
580 status
= libnet_join_connect_ads(mem_ctx
, r
);
581 if (!ADS_ERR_OK(status
)) {
586 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
587 if (!ADS_ERR_OK(status
)) {
588 libnet_join_set_error_string(mem_ctx
, r
,
589 "failed to set machine spn: %s",
594 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
595 if (!ADS_ERR_OK(status
)) {
596 libnet_join_set_error_string(mem_ctx
, r
,
597 "failed to set machine os attributes: %s",
602 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
603 if (!ADS_ERR_OK(status
)) {
604 libnet_join_set_error_string(mem_ctx
, r
,
605 "failed to set machine upn: %s",
610 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
611 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
614 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
615 libnet_join_set_error_string(mem_ctx
, r
,
616 "failed to create kerberos keytab");
617 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
622 #endif /* WITH_ADS */
624 /****************************************************************
625 Store the machine password and domain SID
626 ****************************************************************/
628 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
629 struct libnet_JoinCtx
*r
)
631 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
634 DEBUG(1,("Failed to save domain sid\n"));
638 if (!secrets_store_machine_password(r
->in
.machine_password
,
639 r
->out
.netbios_domain_name
,
640 r
->in
.secure_channel_type
))
642 DEBUG(1,("Failed to save machine password\n"));
649 /****************************************************************
650 Lookup domain dc's info
651 ****************************************************************/
653 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
654 struct libnet_JoinCtx
*r
,
655 struct cli_state
**cli
)
657 struct rpc_pipe_client
*pipe_hnd
= NULL
;
659 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
660 union lsa_PolicyInformation
*info
= NULL
;
662 status
= cli_full_connection(cli
, NULL
,
668 r
->in
.admin_password
,
672 if (!NT_STATUS_IS_OK(status
)) {
676 pipe_hnd
= cli_rpc_pipe_open_noauth(*cli
, PI_LSARPC
, &status
);
678 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
683 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
684 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
685 if (!NT_STATUS_IS_OK(status
)) {
689 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
693 if (NT_STATUS_IS_OK(status
)) {
694 r
->out
.domain_is_ad
= true;
695 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
696 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
697 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->dns
.sid
);
698 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
701 if (!NT_STATUS_IS_OK(status
)) {
702 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
704 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
706 if (!NT_STATUS_IS_OK(status
)) {
710 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
711 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->account_domain
.sid
);
712 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
715 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
716 cli_rpc_pipe_close(pipe_hnd
);
722 /****************************************************************
724 ****************************************************************/
726 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
727 struct libnet_JoinCtx
*r
,
728 struct cli_state
*cli
)
730 struct rpc_pipe_client
*pipe_hnd
= NULL
;
731 POLICY_HND sam_pol
, domain_pol
, user_pol
;
732 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
734 struct lsa_String lsa_acct_name
;
736 uint32_t acct_flags
= ACB_WSTRUST
;
737 struct samr_Ids user_rids
;
738 struct samr_Ids name_types
;
739 union samr_UserInfo user_info
;
741 struct samr_CryptPassword crypt_pwd
;
742 struct samr_CryptPasswordEx crypt_pwd_ex
;
744 ZERO_STRUCT(sam_pol
);
745 ZERO_STRUCT(domain_pol
);
746 ZERO_STRUCT(user_pol
);
748 if (!r
->in
.machine_password
) {
749 r
->in
.machine_password
= talloc_strdup(mem_ctx
, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
));
750 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
753 /* Open the domain */
755 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &status
);
757 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
762 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
763 pipe_hnd
->cli
->desthost
,
764 SEC_RIGHTS_MAXIMUM_ALLOWED
,
766 if (!NT_STATUS_IS_OK(status
)) {
770 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
772 SEC_RIGHTS_MAXIMUM_ALLOWED
,
775 if (!NT_STATUS_IS_OK(status
)) {
779 /* Create domain user */
781 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
782 strlower_m(acct_name
);
784 init_lsa_String(&lsa_acct_name
, acct_name
);
786 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
787 uint32_t access_desired
=
788 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
789 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
790 SAMR_USER_ACCESS_SET_PASSWORD
|
791 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
792 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
793 uint32_t access_granted
= 0;
795 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
797 DEBUG(10,("Creating account with desired access mask: %d\n",
800 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
808 if (!NT_STATUS_IS_OK(status
) &&
809 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
811 DEBUG(10,("Creation of workstation account failed: %s\n",
814 /* If NT_STATUS_ACCESS_DENIED then we have a valid
815 username/password combo but the user does not have
816 administrator access. */
818 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
819 libnet_join_set_error_string(mem_ctx
, r
,
820 "User specified does not have "
821 "administrator privileges");
827 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
828 if (!(r
->in
.join_flags
&
829 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
834 /* We *must* do this.... don't ask... */
836 if (NT_STATUS_IS_OK(status
)) {
837 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
841 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
847 if (!NT_STATUS_IS_OK(status
)) {
851 if (name_types
.ids
[0] != SID_NAME_USER
) {
852 DEBUG(0,("%s is not a user account (type=%d)\n",
853 acct_name
, name_types
.ids
[0]));
854 status
= NT_STATUS_INVALID_WORKSTATION
;
858 user_rid
= user_rids
.ids
[0];
860 /* Open handle on user */
862 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
864 SEC_RIGHTS_MAXIMUM_ALLOWED
,
867 if (!NT_STATUS_IS_OK(status
)) {
871 /* Fill in the additional account flags now */
873 acct_flags
|= ACB_PWNOEXP
;
874 if (r
->out
.domain_is_ad
) {
875 #if !defined(ENCTYPE_ARCFOUR_HMAC)
876 acct_flags
|= ACB_USE_DES_KEY_ONLY
;
881 /* Set account flags on machine account */
882 ZERO_STRUCT(user_info
.info16
);
883 user_info
.info16
.acct_flags
= acct_flags
;
885 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
890 if (!NT_STATUS_IS_OK(status
)) {
892 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
895 libnet_join_set_error_string(mem_ctx
, r
,
896 "Failed to set account flags for machine account (%s)\n",
901 /* Set password on machine account - first try level 26 */
903 init_samr_CryptPasswordEx(r
->in
.machine_password
,
904 &cli
->user_session_key
,
907 init_samr_user_info26(&user_info
.info26
, &crypt_pwd_ex
,
908 PASS_DONT_CHANGE_AT_NEXT_LOGON
);
910 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
915 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
917 /* retry with level 24 */
919 init_samr_CryptPassword(r
->in
.machine_password
,
920 &cli
->user_session_key
,
923 init_samr_user_info24(&user_info
.info24
, &crypt_pwd
,
924 PASS_DONT_CHANGE_AT_NEXT_LOGON
);
926 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
932 if (!NT_STATUS_IS_OK(status
)) {
934 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
937 libnet_join_set_error_string(mem_ctx
, r
,
938 "Failed to set password for machine account (%s)\n",
943 status
= NT_STATUS_OK
;
950 if (is_valid_policy_hnd(&sam_pol
)) {
951 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
953 if (is_valid_policy_hnd(&domain_pol
)) {
954 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
956 if (is_valid_policy_hnd(&user_pol
)) {
957 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
959 cli_rpc_pipe_close(pipe_hnd
);
964 /****************************************************************
965 ****************************************************************/
967 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
968 const char *machine_name
,
971 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
972 struct cli_state
*cli
= NULL
;
973 struct rpc_pipe_client
*pipe_hnd
= NULL
;
974 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
976 char *machine_password
= NULL
;
977 char *machine_account
= NULL
;
980 return NT_STATUS_INVALID_PARAMETER
;
983 if (!secrets_init()) {
984 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
987 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
989 if (!machine_password
) {
990 return NT_STATUS_NO_TRUST_LSA_SECRET
;
993 asprintf(&machine_account
, "%s$", machine_name
);
994 if (!machine_account
) {
995 SAFE_FREE(machine_password
);
996 return NT_STATUS_NO_MEMORY
;
999 status
= cli_full_connection(&cli
, NULL
,
1008 free(machine_account
);
1009 free(machine_password
);
1011 if (!NT_STATUS_IS_OK(status
)) {
1012 status
= cli_full_connection(&cli
, NULL
,
1023 if (!NT_STATUS_IS_OK(status
)) {
1027 netlogon_pipe
= get_schannel_session_key(cli
,
1028 netbios_domain_name
,
1029 &neg_flags
, &status
);
1030 if (!netlogon_pipe
) {
1031 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1033 return NT_STATUS_OK
;
1036 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1037 "key from server %s for domain %s. Error was %s\n",
1038 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1043 if (!lp_client_schannel()) {
1045 return NT_STATUS_OK
;
1048 pipe_hnd
= cli_rpc_pipe_open_schannel_with_key(cli
, PI_NETLOGON
,
1049 PIPE_AUTH_LEVEL_PRIVACY
,
1050 netbios_domain_name
,
1057 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1058 "on netlogon pipe to server %s for domain %s. "
1060 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1064 return NT_STATUS_OK
;
1067 /****************************************************************
1068 ****************************************************************/
1070 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1071 struct libnet_JoinCtx
*r
)
1075 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1078 if (!NT_STATUS_IS_OK(status
)) {
1079 libnet_join_set_error_string(mem_ctx
, r
,
1080 "failed to verify domain membership after joining: %s",
1081 get_friendly_nt_error_msg(status
));
1082 return WERR_SETUP_NOT_JOINED
;
1088 /****************************************************************
1089 ****************************************************************/
1091 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1092 struct libnet_UnjoinCtx
*r
)
1094 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1098 if (!secrets_delete_domain_sid(lp_workgroup())) {
1105 /****************************************************************
1106 ****************************************************************/
1108 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1109 struct libnet_UnjoinCtx
*r
)
1111 struct cli_state
*cli
= NULL
;
1112 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1113 POLICY_HND sam_pol
, domain_pol
, user_pol
;
1114 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1117 struct lsa_String lsa_acct_name
;
1118 struct samr_Ids user_rids
;
1119 struct samr_Ids name_types
;
1120 union samr_UserInfo
*info
= NULL
;
1122 ZERO_STRUCT(sam_pol
);
1123 ZERO_STRUCT(domain_pol
);
1124 ZERO_STRUCT(user_pol
);
1126 status
= cli_full_connection(&cli
, NULL
,
1130 r
->in
.admin_account
,
1132 r
->in
.admin_password
,
1133 0, Undefined
, NULL
);
1135 if (!NT_STATUS_IS_OK(status
)) {
1139 /* Open the domain */
1141 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &status
);
1143 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1144 nt_errstr(status
)));
1148 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1149 pipe_hnd
->cli
->desthost
,
1150 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1152 if (!NT_STATUS_IS_OK(status
)) {
1156 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1158 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1161 if (!NT_STATUS_IS_OK(status
)) {
1165 /* Create domain user */
1167 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1168 strlower_m(acct_name
);
1170 init_lsa_String(&lsa_acct_name
, acct_name
);
1172 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1179 if (!NT_STATUS_IS_OK(status
)) {
1183 if (name_types
.ids
[0] != SID_NAME_USER
) {
1184 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1185 name_types
.ids
[0]));
1186 status
= NT_STATUS_INVALID_WORKSTATION
;
1190 user_rid
= user_rids
.ids
[0];
1192 /* Open handle on user */
1194 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1196 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1199 if (!NT_STATUS_IS_OK(status
)) {
1205 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1209 if (!NT_STATUS_IS_OK(status
)) {
1210 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1214 /* now disable and setuser info */
1216 info
->info16
.acct_flags
|= ACB_DISABLED
;
1218 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1223 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1227 if (is_valid_policy_hnd(&domain_pol
)) {
1228 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1230 if (is_valid_policy_hnd(&sam_pol
)) {
1231 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1233 cli_rpc_pipe_close(pipe_hnd
);
1243 /****************************************************************
1244 ****************************************************************/
1246 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1249 struct smbconf_ctx
*ctx
;
1251 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1252 if (!W_ERROR_IS_OK(werr
)) {
1256 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1258 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1259 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1261 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1264 smbconf_delete_global_parameter(ctx
, "realm");
1268 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1269 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1271 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1272 r
->out
.netbios_domain_name
);
1273 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1275 if (r
->out
.domain_is_ad
) {
1276 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1277 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1279 werr
= smbconf_set_global_parameter(ctx
, "realm",
1280 r
->out
.dns_domain_name
);
1281 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1285 smbconf_shutdown(ctx
);
1289 /****************************************************************
1290 ****************************************************************/
1292 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1294 WERROR werr
= WERR_OK
;
1295 struct smbconf_ctx
*ctx
;
1297 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1298 if (!W_ERROR_IS_OK(werr
)) {
1302 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1304 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1305 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1307 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1308 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1310 smbconf_delete_global_parameter(ctx
, "realm");
1314 smbconf_shutdown(ctx
);
1318 /****************************************************************
1319 ****************************************************************/
1321 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1325 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1326 return r
->out
.result
;
1329 if (!r
->in
.modify_config
) {
1333 werr
= do_join_modify_vals_config(r
);
1334 if (!W_ERROR_IS_OK(werr
)) {
1338 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1340 r
->out
.modified_config
= true;
1341 r
->out
.result
= werr
;
1346 /****************************************************************
1347 ****************************************************************/
1349 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1353 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1354 return r
->out
.result
;
1357 if (!r
->in
.modify_config
) {
1361 werr
= do_unjoin_modify_vals_config(r
);
1362 if (!W_ERROR_IS_OK(werr
)) {
1366 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1368 r
->out
.modified_config
= true;
1369 r
->out
.result
= werr
;
1374 /****************************************************************
1375 ****************************************************************/
1377 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1378 const char *domain_str
,
1379 const char **domain_p
,
1382 char *domain
= NULL
;
1384 const char *p
= NULL
;
1386 if (!domain_str
|| !domain_p
|| !dc_p
) {
1390 p
= strchr_m(domain_str
, '\\');
1393 domain
= talloc_strndup(mem_ctx
, domain_str
,
1394 PTR_DIFF(p
, domain_str
));
1395 dc
= talloc_strdup(mem_ctx
, p
+1);
1400 domain
= talloc_strdup(mem_ctx
, domain_str
);
1416 /****************************************************************
1417 ****************************************************************/
1419 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1420 struct libnet_JoinCtx
*r
)
1422 if (!r
->in
.domain_name
) {
1423 libnet_join_set_error_string(mem_ctx
, r
,
1424 "No domain name defined");
1425 return WERR_INVALID_PARAM
;
1428 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1431 libnet_join_set_error_string(mem_ctx
, r
,
1432 "Failed to parse domain name");
1433 return WERR_INVALID_PARAM
;
1437 return WERR_SETUP_DOMAIN_CONTROLLER
;
1440 if (!secrets_init()) {
1441 libnet_join_set_error_string(mem_ctx
, r
,
1442 "Unable to open secrets database");
1443 return WERR_CAN_NOT_COMPLETE
;
1449 /****************************************************************
1450 ****************************************************************/
1452 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1453 struct libnet_JoinCtx
*r
)
1457 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1458 return r
->out
.result
;
1461 werr
= do_JoinConfig(r
);
1462 if (!W_ERROR_IS_OK(werr
)) {
1466 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1470 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1471 if (r
->out
.dns_domain_name
) {
1472 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1476 if (r
->out
.domain_is_ad
) {
1477 ADS_STATUS ads_status
;
1479 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1480 if (!ADS_ERR_OK(ads_status
)) {
1481 return WERR_GENERAL_FAILURE
;
1484 #endif /* WITH_ADS */
1489 /****************************************************************
1490 ****************************************************************/
1492 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1494 const char *krb5_cc_env
= NULL
;
1497 ads_destroy(&r
->in
.ads
);
1500 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1501 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1502 unsetenv(KRB5_ENV_CCNAME
);
1508 /****************************************************************
1509 ****************************************************************/
1511 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1513 const char *krb5_cc_env
= NULL
;
1516 ads_destroy(&r
->in
.ads
);
1519 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1520 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1521 unsetenv(KRB5_ENV_CCNAME
);
1527 /****************************************************************
1528 ****************************************************************/
1530 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1531 struct libnet_JoinCtx
**r
)
1533 struct libnet_JoinCtx
*ctx
;
1534 const char *krb5_cc_env
= NULL
;
1536 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1541 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1543 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1544 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1546 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1547 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1548 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1549 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1550 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1553 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1560 /****************************************************************
1561 ****************************************************************/
1563 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1564 struct libnet_UnjoinCtx
**r
)
1566 struct libnet_UnjoinCtx
*ctx
;
1567 const char *krb5_cc_env
= NULL
;
1569 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1574 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1576 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1577 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1579 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1580 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1581 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1582 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1583 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1591 /****************************************************************
1592 ****************************************************************/
1594 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1595 struct libnet_JoinCtx
*r
)
1597 /* check if configuration is already set correctly */
1599 switch (r
->out
.domain_is_ad
) {
1601 if ((strequal(lp_workgroup(),
1602 r
->out
.netbios_domain_name
)) &&
1603 (lp_security() == SEC_DOMAIN
)) {
1604 /* nothing to be done */
1609 if ((strequal(lp_workgroup(),
1610 r
->out
.netbios_domain_name
)) &&
1611 (strequal(lp_realm(),
1612 r
->out
.dns_domain_name
)) &&
1613 ((lp_security() == SEC_ADS
) ||
1614 (lp_security() == SEC_DOMAIN
))) {
1615 /* nothing to be done */
1621 /* check if we are supposed to manipulate configuration */
1623 if (!r
->in
.modify_config
) {
1624 libnet_join_set_error_string(mem_ctx
, r
,
1625 "Invalid configuration and configuration modification "
1626 "was not requested");
1627 return WERR_CAN_NOT_COMPLETE
;
1630 /* check if we are able to manipulate configuration */
1632 if (!lp_config_backend_is_registry()) {
1633 libnet_join_set_error_string(mem_ctx
, r
,
1634 "Configuration manipulation requested but not "
1635 "supported by backend");
1636 return WERR_NOT_SUPPORTED
;
1642 /****************************************************************
1643 ****************************************************************/
1645 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1646 struct libnet_JoinCtx
*r
)
1650 struct cli_state
*cli
= NULL
;
1652 ADS_STATUS ads_status
;
1653 #endif /* WITH_ADS */
1655 if (!r
->in
.dc_name
) {
1656 struct netr_DsRGetDCNameInfo
*info
;
1658 status
= dsgetdcname(mem_ctx
,
1663 DS_FORCE_REDISCOVERY
|
1664 DS_DIRECTORY_SERVICE_REQUIRED
|
1665 DS_WRITABLE_REQUIRED
|
1668 if (!NT_STATUS_IS_OK(status
)) {
1669 libnet_join_set_error_string(mem_ctx
, r
,
1670 "failed to find DC for domain %s",
1672 get_friendly_nt_error_msg(status
));
1673 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1676 dc
= strip_hostname(info
->dc_unc
);
1677 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1678 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1681 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1682 if (!NT_STATUS_IS_OK(status
)) {
1683 libnet_join_set_error_string(mem_ctx
, r
,
1684 "failed to lookup DC info for domain '%s' over rpc: %s",
1685 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1686 return ntstatus_to_werror(status
);
1689 werr
= libnet_join_check_config(mem_ctx
, r
);
1690 if (!W_ERROR_IS_OK(werr
)) {
1695 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
) {
1697 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1698 if (!ADS_ERR_OK(ads_status
)) {
1699 return WERR_DEFAULT_JOIN_REQUIRED
;
1702 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1703 if (!ADS_ERR_OK(ads_status
)) {
1704 libnet_join_set_error_string(mem_ctx
, r
,
1705 "failed to precreate account in ou %s: %s",
1707 ads_errstr(ads_status
));
1708 return WERR_DEFAULT_JOIN_REQUIRED
;
1711 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1713 #endif /* WITH_ADS */
1715 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1716 if (!NT_STATUS_IS_OK(status
)) {
1717 libnet_join_set_error_string(mem_ctx
, r
,
1718 "failed to join domain '%s' over rpc: %s",
1719 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1720 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1721 return WERR_SETUP_ALREADY_JOINED
;
1723 werr
= ntstatus_to_werror(status
);
1727 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1728 werr
= WERR_SETUP_NOT_JOINED
;
1742 /****************************************************************
1743 ****************************************************************/
1745 WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
1746 struct libnet_JoinCtx
*r
)
1749 struct libnet_UnjoinCtx
*u
= NULL
;
1751 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
1752 if (!W_ERROR_IS_OK(werr
)) {
1756 u
->in
.debug
= r
->in
.debug
;
1757 u
->in
.dc_name
= r
->in
.dc_name
;
1758 u
->in
.domain_name
= r
->in
.domain_name
;
1759 u
->in
.admin_account
= r
->in
.admin_account
;
1760 u
->in
.admin_password
= r
->in
.admin_password
;
1761 u
->in
.modify_config
= r
->in
.modify_config
;
1762 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1763 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1765 werr
= libnet_Unjoin(mem_ctx
, u
);
1771 /****************************************************************
1772 ****************************************************************/
1774 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1775 struct libnet_JoinCtx
*r
)
1780 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1783 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1784 if (!W_ERROR_IS_OK(werr
)) {
1788 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1789 werr
= libnet_DomainJoin(mem_ctx
, r
);
1790 if (!W_ERROR_IS_OK(werr
)) {
1795 werr
= libnet_join_post_processing(mem_ctx
, r
);
1796 if (!W_ERROR_IS_OK(werr
)) {
1800 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1801 werr
= libnet_join_post_verify(mem_ctx
, r
);
1802 if (!W_ERROR_IS_OK(werr
)) {
1803 libnet_join_rollback(mem_ctx
, r
);
1808 r
->out
.result
= werr
;
1811 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1816 /****************************************************************
1817 ****************************************************************/
1819 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
1820 struct libnet_UnjoinCtx
*r
)
1824 if (!r
->in
.domain_sid
) {
1826 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
1827 libnet_unjoin_set_error_string(mem_ctx
, r
,
1828 "Unable to fetch domain sid: are we joined?");
1829 return WERR_SETUP_NOT_JOINED
;
1831 r
->in
.domain_sid
= sid_dup_talloc(mem_ctx
, &sid
);
1832 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
1835 if (!r
->in
.dc_name
) {
1836 struct netr_DsRGetDCNameInfo
*info
;
1838 status
= dsgetdcname(mem_ctx
,
1843 DS_DIRECTORY_SERVICE_REQUIRED
|
1844 DS_WRITABLE_REQUIRED
|
1847 if (!NT_STATUS_IS_OK(status
)) {
1848 libnet_unjoin_set_error_string(mem_ctx
, r
,
1849 "failed to find DC for domain %s",
1851 get_friendly_nt_error_msg(status
));
1852 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1855 dc
= strip_hostname(info
->dc_unc
);
1856 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1857 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1860 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
1861 if (!NT_STATUS_IS_OK(status
)) {
1862 libnet_unjoin_set_error_string(mem_ctx
, r
,
1863 "failed to disable machine account via rpc: %s",
1864 get_friendly_nt_error_msg(status
));
1865 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
1866 return WERR_SETUP_NOT_JOINED
;
1868 return ntstatus_to_werror(status
);
1871 r
->out
.disabled_machine_account
= true;
1874 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
1875 ADS_STATUS ads_status
;
1876 libnet_unjoin_connect_ads(mem_ctx
, r
);
1877 ads_status
= libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
1878 if (!ADS_ERR_OK(ads_status
)) {
1879 libnet_unjoin_set_error_string(mem_ctx
, r
,
1880 "failed to remove machine account from AD: %s",
1881 ads_errstr(ads_status
));
1883 r
->out
.deleted_machine_account
= true;
1885 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
1886 r
->in
.ads
->server
.realm
);
1887 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
1890 #endif /* WITH_ADS */
1892 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
1897 /****************************************************************
1898 ****************************************************************/
1900 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
1901 struct libnet_UnjoinCtx
*r
)
1903 if (!r
->in
.domain_name
) {
1904 libnet_unjoin_set_error_string(mem_ctx
, r
,
1905 "No domain name defined");
1906 return WERR_INVALID_PARAM
;
1909 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1912 libnet_unjoin_set_error_string(mem_ctx
, r
,
1913 "Failed to parse domain name");
1914 return WERR_INVALID_PARAM
;
1918 return WERR_SETUP_DOMAIN_CONTROLLER
;
1921 if (!secrets_init()) {
1922 libnet_unjoin_set_error_string(mem_ctx
, r
,
1923 "Unable to open secrets database");
1924 return WERR_CAN_NOT_COMPLETE
;
1930 /****************************************************************
1931 ****************************************************************/
1933 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
1934 struct libnet_UnjoinCtx
*r
)
1936 saf_delete(r
->out
.netbios_domain_name
);
1937 saf_delete(r
->out
.dns_domain_name
);
1939 return libnet_unjoin_config(r
);
1942 /****************************************************************
1943 ****************************************************************/
1945 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
1946 struct libnet_UnjoinCtx
*r
)
1951 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
1954 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
1955 if (!W_ERROR_IS_OK(werr
)) {
1959 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1960 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
1961 if (!W_ERROR_IS_OK(werr
)) {
1962 libnet_unjoin_config(r
);
1967 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
1968 if (!W_ERROR_IS_OK(werr
)) {
1973 r
->out
.result
= werr
;
1976 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);