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"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/param/loadparm.h"
44 /****************************************************************
45 ****************************************************************/
47 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
50 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
51 DEBUG(1,("libnet_Join:\n%s", str)); \
55 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
56 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
57 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
58 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
60 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
63 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
64 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
68 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
69 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
70 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
71 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
73 /****************************************************************
74 ****************************************************************/
76 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
77 struct libnet_JoinCtx
*r
,
78 const char *format
, ...)
82 if (r
->out
.error_string
) {
86 va_start(args
, format
);
87 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
91 /****************************************************************
92 ****************************************************************/
94 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
95 struct libnet_UnjoinCtx
*r
,
96 const char *format
, ...)
100 if (r
->out
.error_string
) {
104 va_start(args
, format
);
105 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
111 /****************************************************************
112 ****************************************************************/
114 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
115 const char *netbios_domain_name
,
117 const char *user_name
,
118 const char *password
,
122 ADS_STRUCT
*my_ads
= NULL
;
125 my_ads
= ads_init(dns_domain_name
,
129 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
133 SAFE_FREE(my_ads
->auth
.user_name
);
134 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
135 if ((cp
= strchr_m(my_ads
->auth
.user_name
, '@'))!=0) {
137 SAFE_FREE(my_ads
->auth
.realm
);
138 my_ads
->auth
.realm
= smb_xstrdup(cp
);
139 if (!strupper_m(my_ads
->auth
.realm
)) {
140 ads_destroy(&my_ads
);
141 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
147 SAFE_FREE(my_ads
->auth
.password
);
148 my_ads
->auth
.password
= SMB_STRDUP(password
);
151 status
= ads_connect_user_creds(my_ads
);
152 if (!ADS_ERR_OK(status
)) {
153 ads_destroy(&my_ads
);
161 /****************************************************************
162 ****************************************************************/
164 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
165 struct libnet_JoinCtx
*r
)
169 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
170 r
->out
.netbios_domain_name
,
173 r
->in
.admin_password
,
175 if (!ADS_ERR_OK(status
)) {
176 libnet_join_set_error_string(mem_ctx
, r
,
177 "failed to connect to AD: %s",
182 if (!r
->out
.netbios_domain_name
) {
183 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
184 r
->in
.ads
->server
.workgroup
);
185 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
188 if (!r
->out
.dns_domain_name
) {
189 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
190 r
->in
.ads
->config
.realm
);
191 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
194 r
->out
.domain_is_ad
= true;
199 /****************************************************************
200 ****************************************************************/
202 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
203 struct libnet_UnjoinCtx
*r
)
207 status
= libnet_connect_ads(r
->in
.domain_name
,
211 r
->in
.admin_password
,
213 if (!ADS_ERR_OK(status
)) {
214 libnet_unjoin_set_error_string(mem_ctx
, r
,
215 "failed to connect to AD: %s",
222 /****************************************************************
223 join a domain using ADS (LDAP mods)
224 ****************************************************************/
226 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
227 struct libnet_JoinCtx
*r
)
230 LDAPMessage
*res
= NULL
;
231 const char *attrs
[] = { "dn", NULL
};
234 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
235 if (!ADS_ERR_OK(status
)) {
239 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
240 if (!ADS_ERR_OK(status
)) {
244 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
245 ads_msgfree(r
->in
.ads
, res
);
246 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
249 ads_msgfree(r
->in
.ads
, res
);
251 /* Attempt to create the machine account and bail if this fails.
252 Assume that the admin wants exactly what they requested */
254 status
= ads_create_machine_acct(r
->in
.ads
,
258 if (ADS_ERR_OK(status
)) {
259 DEBUG(1,("machine account creation created\n"));
261 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
262 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
263 status
= ADS_SUCCESS
;
266 if (!ADS_ERR_OK(status
)) {
267 DEBUG(1,("machine account creation failed\n"));
271 status
= ads_move_machine_acct(r
->in
.ads
,
275 if (!ADS_ERR_OK(status
)) {
276 DEBUG(1,("failure to locate/move pre-existing "
277 "machine account\n"));
281 DEBUG(1,("The machine account %s the specified OU.\n",
282 moved
? "was moved into" : "already exists in"));
287 /****************************************************************
288 ****************************************************************/
290 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
291 struct libnet_UnjoinCtx
*r
)
296 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
297 if (!ADS_ERR_OK(status
)) {
298 libnet_unjoin_set_error_string(mem_ctx
, r
,
299 "failed to connect to AD: %s",
305 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
306 if (!ADS_ERR_OK(status
)) {
307 libnet_unjoin_set_error_string(mem_ctx
, r
,
308 "failed to leave realm: %s",
316 /****************************************************************
317 ****************************************************************/
319 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
320 struct libnet_JoinCtx
*r
)
323 LDAPMessage
*res
= NULL
;
326 if (!r
->in
.machine_name
) {
327 return ADS_ERROR(LDAP_NO_MEMORY
);
330 status
= ads_find_machine_acct(r
->in
.ads
,
333 if (!ADS_ERR_OK(status
)) {
337 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
338 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
342 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
344 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
348 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
350 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
355 ads_msgfree(r
->in
.ads
, res
);
361 /****************************************************************
362 Set a machines dNSHostName and servicePrincipalName attributes
363 ****************************************************************/
365 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
366 struct libnet_JoinCtx
*r
)
371 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
376 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
377 if (!ADS_ERR_OK(status
)) {
381 /* Windows only creates HOST/shortname & HOST/fqdn. */
383 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
385 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
387 if (!strupper_m(spn
)) {
388 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
392 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
393 || (strchr(my_fqdn
, '.') == NULL
)) {
394 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
395 r
->out
.dns_domain_name
);
398 if (!strlower_m(my_fqdn
)) {
399 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
402 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
403 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
405 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
410 mods
= ads_init_mods(mem_ctx
);
412 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
415 /* fields of primary importance */
417 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
418 if (!ADS_ERR_OK(status
)) {
419 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
422 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
424 if (!ADS_ERR_OK(status
)) {
425 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
428 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
431 /****************************************************************
432 ****************************************************************/
434 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
435 struct libnet_JoinCtx
*r
)
440 if (!r
->in
.create_upn
) {
446 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
447 if (!ADS_ERR_OK(status
)) {
452 r
->in
.upn
= talloc_asprintf(mem_ctx
,
455 r
->out
.dns_domain_name
);
457 return ADS_ERROR(LDAP_NO_MEMORY
);
461 /* now do the mods */
463 mods
= ads_init_mods(mem_ctx
);
465 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
468 /* fields of primary importance */
470 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
471 if (!ADS_ERR_OK(status
)) {
472 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
475 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
479 /****************************************************************
480 ****************************************************************/
482 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
483 struct libnet_JoinCtx
*r
)
489 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
495 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
496 if (!ADS_ERR_OK(status
)) {
500 /* now do the mods */
502 mods
= ads_init_mods(mem_ctx
);
504 return ADS_ERROR(LDAP_NO_MEMORY
);
507 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
509 return ADS_ERROR(LDAP_NO_MEMORY
);
512 /* fields of primary importance */
514 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
516 if (!ADS_ERR_OK(status
)) {
520 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
522 if (!ADS_ERR_OK(status
)) {
526 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
528 if (!ADS_ERR_OK(status
)) {
532 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
535 /****************************************************************
536 ****************************************************************/
538 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
539 struct libnet_JoinCtx
*r
)
541 if (!USE_SYSTEM_KEYTAB
) {
545 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
552 /****************************************************************
553 ****************************************************************/
555 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
556 struct libnet_JoinCtx
*r
)
558 uint32_t domain_func
;
560 const char *salt
= NULL
;
561 char *std_salt
= NULL
;
563 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
564 if (!ADS_ERR_OK(status
)) {
565 libnet_join_set_error_string(mem_ctx
, r
,
566 "failed to determine domain functional level: %s",
571 /* go ahead and setup the default salt */
573 std_salt
= kerberos_standard_des_salt();
575 libnet_join_set_error_string(mem_ctx
, r
,
576 "failed to obtain standard DES salt");
580 salt
= talloc_strdup(mem_ctx
, std_salt
);
587 /* if it's a Windows functional domain, we have to look for the UPN */
589 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
592 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
595 salt
= talloc_strdup(mem_ctx
, upn
);
602 return kerberos_secrets_store_des_salt(salt
);
605 /****************************************************************
606 ****************************************************************/
608 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
609 struct libnet_JoinCtx
*r
)
614 status
= libnet_join_connect_ads(mem_ctx
, r
);
615 if (!ADS_ERR_OK(status
)) {
620 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
621 if (!ADS_ERR_OK(status
)) {
622 libnet_join_set_error_string(mem_ctx
, r
,
623 "failed to set machine spn: %s",
628 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
629 if (!ADS_ERR_OK(status
)) {
630 libnet_join_set_error_string(mem_ctx
, r
,
631 "failed to set machine os attributes: %s",
636 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
637 if (!ADS_ERR_OK(status
)) {
638 libnet_join_set_error_string(mem_ctx
, r
,
639 "failed to set machine upn: %s",
644 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
645 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
648 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
649 libnet_join_set_error_string(mem_ctx
, r
,
650 "failed to create kerberos keytab");
651 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
656 #endif /* HAVE_ADS */
658 /****************************************************************
659 Store the machine password and domain SID
660 ****************************************************************/
662 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
663 struct libnet_JoinCtx
*r
)
665 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
668 DEBUG(1,("Failed to save domain sid\n"));
672 if (!secrets_store_machine_password(r
->in
.machine_password
,
673 r
->out
.netbios_domain_name
,
674 r
->in
.secure_channel_type
))
676 DEBUG(1,("Failed to save machine password\n"));
683 /****************************************************************
684 Connect dc's IPC$ share
685 ****************************************************************/
687 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
691 struct cli_state
**cli
)
696 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
699 if (use_kerberos
&& pass
) {
700 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
703 return cli_full_connection(cli
, NULL
,
711 SMB_SIGNING_DEFAULT
);
714 /****************************************************************
715 Lookup domain dc's info
716 ****************************************************************/
718 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
719 struct libnet_JoinCtx
*r
,
720 struct cli_state
**cli
)
722 struct rpc_pipe_client
*pipe_hnd
= NULL
;
723 struct policy_handle lsa_pol
;
724 NTSTATUS status
, result
;
725 union lsa_PolicyInformation
*info
= NULL
;
726 struct dcerpc_binding_handle
*b
;
728 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
730 r
->in
.admin_password
,
733 if (!NT_STATUS_IS_OK(status
)) {
737 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
739 if (!NT_STATUS_IS_OK(status
)) {
740 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
745 b
= pipe_hnd
->binding_handle
;
747 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
748 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
749 if (!NT_STATUS_IS_OK(status
)) {
753 status
= dcerpc_lsa_QueryInfoPolicy2(b
, mem_ctx
,
758 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
759 r
->out
.domain_is_ad
= true;
760 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
761 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
762 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
763 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
764 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
767 if (!NT_STATUS_IS_OK(status
)) {
768 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
770 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
773 if (!NT_STATUS_IS_OK(status
)) {
776 if (!NT_STATUS_IS_OK(result
)) {
781 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
782 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
783 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
786 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
787 TALLOC_FREE(pipe_hnd
);
793 /****************************************************************
794 Do the domain join unsecure
795 ****************************************************************/
797 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
798 struct libnet_JoinCtx
*r
,
799 struct cli_state
*cli
)
801 struct rpc_pipe_client
*pipe_hnd
= NULL
;
802 unsigned char orig_trust_passwd_hash
[16];
803 unsigned char new_trust_passwd_hash
[16];
804 fstring trust_passwd
;
807 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
809 if (!NT_STATUS_IS_OK(status
)) {
813 if (!r
->in
.machine_password
) {
814 r
->in
.machine_password
= generate_random_password(mem_ctx
,
815 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
,
816 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
817 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
820 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
822 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
823 fstrcpy(trust_passwd
, r
->in
.admin_password
);
824 if (!strlower_m(trust_passwd
)) {
825 return NT_STATUS_INVALID_PARAMETER
;
829 * Machine names can be 15 characters, but the max length on
830 * a password is 14. --jerry
833 trust_passwd
[14] = '\0';
835 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
837 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
839 orig_trust_passwd_hash
,
840 r
->in
.machine_password
,
841 new_trust_passwd_hash
,
842 r
->in
.secure_channel_type
);
847 /****************************************************************
849 ****************************************************************/
851 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
852 struct libnet_JoinCtx
*r
,
853 struct cli_state
*cli
)
855 struct rpc_pipe_client
*pipe_hnd
= NULL
;
856 struct policy_handle sam_pol
, domain_pol
, user_pol
;
857 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
859 struct lsa_String lsa_acct_name
;
861 uint32_t acct_flags
= ACB_WSTRUST
;
862 struct samr_Ids user_rids
;
863 struct samr_Ids name_types
;
864 union samr_UserInfo user_info
;
865 struct dcerpc_binding_handle
*b
= NULL
;
867 DATA_BLOB session_key
= data_blob_null
;
868 struct samr_CryptPassword crypt_pwd
;
869 struct samr_CryptPasswordEx crypt_pwd_ex
;
871 ZERO_STRUCT(sam_pol
);
872 ZERO_STRUCT(domain_pol
);
873 ZERO_STRUCT(user_pol
);
875 switch (r
->in
.secure_channel_type
) {
877 acct_flags
= ACB_WSTRUST
;
880 acct_flags
= ACB_SVRTRUST
;
883 return NT_STATUS_INVALID_PARAMETER
;
886 if (!r
->in
.machine_password
) {
887 r
->in
.machine_password
= generate_random_password(mem_ctx
,
888 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
,
889 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
890 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
893 /* Open the domain */
895 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
897 if (!NT_STATUS_IS_OK(status
)) {
898 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
903 b
= pipe_hnd
->binding_handle
;
905 status
= cli_get_session_key(mem_ctx
, pipe_hnd
, &session_key
);
906 if (!NT_STATUS_IS_OK(status
)) {
907 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
912 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
914 SAMR_ACCESS_ENUM_DOMAINS
915 | SAMR_ACCESS_LOOKUP_DOMAIN
,
918 if (!NT_STATUS_IS_OK(status
)) {
921 if (!NT_STATUS_IS_OK(result
)) {
926 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
928 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
929 | SAMR_DOMAIN_ACCESS_CREATE_USER
930 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
934 if (!NT_STATUS_IS_OK(status
)) {
937 if (!NT_STATUS_IS_OK(result
)) {
942 /* Create domain user */
944 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
945 if (!strlower_m(acct_name
)) {
946 status
= NT_STATUS_INVALID_PARAMETER
;
950 init_lsa_String(&lsa_acct_name
, acct_name
);
952 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
953 uint32_t access_desired
=
954 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
955 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
956 SAMR_USER_ACCESS_SET_PASSWORD
|
957 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
958 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
959 uint32_t access_granted
= 0;
961 DEBUG(10,("Creating account with desired access mask: %d\n",
964 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
973 if (!NT_STATUS_IS_OK(status
)) {
978 if (!NT_STATUS_IS_OK(status
) &&
979 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
981 DEBUG(10,("Creation of workstation account failed: %s\n",
984 /* If NT_STATUS_ACCESS_DENIED then we have a valid
985 username/password combo but the user does not have
986 administrator access. */
988 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
989 libnet_join_set_error_string(mem_ctx
, r
,
990 "User specified does not have "
991 "administrator privileges");
997 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
998 if (!(r
->in
.join_flags
&
999 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
1004 /* We *must* do this.... don't ask... */
1006 if (NT_STATUS_IS_OK(status
)) {
1007 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1011 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1018 if (!NT_STATUS_IS_OK(status
)) {
1021 if (!NT_STATUS_IS_OK(result
)) {
1026 if (name_types
.ids
[0] != SID_NAME_USER
) {
1027 DEBUG(0,("%s is not a user account (type=%d)\n",
1028 acct_name
, name_types
.ids
[0]));
1029 status
= NT_STATUS_INVALID_WORKSTATION
;
1033 user_rid
= user_rids
.ids
[0];
1035 /* Open handle on user */
1037 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1039 SEC_FLAG_MAXIMUM_ALLOWED
,
1043 if (!NT_STATUS_IS_OK(status
)) {
1046 if (!NT_STATUS_IS_OK(result
)) {
1051 /* Fill in the additional account flags now */
1053 acct_flags
|= ACB_PWNOEXP
;
1055 /* Set account flags on machine account */
1056 ZERO_STRUCT(user_info
.info16
);
1057 user_info
.info16
.acct_flags
= acct_flags
;
1059 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1064 if (!NT_STATUS_IS_OK(status
)) {
1065 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1069 libnet_join_set_error_string(mem_ctx
, r
,
1070 "Failed to set account flags for machine account (%s)\n",
1075 if (!NT_STATUS_IS_OK(result
)) {
1078 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1082 libnet_join_set_error_string(mem_ctx
, r
,
1083 "Failed to set account flags for machine account (%s)\n",
1088 /* Set password on machine account - first try level 26 */
1090 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1094 user_info
.info26
.password
= crypt_pwd_ex
;
1095 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1097 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1103 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
1105 /* retry with level 24 */
1107 init_samr_CryptPassword(r
->in
.machine_password
,
1111 user_info
.info24
.password
= crypt_pwd
;
1112 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1114 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1121 if (!NT_STATUS_IS_OK(status
)) {
1123 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1127 libnet_join_set_error_string(mem_ctx
, r
,
1128 "Failed to set password for machine account (%s)\n",
1132 if (!NT_STATUS_IS_OK(result
)) {
1135 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1139 libnet_join_set_error_string(mem_ctx
, r
,
1140 "Failed to set password for machine account (%s)\n",
1145 status
= NT_STATUS_OK
;
1152 data_blob_clear_free(&session_key
);
1154 if (is_valid_policy_hnd(&sam_pol
)) {
1155 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1157 if (is_valid_policy_hnd(&domain_pol
)) {
1158 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1160 if (is_valid_policy_hnd(&user_pol
)) {
1161 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1163 TALLOC_FREE(pipe_hnd
);
1168 /****************************************************************
1169 ****************************************************************/
1171 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1172 const char *machine_name
,
1173 const char *dc_name
)
1175 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1176 struct cli_state
*cli
= NULL
;
1177 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1178 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1180 char *machine_password
= NULL
;
1181 char *machine_account
= NULL
;
1184 return NT_STATUS_INVALID_PARAMETER
;
1187 if (!secrets_init()) {
1188 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1191 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1193 if (!machine_password
) {
1194 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1197 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1198 SAFE_FREE(machine_password
);
1199 return NT_STATUS_NO_MEMORY
;
1202 status
= cli_full_connection(&cli
, NULL
,
1210 SMB_SIGNING_DEFAULT
);
1211 free(machine_account
);
1212 free(machine_password
);
1214 if (!NT_STATUS_IS_OK(status
)) {
1215 status
= cli_full_connection(&cli
, NULL
,
1223 SMB_SIGNING_DEFAULT
);
1226 if (!NT_STATUS_IS_OK(status
)) {
1230 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1231 &neg_flags
, &netlogon_pipe
);
1232 if (!NT_STATUS_IS_OK(status
)) {
1233 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1235 return NT_STATUS_OK
;
1238 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1239 "key from server %s for domain %s. Error was %s\n",
1240 smbXcli_conn_remote_name(cli
->conn
),
1241 netbios_domain_name
, nt_errstr(status
)));
1246 if (!lp_client_schannel()) {
1248 return NT_STATUS_OK
;
1251 status
= cli_rpc_pipe_open_schannel_with_key(
1252 cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
1253 DCERPC_AUTH_LEVEL_PRIVACY
,
1254 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1258 if (!NT_STATUS_IS_OK(status
)) {
1259 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1260 "on netlogon pipe to server %s for domain %s. "
1262 smbXcli_conn_remote_name(cli
->conn
),
1263 netbios_domain_name
, nt_errstr(status
)));
1267 return NT_STATUS_OK
;
1270 /****************************************************************
1271 ****************************************************************/
1273 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1274 struct libnet_JoinCtx
*r
)
1278 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1281 if (!NT_STATUS_IS_OK(status
)) {
1282 libnet_join_set_error_string(mem_ctx
, r
,
1283 "failed to verify domain membership after joining: %s",
1284 get_friendly_nt_error_msg(status
));
1285 return WERR_SETUP_NOT_JOINED
;
1291 /****************************************************************
1292 ****************************************************************/
1294 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1295 struct libnet_UnjoinCtx
*r
)
1297 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1301 if (!secrets_delete_domain_sid(lp_workgroup())) {
1308 /****************************************************************
1309 ****************************************************************/
1311 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1312 struct libnet_UnjoinCtx
*r
)
1314 struct cli_state
*cli
= NULL
;
1315 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1316 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1317 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1320 struct lsa_String lsa_acct_name
;
1321 struct samr_Ids user_rids
;
1322 struct samr_Ids name_types
;
1323 union samr_UserInfo
*info
= NULL
;
1324 struct dcerpc_binding_handle
*b
= NULL
;
1326 ZERO_STRUCT(sam_pol
);
1327 ZERO_STRUCT(domain_pol
);
1328 ZERO_STRUCT(user_pol
);
1330 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1331 r
->in
.admin_account
,
1332 r
->in
.admin_password
,
1335 if (!NT_STATUS_IS_OK(status
)) {
1339 /* Open the domain */
1341 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1343 if (!NT_STATUS_IS_OK(status
)) {
1344 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1345 nt_errstr(status
)));
1349 b
= pipe_hnd
->binding_handle
;
1351 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1353 SEC_FLAG_MAXIMUM_ALLOWED
,
1356 if (!NT_STATUS_IS_OK(status
)) {
1359 if (!NT_STATUS_IS_OK(result
)) {
1364 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1366 SEC_FLAG_MAXIMUM_ALLOWED
,
1370 if (!NT_STATUS_IS_OK(status
)) {
1373 if (!NT_STATUS_IS_OK(result
)) {
1378 /* Create domain user */
1380 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1381 if (!strlower_m(acct_name
)) {
1382 status
= NT_STATUS_INVALID_PARAMETER
;
1386 init_lsa_String(&lsa_acct_name
, acct_name
);
1388 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1396 if (!NT_STATUS_IS_OK(status
)) {
1399 if (!NT_STATUS_IS_OK(result
)) {
1404 if (name_types
.ids
[0] != SID_NAME_USER
) {
1405 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1406 name_types
.ids
[0]));
1407 status
= NT_STATUS_INVALID_WORKSTATION
;
1411 user_rid
= user_rids
.ids
[0];
1413 /* Open handle on user */
1415 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1417 SEC_FLAG_MAXIMUM_ALLOWED
,
1421 if (!NT_STATUS_IS_OK(status
)) {
1424 if (!NT_STATUS_IS_OK(result
)) {
1431 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1436 if (!NT_STATUS_IS_OK(status
)) {
1437 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1440 if (!NT_STATUS_IS_OK(result
)) {
1442 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1446 /* now disable and setuser info */
1448 info
->info16
.acct_flags
|= ACB_DISABLED
;
1450 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1455 if (!NT_STATUS_IS_OK(status
)) {
1456 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1459 if (!NT_STATUS_IS_OK(result
)) {
1461 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1465 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1468 if (pipe_hnd
&& b
) {
1469 if (is_valid_policy_hnd(&domain_pol
)) {
1470 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1472 if (is_valid_policy_hnd(&sam_pol
)) {
1473 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1475 TALLOC_FREE(pipe_hnd
);
1485 /****************************************************************
1486 ****************************************************************/
1488 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1490 WERROR werr
= WERR_OK
;
1492 struct smbconf_ctx
*ctx
;
1494 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1495 if (!SBC_ERROR_IS_OK(err
)) {
1496 werr
= WERR_NO_SUCH_SERVICE
;
1500 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1502 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1503 if (!SBC_ERROR_IS_OK(err
)) {
1504 werr
= WERR_NO_SUCH_SERVICE
;
1508 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1510 if (!SBC_ERROR_IS_OK(err
)) {
1511 werr
= WERR_NO_SUCH_SERVICE
;
1515 smbconf_delete_global_parameter(ctx
, "realm");
1519 err
= smbconf_set_global_parameter(ctx
, "security", "domain");
1520 if (!SBC_ERROR_IS_OK(err
)) {
1521 werr
= WERR_NO_SUCH_SERVICE
;
1525 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1526 r
->out
.netbios_domain_name
);
1527 if (!SBC_ERROR_IS_OK(err
)) {
1528 werr
= WERR_NO_SUCH_SERVICE
;
1532 if (r
->out
.domain_is_ad
) {
1533 err
= smbconf_set_global_parameter(ctx
, "security", "ads");
1534 if (!SBC_ERROR_IS_OK(err
)) {
1535 werr
= WERR_NO_SUCH_SERVICE
;
1539 err
= smbconf_set_global_parameter(ctx
, "realm",
1540 r
->out
.dns_domain_name
);
1541 if (!SBC_ERROR_IS_OK(err
)) {
1542 werr
= WERR_NO_SUCH_SERVICE
;
1548 smbconf_shutdown(ctx
);
1552 /****************************************************************
1553 ****************************************************************/
1555 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1557 WERROR werr
= WERR_OK
;
1559 struct smbconf_ctx
*ctx
;
1561 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1562 if (!SBC_ERROR_IS_OK(err
)) {
1563 werr
= WERR_NO_SUCH_SERVICE
;
1567 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1569 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1570 if (!SBC_ERROR_IS_OK(err
)) {
1571 werr
= WERR_NO_SUCH_SERVICE
;
1575 err
= smbconf_delete_global_parameter(ctx
, "workgroup");
1576 if (!SBC_ERROR_IS_OK(err
)) {
1577 werr
= WERR_NO_SUCH_SERVICE
;
1581 smbconf_delete_global_parameter(ctx
, "realm");
1585 smbconf_shutdown(ctx
);
1589 /****************************************************************
1590 ****************************************************************/
1592 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1596 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1597 return r
->out
.result
;
1600 if (!r
->in
.modify_config
) {
1604 werr
= do_join_modify_vals_config(r
);
1605 if (!W_ERROR_IS_OK(werr
)) {
1609 lp_load_global(get_dyn_CONFIGFILE());
1611 r
->out
.modified_config
= true;
1612 r
->out
.result
= werr
;
1617 /****************************************************************
1618 ****************************************************************/
1620 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1624 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1625 return r
->out
.result
;
1628 if (!r
->in
.modify_config
) {
1632 werr
= do_unjoin_modify_vals_config(r
);
1633 if (!W_ERROR_IS_OK(werr
)) {
1637 lp_load_global(get_dyn_CONFIGFILE());
1639 r
->out
.modified_config
= true;
1640 r
->out
.result
= werr
;
1645 /****************************************************************
1646 ****************************************************************/
1648 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1649 const char *domain_str
,
1650 const char **domain_p
,
1653 char *domain
= NULL
;
1655 const char *p
= NULL
;
1657 if (!domain_str
|| !domain_p
|| !dc_p
) {
1661 p
= strchr_m(domain_str
, '\\');
1664 domain
= talloc_strndup(mem_ctx
, domain_str
,
1665 PTR_DIFF(p
, domain_str
));
1666 dc
= talloc_strdup(mem_ctx
, p
+1);
1671 domain
= talloc_strdup(mem_ctx
, domain_str
);
1687 /****************************************************************
1688 ****************************************************************/
1690 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1691 struct libnet_JoinCtx
*r
)
1693 if (!r
->in
.domain_name
) {
1694 libnet_join_set_error_string(mem_ctx
, r
,
1695 "No domain name defined");
1696 return WERR_INVALID_PARAM
;
1699 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1702 libnet_join_set_error_string(mem_ctx
, r
,
1703 "Failed to parse domain name");
1704 return WERR_INVALID_PARAM
;
1708 return WERR_SETUP_DOMAIN_CONTROLLER
;
1711 if (!secrets_init()) {
1712 libnet_join_set_error_string(mem_ctx
, r
,
1713 "Unable to open secrets database");
1714 return WERR_CAN_NOT_COMPLETE
;
1720 /****************************************************************
1721 ****************************************************************/
1723 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1727 /* Try adding dom admins to builtin\admins. Only log failures. */
1728 status
= create_builtin_administrators(domain_sid
);
1729 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1730 DEBUG(10,("Unable to auto-add domain administrators to "
1731 "BUILTIN\\Administrators during join because "
1732 "winbindd must be running.\n"));
1733 } else if (!NT_STATUS_IS_OK(status
)) {
1734 DEBUG(5, ("Failed to auto-add domain administrators to "
1735 "BUILTIN\\Administrators during join: %s\n",
1736 nt_errstr(status
)));
1739 /* Try adding dom users to builtin\users. Only log failures. */
1740 status
= create_builtin_users(domain_sid
);
1741 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1742 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1743 "during join because winbindd must be running.\n"));
1744 } else if (!NT_STATUS_IS_OK(status
)) {
1745 DEBUG(5, ("Failed to auto-add domain administrators to "
1746 "BUILTIN\\Administrators during join: %s\n",
1747 nt_errstr(status
)));
1751 /****************************************************************
1752 ****************************************************************/
1754 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1755 struct libnet_JoinCtx
*r
)
1759 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1760 return r
->out
.result
;
1763 werr
= do_JoinConfig(r
);
1764 if (!W_ERROR_IS_OK(werr
)) {
1768 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1772 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1773 if (r
->out
.dns_domain_name
) {
1774 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1778 if (r
->out
.domain_is_ad
&&
1779 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1780 ADS_STATUS ads_status
;
1782 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1783 if (!ADS_ERR_OK(ads_status
)) {
1784 return WERR_GENERAL_FAILURE
;
1787 #endif /* HAVE_ADS */
1789 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1794 /****************************************************************
1795 ****************************************************************/
1797 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1800 ads_destroy(&r
->in
.ads
);
1806 /****************************************************************
1807 ****************************************************************/
1809 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1812 ads_destroy(&r
->in
.ads
);
1818 /****************************************************************
1819 ****************************************************************/
1821 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1822 struct libnet_JoinCtx
**r
)
1824 struct libnet_JoinCtx
*ctx
;
1826 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1831 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1833 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
1834 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1836 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1843 /****************************************************************
1844 ****************************************************************/
1846 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1847 struct libnet_UnjoinCtx
**r
)
1849 struct libnet_UnjoinCtx
*ctx
;
1851 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1856 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1858 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
1859 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1866 /****************************************************************
1867 ****************************************************************/
1869 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1870 struct libnet_JoinCtx
*r
)
1872 bool valid_security
= false;
1873 bool valid_workgroup
= false;
1874 bool valid_realm
= false;
1876 /* check if configuration is already set correctly */
1878 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1880 switch (r
->out
.domain_is_ad
) {
1882 valid_security
= (lp_security() == SEC_DOMAIN
);
1883 if (valid_workgroup
&& valid_security
) {
1884 /* nothing to be done */
1889 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1890 switch (lp_security()) {
1893 valid_security
= true;
1896 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1897 /* nothing to be done */
1903 /* check if we are supposed to manipulate configuration */
1905 if (!r
->in
.modify_config
) {
1907 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1909 if (!valid_workgroup
) {
1910 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1911 "\"workgroup\" set to '%s', should be '%s'",
1912 lp_workgroup(), r
->out
.netbios_domain_name
);
1913 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1917 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1918 "\"realm\" set to '%s', should be '%s'",
1919 lp_realm(), r
->out
.dns_domain_name
);
1920 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1923 if (!valid_security
) {
1924 const char *sec
= NULL
;
1925 switch (lp_security()) {
1926 case SEC_USER
: sec
= "user"; break;
1927 case SEC_DOMAIN
: sec
= "domain"; break;
1928 case SEC_ADS
: sec
= "ads"; break;
1930 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1931 "\"security\" set to '%s', should be %s",
1932 sec
, r
->out
.domain_is_ad
?
1933 "either 'domain' or 'ads'" : "'domain'");
1934 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1937 libnet_join_set_error_string(mem_ctx
, r
,
1938 "Invalid configuration (%s) and configuration modification "
1939 "was not requested", wrong_conf
);
1940 return WERR_CAN_NOT_COMPLETE
;
1943 /* check if we are able to manipulate configuration */
1945 if (!lp_config_backend_is_registry()) {
1946 libnet_join_set_error_string(mem_ctx
, r
,
1947 "Configuration manipulation requested but not "
1948 "supported by backend");
1949 return WERR_NOT_SUPPORTED
;
1955 /****************************************************************
1956 ****************************************************************/
1958 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1959 struct libnet_JoinCtx
*r
)
1963 struct cli_state
*cli
= NULL
;
1965 ADS_STATUS ads_status
;
1966 #endif /* HAVE_ADS */
1968 if (!r
->in
.dc_name
) {
1969 struct netr_DsRGetDCNameInfo
*info
;
1971 status
= dsgetdcname(mem_ctx
,
1976 DS_FORCE_REDISCOVERY
|
1977 DS_DIRECTORY_SERVICE_REQUIRED
|
1978 DS_WRITABLE_REQUIRED
|
1981 if (!NT_STATUS_IS_OK(status
)) {
1982 libnet_join_set_error_string(mem_ctx
, r
,
1983 "failed to find DC for domain %s",
1985 get_friendly_nt_error_msg(status
));
1986 return WERR_DCNOTFOUND
;
1989 dc
= strip_hostname(info
->dc_unc
);
1990 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1991 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1994 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1995 if (!NT_STATUS_IS_OK(status
)) {
1996 libnet_join_set_error_string(mem_ctx
, r
,
1997 "failed to lookup DC info for domain '%s' over rpc: %s",
1998 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1999 return ntstatus_to_werror(status
);
2002 werr
= libnet_join_check_config(mem_ctx
, r
);
2003 if (!W_ERROR_IS_OK(werr
)) {
2009 create_local_private_krb5_conf_for_domain(
2010 r
->out
.dns_domain_name
, r
->out
.netbios_domain_name
,
2011 NULL
, smbXcli_conn_remote_sockaddr(cli
->conn
),
2012 smbXcli_conn_remote_name(cli
->conn
));
2014 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
2015 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2017 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
2018 if (!ADS_ERR_OK(ads_status
)) {
2019 return WERR_DEFAULT_JOIN_REQUIRED
;
2022 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
2023 if (!ADS_ERR_OK(ads_status
)) {
2024 libnet_join_set_error_string(mem_ctx
, r
,
2025 "failed to precreate account in ou %s: %s",
2027 ads_errstr(ads_status
));
2028 return WERR_DEFAULT_JOIN_REQUIRED
;
2031 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
2033 #endif /* HAVE_ADS */
2035 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
2036 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
2037 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2039 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2041 if (!NT_STATUS_IS_OK(status
)) {
2042 libnet_join_set_error_string(mem_ctx
, r
,
2043 "failed to join domain '%s' over rpc: %s",
2044 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2045 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2046 return WERR_SETUP_ALREADY_JOINED
;
2048 werr
= ntstatus_to_werror(status
);
2052 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2053 werr
= WERR_SETUP_NOT_JOINED
;
2067 /****************************************************************
2068 ****************************************************************/
2070 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2071 struct libnet_JoinCtx
*r
)
2074 struct libnet_UnjoinCtx
*u
= NULL
;
2076 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2077 if (!W_ERROR_IS_OK(werr
)) {
2081 u
->in
.debug
= r
->in
.debug
;
2082 u
->in
.dc_name
= r
->in
.dc_name
;
2083 u
->in
.domain_name
= r
->in
.domain_name
;
2084 u
->in
.admin_account
= r
->in
.admin_account
;
2085 u
->in
.admin_password
= r
->in
.admin_password
;
2086 u
->in
.modify_config
= r
->in
.modify_config
;
2087 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2088 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2090 werr
= libnet_Unjoin(mem_ctx
, u
);
2096 /****************************************************************
2097 ****************************************************************/
2099 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2100 struct libnet_JoinCtx
*r
)
2105 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2108 ZERO_STRUCT(r
->out
);
2110 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2111 if (!W_ERROR_IS_OK(werr
)) {
2115 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2116 werr
= libnet_DomainJoin(mem_ctx
, r
);
2117 if (!W_ERROR_IS_OK(werr
)) {
2122 werr
= libnet_join_post_processing(mem_ctx
, r
);
2123 if (!W_ERROR_IS_OK(werr
)) {
2127 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2128 werr
= libnet_join_post_verify(mem_ctx
, r
);
2129 if (!W_ERROR_IS_OK(werr
)) {
2130 libnet_join_rollback(mem_ctx
, r
);
2135 r
->out
.result
= werr
;
2138 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2143 /****************************************************************
2144 ****************************************************************/
2146 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2147 struct libnet_UnjoinCtx
*r
)
2151 if (!r
->in
.domain_sid
) {
2153 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2154 libnet_unjoin_set_error_string(mem_ctx
, r
,
2155 "Unable to fetch domain sid: are we joined?");
2156 return WERR_SETUP_NOT_JOINED
;
2158 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2159 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2162 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2163 !r
->in
.delete_machine_account
) {
2164 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2168 if (!r
->in
.dc_name
) {
2169 struct netr_DsRGetDCNameInfo
*info
;
2171 status
= dsgetdcname(mem_ctx
,
2176 DS_DIRECTORY_SERVICE_REQUIRED
|
2177 DS_WRITABLE_REQUIRED
|
2180 if (!NT_STATUS_IS_OK(status
)) {
2181 libnet_unjoin_set_error_string(mem_ctx
, r
,
2182 "failed to find DC for domain %s",
2184 get_friendly_nt_error_msg(status
));
2185 return WERR_DCNOTFOUND
;
2188 dc
= strip_hostname(info
->dc_unc
);
2189 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2190 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2194 /* for net ads leave, try to delete the account. If it works,
2195 no sense in disabling. If it fails, we can still try to
2198 if (r
->in
.delete_machine_account
) {
2199 ADS_STATUS ads_status
;
2200 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2201 if (ADS_ERR_OK(ads_status
)) {
2203 r
->out
.dns_domain_name
=
2204 talloc_strdup(mem_ctx
,
2205 r
->in
.ads
->server
.realm
);
2207 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2209 if (!ADS_ERR_OK(ads_status
)) {
2210 libnet_unjoin_set_error_string(mem_ctx
, r
,
2211 "failed to remove machine account from AD: %s",
2212 ads_errstr(ads_status
));
2214 r
->out
.deleted_machine_account
= true;
2215 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2216 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2220 #endif /* HAVE_ADS */
2222 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2224 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2225 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2226 if (!NT_STATUS_IS_OK(status
)) {
2227 libnet_unjoin_set_error_string(mem_ctx
, r
,
2228 "failed to disable machine account via rpc: %s",
2229 get_friendly_nt_error_msg(status
));
2230 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2231 return WERR_SETUP_NOT_JOINED
;
2233 return ntstatus_to_werror(status
);
2236 r
->out
.disabled_machine_account
= true;
2239 /* If disable succeeded or was not requested at all, we
2240 should be getting rid of our end of things */
2242 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2247 /****************************************************************
2248 ****************************************************************/
2250 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2251 struct libnet_UnjoinCtx
*r
)
2253 if (!r
->in
.domain_name
) {
2254 libnet_unjoin_set_error_string(mem_ctx
, r
,
2255 "No domain name defined");
2256 return WERR_INVALID_PARAM
;
2259 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2262 libnet_unjoin_set_error_string(mem_ctx
, r
,
2263 "Failed to parse domain name");
2264 return WERR_INVALID_PARAM
;
2268 return WERR_SETUP_DOMAIN_CONTROLLER
;
2271 if (!secrets_init()) {
2272 libnet_unjoin_set_error_string(mem_ctx
, r
,
2273 "Unable to open secrets database");
2274 return WERR_CAN_NOT_COMPLETE
;
2280 /****************************************************************
2281 ****************************************************************/
2283 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2284 struct libnet_UnjoinCtx
*r
)
2286 saf_delete(r
->out
.netbios_domain_name
);
2287 saf_delete(r
->out
.dns_domain_name
);
2289 return libnet_unjoin_config(r
);
2292 /****************************************************************
2293 ****************************************************************/
2295 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2296 struct libnet_UnjoinCtx
*r
)
2301 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2304 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2305 if (!W_ERROR_IS_OK(werr
)) {
2309 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2310 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2311 if (!W_ERROR_IS_OK(werr
)) {
2312 libnet_unjoin_config(r
);
2317 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2318 if (!W_ERROR_IS_OK(werr
)) {
2323 r
->out
.result
= werr
;
2326 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);