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/>.
23 #include "librpc/gen_ndr/ndr_libnet_join.h"
24 #include "libnet/libnet_join.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_lsa_c.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/ndr_netlogon.h"
31 #include "rpc_client/cli_netlogon.h"
32 #include "lib/smbconf/smbconf.h"
33 #include "lib/smbconf/smbconf_reg.h"
34 #include "../libds/common/flags.h"
36 #include "rpc_client/init_lsa.h"
37 #include "rpc_client/cli_pipe.h"
38 #include "../libcli/security/security.h"
40 #include "libsmb/libsmb.h"
42 /****************************************************************
43 ****************************************************************/
45 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
48 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
49 DEBUG(1,("libnet_Join:\n%s", str)); \
53 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
54 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
55 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
56 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
58 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
61 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
62 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
66 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
67 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
68 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
69 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
71 /****************************************************************
72 ****************************************************************/
74 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
75 struct libnet_JoinCtx
*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
);
89 /****************************************************************
90 ****************************************************************/
92 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
93 struct libnet_UnjoinCtx
*r
,
94 const char *format
, ...)
98 if (r
->out
.error_string
) {
102 va_start(args
, format
);
103 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
109 /****************************************************************
110 ****************************************************************/
112 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
113 const char *netbios_domain_name
,
115 const char *user_name
,
116 const char *password
,
120 ADS_STRUCT
*my_ads
= NULL
;
123 my_ads
= ads_init(dns_domain_name
,
127 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
131 SAFE_FREE(my_ads
->auth
.user_name
);
132 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
133 if ((cp
= strchr_m(my_ads
->auth
.user_name
, '@'))!=0) {
135 SAFE_FREE(my_ads
->auth
.realm
);
136 my_ads
->auth
.realm
= smb_xstrdup(cp
);
137 strupper_m(my_ads
->auth
.realm
);
142 SAFE_FREE(my_ads
->auth
.password
);
143 my_ads
->auth
.password
= SMB_STRDUP(password
);
146 status
= ads_connect_user_creds(my_ads
);
147 if (!ADS_ERR_OK(status
)) {
148 ads_destroy(&my_ads
);
156 /****************************************************************
157 ****************************************************************/
159 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
160 struct libnet_JoinCtx
*r
)
164 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
165 r
->out
.netbios_domain_name
,
168 r
->in
.admin_password
,
170 if (!ADS_ERR_OK(status
)) {
171 libnet_join_set_error_string(mem_ctx
, r
,
172 "failed to connect to AD: %s",
177 if (!r
->out
.netbios_domain_name
) {
178 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
179 r
->in
.ads
->server
.workgroup
);
180 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
183 if (!r
->out
.dns_domain_name
) {
184 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
185 r
->in
.ads
->config
.realm
);
186 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
189 r
->out
.domain_is_ad
= true;
194 /****************************************************************
195 ****************************************************************/
197 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
198 struct libnet_UnjoinCtx
*r
)
202 status
= libnet_connect_ads(r
->in
.domain_name
,
206 r
->in
.admin_password
,
208 if (!ADS_ERR_OK(status
)) {
209 libnet_unjoin_set_error_string(mem_ctx
, r
,
210 "failed to connect to AD: %s",
217 /****************************************************************
218 join a domain using ADS (LDAP mods)
219 ****************************************************************/
221 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
222 struct libnet_JoinCtx
*r
)
225 LDAPMessage
*res
= NULL
;
226 const char *attrs
[] = { "dn", NULL
};
229 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
230 if (!ADS_ERR_OK(status
)) {
234 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
235 if (!ADS_ERR_OK(status
)) {
239 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
240 ads_msgfree(r
->in
.ads
, res
);
241 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
244 ads_msgfree(r
->in
.ads
, res
);
246 /* Attempt to create the machine account and bail if this fails.
247 Assume that the admin wants exactly what they requested */
249 status
= ads_create_machine_acct(r
->in
.ads
,
253 if (ADS_ERR_OK(status
)) {
254 DEBUG(1,("machine account creation created\n"));
256 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
257 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
258 status
= ADS_SUCCESS
;
261 if (!ADS_ERR_OK(status
)) {
262 DEBUG(1,("machine account creation failed\n"));
266 status
= ads_move_machine_acct(r
->in
.ads
,
270 if (!ADS_ERR_OK(status
)) {
271 DEBUG(1,("failure to locate/move pre-existing "
272 "machine account\n"));
276 DEBUG(1,("The machine account %s the specified OU.\n",
277 moved
? "was moved into" : "already exists in"));
282 /****************************************************************
283 ****************************************************************/
285 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
286 struct libnet_UnjoinCtx
*r
)
291 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
292 if (!ADS_ERR_OK(status
)) {
293 libnet_unjoin_set_error_string(mem_ctx
, r
,
294 "failed to connect to AD: %s",
300 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
301 if (!ADS_ERR_OK(status
)) {
302 libnet_unjoin_set_error_string(mem_ctx
, r
,
303 "failed to leave realm: %s",
311 /****************************************************************
312 ****************************************************************/
314 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
315 struct libnet_JoinCtx
*r
)
318 LDAPMessage
*res
= NULL
;
321 if (!r
->in
.machine_name
) {
322 return ADS_ERROR(LDAP_NO_MEMORY
);
325 status
= ads_find_machine_acct(r
->in
.ads
,
328 if (!ADS_ERR_OK(status
)) {
332 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
333 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
337 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
339 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
343 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
345 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
350 ads_msgfree(r
->in
.ads
, res
);
356 /****************************************************************
357 Set a machines dNSHostName and servicePrincipalName attributes
358 ****************************************************************/
360 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
361 struct libnet_JoinCtx
*r
)
366 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
371 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
372 if (!ADS_ERR_OK(status
)) {
376 /* Windows only creates HOST/shortname & HOST/fqdn. */
378 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
380 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
385 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
386 || (strchr(my_fqdn
, '.') == NULL
)) {
387 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
388 r
->out
.dns_domain_name
);
393 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
394 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
396 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
401 mods
= ads_init_mods(mem_ctx
);
403 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
406 /* fields of primary importance */
408 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
409 if (!ADS_ERR_OK(status
)) {
410 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
413 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
415 if (!ADS_ERR_OK(status
)) {
416 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
419 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
422 /****************************************************************
423 ****************************************************************/
425 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
426 struct libnet_JoinCtx
*r
)
431 if (!r
->in
.create_upn
) {
437 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
438 if (!ADS_ERR_OK(status
)) {
443 r
->in
.upn
= talloc_asprintf(mem_ctx
,
446 r
->out
.dns_domain_name
);
448 return ADS_ERROR(LDAP_NO_MEMORY
);
452 /* now do the mods */
454 mods
= ads_init_mods(mem_ctx
);
456 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
459 /* fields of primary importance */
461 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
462 if (!ADS_ERR_OK(status
)) {
463 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
466 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
470 /****************************************************************
471 ****************************************************************/
473 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
474 struct libnet_JoinCtx
*r
)
480 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
486 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
487 if (!ADS_ERR_OK(status
)) {
491 /* now do the mods */
493 mods
= ads_init_mods(mem_ctx
);
495 return ADS_ERROR(LDAP_NO_MEMORY
);
498 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
500 return ADS_ERROR(LDAP_NO_MEMORY
);
503 /* fields of primary importance */
505 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
507 if (!ADS_ERR_OK(status
)) {
511 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
513 if (!ADS_ERR_OK(status
)) {
517 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
519 if (!ADS_ERR_OK(status
)) {
523 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
526 /****************************************************************
527 ****************************************************************/
529 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
530 struct libnet_JoinCtx
*r
)
532 if (!USE_SYSTEM_KEYTAB
) {
536 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
543 /****************************************************************
544 ****************************************************************/
546 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
547 struct libnet_JoinCtx
*r
)
549 uint32_t domain_func
;
551 const char *salt
= NULL
;
552 char *std_salt
= NULL
;
554 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
555 if (!ADS_ERR_OK(status
)) {
556 libnet_join_set_error_string(mem_ctx
, r
,
557 "failed to determine domain functional level: %s",
562 /* go ahead and setup the default salt */
564 std_salt
= kerberos_standard_des_salt();
566 libnet_join_set_error_string(mem_ctx
, r
,
567 "failed to obtain standard DES salt");
571 salt
= talloc_strdup(mem_ctx
, std_salt
);
578 /* if it's a Windows functional domain, we have to look for the UPN */
580 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
583 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
586 salt
= talloc_strdup(mem_ctx
, upn
);
593 return kerberos_secrets_store_des_salt(salt
);
596 /****************************************************************
597 ****************************************************************/
599 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
600 struct libnet_JoinCtx
*r
)
605 status
= libnet_join_connect_ads(mem_ctx
, r
);
606 if (!ADS_ERR_OK(status
)) {
611 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
612 if (!ADS_ERR_OK(status
)) {
613 libnet_join_set_error_string(mem_ctx
, r
,
614 "failed to set machine spn: %s",
619 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
620 if (!ADS_ERR_OK(status
)) {
621 libnet_join_set_error_string(mem_ctx
, r
,
622 "failed to set machine os attributes: %s",
627 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
628 if (!ADS_ERR_OK(status
)) {
629 libnet_join_set_error_string(mem_ctx
, r
,
630 "failed to set machine upn: %s",
635 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
636 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
639 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
640 libnet_join_set_error_string(mem_ctx
, r
,
641 "failed to create kerberos keytab");
642 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
647 #endif /* HAVE_ADS */
649 /****************************************************************
650 Store the machine password and domain SID
651 ****************************************************************/
653 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
654 struct libnet_JoinCtx
*r
)
656 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
659 DEBUG(1,("Failed to save domain sid\n"));
663 if (!secrets_store_machine_password(r
->in
.machine_password
,
664 r
->out
.netbios_domain_name
,
665 r
->in
.secure_channel_type
))
667 DEBUG(1,("Failed to save machine password\n"));
674 /****************************************************************
675 Connect dc's IPC$ share
676 ****************************************************************/
678 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
682 struct cli_state
**cli
)
687 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
690 if (use_kerberos
&& pass
) {
691 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
694 return cli_full_connection(cli
, NULL
,
705 /****************************************************************
706 Lookup domain dc's info
707 ****************************************************************/
709 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
710 struct libnet_JoinCtx
*r
,
711 struct cli_state
**cli
)
713 struct rpc_pipe_client
*pipe_hnd
= NULL
;
714 struct policy_handle lsa_pol
;
715 NTSTATUS status
, result
;
716 union lsa_PolicyInformation
*info
= NULL
;
717 struct dcerpc_binding_handle
*b
;
719 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
721 r
->in
.admin_password
,
724 if (!NT_STATUS_IS_OK(status
)) {
728 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
730 if (!NT_STATUS_IS_OK(status
)) {
731 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
736 b
= pipe_hnd
->binding_handle
;
738 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
739 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
740 if (!NT_STATUS_IS_OK(status
)) {
744 status
= dcerpc_lsa_QueryInfoPolicy2(b
, mem_ctx
,
749 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
750 r
->out
.domain_is_ad
= true;
751 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
752 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
753 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
754 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
755 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
758 if (!NT_STATUS_IS_OK(status
)) {
759 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
761 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
764 if (!NT_STATUS_IS_OK(status
)) {
767 if (!NT_STATUS_IS_OK(result
)) {
772 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
773 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
774 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
777 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
778 TALLOC_FREE(pipe_hnd
);
784 /****************************************************************
785 Do the domain join unsecure
786 ****************************************************************/
788 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
789 struct libnet_JoinCtx
*r
,
790 struct cli_state
*cli
)
792 struct rpc_pipe_client
*pipe_hnd
= NULL
;
793 unsigned char orig_trust_passwd_hash
[16];
794 unsigned char new_trust_passwd_hash
[16];
795 fstring trust_passwd
;
798 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
800 if (!NT_STATUS_IS_OK(status
)) {
804 if (!r
->in
.machine_password
) {
805 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
806 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
809 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
811 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
812 fstrcpy(trust_passwd
, r
->in
.admin_password
);
813 strlower_m(trust_passwd
);
816 * Machine names can be 15 characters, but the max length on
817 * a password is 14. --jerry
820 trust_passwd
[14] = '\0';
822 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
824 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
826 orig_trust_passwd_hash
,
827 r
->in
.machine_password
,
828 new_trust_passwd_hash
,
829 r
->in
.secure_channel_type
);
834 /****************************************************************
836 ****************************************************************/
838 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
839 struct libnet_JoinCtx
*r
,
840 struct cli_state
*cli
)
842 struct rpc_pipe_client
*pipe_hnd
= NULL
;
843 struct policy_handle sam_pol
, domain_pol
, user_pol
;
844 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
846 struct lsa_String lsa_acct_name
;
848 uint32_t acct_flags
= ACB_WSTRUST
;
849 struct samr_Ids user_rids
;
850 struct samr_Ids name_types
;
851 union samr_UserInfo user_info
;
852 struct dcerpc_binding_handle
*b
= NULL
;
854 struct samr_CryptPassword crypt_pwd
;
855 struct samr_CryptPasswordEx crypt_pwd_ex
;
857 ZERO_STRUCT(sam_pol
);
858 ZERO_STRUCT(domain_pol
);
859 ZERO_STRUCT(user_pol
);
861 switch (r
->in
.secure_channel_type
) {
863 acct_flags
= ACB_WSTRUST
;
866 acct_flags
= ACB_SVRTRUST
;
869 return NT_STATUS_INVALID_PARAMETER
;
872 if (!r
->in
.machine_password
) {
873 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
874 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
877 /* Open the domain */
879 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
881 if (!NT_STATUS_IS_OK(status
)) {
882 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
887 b
= pipe_hnd
->binding_handle
;
889 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
891 SAMR_ACCESS_ENUM_DOMAINS
892 | SAMR_ACCESS_LOOKUP_DOMAIN
,
895 if (!NT_STATUS_IS_OK(status
)) {
898 if (!NT_STATUS_IS_OK(result
)) {
903 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
905 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
906 | SAMR_DOMAIN_ACCESS_CREATE_USER
907 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
911 if (!NT_STATUS_IS_OK(status
)) {
914 if (!NT_STATUS_IS_OK(result
)) {
919 /* Create domain user */
921 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
922 strlower_m(acct_name
);
924 init_lsa_String(&lsa_acct_name
, acct_name
);
926 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
927 uint32_t access_desired
=
928 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
929 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
930 SAMR_USER_ACCESS_SET_PASSWORD
|
931 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
932 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
933 uint32_t access_granted
= 0;
935 DEBUG(10,("Creating account with desired access mask: %d\n",
938 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
947 if (!NT_STATUS_IS_OK(status
)) {
952 if (!NT_STATUS_IS_OK(status
) &&
953 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
955 DEBUG(10,("Creation of workstation account failed: %s\n",
958 /* If NT_STATUS_ACCESS_DENIED then we have a valid
959 username/password combo but the user does not have
960 administrator access. */
962 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
963 libnet_join_set_error_string(mem_ctx
, r
,
964 "User specified does not have "
965 "administrator privileges");
971 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
972 if (!(r
->in
.join_flags
&
973 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
978 /* We *must* do this.... don't ask... */
980 if (NT_STATUS_IS_OK(status
)) {
981 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
985 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
992 if (!NT_STATUS_IS_OK(status
)) {
995 if (!NT_STATUS_IS_OK(result
)) {
1000 if (name_types
.ids
[0] != SID_NAME_USER
) {
1001 DEBUG(0,("%s is not a user account (type=%d)\n",
1002 acct_name
, name_types
.ids
[0]));
1003 status
= NT_STATUS_INVALID_WORKSTATION
;
1007 user_rid
= user_rids
.ids
[0];
1009 /* Open handle on user */
1011 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1013 SEC_FLAG_MAXIMUM_ALLOWED
,
1017 if (!NT_STATUS_IS_OK(status
)) {
1020 if (!NT_STATUS_IS_OK(result
)) {
1025 /* Fill in the additional account flags now */
1027 acct_flags
|= ACB_PWNOEXP
;
1029 /* Set account flags on machine account */
1030 ZERO_STRUCT(user_info
.info16
);
1031 user_info
.info16
.acct_flags
= acct_flags
;
1033 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1038 if (!NT_STATUS_IS_OK(status
)) {
1039 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1043 libnet_join_set_error_string(mem_ctx
, r
,
1044 "Failed to set account flags for machine account (%s)\n",
1049 if (!NT_STATUS_IS_OK(result
)) {
1052 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1056 libnet_join_set_error_string(mem_ctx
, r
,
1057 "Failed to set account flags for machine account (%s)\n",
1062 /* Set password on machine account - first try level 26 */
1064 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1065 &cli
->user_session_key
,
1068 user_info
.info26
.password
= crypt_pwd_ex
;
1069 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1071 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1077 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
1079 /* retry with level 24 */
1081 init_samr_CryptPassword(r
->in
.machine_password
,
1082 &cli
->user_session_key
,
1085 user_info
.info24
.password
= crypt_pwd
;
1086 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1088 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1095 if (!NT_STATUS_IS_OK(status
)) {
1097 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1101 libnet_join_set_error_string(mem_ctx
, r
,
1102 "Failed to set password for machine account (%s)\n",
1106 if (!NT_STATUS_IS_OK(result
)) {
1109 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1113 libnet_join_set_error_string(mem_ctx
, r
,
1114 "Failed to set password for machine account (%s)\n",
1119 status
= NT_STATUS_OK
;
1126 if (is_valid_policy_hnd(&sam_pol
)) {
1127 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1129 if (is_valid_policy_hnd(&domain_pol
)) {
1130 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1132 if (is_valid_policy_hnd(&user_pol
)) {
1133 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1135 TALLOC_FREE(pipe_hnd
);
1140 /****************************************************************
1141 ****************************************************************/
1143 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1144 const char *machine_name
,
1145 const char *dc_name
)
1147 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1148 struct cli_state
*cli
= NULL
;
1149 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1150 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1152 char *machine_password
= NULL
;
1153 char *machine_account
= NULL
;
1156 return NT_STATUS_INVALID_PARAMETER
;
1159 if (!secrets_init()) {
1160 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1163 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1165 if (!machine_password
) {
1166 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1169 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1170 SAFE_FREE(machine_password
);
1171 return NT_STATUS_NO_MEMORY
;
1174 status
= cli_full_connection(&cli
, NULL
,
1183 free(machine_account
);
1184 free(machine_password
);
1186 if (!NT_STATUS_IS_OK(status
)) {
1187 status
= cli_full_connection(&cli
, NULL
,
1198 if (!NT_STATUS_IS_OK(status
)) {
1202 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1203 &neg_flags
, &netlogon_pipe
);
1204 if (!NT_STATUS_IS_OK(status
)) {
1205 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1207 return NT_STATUS_OK
;
1210 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1211 "key from server %s for domain %s. Error was %s\n",
1212 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1217 if (!lp_client_schannel()) {
1219 return NT_STATUS_OK
;
1222 status
= cli_rpc_pipe_open_schannel_with_key(
1223 cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
1224 DCERPC_AUTH_LEVEL_PRIVACY
,
1225 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1229 if (!NT_STATUS_IS_OK(status
)) {
1230 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1231 "on netlogon pipe to server %s for domain %s. "
1233 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1237 return NT_STATUS_OK
;
1240 /****************************************************************
1241 ****************************************************************/
1243 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1244 struct libnet_JoinCtx
*r
)
1248 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1251 if (!NT_STATUS_IS_OK(status
)) {
1252 libnet_join_set_error_string(mem_ctx
, r
,
1253 "failed to verify domain membership after joining: %s",
1254 get_friendly_nt_error_msg(status
));
1255 return WERR_SETUP_NOT_JOINED
;
1261 /****************************************************************
1262 ****************************************************************/
1264 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1265 struct libnet_UnjoinCtx
*r
)
1267 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1271 if (!secrets_delete_domain_sid(lp_workgroup())) {
1278 /****************************************************************
1279 ****************************************************************/
1281 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1282 struct libnet_UnjoinCtx
*r
)
1284 struct cli_state
*cli
= NULL
;
1285 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1286 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1287 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1290 struct lsa_String lsa_acct_name
;
1291 struct samr_Ids user_rids
;
1292 struct samr_Ids name_types
;
1293 union samr_UserInfo
*info
= NULL
;
1294 struct dcerpc_binding_handle
*b
= NULL
;
1296 ZERO_STRUCT(sam_pol
);
1297 ZERO_STRUCT(domain_pol
);
1298 ZERO_STRUCT(user_pol
);
1300 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1301 r
->in
.admin_account
,
1302 r
->in
.admin_password
,
1305 if (!NT_STATUS_IS_OK(status
)) {
1309 /* Open the domain */
1311 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1313 if (!NT_STATUS_IS_OK(status
)) {
1314 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1315 nt_errstr(status
)));
1319 b
= pipe_hnd
->binding_handle
;
1321 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1323 SEC_FLAG_MAXIMUM_ALLOWED
,
1326 if (!NT_STATUS_IS_OK(status
)) {
1329 if (!NT_STATUS_IS_OK(result
)) {
1334 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1336 SEC_FLAG_MAXIMUM_ALLOWED
,
1340 if (!NT_STATUS_IS_OK(status
)) {
1343 if (!NT_STATUS_IS_OK(result
)) {
1348 /* Create domain user */
1350 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1351 strlower_m(acct_name
);
1353 init_lsa_String(&lsa_acct_name
, acct_name
);
1355 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1363 if (!NT_STATUS_IS_OK(status
)) {
1366 if (!NT_STATUS_IS_OK(result
)) {
1371 if (name_types
.ids
[0] != SID_NAME_USER
) {
1372 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1373 name_types
.ids
[0]));
1374 status
= NT_STATUS_INVALID_WORKSTATION
;
1378 user_rid
= user_rids
.ids
[0];
1380 /* Open handle on user */
1382 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1384 SEC_FLAG_MAXIMUM_ALLOWED
,
1388 if (!NT_STATUS_IS_OK(status
)) {
1391 if (!NT_STATUS_IS_OK(result
)) {
1398 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1403 if (!NT_STATUS_IS_OK(status
)) {
1404 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1407 if (!NT_STATUS_IS_OK(result
)) {
1409 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1413 /* now disable and setuser info */
1415 info
->info16
.acct_flags
|= ACB_DISABLED
;
1417 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1422 if (!NT_STATUS_IS_OK(status
)) {
1423 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1426 if (!NT_STATUS_IS_OK(result
)) {
1428 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1432 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1435 if (pipe_hnd
&& b
) {
1436 if (is_valid_policy_hnd(&domain_pol
)) {
1437 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1439 if (is_valid_policy_hnd(&sam_pol
)) {
1440 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1442 TALLOC_FREE(pipe_hnd
);
1452 /****************************************************************
1453 ****************************************************************/
1455 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1457 WERROR werr
= WERR_OK
;
1459 struct smbconf_ctx
*ctx
;
1461 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1462 if (!SBC_ERROR_IS_OK(err
)) {
1463 werr
= WERR_NO_SUCH_SERVICE
;
1467 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1469 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1470 if (!SBC_ERROR_IS_OK(err
)) {
1471 werr
= WERR_NO_SUCH_SERVICE
;
1475 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1477 if (!SBC_ERROR_IS_OK(err
)) {
1478 werr
= WERR_NO_SUCH_SERVICE
;
1482 smbconf_delete_global_parameter(ctx
, "realm");
1486 err
= smbconf_set_global_parameter(ctx
, "security", "domain");
1487 if (!SBC_ERROR_IS_OK(err
)) {
1488 werr
= WERR_NO_SUCH_SERVICE
;
1492 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1493 r
->out
.netbios_domain_name
);
1494 if (!SBC_ERROR_IS_OK(err
)) {
1495 werr
= WERR_NO_SUCH_SERVICE
;
1499 if (r
->out
.domain_is_ad
) {
1500 err
= smbconf_set_global_parameter(ctx
, "security", "ads");
1501 if (!SBC_ERROR_IS_OK(err
)) {
1502 werr
= WERR_NO_SUCH_SERVICE
;
1506 err
= smbconf_set_global_parameter(ctx
, "realm",
1507 r
->out
.dns_domain_name
);
1508 if (!SBC_ERROR_IS_OK(err
)) {
1509 werr
= WERR_NO_SUCH_SERVICE
;
1515 smbconf_shutdown(ctx
);
1519 /****************************************************************
1520 ****************************************************************/
1522 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1524 WERROR werr
= WERR_OK
;
1526 struct smbconf_ctx
*ctx
;
1528 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1529 if (!SBC_ERROR_IS_OK(err
)) {
1530 werr
= WERR_NO_SUCH_SERVICE
;
1534 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1536 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1537 if (!SBC_ERROR_IS_OK(err
)) {
1538 werr
= WERR_NO_SUCH_SERVICE
;
1542 err
= smbconf_delete_global_parameter(ctx
, "workgroup");
1543 if (!SBC_ERROR_IS_OK(err
)) {
1544 werr
= WERR_NO_SUCH_SERVICE
;
1548 smbconf_delete_global_parameter(ctx
, "realm");
1552 smbconf_shutdown(ctx
);
1556 /****************************************************************
1557 ****************************************************************/
1559 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1563 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1564 return r
->out
.result
;
1567 if (!r
->in
.modify_config
) {
1571 werr
= do_join_modify_vals_config(r
);
1572 if (!W_ERROR_IS_OK(werr
)) {
1576 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1578 r
->out
.modified_config
= true;
1579 r
->out
.result
= werr
;
1584 /****************************************************************
1585 ****************************************************************/
1587 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1591 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1592 return r
->out
.result
;
1595 if (!r
->in
.modify_config
) {
1599 werr
= do_unjoin_modify_vals_config(r
);
1600 if (!W_ERROR_IS_OK(werr
)) {
1604 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1606 r
->out
.modified_config
= true;
1607 r
->out
.result
= werr
;
1612 /****************************************************************
1613 ****************************************************************/
1615 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1616 const char *domain_str
,
1617 const char **domain_p
,
1620 char *domain
= NULL
;
1622 const char *p
= NULL
;
1624 if (!domain_str
|| !domain_p
|| !dc_p
) {
1628 p
= strchr_m(domain_str
, '\\');
1631 domain
= talloc_strndup(mem_ctx
, domain_str
,
1632 PTR_DIFF(p
, domain_str
));
1633 dc
= talloc_strdup(mem_ctx
, p
+1);
1638 domain
= talloc_strdup(mem_ctx
, domain_str
);
1654 /****************************************************************
1655 ****************************************************************/
1657 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1658 struct libnet_JoinCtx
*r
)
1660 if (!r
->in
.domain_name
) {
1661 libnet_join_set_error_string(mem_ctx
, r
,
1662 "No domain name defined");
1663 return WERR_INVALID_PARAM
;
1666 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1669 libnet_join_set_error_string(mem_ctx
, r
,
1670 "Failed to parse domain name");
1671 return WERR_INVALID_PARAM
;
1675 return WERR_SETUP_DOMAIN_CONTROLLER
;
1678 if (!secrets_init()) {
1679 libnet_join_set_error_string(mem_ctx
, r
,
1680 "Unable to open secrets database");
1681 return WERR_CAN_NOT_COMPLETE
;
1687 /****************************************************************
1688 ****************************************************************/
1690 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1694 /* Try adding dom admins to builtin\admins. Only log failures. */
1695 status
= create_builtin_administrators(domain_sid
);
1696 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1697 DEBUG(10,("Unable to auto-add domain administrators to "
1698 "BUILTIN\\Administrators during join because "
1699 "winbindd must be running."));
1700 } else if (!NT_STATUS_IS_OK(status
)) {
1701 DEBUG(5, ("Failed to auto-add domain administrators to "
1702 "BUILTIN\\Administrators during join: %s\n",
1703 nt_errstr(status
)));
1706 /* Try adding dom users to builtin\users. Only log failures. */
1707 status
= create_builtin_users(domain_sid
);
1708 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1709 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1710 "during join because winbindd must be running."));
1711 } else if (!NT_STATUS_IS_OK(status
)) {
1712 DEBUG(5, ("Failed to auto-add domain administrators to "
1713 "BUILTIN\\Administrators during join: %s\n",
1714 nt_errstr(status
)));
1718 /****************************************************************
1719 ****************************************************************/
1721 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1722 struct libnet_JoinCtx
*r
)
1726 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1727 return r
->out
.result
;
1730 werr
= do_JoinConfig(r
);
1731 if (!W_ERROR_IS_OK(werr
)) {
1735 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1739 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1740 if (r
->out
.dns_domain_name
) {
1741 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1745 if (r
->out
.domain_is_ad
&&
1746 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1747 ADS_STATUS ads_status
;
1749 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1750 if (!ADS_ERR_OK(ads_status
)) {
1751 return WERR_GENERAL_FAILURE
;
1754 #endif /* HAVE_ADS */
1756 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1761 /****************************************************************
1762 ****************************************************************/
1764 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1767 ads_destroy(&r
->in
.ads
);
1773 /****************************************************************
1774 ****************************************************************/
1776 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1779 ads_destroy(&r
->in
.ads
);
1785 /****************************************************************
1786 ****************************************************************/
1788 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1789 struct libnet_JoinCtx
**r
)
1791 struct libnet_JoinCtx
*ctx
;
1793 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1798 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1800 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1801 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1803 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1810 /****************************************************************
1811 ****************************************************************/
1813 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1814 struct libnet_UnjoinCtx
**r
)
1816 struct libnet_UnjoinCtx
*ctx
;
1818 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1823 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1825 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1826 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1833 /****************************************************************
1834 ****************************************************************/
1836 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1837 struct libnet_JoinCtx
*r
)
1839 bool valid_security
= false;
1840 bool valid_workgroup
= false;
1841 bool valid_realm
= false;
1843 /* check if configuration is already set correctly */
1845 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1847 switch (r
->out
.domain_is_ad
) {
1849 valid_security
= (lp_security() == SEC_DOMAIN
);
1850 if (valid_workgroup
&& valid_security
) {
1851 /* nothing to be done */
1856 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1857 switch (lp_security()) {
1860 valid_security
= true;
1863 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1864 /* nothing to be done */
1870 /* check if we are supposed to manipulate configuration */
1872 if (!r
->in
.modify_config
) {
1874 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1876 if (!valid_workgroup
) {
1877 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1878 "\"workgroup\" set to '%s', should be '%s'",
1879 lp_workgroup(), r
->out
.netbios_domain_name
);
1880 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1884 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1885 "\"realm\" set to '%s', should be '%s'",
1886 lp_realm(), r
->out
.dns_domain_name
);
1887 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1890 if (!valid_security
) {
1891 const char *sec
= NULL
;
1892 switch (lp_security()) {
1893 case SEC_SHARE
: sec
= "share"; break;
1894 case SEC_USER
: sec
= "user"; break;
1895 case SEC_DOMAIN
: sec
= "domain"; break;
1896 case SEC_ADS
: sec
= "ads"; break;
1898 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1899 "\"security\" set to '%s', should be %s",
1900 sec
, r
->out
.domain_is_ad
?
1901 "either 'domain' or 'ads'" : "'domain'");
1902 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1905 libnet_join_set_error_string(mem_ctx
, r
,
1906 "Invalid configuration (%s) and configuration modification "
1907 "was not requested", wrong_conf
);
1908 return WERR_CAN_NOT_COMPLETE
;
1911 /* check if we are able to manipulate configuration */
1913 if (!lp_config_backend_is_registry()) {
1914 libnet_join_set_error_string(mem_ctx
, r
,
1915 "Configuration manipulation requested but not "
1916 "supported by backend");
1917 return WERR_NOT_SUPPORTED
;
1923 /****************************************************************
1924 ****************************************************************/
1926 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1927 struct libnet_JoinCtx
*r
)
1931 struct cli_state
*cli
= NULL
;
1933 ADS_STATUS ads_status
;
1934 #endif /* HAVE_ADS */
1936 if (!r
->in
.dc_name
) {
1937 struct netr_DsRGetDCNameInfo
*info
;
1939 status
= dsgetdcname(mem_ctx
,
1944 DS_FORCE_REDISCOVERY
|
1945 DS_DIRECTORY_SERVICE_REQUIRED
|
1946 DS_WRITABLE_REQUIRED
|
1949 if (!NT_STATUS_IS_OK(status
)) {
1950 libnet_join_set_error_string(mem_ctx
, r
,
1951 "failed to find DC for domain %s",
1953 get_friendly_nt_error_msg(status
));
1954 return WERR_DCNOTFOUND
;
1957 dc
= strip_hostname(info
->dc_unc
);
1958 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1959 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1962 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1963 if (!NT_STATUS_IS_OK(status
)) {
1964 libnet_join_set_error_string(mem_ctx
, r
,
1965 "failed to lookup DC info for domain '%s' over rpc: %s",
1966 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1967 return ntstatus_to_werror(status
);
1970 werr
= libnet_join_check_config(mem_ctx
, r
);
1971 if (!W_ERROR_IS_OK(werr
)) {
1977 create_local_private_krb5_conf_for_domain(
1978 r
->out
.dns_domain_name
, r
->out
.netbios_domain_name
,
1979 NULL
, &cli
->dest_ss
, cli
->desthost
);
1981 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
1982 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1984 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1985 if (!ADS_ERR_OK(ads_status
)) {
1986 return WERR_DEFAULT_JOIN_REQUIRED
;
1989 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1990 if (!ADS_ERR_OK(ads_status
)) {
1991 libnet_join_set_error_string(mem_ctx
, r
,
1992 "failed to precreate account in ou %s: %s",
1994 ads_errstr(ads_status
));
1995 return WERR_DEFAULT_JOIN_REQUIRED
;
1998 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
2000 #endif /* HAVE_ADS */
2002 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
2003 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
2004 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2006 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2008 if (!NT_STATUS_IS_OK(status
)) {
2009 libnet_join_set_error_string(mem_ctx
, r
,
2010 "failed to join domain '%s' over rpc: %s",
2011 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2012 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2013 return WERR_SETUP_ALREADY_JOINED
;
2015 werr
= ntstatus_to_werror(status
);
2019 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2020 werr
= WERR_SETUP_NOT_JOINED
;
2034 /****************************************************************
2035 ****************************************************************/
2037 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2038 struct libnet_JoinCtx
*r
)
2041 struct libnet_UnjoinCtx
*u
= NULL
;
2043 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2044 if (!W_ERROR_IS_OK(werr
)) {
2048 u
->in
.debug
= r
->in
.debug
;
2049 u
->in
.dc_name
= r
->in
.dc_name
;
2050 u
->in
.domain_name
= r
->in
.domain_name
;
2051 u
->in
.admin_account
= r
->in
.admin_account
;
2052 u
->in
.admin_password
= r
->in
.admin_password
;
2053 u
->in
.modify_config
= r
->in
.modify_config
;
2054 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2055 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2057 werr
= libnet_Unjoin(mem_ctx
, u
);
2063 /****************************************************************
2064 ****************************************************************/
2066 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2067 struct libnet_JoinCtx
*r
)
2072 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2075 ZERO_STRUCT(r
->out
);
2077 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2078 if (!W_ERROR_IS_OK(werr
)) {
2082 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2083 werr
= libnet_DomainJoin(mem_ctx
, r
);
2084 if (!W_ERROR_IS_OK(werr
)) {
2089 werr
= libnet_join_post_processing(mem_ctx
, r
);
2090 if (!W_ERROR_IS_OK(werr
)) {
2094 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2095 werr
= libnet_join_post_verify(mem_ctx
, r
);
2096 if (!W_ERROR_IS_OK(werr
)) {
2097 libnet_join_rollback(mem_ctx
, r
);
2102 r
->out
.result
= werr
;
2105 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2110 /****************************************************************
2111 ****************************************************************/
2113 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2114 struct libnet_UnjoinCtx
*r
)
2118 if (!r
->in
.domain_sid
) {
2120 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2121 libnet_unjoin_set_error_string(mem_ctx
, r
,
2122 "Unable to fetch domain sid: are we joined?");
2123 return WERR_SETUP_NOT_JOINED
;
2125 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2126 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2129 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2130 !r
->in
.delete_machine_account
) {
2131 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2135 if (!r
->in
.dc_name
) {
2136 struct netr_DsRGetDCNameInfo
*info
;
2138 status
= dsgetdcname(mem_ctx
,
2143 DS_DIRECTORY_SERVICE_REQUIRED
|
2144 DS_WRITABLE_REQUIRED
|
2147 if (!NT_STATUS_IS_OK(status
)) {
2148 libnet_unjoin_set_error_string(mem_ctx
, r
,
2149 "failed to find DC for domain %s",
2151 get_friendly_nt_error_msg(status
));
2152 return WERR_DCNOTFOUND
;
2155 dc
= strip_hostname(info
->dc_unc
);
2156 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2157 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2161 /* for net ads leave, try to delete the account. If it works,
2162 no sense in disabling. If it fails, we can still try to
2165 if (r
->in
.delete_machine_account
) {
2166 ADS_STATUS ads_status
;
2167 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2168 if (ADS_ERR_OK(ads_status
)) {
2170 r
->out
.dns_domain_name
=
2171 talloc_strdup(mem_ctx
,
2172 r
->in
.ads
->server
.realm
);
2174 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2176 if (!ADS_ERR_OK(ads_status
)) {
2177 libnet_unjoin_set_error_string(mem_ctx
, r
,
2178 "failed to remove machine account from AD: %s",
2179 ads_errstr(ads_status
));
2181 r
->out
.deleted_machine_account
= true;
2182 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2183 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2187 #endif /* HAVE_ADS */
2189 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2191 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2192 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2193 if (!NT_STATUS_IS_OK(status
)) {
2194 libnet_unjoin_set_error_string(mem_ctx
, r
,
2195 "failed to disable machine account via rpc: %s",
2196 get_friendly_nt_error_msg(status
));
2197 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2198 return WERR_SETUP_NOT_JOINED
;
2200 return ntstatus_to_werror(status
);
2203 r
->out
.disabled_machine_account
= true;
2206 /* If disable succeeded or was not requested at all, we
2207 should be getting rid of our end of things */
2209 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2214 /****************************************************************
2215 ****************************************************************/
2217 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2218 struct libnet_UnjoinCtx
*r
)
2220 if (!r
->in
.domain_name
) {
2221 libnet_unjoin_set_error_string(mem_ctx
, r
,
2222 "No domain name defined");
2223 return WERR_INVALID_PARAM
;
2226 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2229 libnet_unjoin_set_error_string(mem_ctx
, r
,
2230 "Failed to parse domain name");
2231 return WERR_INVALID_PARAM
;
2235 return WERR_SETUP_DOMAIN_CONTROLLER
;
2238 if (!secrets_init()) {
2239 libnet_unjoin_set_error_string(mem_ctx
, r
,
2240 "Unable to open secrets database");
2241 return WERR_CAN_NOT_COMPLETE
;
2247 /****************************************************************
2248 ****************************************************************/
2250 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2251 struct libnet_UnjoinCtx
*r
)
2253 saf_delete(r
->out
.netbios_domain_name
);
2254 saf_delete(r
->out
.dns_domain_name
);
2256 return libnet_unjoin_config(r
);
2259 /****************************************************************
2260 ****************************************************************/
2262 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2263 struct libnet_UnjoinCtx
*r
)
2268 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2271 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2272 if (!W_ERROR_IS_OK(werr
)) {
2276 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2277 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2278 if (!W_ERROR_IS_OK(werr
)) {
2279 libnet_unjoin_config(r
);
2284 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2285 if (!W_ERROR_IS_OK(werr
)) {
2290 r
->out
.result
= werr
;
2293 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);