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 || (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 Connect dc's IPC$ share
651 ****************************************************************/
653 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
657 struct cli_state
**cli
)
662 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
665 if (use_kerberos
&& pass
) {
666 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
669 return cli_full_connection(cli
, NULL
,
680 /****************************************************************
681 Lookup domain dc's info
682 ****************************************************************/
684 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
685 struct libnet_JoinCtx
*r
,
686 struct cli_state
**cli
)
688 struct rpc_pipe_client
*pipe_hnd
= NULL
;
690 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
691 union lsa_PolicyInformation
*info
= NULL
;
693 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
695 r
->in
.admin_password
,
698 if (!NT_STATUS_IS_OK(status
)) {
702 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
704 if (!NT_STATUS_IS_OK(status
)) {
705 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
710 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
711 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
712 if (!NT_STATUS_IS_OK(status
)) {
716 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
720 if (NT_STATUS_IS_OK(status
)) {
721 r
->out
.domain_is_ad
= true;
722 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
723 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
724 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
725 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->dns
.sid
);
726 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
729 if (!NT_STATUS_IS_OK(status
)) {
730 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
732 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
734 if (!NT_STATUS_IS_OK(status
)) {
738 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
739 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->account_domain
.sid
);
740 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
743 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
744 TALLOC_FREE(pipe_hnd
);
750 /****************************************************************
752 ****************************************************************/
754 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
755 struct libnet_JoinCtx
*r
,
756 struct cli_state
*cli
)
758 struct rpc_pipe_client
*pipe_hnd
= NULL
;
759 POLICY_HND sam_pol
, domain_pol
, user_pol
;
760 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
762 struct lsa_String lsa_acct_name
;
764 uint32_t acct_flags
= ACB_WSTRUST
;
765 struct samr_Ids user_rids
;
766 struct samr_Ids name_types
;
767 union samr_UserInfo user_info
;
769 struct samr_CryptPassword crypt_pwd
;
770 struct samr_CryptPasswordEx crypt_pwd_ex
;
772 ZERO_STRUCT(sam_pol
);
773 ZERO_STRUCT(domain_pol
);
774 ZERO_STRUCT(user_pol
);
776 if (!r
->in
.machine_password
) {
777 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
778 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
781 /* Open the domain */
783 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
785 if (!NT_STATUS_IS_OK(status
)) {
786 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
791 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
793 SEC_RIGHTS_MAXIMUM_ALLOWED
,
795 if (!NT_STATUS_IS_OK(status
)) {
799 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
801 SEC_RIGHTS_MAXIMUM_ALLOWED
,
804 if (!NT_STATUS_IS_OK(status
)) {
808 /* Create domain user */
810 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
811 strlower_m(acct_name
);
813 init_lsa_String(&lsa_acct_name
, acct_name
);
815 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
816 uint32_t access_desired
=
817 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
818 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
819 SAMR_USER_ACCESS_SET_PASSWORD
|
820 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
821 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
822 uint32_t access_granted
= 0;
824 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
826 DEBUG(10,("Creating account with desired access mask: %d\n",
829 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
837 if (!NT_STATUS_IS_OK(status
) &&
838 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
840 DEBUG(10,("Creation of workstation account failed: %s\n",
843 /* If NT_STATUS_ACCESS_DENIED then we have a valid
844 username/password combo but the user does not have
845 administrator access. */
847 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
848 libnet_join_set_error_string(mem_ctx
, r
,
849 "User specified does not have "
850 "administrator privileges");
856 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
857 if (!(r
->in
.join_flags
&
858 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
863 /* We *must* do this.... don't ask... */
865 if (NT_STATUS_IS_OK(status
)) {
866 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
870 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
876 if (!NT_STATUS_IS_OK(status
)) {
880 if (name_types
.ids
[0] != SID_NAME_USER
) {
881 DEBUG(0,("%s is not a user account (type=%d)\n",
882 acct_name
, name_types
.ids
[0]));
883 status
= NT_STATUS_INVALID_WORKSTATION
;
887 user_rid
= user_rids
.ids
[0];
889 /* Open handle on user */
891 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
893 SEC_RIGHTS_MAXIMUM_ALLOWED
,
896 if (!NT_STATUS_IS_OK(status
)) {
900 /* Fill in the additional account flags now */
902 acct_flags
|= ACB_PWNOEXP
;
903 if (r
->out
.domain_is_ad
) {
904 #if !defined(ENCTYPE_ARCFOUR_HMAC)
905 acct_flags
|= ACB_USE_DES_KEY_ONLY
;
910 /* Set account flags on machine account */
911 ZERO_STRUCT(user_info
.info16
);
912 user_info
.info16
.acct_flags
= acct_flags
;
914 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
919 if (!NT_STATUS_IS_OK(status
)) {
921 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
924 libnet_join_set_error_string(mem_ctx
, r
,
925 "Failed to set account flags for machine account (%s)\n",
930 /* Set password on machine account - first try level 26 */
932 init_samr_CryptPasswordEx(r
->in
.machine_password
,
933 &cli
->user_session_key
,
936 user_info
.info26
.password
= crypt_pwd_ex
;
937 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
939 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
944 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
946 /* retry with level 24 */
948 init_samr_CryptPassword(r
->in
.machine_password
,
949 &cli
->user_session_key
,
952 user_info
.info24
.password
= crypt_pwd
;
953 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
955 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
961 if (!NT_STATUS_IS_OK(status
)) {
963 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
966 libnet_join_set_error_string(mem_ctx
, r
,
967 "Failed to set password for machine account (%s)\n",
972 status
= NT_STATUS_OK
;
979 if (is_valid_policy_hnd(&sam_pol
)) {
980 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
982 if (is_valid_policy_hnd(&domain_pol
)) {
983 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
985 if (is_valid_policy_hnd(&user_pol
)) {
986 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
988 TALLOC_FREE(pipe_hnd
);
993 /****************************************************************
994 ****************************************************************/
996 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
997 const char *machine_name
,
1000 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1001 struct cli_state
*cli
= NULL
;
1002 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1003 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1005 char *machine_password
= NULL
;
1006 char *machine_account
= NULL
;
1009 return NT_STATUS_INVALID_PARAMETER
;
1012 if (!secrets_init()) {
1013 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1016 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1018 if (!machine_password
) {
1019 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1022 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1023 SAFE_FREE(machine_password
);
1024 return NT_STATUS_NO_MEMORY
;
1027 status
= cli_full_connection(&cli
, NULL
,
1036 free(machine_account
);
1037 free(machine_password
);
1039 if (!NT_STATUS_IS_OK(status
)) {
1040 status
= cli_full_connection(&cli
, NULL
,
1051 if (!NT_STATUS_IS_OK(status
)) {
1055 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1056 &neg_flags
, &netlogon_pipe
);
1057 if (!NT_STATUS_IS_OK(status
)) {
1058 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1060 return NT_STATUS_OK
;
1063 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1064 "key from server %s for domain %s. Error was %s\n",
1065 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1070 if (!lp_client_schannel()) {
1072 return NT_STATUS_OK
;
1075 status
= cli_rpc_pipe_open_schannel_with_key(
1076 cli
, &ndr_table_netlogon
.syntax_id
, PIPE_AUTH_LEVEL_PRIVACY
,
1077 netbios_domain_name
, netlogon_pipe
->dc
, &pipe_hnd
);
1081 if (!NT_STATUS_IS_OK(status
)) {
1082 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1083 "on netlogon pipe to server %s for domain %s. "
1085 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1089 return NT_STATUS_OK
;
1092 /****************************************************************
1093 ****************************************************************/
1095 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1096 struct libnet_JoinCtx
*r
)
1100 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1103 if (!NT_STATUS_IS_OK(status
)) {
1104 libnet_join_set_error_string(mem_ctx
, r
,
1105 "failed to verify domain membership after joining: %s",
1106 get_friendly_nt_error_msg(status
));
1107 return WERR_SETUP_NOT_JOINED
;
1113 /****************************************************************
1114 ****************************************************************/
1116 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1117 struct libnet_UnjoinCtx
*r
)
1119 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1123 if (!secrets_delete_domain_sid(lp_workgroup())) {
1130 /****************************************************************
1131 ****************************************************************/
1133 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1134 struct libnet_UnjoinCtx
*r
)
1136 struct cli_state
*cli
= NULL
;
1137 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1138 POLICY_HND sam_pol
, domain_pol
, user_pol
;
1139 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1142 struct lsa_String lsa_acct_name
;
1143 struct samr_Ids user_rids
;
1144 struct samr_Ids name_types
;
1145 union samr_UserInfo
*info
= NULL
;
1147 ZERO_STRUCT(sam_pol
);
1148 ZERO_STRUCT(domain_pol
);
1149 ZERO_STRUCT(user_pol
);
1151 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1152 r
->in
.admin_account
,
1153 r
->in
.admin_password
,
1156 if (!NT_STATUS_IS_OK(status
)) {
1160 /* Open the domain */
1162 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1164 if (!NT_STATUS_IS_OK(status
)) {
1165 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1166 nt_errstr(status
)));
1170 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1172 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1174 if (!NT_STATUS_IS_OK(status
)) {
1178 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1180 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1183 if (!NT_STATUS_IS_OK(status
)) {
1187 /* Create domain user */
1189 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1190 strlower_m(acct_name
);
1192 init_lsa_String(&lsa_acct_name
, acct_name
);
1194 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1201 if (!NT_STATUS_IS_OK(status
)) {
1205 if (name_types
.ids
[0] != SID_NAME_USER
) {
1206 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1207 name_types
.ids
[0]));
1208 status
= NT_STATUS_INVALID_WORKSTATION
;
1212 user_rid
= user_rids
.ids
[0];
1214 /* Open handle on user */
1216 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1218 SEC_RIGHTS_MAXIMUM_ALLOWED
,
1221 if (!NT_STATUS_IS_OK(status
)) {
1227 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1231 if (!NT_STATUS_IS_OK(status
)) {
1232 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1236 /* now disable and setuser info */
1238 info
->info16
.acct_flags
|= ACB_DISABLED
;
1240 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1245 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1249 if (is_valid_policy_hnd(&domain_pol
)) {
1250 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1252 if (is_valid_policy_hnd(&sam_pol
)) {
1253 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1255 TALLOC_FREE(pipe_hnd
);
1265 /****************************************************************
1266 ****************************************************************/
1268 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1271 struct smbconf_ctx
*ctx
;
1273 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1274 if (!W_ERROR_IS_OK(werr
)) {
1278 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1280 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1281 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1283 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1286 smbconf_delete_global_parameter(ctx
, "realm");
1290 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1291 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1293 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1294 r
->out
.netbios_domain_name
);
1295 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1297 if (r
->out
.domain_is_ad
) {
1298 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1299 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1301 werr
= smbconf_set_global_parameter(ctx
, "realm",
1302 r
->out
.dns_domain_name
);
1303 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1307 smbconf_shutdown(ctx
);
1311 /****************************************************************
1312 ****************************************************************/
1314 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1316 WERROR werr
= WERR_OK
;
1317 struct smbconf_ctx
*ctx
;
1319 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1320 if (!W_ERROR_IS_OK(werr
)) {
1324 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1326 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1327 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1329 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1330 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1332 smbconf_delete_global_parameter(ctx
, "realm");
1336 smbconf_shutdown(ctx
);
1340 /****************************************************************
1341 ****************************************************************/
1343 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1347 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1348 return r
->out
.result
;
1351 if (!r
->in
.modify_config
) {
1355 werr
= do_join_modify_vals_config(r
);
1356 if (!W_ERROR_IS_OK(werr
)) {
1360 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1362 r
->out
.modified_config
= true;
1363 r
->out
.result
= werr
;
1368 /****************************************************************
1369 ****************************************************************/
1371 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1375 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1376 return r
->out
.result
;
1379 if (!r
->in
.modify_config
) {
1383 werr
= do_unjoin_modify_vals_config(r
);
1384 if (!W_ERROR_IS_OK(werr
)) {
1388 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1390 r
->out
.modified_config
= true;
1391 r
->out
.result
= werr
;
1396 /****************************************************************
1397 ****************************************************************/
1399 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1400 const char *domain_str
,
1401 const char **domain_p
,
1404 char *domain
= NULL
;
1406 const char *p
= NULL
;
1408 if (!domain_str
|| !domain_p
|| !dc_p
) {
1412 p
= strchr_m(domain_str
, '\\');
1415 domain
= talloc_strndup(mem_ctx
, domain_str
,
1416 PTR_DIFF(p
, domain_str
));
1417 dc
= talloc_strdup(mem_ctx
, p
+1);
1422 domain
= talloc_strdup(mem_ctx
, domain_str
);
1438 /****************************************************************
1439 ****************************************************************/
1441 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1442 struct libnet_JoinCtx
*r
)
1444 if (!r
->in
.domain_name
) {
1445 libnet_join_set_error_string(mem_ctx
, r
,
1446 "No domain name defined");
1447 return WERR_INVALID_PARAM
;
1450 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1453 libnet_join_set_error_string(mem_ctx
, r
,
1454 "Failed to parse domain name");
1455 return WERR_INVALID_PARAM
;
1459 return WERR_SETUP_DOMAIN_CONTROLLER
;
1462 if (!secrets_init()) {
1463 libnet_join_set_error_string(mem_ctx
, r
,
1464 "Unable to open secrets database");
1465 return WERR_CAN_NOT_COMPLETE
;
1471 /****************************************************************
1472 ****************************************************************/
1474 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1478 /* Try adding dom admins to builtin\admins. Only log failures. */
1479 status
= create_builtin_administrators(domain_sid
);
1480 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1481 DEBUG(10,("Unable to auto-add domain administrators to "
1482 "BUILTIN\\Administrators during join because "
1483 "winbindd must be running."));
1484 } else if (!NT_STATUS_IS_OK(status
)) {
1485 DEBUG(5, ("Failed to auto-add domain administrators to "
1486 "BUILTIN\\Administrators during join: %s\n",
1487 nt_errstr(status
)));
1490 /* Try adding dom users to builtin\users. Only log failures. */
1491 status
= create_builtin_users(domain_sid
);
1492 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1493 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1494 "during join because winbindd must be running."));
1495 } else if (!NT_STATUS_IS_OK(status
)) {
1496 DEBUG(5, ("Failed to auto-add domain administrators to "
1497 "BUILTIN\\Administrators during join: %s\n",
1498 nt_errstr(status
)));
1502 /****************************************************************
1503 ****************************************************************/
1505 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1506 struct libnet_JoinCtx
*r
)
1510 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1511 return r
->out
.result
;
1514 werr
= do_JoinConfig(r
);
1515 if (!W_ERROR_IS_OK(werr
)) {
1519 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1523 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1524 if (r
->out
.dns_domain_name
) {
1525 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1529 if (r
->out
.domain_is_ad
) {
1530 ADS_STATUS ads_status
;
1532 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1533 if (!ADS_ERR_OK(ads_status
)) {
1534 return WERR_GENERAL_FAILURE
;
1537 #endif /* WITH_ADS */
1539 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1544 /****************************************************************
1545 ****************************************************************/
1547 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1549 const char *krb5_cc_env
= NULL
;
1552 ads_destroy(&r
->in
.ads
);
1555 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1556 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1557 unsetenv(KRB5_ENV_CCNAME
);
1563 /****************************************************************
1564 ****************************************************************/
1566 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1568 const char *krb5_cc_env
= NULL
;
1571 ads_destroy(&r
->in
.ads
);
1574 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1575 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1576 unsetenv(KRB5_ENV_CCNAME
);
1582 /****************************************************************
1583 ****************************************************************/
1585 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1586 struct libnet_JoinCtx
**r
)
1588 struct libnet_JoinCtx
*ctx
;
1589 const char *krb5_cc_env
= NULL
;
1591 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1596 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1598 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1599 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1601 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1602 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1603 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1604 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1605 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1608 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1615 /****************************************************************
1616 ****************************************************************/
1618 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1619 struct libnet_UnjoinCtx
**r
)
1621 struct libnet_UnjoinCtx
*ctx
;
1622 const char *krb5_cc_env
= NULL
;
1624 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1629 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1631 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1632 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1634 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1635 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1636 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1637 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1638 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1646 /****************************************************************
1647 ****************************************************************/
1649 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1650 struct libnet_JoinCtx
*r
)
1652 bool valid_security
= false;
1653 bool valid_workgroup
= false;
1654 bool valid_realm
= false;
1656 /* check if configuration is already set correctly */
1658 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1660 switch (r
->out
.domain_is_ad
) {
1662 valid_security
= (lp_security() == SEC_DOMAIN
);
1663 if (valid_workgroup
&& valid_security
) {
1664 /* nothing to be done */
1669 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1670 switch (lp_security()) {
1673 valid_security
= true;
1676 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1677 /* nothing to be done */
1683 /* check if we are supposed to manipulate configuration */
1685 if (!r
->in
.modify_config
) {
1687 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1689 if (!valid_workgroup
) {
1690 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1691 "\"workgroup\" set to '%s', should be '%s'",
1692 lp_workgroup(), r
->out
.netbios_domain_name
);
1693 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1697 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1698 "\"realm\" set to '%s', should be '%s'",
1699 lp_realm(), r
->out
.dns_domain_name
);
1700 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1703 if (!valid_security
) {
1704 const char *sec
= NULL
;
1705 switch (lp_security()) {
1706 case SEC_SHARE
: sec
= "share"; break;
1707 case SEC_USER
: sec
= "user"; break;
1708 case SEC_DOMAIN
: sec
= "domain"; break;
1709 case SEC_ADS
: sec
= "ads"; break;
1711 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1712 "\"security\" set to '%s', should be %s",
1713 sec
, r
->out
.domain_is_ad
?
1714 "either 'domain' or 'ads'" : "'domain'");
1715 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1718 libnet_join_set_error_string(mem_ctx
, r
,
1719 "Invalid configuration (%s) and configuration modification "
1720 "was not requested", wrong_conf
);
1721 return WERR_CAN_NOT_COMPLETE
;
1724 /* check if we are able to manipulate configuration */
1726 if (!lp_config_backend_is_registry()) {
1727 libnet_join_set_error_string(mem_ctx
, r
,
1728 "Configuration manipulation requested but not "
1729 "supported by backend");
1730 return WERR_NOT_SUPPORTED
;
1736 /****************************************************************
1737 ****************************************************************/
1739 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1740 struct libnet_JoinCtx
*r
)
1744 struct cli_state
*cli
= NULL
;
1746 ADS_STATUS ads_status
;
1747 #endif /* WITH_ADS */
1749 if (!r
->in
.dc_name
) {
1750 struct netr_DsRGetDCNameInfo
*info
;
1752 status
= dsgetdcname(mem_ctx
,
1757 DS_FORCE_REDISCOVERY
|
1758 DS_DIRECTORY_SERVICE_REQUIRED
|
1759 DS_WRITABLE_REQUIRED
|
1762 if (!NT_STATUS_IS_OK(status
)) {
1763 libnet_join_set_error_string(mem_ctx
, r
,
1764 "failed to find DC for domain %s",
1766 get_friendly_nt_error_msg(status
));
1767 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1770 dc
= strip_hostname(info
->dc_unc
);
1771 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1772 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1775 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1776 if (!NT_STATUS_IS_OK(status
)) {
1777 libnet_join_set_error_string(mem_ctx
, r
,
1778 "failed to lookup DC info for domain '%s' over rpc: %s",
1779 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1780 return ntstatus_to_werror(status
);
1783 werr
= libnet_join_check_config(mem_ctx
, r
);
1784 if (!W_ERROR_IS_OK(werr
)) {
1789 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
) {
1791 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1792 if (!ADS_ERR_OK(ads_status
)) {
1793 return WERR_DEFAULT_JOIN_REQUIRED
;
1796 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1797 if (!ADS_ERR_OK(ads_status
)) {
1798 libnet_join_set_error_string(mem_ctx
, r
,
1799 "failed to precreate account in ou %s: %s",
1801 ads_errstr(ads_status
));
1802 return WERR_DEFAULT_JOIN_REQUIRED
;
1805 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1807 #endif /* WITH_ADS */
1809 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1810 if (!NT_STATUS_IS_OK(status
)) {
1811 libnet_join_set_error_string(mem_ctx
, r
,
1812 "failed to join domain '%s' over rpc: %s",
1813 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1814 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1815 return WERR_SETUP_ALREADY_JOINED
;
1817 werr
= ntstatus_to_werror(status
);
1821 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1822 werr
= WERR_SETUP_NOT_JOINED
;
1836 /****************************************************************
1837 ****************************************************************/
1839 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
1840 struct libnet_JoinCtx
*r
)
1843 struct libnet_UnjoinCtx
*u
= NULL
;
1845 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
1846 if (!W_ERROR_IS_OK(werr
)) {
1850 u
->in
.debug
= r
->in
.debug
;
1851 u
->in
.dc_name
= r
->in
.dc_name
;
1852 u
->in
.domain_name
= r
->in
.domain_name
;
1853 u
->in
.admin_account
= r
->in
.admin_account
;
1854 u
->in
.admin_password
= r
->in
.admin_password
;
1855 u
->in
.modify_config
= r
->in
.modify_config
;
1856 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1857 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1859 werr
= libnet_Unjoin(mem_ctx
, u
);
1865 /****************************************************************
1866 ****************************************************************/
1868 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1869 struct libnet_JoinCtx
*r
)
1874 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1877 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1878 if (!W_ERROR_IS_OK(werr
)) {
1882 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1883 werr
= libnet_DomainJoin(mem_ctx
, r
);
1884 if (!W_ERROR_IS_OK(werr
)) {
1889 werr
= libnet_join_post_processing(mem_ctx
, r
);
1890 if (!W_ERROR_IS_OK(werr
)) {
1894 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1895 werr
= libnet_join_post_verify(mem_ctx
, r
);
1896 if (!W_ERROR_IS_OK(werr
)) {
1897 libnet_join_rollback(mem_ctx
, r
);
1902 r
->out
.result
= werr
;
1905 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1910 /****************************************************************
1911 ****************************************************************/
1913 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
1914 struct libnet_UnjoinCtx
*r
)
1918 if (!r
->in
.domain_sid
) {
1920 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
1921 libnet_unjoin_set_error_string(mem_ctx
, r
,
1922 "Unable to fetch domain sid: are we joined?");
1923 return WERR_SETUP_NOT_JOINED
;
1925 r
->in
.domain_sid
= sid_dup_talloc(mem_ctx
, &sid
);
1926 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
1929 if (!r
->in
.dc_name
) {
1930 struct netr_DsRGetDCNameInfo
*info
;
1932 status
= dsgetdcname(mem_ctx
,
1937 DS_DIRECTORY_SERVICE_REQUIRED
|
1938 DS_WRITABLE_REQUIRED
|
1941 if (!NT_STATUS_IS_OK(status
)) {
1942 libnet_unjoin_set_error_string(mem_ctx
, r
,
1943 "failed to find DC for domain %s",
1945 get_friendly_nt_error_msg(status
));
1946 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1949 dc
= strip_hostname(info
->dc_unc
);
1950 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1951 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1954 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
1955 if (!NT_STATUS_IS_OK(status
)) {
1956 libnet_unjoin_set_error_string(mem_ctx
, r
,
1957 "failed to disable machine account via rpc: %s",
1958 get_friendly_nt_error_msg(status
));
1959 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
1960 return WERR_SETUP_NOT_JOINED
;
1962 return ntstatus_to_werror(status
);
1965 r
->out
.disabled_machine_account
= true;
1968 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
1969 ADS_STATUS ads_status
;
1970 libnet_unjoin_connect_ads(mem_ctx
, r
);
1971 ads_status
= libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
1972 if (!ADS_ERR_OK(ads_status
)) {
1973 libnet_unjoin_set_error_string(mem_ctx
, r
,
1974 "failed to remove machine account from AD: %s",
1975 ads_errstr(ads_status
));
1977 r
->out
.deleted_machine_account
= true;
1979 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
1980 r
->in
.ads
->server
.realm
);
1981 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
1984 #endif /* WITH_ADS */
1986 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
1991 /****************************************************************
1992 ****************************************************************/
1994 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
1995 struct libnet_UnjoinCtx
*r
)
1997 if (!r
->in
.domain_name
) {
1998 libnet_unjoin_set_error_string(mem_ctx
, r
,
1999 "No domain name defined");
2000 return WERR_INVALID_PARAM
;
2003 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2006 libnet_unjoin_set_error_string(mem_ctx
, r
,
2007 "Failed to parse domain name");
2008 return WERR_INVALID_PARAM
;
2012 return WERR_SETUP_DOMAIN_CONTROLLER
;
2015 if (!secrets_init()) {
2016 libnet_unjoin_set_error_string(mem_ctx
, r
,
2017 "Unable to open secrets database");
2018 return WERR_CAN_NOT_COMPLETE
;
2024 /****************************************************************
2025 ****************************************************************/
2027 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2028 struct libnet_UnjoinCtx
*r
)
2030 saf_delete(r
->out
.netbios_domain_name
);
2031 saf_delete(r
->out
.dns_domain_name
);
2033 return libnet_unjoin_config(r
);
2036 /****************************************************************
2037 ****************************************************************/
2039 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2040 struct libnet_UnjoinCtx
*r
)
2045 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2048 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2049 if (!W_ERROR_IS_OK(werr
)) {
2053 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2054 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2055 if (!W_ERROR_IS_OK(werr
)) {
2056 libnet_unjoin_config(r
);
2061 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2062 if (!W_ERROR_IS_OK(werr
)) {
2067 r
->out
.result
= werr
;
2070 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);