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 /****************************************************************
54 ****************************************************************/
56 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
57 struct libnet_JoinCtx
*r
,
58 const char *format
, ...)
62 if (r
->out
.error_string
) {
66 va_start(args
, format
);
67 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
71 /****************************************************************
72 ****************************************************************/
74 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
75 struct libnet_UnjoinCtx
*r
,
76 const char *format
, ...)
80 if (r
->out
.error_string
) {
84 va_start(args
, format
);
85 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
91 /****************************************************************
92 ****************************************************************/
94 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
95 const char *netbios_domain_name
,
97 const char *user_name
,
102 ADS_STRUCT
*my_ads
= NULL
;
104 my_ads
= ads_init(dns_domain_name
,
108 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
112 SAFE_FREE(my_ads
->auth
.user_name
);
113 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
117 SAFE_FREE(my_ads
->auth
.password
);
118 my_ads
->auth
.password
= SMB_STRDUP(password
);
121 status
= ads_connect_user_creds(my_ads
);
122 if (!ADS_ERR_OK(status
)) {
123 ads_destroy(&my_ads
);
131 /****************************************************************
132 ****************************************************************/
134 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
135 struct libnet_JoinCtx
*r
)
139 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
140 r
->out
.netbios_domain_name
,
143 r
->in
.admin_password
,
145 if (!ADS_ERR_OK(status
)) {
146 libnet_join_set_error_string(mem_ctx
, r
,
147 "failed to connect to AD: %s",
152 if (!r
->out
.netbios_domain_name
) {
153 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
154 r
->in
.ads
->server
.workgroup
);
155 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
158 if (!r
->out
.dns_domain_name
) {
159 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
160 r
->in
.ads
->config
.realm
);
161 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
164 r
->out
.domain_is_ad
= true;
169 /****************************************************************
170 ****************************************************************/
172 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
173 struct libnet_UnjoinCtx
*r
)
177 status
= libnet_connect_ads(r
->in
.domain_name
,
181 r
->in
.admin_password
,
183 if (!ADS_ERR_OK(status
)) {
184 libnet_unjoin_set_error_string(mem_ctx
, r
,
185 "failed to connect to AD: %s",
192 /****************************************************************
193 join a domain using ADS (LDAP mods)
194 ****************************************************************/
196 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
197 struct libnet_JoinCtx
*r
)
200 LDAPMessage
*res
= NULL
;
201 const char *attrs
[] = { "dn", NULL
};
204 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
205 if (!ADS_ERR_OK(status
)) {
209 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
210 if (!ADS_ERR_OK(status
)) {
214 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
215 ads_msgfree(r
->in
.ads
, res
);
216 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
219 ads_msgfree(r
->in
.ads
, res
);
221 /* Attempt to create the machine account and bail if this fails.
222 Assume that the admin wants exactly what they requested */
224 status
= ads_create_machine_acct(r
->in
.ads
,
228 if (ADS_ERR_OK(status
)) {
229 DEBUG(1,("machine account creation created\n"));
231 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
232 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
233 status
= ADS_SUCCESS
;
236 if (!ADS_ERR_OK(status
)) {
237 DEBUG(1,("machine account creation failed\n"));
241 status
= ads_move_machine_acct(r
->in
.ads
,
245 if (!ADS_ERR_OK(status
)) {
246 DEBUG(1,("failure to locate/move pre-existing "
247 "machine account\n"));
251 DEBUG(1,("The machine account %s the specified OU.\n",
252 moved
? "was moved into" : "already exists in"));
257 /****************************************************************
258 ****************************************************************/
260 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
261 struct libnet_UnjoinCtx
*r
)
266 return libnet_unjoin_connect_ads(mem_ctx
, r
);
269 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
270 if (!ADS_ERR_OK(status
)) {
271 libnet_unjoin_set_error_string(mem_ctx
, r
,
272 "failed to leave realm: %s",
280 /****************************************************************
281 ****************************************************************/
283 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
284 struct libnet_JoinCtx
*r
)
287 LDAPMessage
*res
= NULL
;
290 if (!r
->in
.machine_name
) {
291 return ADS_ERROR(LDAP_NO_MEMORY
);
294 status
= ads_find_machine_acct(r
->in
.ads
,
297 if (!ADS_ERR_OK(status
)) {
301 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
302 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
306 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
308 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
312 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
314 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
319 ads_msgfree(r
->in
.ads
, res
);
325 /****************************************************************
326 Set a machines dNSHostName and servicePrincipalName attributes
327 ****************************************************************/
329 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
330 struct libnet_JoinCtx
*r
)
335 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
340 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
341 if (!ADS_ERR_OK(status
)) {
345 /* Windows only creates HOST/shortname & HOST/fqdn. */
347 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
349 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
354 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
355 || (strchr(my_fqdn
, '.') == NULL
)) {
356 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
357 r
->out
.dns_domain_name
);
362 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
363 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
365 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
370 mods
= ads_init_mods(mem_ctx
);
372 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
375 /* fields of primary importance */
377 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
378 if (!ADS_ERR_OK(status
)) {
379 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
382 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
384 if (!ADS_ERR_OK(status
)) {
385 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
388 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
391 /****************************************************************
392 ****************************************************************/
394 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
395 struct libnet_JoinCtx
*r
)
400 if (!r
->in
.create_upn
) {
406 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
407 if (!ADS_ERR_OK(status
)) {
412 r
->in
.upn
= talloc_asprintf(mem_ctx
,
415 r
->out
.dns_domain_name
);
417 return ADS_ERROR(LDAP_NO_MEMORY
);
421 /* now do the mods */
423 mods
= ads_init_mods(mem_ctx
);
425 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
428 /* fields of primary importance */
430 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
431 if (!ADS_ERR_OK(status
)) {
432 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
435 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
439 /****************************************************************
440 ****************************************************************/
442 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
443 struct libnet_JoinCtx
*r
)
449 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
455 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
456 if (!ADS_ERR_OK(status
)) {
460 /* now do the mods */
462 mods
= ads_init_mods(mem_ctx
);
464 return ADS_ERROR(LDAP_NO_MEMORY
);
467 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
469 return ADS_ERROR(LDAP_NO_MEMORY
);
472 /* fields of primary importance */
474 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
476 if (!ADS_ERR_OK(status
)) {
480 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
482 if (!ADS_ERR_OK(status
)) {
486 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
488 if (!ADS_ERR_OK(status
)) {
492 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
495 /****************************************************************
496 ****************************************************************/
498 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
499 struct libnet_JoinCtx
*r
)
501 if (!USE_SYSTEM_KEYTAB
) {
505 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
512 /****************************************************************
513 ****************************************************************/
515 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
516 struct libnet_JoinCtx
*r
)
518 uint32_t domain_func
;
520 const char *salt
= NULL
;
521 char *std_salt
= NULL
;
523 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
524 if (!ADS_ERR_OK(status
)) {
525 libnet_join_set_error_string(mem_ctx
, r
,
526 "failed to determine domain functional level: %s",
531 /* go ahead and setup the default salt */
533 std_salt
= kerberos_standard_des_salt();
535 libnet_join_set_error_string(mem_ctx
, r
,
536 "failed to obtain standard DES salt");
540 salt
= talloc_strdup(mem_ctx
, std_salt
);
547 /* if it's a Windows functional domain, we have to look for the UPN */
549 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
552 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
555 salt
= talloc_strdup(mem_ctx
, upn
);
562 return kerberos_secrets_store_des_salt(salt
);
565 /****************************************************************
566 ****************************************************************/
568 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
569 struct libnet_JoinCtx
*r
)
574 status
= libnet_join_connect_ads(mem_ctx
, r
);
575 if (!ADS_ERR_OK(status
)) {
580 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
581 if (!ADS_ERR_OK(status
)) {
582 libnet_join_set_error_string(mem_ctx
, r
,
583 "failed to set machine spn: %s",
588 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
589 if (!ADS_ERR_OK(status
)) {
590 libnet_join_set_error_string(mem_ctx
, r
,
591 "failed to set machine os attributes: %s",
596 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
597 if (!ADS_ERR_OK(status
)) {
598 libnet_join_set_error_string(mem_ctx
, r
,
599 "failed to set machine upn: %s",
604 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
605 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
608 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
609 libnet_join_set_error_string(mem_ctx
, r
,
610 "failed to create kerberos keytab");
611 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
616 #endif /* WITH_ADS */
618 /****************************************************************
619 Store the machine password and domain SID
620 ****************************************************************/
622 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
623 struct libnet_JoinCtx
*r
)
625 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
628 DEBUG(1,("Failed to save domain sid\n"));
632 if (!secrets_store_machine_password(r
->in
.machine_password
,
633 r
->out
.netbios_domain_name
,
634 r
->in
.secure_channel_type
))
636 DEBUG(1,("Failed to save machine password\n"));
643 /****************************************************************
644 Connect dc's IPC$ share
645 ****************************************************************/
647 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
651 struct cli_state
**cli
)
656 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
659 if (use_kerberos
&& pass
) {
660 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
663 return cli_full_connection(cli
, NULL
,
674 /****************************************************************
675 Lookup domain dc's info
676 ****************************************************************/
678 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
679 struct libnet_JoinCtx
*r
,
680 struct cli_state
**cli
)
682 struct rpc_pipe_client
*pipe_hnd
= NULL
;
683 struct policy_handle lsa_pol
;
684 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
685 union lsa_PolicyInformation
*info
= NULL
;
687 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
689 r
->in
.admin_password
,
692 if (!NT_STATUS_IS_OK(status
)) {
696 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
698 if (!NT_STATUS_IS_OK(status
)) {
699 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
704 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
705 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
706 if (!NT_STATUS_IS_OK(status
)) {
710 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
714 if (NT_STATUS_IS_OK(status
)) {
715 r
->out
.domain_is_ad
= true;
716 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
717 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
718 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
719 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->dns
.sid
);
720 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
723 if (!NT_STATUS_IS_OK(status
)) {
724 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
726 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
728 if (!NT_STATUS_IS_OK(status
)) {
732 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
733 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->account_domain
.sid
);
734 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
737 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
738 TALLOC_FREE(pipe_hnd
);
744 /****************************************************************
746 ****************************************************************/
748 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
749 struct libnet_JoinCtx
*r
,
750 struct cli_state
*cli
)
752 struct rpc_pipe_client
*pipe_hnd
= NULL
;
753 struct policy_handle sam_pol
, domain_pol
, user_pol
;
754 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
756 struct lsa_String lsa_acct_name
;
758 uint32_t acct_flags
= ACB_WSTRUST
;
759 struct samr_Ids user_rids
;
760 struct samr_Ids name_types
;
761 union samr_UserInfo user_info
;
763 struct samr_CryptPassword crypt_pwd
;
764 struct samr_CryptPasswordEx crypt_pwd_ex
;
766 ZERO_STRUCT(sam_pol
);
767 ZERO_STRUCT(domain_pol
);
768 ZERO_STRUCT(user_pol
);
770 if (!r
->in
.machine_password
) {
771 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
772 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
775 /* Open the domain */
777 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
779 if (!NT_STATUS_IS_OK(status
)) {
780 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
785 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
787 SAMR_ACCESS_ENUM_DOMAINS
788 | SAMR_ACCESS_LOOKUP_DOMAIN
,
790 if (!NT_STATUS_IS_OK(status
)) {
794 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
796 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
797 | SAMR_DOMAIN_ACCESS_CREATE_USER
798 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
801 if (!NT_STATUS_IS_OK(status
)) {
805 /* Create domain user */
807 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
808 strlower_m(acct_name
);
810 init_lsa_String(&lsa_acct_name
, acct_name
);
812 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
813 uint32_t access_desired
=
814 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
815 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
816 SAMR_USER_ACCESS_SET_PASSWORD
|
817 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
818 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
819 uint32_t access_granted
= 0;
821 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
823 DEBUG(10,("Creating account with desired access mask: %d\n",
826 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
834 if (!NT_STATUS_IS_OK(status
) &&
835 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
837 DEBUG(10,("Creation of workstation account failed: %s\n",
840 /* If NT_STATUS_ACCESS_DENIED then we have a valid
841 username/password combo but the user does not have
842 administrator access. */
844 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
845 libnet_join_set_error_string(mem_ctx
, r
,
846 "User specified does not have "
847 "administrator privileges");
853 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
854 if (!(r
->in
.join_flags
&
855 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
860 /* We *must* do this.... don't ask... */
862 if (NT_STATUS_IS_OK(status
)) {
863 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
867 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
873 if (!NT_STATUS_IS_OK(status
)) {
877 if (name_types
.ids
[0] != SID_NAME_USER
) {
878 DEBUG(0,("%s is not a user account (type=%d)\n",
879 acct_name
, name_types
.ids
[0]));
880 status
= NT_STATUS_INVALID_WORKSTATION
;
884 user_rid
= user_rids
.ids
[0];
886 /* Open handle on user */
888 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
890 SEC_FLAG_MAXIMUM_ALLOWED
,
893 if (!NT_STATUS_IS_OK(status
)) {
897 /* Fill in the additional account flags now */
899 acct_flags
|= ACB_PWNOEXP
;
900 if (r
->out
.domain_is_ad
) {
901 #if !defined(ENCTYPE_ARCFOUR_HMAC)
902 acct_flags
|= ACB_USE_DES_KEY_ONLY
;
907 /* Set account flags on machine account */
908 ZERO_STRUCT(user_info
.info16
);
909 user_info
.info16
.acct_flags
= acct_flags
;
911 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
916 if (!NT_STATUS_IS_OK(status
)) {
918 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
921 libnet_join_set_error_string(mem_ctx
, r
,
922 "Failed to set account flags for machine account (%s)\n",
927 /* Set password on machine account - first try level 26 */
929 init_samr_CryptPasswordEx(r
->in
.machine_password
,
930 &cli
->user_session_key
,
933 user_info
.info26
.password
= crypt_pwd_ex
;
934 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
936 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
941 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
943 /* retry with level 24 */
945 init_samr_CryptPassword(r
->in
.machine_password
,
946 &cli
->user_session_key
,
949 user_info
.info24
.password
= crypt_pwd
;
950 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
952 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
958 if (!NT_STATUS_IS_OK(status
)) {
960 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
963 libnet_join_set_error_string(mem_ctx
, r
,
964 "Failed to set password for machine account (%s)\n",
969 status
= NT_STATUS_OK
;
976 if (is_valid_policy_hnd(&sam_pol
)) {
977 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
979 if (is_valid_policy_hnd(&domain_pol
)) {
980 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
982 if (is_valid_policy_hnd(&user_pol
)) {
983 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
985 TALLOC_FREE(pipe_hnd
);
990 /****************************************************************
991 ****************************************************************/
993 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
994 const char *machine_name
,
997 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
998 struct cli_state
*cli
= NULL
;
999 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1000 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1002 char *machine_password
= NULL
;
1003 char *machine_account
= NULL
;
1006 return NT_STATUS_INVALID_PARAMETER
;
1009 if (!secrets_init()) {
1010 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1013 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1015 if (!machine_password
) {
1016 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1019 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1020 SAFE_FREE(machine_password
);
1021 return NT_STATUS_NO_MEMORY
;
1024 status
= cli_full_connection(&cli
, NULL
,
1033 free(machine_account
);
1034 free(machine_password
);
1036 if (!NT_STATUS_IS_OK(status
)) {
1037 status
= cli_full_connection(&cli
, NULL
,
1048 if (!NT_STATUS_IS_OK(status
)) {
1052 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1053 &neg_flags
, &netlogon_pipe
);
1054 if (!NT_STATUS_IS_OK(status
)) {
1055 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1057 return NT_STATUS_OK
;
1060 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1061 "key from server %s for domain %s. Error was %s\n",
1062 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1067 if (!lp_client_schannel()) {
1069 return NT_STATUS_OK
;
1072 status
= cli_rpc_pipe_open_schannel_with_key(
1073 cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
1074 PIPE_AUTH_LEVEL_PRIVACY
,
1075 netbios_domain_name
, netlogon_pipe
->dc
, &pipe_hnd
);
1079 if (!NT_STATUS_IS_OK(status
)) {
1080 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1081 "on netlogon pipe to server %s for domain %s. "
1083 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1087 return NT_STATUS_OK
;
1090 /****************************************************************
1091 ****************************************************************/
1093 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1094 struct libnet_JoinCtx
*r
)
1098 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1101 if (!NT_STATUS_IS_OK(status
)) {
1102 libnet_join_set_error_string(mem_ctx
, r
,
1103 "failed to verify domain membership after joining: %s",
1104 get_friendly_nt_error_msg(status
));
1105 return WERR_SETUP_NOT_JOINED
;
1111 /****************************************************************
1112 ****************************************************************/
1114 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1115 struct libnet_UnjoinCtx
*r
)
1117 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1121 if (!secrets_delete_domain_sid(lp_workgroup())) {
1128 /****************************************************************
1129 ****************************************************************/
1131 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1132 struct libnet_UnjoinCtx
*r
)
1134 struct cli_state
*cli
= NULL
;
1135 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1136 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1137 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1140 struct lsa_String lsa_acct_name
;
1141 struct samr_Ids user_rids
;
1142 struct samr_Ids name_types
;
1143 union samr_UserInfo
*info
= NULL
;
1145 ZERO_STRUCT(sam_pol
);
1146 ZERO_STRUCT(domain_pol
);
1147 ZERO_STRUCT(user_pol
);
1149 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1150 r
->in
.admin_account
,
1151 r
->in
.admin_password
,
1154 if (!NT_STATUS_IS_OK(status
)) {
1158 /* Open the domain */
1160 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1162 if (!NT_STATUS_IS_OK(status
)) {
1163 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1164 nt_errstr(status
)));
1168 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1170 SEC_FLAG_MAXIMUM_ALLOWED
,
1172 if (!NT_STATUS_IS_OK(status
)) {
1176 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1178 SEC_FLAG_MAXIMUM_ALLOWED
,
1181 if (!NT_STATUS_IS_OK(status
)) {
1185 /* Create domain user */
1187 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1188 strlower_m(acct_name
);
1190 init_lsa_String(&lsa_acct_name
, acct_name
);
1192 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1199 if (!NT_STATUS_IS_OK(status
)) {
1203 if (name_types
.ids
[0] != SID_NAME_USER
) {
1204 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1205 name_types
.ids
[0]));
1206 status
= NT_STATUS_INVALID_WORKSTATION
;
1210 user_rid
= user_rids
.ids
[0];
1212 /* Open handle on user */
1214 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1216 SEC_FLAG_MAXIMUM_ALLOWED
,
1219 if (!NT_STATUS_IS_OK(status
)) {
1225 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1229 if (!NT_STATUS_IS_OK(status
)) {
1230 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1234 /* now disable and setuser info */
1236 info
->info16
.acct_flags
|= ACB_DISABLED
;
1238 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1243 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1247 if (is_valid_policy_hnd(&domain_pol
)) {
1248 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1250 if (is_valid_policy_hnd(&sam_pol
)) {
1251 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1253 TALLOC_FREE(pipe_hnd
);
1263 /****************************************************************
1264 ****************************************************************/
1266 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1269 struct smbconf_ctx
*ctx
;
1271 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1272 if (!W_ERROR_IS_OK(werr
)) {
1276 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1278 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1279 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1281 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1284 smbconf_delete_global_parameter(ctx
, "realm");
1288 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1289 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1291 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1292 r
->out
.netbios_domain_name
);
1293 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1295 if (r
->out
.domain_is_ad
) {
1296 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1297 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1299 werr
= smbconf_set_global_parameter(ctx
, "realm",
1300 r
->out
.dns_domain_name
);
1301 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1305 smbconf_shutdown(ctx
);
1309 /****************************************************************
1310 ****************************************************************/
1312 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1314 WERROR werr
= WERR_OK
;
1315 struct smbconf_ctx
*ctx
;
1317 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1318 if (!W_ERROR_IS_OK(werr
)) {
1322 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1324 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1325 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1327 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1328 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1330 smbconf_delete_global_parameter(ctx
, "realm");
1334 smbconf_shutdown(ctx
);
1338 /****************************************************************
1339 ****************************************************************/
1341 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1345 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1346 return r
->out
.result
;
1349 if (!r
->in
.modify_config
) {
1353 werr
= do_join_modify_vals_config(r
);
1354 if (!W_ERROR_IS_OK(werr
)) {
1358 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1360 r
->out
.modified_config
= true;
1361 r
->out
.result
= werr
;
1366 /****************************************************************
1367 ****************************************************************/
1369 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1373 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1374 return r
->out
.result
;
1377 if (!r
->in
.modify_config
) {
1381 werr
= do_unjoin_modify_vals_config(r
);
1382 if (!W_ERROR_IS_OK(werr
)) {
1386 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1388 r
->out
.modified_config
= true;
1389 r
->out
.result
= werr
;
1394 /****************************************************************
1395 ****************************************************************/
1397 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1398 const char *domain_str
,
1399 const char **domain_p
,
1402 char *domain
= NULL
;
1404 const char *p
= NULL
;
1406 if (!domain_str
|| !domain_p
|| !dc_p
) {
1410 p
= strchr_m(domain_str
, '\\');
1413 domain
= talloc_strndup(mem_ctx
, domain_str
,
1414 PTR_DIFF(p
, domain_str
));
1415 dc
= talloc_strdup(mem_ctx
, p
+1);
1420 domain
= talloc_strdup(mem_ctx
, domain_str
);
1436 /****************************************************************
1437 ****************************************************************/
1439 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1440 struct libnet_JoinCtx
*r
)
1442 if (!r
->in
.domain_name
) {
1443 libnet_join_set_error_string(mem_ctx
, r
,
1444 "No domain name defined");
1445 return WERR_INVALID_PARAM
;
1448 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1451 libnet_join_set_error_string(mem_ctx
, r
,
1452 "Failed to parse domain name");
1453 return WERR_INVALID_PARAM
;
1457 return WERR_SETUP_DOMAIN_CONTROLLER
;
1460 if (!secrets_init()) {
1461 libnet_join_set_error_string(mem_ctx
, r
,
1462 "Unable to open secrets database");
1463 return WERR_CAN_NOT_COMPLETE
;
1469 /****************************************************************
1470 ****************************************************************/
1472 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1476 /* Try adding dom admins to builtin\admins. Only log failures. */
1477 status
= create_builtin_administrators(domain_sid
);
1478 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1479 DEBUG(10,("Unable to auto-add domain administrators to "
1480 "BUILTIN\\Administrators during join because "
1481 "winbindd must be running."));
1482 } else if (!NT_STATUS_IS_OK(status
)) {
1483 DEBUG(5, ("Failed to auto-add domain administrators to "
1484 "BUILTIN\\Administrators during join: %s\n",
1485 nt_errstr(status
)));
1488 /* Try adding dom users to builtin\users. Only log failures. */
1489 status
= create_builtin_users(domain_sid
);
1490 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1491 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1492 "during join because winbindd must be running."));
1493 } else if (!NT_STATUS_IS_OK(status
)) {
1494 DEBUG(5, ("Failed to auto-add domain administrators to "
1495 "BUILTIN\\Administrators during join: %s\n",
1496 nt_errstr(status
)));
1500 /****************************************************************
1501 ****************************************************************/
1503 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1504 struct libnet_JoinCtx
*r
)
1508 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1509 return r
->out
.result
;
1512 werr
= do_JoinConfig(r
);
1513 if (!W_ERROR_IS_OK(werr
)) {
1517 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1521 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1522 if (r
->out
.dns_domain_name
) {
1523 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1527 if (r
->out
.domain_is_ad
) {
1528 ADS_STATUS ads_status
;
1530 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1531 if (!ADS_ERR_OK(ads_status
)) {
1532 return WERR_GENERAL_FAILURE
;
1535 #endif /* WITH_ADS */
1537 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1542 /****************************************************************
1543 ****************************************************************/
1545 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1547 const char *krb5_cc_env
= NULL
;
1550 ads_destroy(&r
->in
.ads
);
1553 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1554 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1555 unsetenv(KRB5_ENV_CCNAME
);
1561 /****************************************************************
1562 ****************************************************************/
1564 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1566 const char *krb5_cc_env
= NULL
;
1569 ads_destroy(&r
->in
.ads
);
1572 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1573 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1574 unsetenv(KRB5_ENV_CCNAME
);
1580 /****************************************************************
1581 ****************************************************************/
1583 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1584 struct libnet_JoinCtx
**r
)
1586 struct libnet_JoinCtx
*ctx
;
1587 const char *krb5_cc_env
= NULL
;
1589 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1594 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1596 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1597 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1599 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1600 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1601 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1602 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1603 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1606 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1613 /****************************************************************
1614 ****************************************************************/
1616 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1617 struct libnet_UnjoinCtx
**r
)
1619 struct libnet_UnjoinCtx
*ctx
;
1620 const char *krb5_cc_env
= NULL
;
1622 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1627 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1629 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1630 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1632 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1633 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1634 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1635 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1636 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1644 /****************************************************************
1645 ****************************************************************/
1647 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1648 struct libnet_JoinCtx
*r
)
1650 bool valid_security
= false;
1651 bool valid_workgroup
= false;
1652 bool valid_realm
= false;
1654 /* check if configuration is already set correctly */
1656 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1658 switch (r
->out
.domain_is_ad
) {
1660 valid_security
= (lp_security() == SEC_DOMAIN
);
1661 if (valid_workgroup
&& valid_security
) {
1662 /* nothing to be done */
1667 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1668 switch (lp_security()) {
1671 valid_security
= true;
1674 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1675 /* nothing to be done */
1681 /* check if we are supposed to manipulate configuration */
1683 if (!r
->in
.modify_config
) {
1685 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1687 if (!valid_workgroup
) {
1688 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1689 "\"workgroup\" set to '%s', should be '%s'",
1690 lp_workgroup(), r
->out
.netbios_domain_name
);
1691 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1695 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1696 "\"realm\" set to '%s', should be '%s'",
1697 lp_realm(), r
->out
.dns_domain_name
);
1698 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1701 if (!valid_security
) {
1702 const char *sec
= NULL
;
1703 switch (lp_security()) {
1704 case SEC_SHARE
: sec
= "share"; break;
1705 case SEC_USER
: sec
= "user"; break;
1706 case SEC_DOMAIN
: sec
= "domain"; break;
1707 case SEC_ADS
: sec
= "ads"; break;
1709 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1710 "\"security\" set to '%s', should be %s",
1711 sec
, r
->out
.domain_is_ad
?
1712 "either 'domain' or 'ads'" : "'domain'");
1713 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1716 libnet_join_set_error_string(mem_ctx
, r
,
1717 "Invalid configuration (%s) and configuration modification "
1718 "was not requested", wrong_conf
);
1719 return WERR_CAN_NOT_COMPLETE
;
1722 /* check if we are able to manipulate configuration */
1724 if (!lp_config_backend_is_registry()) {
1725 libnet_join_set_error_string(mem_ctx
, r
,
1726 "Configuration manipulation requested but not "
1727 "supported by backend");
1728 return WERR_NOT_SUPPORTED
;
1734 /****************************************************************
1735 ****************************************************************/
1737 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1738 struct libnet_JoinCtx
*r
)
1742 struct cli_state
*cli
= NULL
;
1744 ADS_STATUS ads_status
;
1745 #endif /* WITH_ADS */
1747 if (!r
->in
.dc_name
) {
1748 struct netr_DsRGetDCNameInfo
*info
;
1750 status
= dsgetdcname(mem_ctx
,
1755 DS_FORCE_REDISCOVERY
|
1756 DS_DIRECTORY_SERVICE_REQUIRED
|
1757 DS_WRITABLE_REQUIRED
|
1760 if (!NT_STATUS_IS_OK(status
)) {
1761 libnet_join_set_error_string(mem_ctx
, r
,
1762 "failed to find DC for domain %s",
1764 get_friendly_nt_error_msg(status
));
1765 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1768 dc
= strip_hostname(info
->dc_unc
);
1769 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1770 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1773 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1774 if (!NT_STATUS_IS_OK(status
)) {
1775 libnet_join_set_error_string(mem_ctx
, r
,
1776 "failed to lookup DC info for domain '%s' over rpc: %s",
1777 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1778 return ntstatus_to_werror(status
);
1781 werr
= libnet_join_check_config(mem_ctx
, r
);
1782 if (!W_ERROR_IS_OK(werr
)) {
1787 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
) {
1789 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1790 if (!ADS_ERR_OK(ads_status
)) {
1791 return WERR_DEFAULT_JOIN_REQUIRED
;
1794 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1795 if (!ADS_ERR_OK(ads_status
)) {
1796 libnet_join_set_error_string(mem_ctx
, r
,
1797 "failed to precreate account in ou %s: %s",
1799 ads_errstr(ads_status
));
1800 return WERR_DEFAULT_JOIN_REQUIRED
;
1803 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1805 #endif /* WITH_ADS */
1807 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1808 if (!NT_STATUS_IS_OK(status
)) {
1809 libnet_join_set_error_string(mem_ctx
, r
,
1810 "failed to join domain '%s' over rpc: %s",
1811 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1812 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1813 return WERR_SETUP_ALREADY_JOINED
;
1815 werr
= ntstatus_to_werror(status
);
1819 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1820 werr
= WERR_SETUP_NOT_JOINED
;
1834 /****************************************************************
1835 ****************************************************************/
1837 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
1838 struct libnet_JoinCtx
*r
)
1841 struct libnet_UnjoinCtx
*u
= NULL
;
1843 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
1844 if (!W_ERROR_IS_OK(werr
)) {
1848 u
->in
.debug
= r
->in
.debug
;
1849 u
->in
.dc_name
= r
->in
.dc_name
;
1850 u
->in
.domain_name
= r
->in
.domain_name
;
1851 u
->in
.admin_account
= r
->in
.admin_account
;
1852 u
->in
.admin_password
= r
->in
.admin_password
;
1853 u
->in
.modify_config
= r
->in
.modify_config
;
1854 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1855 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1857 werr
= libnet_Unjoin(mem_ctx
, u
);
1863 /****************************************************************
1864 ****************************************************************/
1866 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1867 struct libnet_JoinCtx
*r
)
1872 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1875 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1876 if (!W_ERROR_IS_OK(werr
)) {
1880 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1881 werr
= libnet_DomainJoin(mem_ctx
, r
);
1882 if (!W_ERROR_IS_OK(werr
)) {
1887 werr
= libnet_join_post_processing(mem_ctx
, r
);
1888 if (!W_ERROR_IS_OK(werr
)) {
1892 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1893 werr
= libnet_join_post_verify(mem_ctx
, r
);
1894 if (!W_ERROR_IS_OK(werr
)) {
1895 libnet_join_rollback(mem_ctx
, r
);
1900 r
->out
.result
= werr
;
1903 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1908 /****************************************************************
1909 ****************************************************************/
1911 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
1912 struct libnet_UnjoinCtx
*r
)
1916 if (!r
->in
.domain_sid
) {
1918 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
1919 libnet_unjoin_set_error_string(mem_ctx
, r
,
1920 "Unable to fetch domain sid: are we joined?");
1921 return WERR_SETUP_NOT_JOINED
;
1923 r
->in
.domain_sid
= sid_dup_talloc(mem_ctx
, &sid
);
1924 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
1927 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
1928 !r
->in
.delete_machine_account
) {
1929 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
1933 if (!r
->in
.dc_name
) {
1934 struct netr_DsRGetDCNameInfo
*info
;
1936 status
= dsgetdcname(mem_ctx
,
1941 DS_DIRECTORY_SERVICE_REQUIRED
|
1942 DS_WRITABLE_REQUIRED
|
1945 if (!NT_STATUS_IS_OK(status
)) {
1946 libnet_unjoin_set_error_string(mem_ctx
, r
,
1947 "failed to find DC for domain %s",
1949 get_friendly_nt_error_msg(status
));
1950 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1953 dc
= strip_hostname(info
->dc_unc
);
1954 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1955 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1959 /* for net ads leave, try to delete the account. If it works,
1960 no sense in disabling. If it fails, we can still try to
1963 if (r
->in
.delete_machine_account
) {
1964 ADS_STATUS ads_status
;
1965 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
1966 if (ADS_ERR_OK(ads_status
)) {
1968 r
->out
.dns_domain_name
=
1969 talloc_strdup(mem_ctx
,
1970 r
->in
.ads
->server
.realm
);
1972 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
1974 if (!ADS_ERR_OK(ads_status
)) {
1975 libnet_unjoin_set_error_string(mem_ctx
, r
,
1976 "failed to remove machine account from AD: %s",
1977 ads_errstr(ads_status
));
1979 r
->out
.deleted_machine_account
= true;
1980 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
1981 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
1985 #endif /* WITH_ADS */
1987 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
1989 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
1990 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
1991 if (!NT_STATUS_IS_OK(status
)) {
1992 libnet_unjoin_set_error_string(mem_ctx
, r
,
1993 "failed to disable machine account via rpc: %s",
1994 get_friendly_nt_error_msg(status
));
1995 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
1996 return WERR_SETUP_NOT_JOINED
;
1998 return ntstatus_to_werror(status
);
2001 r
->out
.disabled_machine_account
= true;
2004 /* If disable succeeded or was not requested at all, we
2005 should be getting rid of our end of things */
2007 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2012 /****************************************************************
2013 ****************************************************************/
2015 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2016 struct libnet_UnjoinCtx
*r
)
2018 if (!r
->in
.domain_name
) {
2019 libnet_unjoin_set_error_string(mem_ctx
, r
,
2020 "No domain name defined");
2021 return WERR_INVALID_PARAM
;
2024 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2027 libnet_unjoin_set_error_string(mem_ctx
, r
,
2028 "Failed to parse domain name");
2029 return WERR_INVALID_PARAM
;
2033 return WERR_SETUP_DOMAIN_CONTROLLER
;
2036 if (!secrets_init()) {
2037 libnet_unjoin_set_error_string(mem_ctx
, r
,
2038 "Unable to open secrets database");
2039 return WERR_CAN_NOT_COMPLETE
;
2045 /****************************************************************
2046 ****************************************************************/
2048 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2049 struct libnet_UnjoinCtx
*r
)
2051 saf_delete(r
->out
.netbios_domain_name
);
2052 saf_delete(r
->out
.dns_domain_name
);
2054 return libnet_unjoin_config(r
);
2057 /****************************************************************
2058 ****************************************************************/
2060 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2061 struct libnet_UnjoinCtx
*r
)
2066 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2069 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2070 if (!W_ERROR_IS_OK(werr
)) {
2074 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2075 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2076 if (!W_ERROR_IS_OK(werr
)) {
2077 libnet_unjoin_config(r
);
2082 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2083 if (!W_ERROR_IS_OK(werr
)) {
2088 r
->out
.result
= werr
;
2091 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);