2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Gerald (Jerry) Carter 2006
5 * Copyright (C) Guenther Deschner 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "libnet/libnet.h"
23 #include "libcli/auth/libcli_auth.h"
25 /****************************************************************
26 ****************************************************************/
28 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
31 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
32 DEBUG(1,("libnet_Join:\n%s", str)); \
36 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
37 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
38 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
39 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
41 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
44 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
45 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
49 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
50 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
51 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
52 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
54 /****************************************************************
55 ****************************************************************/
57 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
58 struct libnet_JoinCtx
*r
,
59 const char *format
, ...)
63 if (r
->out
.error_string
) {
67 va_start(args
, format
);
68 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
72 /****************************************************************
73 ****************************************************************/
75 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
76 struct libnet_UnjoinCtx
*r
,
77 const char *format
, ...)
81 if (r
->out
.error_string
) {
85 va_start(args
, format
);
86 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
92 /****************************************************************
93 ****************************************************************/
95 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
96 const char *netbios_domain_name
,
98 const char *user_name
,
103 ADS_STRUCT
*my_ads
= NULL
;
105 my_ads
= ads_init(dns_domain_name
,
109 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
113 SAFE_FREE(my_ads
->auth
.user_name
);
114 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
118 SAFE_FREE(my_ads
->auth
.password
);
119 my_ads
->auth
.password
= SMB_STRDUP(password
);
122 status
= ads_connect_user_creds(my_ads
);
123 if (!ADS_ERR_OK(status
)) {
124 ads_destroy(&my_ads
);
132 /****************************************************************
133 ****************************************************************/
135 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
136 struct libnet_JoinCtx
*r
)
140 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
141 r
->out
.netbios_domain_name
,
144 r
->in
.admin_password
,
146 if (!ADS_ERR_OK(status
)) {
147 libnet_join_set_error_string(mem_ctx
, r
,
148 "failed to connect to AD: %s",
153 if (!r
->out
.netbios_domain_name
) {
154 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
155 r
->in
.ads
->server
.workgroup
);
156 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
159 if (!r
->out
.dns_domain_name
) {
160 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
161 r
->in
.ads
->config
.realm
);
162 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
165 r
->out
.domain_is_ad
= true;
170 /****************************************************************
171 ****************************************************************/
173 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
174 struct libnet_UnjoinCtx
*r
)
178 status
= libnet_connect_ads(r
->in
.domain_name
,
182 r
->in
.admin_password
,
184 if (!ADS_ERR_OK(status
)) {
185 libnet_unjoin_set_error_string(mem_ctx
, r
,
186 "failed to connect to AD: %s",
193 /****************************************************************
194 join a domain using ADS (LDAP mods)
195 ****************************************************************/
197 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
198 struct libnet_JoinCtx
*r
)
201 LDAPMessage
*res
= NULL
;
202 const char *attrs
[] = { "dn", NULL
};
205 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
206 if (!ADS_ERR_OK(status
)) {
210 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
211 if (!ADS_ERR_OK(status
)) {
215 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
216 ads_msgfree(r
->in
.ads
, res
);
217 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
220 ads_msgfree(r
->in
.ads
, res
);
222 /* Attempt to create the machine account and bail if this fails.
223 Assume that the admin wants exactly what they requested */
225 status
= ads_create_machine_acct(r
->in
.ads
,
229 if (ADS_ERR_OK(status
)) {
230 DEBUG(1,("machine account creation created\n"));
232 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
233 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
234 status
= ADS_SUCCESS
;
237 if (!ADS_ERR_OK(status
)) {
238 DEBUG(1,("machine account creation failed\n"));
242 status
= ads_move_machine_acct(r
->in
.ads
,
246 if (!ADS_ERR_OK(status
)) {
247 DEBUG(1,("failure to locate/move pre-existing "
248 "machine account\n"));
252 DEBUG(1,("The machine account %s the specified OU.\n",
253 moved
? "was moved into" : "already exists in"));
258 /****************************************************************
259 ****************************************************************/
261 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
262 struct libnet_UnjoinCtx
*r
)
267 return libnet_unjoin_connect_ads(mem_ctx
, r
);
270 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
271 if (!ADS_ERR_OK(status
)) {
272 libnet_unjoin_set_error_string(mem_ctx
, r
,
273 "failed to leave realm: %s",
281 /****************************************************************
282 ****************************************************************/
284 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
285 struct libnet_JoinCtx
*r
)
288 LDAPMessage
*res
= NULL
;
291 if (!r
->in
.machine_name
) {
292 return ADS_ERROR(LDAP_NO_MEMORY
);
295 status
= ads_find_machine_acct(r
->in
.ads
,
298 if (!ADS_ERR_OK(status
)) {
302 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
303 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
307 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
309 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
313 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
315 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
320 ads_msgfree(r
->in
.ads
, res
);
326 /****************************************************************
327 Set a machines dNSHostName and servicePrincipalName attributes
328 ****************************************************************/
330 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
331 struct libnet_JoinCtx
*r
)
336 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
341 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
342 if (!ADS_ERR_OK(status
)) {
346 /* Windows only creates HOST/shortname & HOST/fqdn. */
348 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
350 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
355 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
356 || (strchr(my_fqdn
, '.') == NULL
)) {
357 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
358 r
->out
.dns_domain_name
);
363 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
364 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
366 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
371 mods
= ads_init_mods(mem_ctx
);
373 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
376 /* fields of primary importance */
378 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
379 if (!ADS_ERR_OK(status
)) {
380 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
383 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
385 if (!ADS_ERR_OK(status
)) {
386 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
389 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
392 /****************************************************************
393 ****************************************************************/
395 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
396 struct libnet_JoinCtx
*r
)
401 if (!r
->in
.create_upn
) {
407 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
408 if (!ADS_ERR_OK(status
)) {
413 r
->in
.upn
= talloc_asprintf(mem_ctx
,
416 r
->out
.dns_domain_name
);
418 return ADS_ERROR(LDAP_NO_MEMORY
);
422 /* now do the mods */
424 mods
= ads_init_mods(mem_ctx
);
426 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
429 /* fields of primary importance */
431 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
432 if (!ADS_ERR_OK(status
)) {
433 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
436 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
440 /****************************************************************
441 ****************************************************************/
443 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
444 struct libnet_JoinCtx
*r
)
450 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
456 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
457 if (!ADS_ERR_OK(status
)) {
461 /* now do the mods */
463 mods
= ads_init_mods(mem_ctx
);
465 return ADS_ERROR(LDAP_NO_MEMORY
);
468 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
470 return ADS_ERROR(LDAP_NO_MEMORY
);
473 /* fields of primary importance */
475 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
477 if (!ADS_ERR_OK(status
)) {
481 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
483 if (!ADS_ERR_OK(status
)) {
487 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
489 if (!ADS_ERR_OK(status
)) {
493 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
496 /****************************************************************
497 ****************************************************************/
499 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
500 struct libnet_JoinCtx
*r
)
502 if (!USE_SYSTEM_KEYTAB
) {
506 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
513 /****************************************************************
514 ****************************************************************/
516 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
517 struct libnet_JoinCtx
*r
)
519 uint32_t domain_func
;
521 const char *salt
= NULL
;
522 char *std_salt
= NULL
;
524 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
525 if (!ADS_ERR_OK(status
)) {
526 libnet_join_set_error_string(mem_ctx
, r
,
527 "failed to determine domain functional level: %s",
532 /* go ahead and setup the default salt */
534 std_salt
= kerberos_standard_des_salt();
536 libnet_join_set_error_string(mem_ctx
, r
,
537 "failed to obtain standard DES salt");
541 salt
= talloc_strdup(mem_ctx
, std_salt
);
548 /* if it's a Windows functional domain, we have to look for the UPN */
550 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
553 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
556 salt
= talloc_strdup(mem_ctx
, upn
);
563 return kerberos_secrets_store_des_salt(salt
);
566 /****************************************************************
567 ****************************************************************/
569 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
570 struct libnet_JoinCtx
*r
)
575 status
= libnet_join_connect_ads(mem_ctx
, r
);
576 if (!ADS_ERR_OK(status
)) {
581 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
582 if (!ADS_ERR_OK(status
)) {
583 libnet_join_set_error_string(mem_ctx
, r
,
584 "failed to set machine spn: %s",
589 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
590 if (!ADS_ERR_OK(status
)) {
591 libnet_join_set_error_string(mem_ctx
, r
,
592 "failed to set machine os attributes: %s",
597 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
598 if (!ADS_ERR_OK(status
)) {
599 libnet_join_set_error_string(mem_ctx
, r
,
600 "failed to set machine upn: %s",
605 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
606 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
609 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
610 libnet_join_set_error_string(mem_ctx
, r
,
611 "failed to create kerberos keytab");
612 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
617 #endif /* WITH_ADS */
619 /****************************************************************
620 Store the machine password and domain SID
621 ****************************************************************/
623 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
624 struct libnet_JoinCtx
*r
)
626 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
629 DEBUG(1,("Failed to save domain sid\n"));
633 if (!secrets_store_machine_password(r
->in
.machine_password
,
634 r
->out
.netbios_domain_name
,
635 r
->in
.secure_channel_type
))
637 DEBUG(1,("Failed to save machine password\n"));
644 /****************************************************************
645 Connect dc's IPC$ share
646 ****************************************************************/
648 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
652 struct cli_state
**cli
)
657 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
660 if (use_kerberos
&& pass
) {
661 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
664 return cli_full_connection(cli
, NULL
,
675 /****************************************************************
676 Lookup domain dc's info
677 ****************************************************************/
679 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
680 struct libnet_JoinCtx
*r
,
681 struct cli_state
**cli
)
683 struct rpc_pipe_client
*pipe_hnd
= NULL
;
684 struct policy_handle lsa_pol
;
685 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
686 union lsa_PolicyInformation
*info
= NULL
;
688 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
690 r
->in
.admin_password
,
693 if (!NT_STATUS_IS_OK(status
)) {
697 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
699 if (!NT_STATUS_IS_OK(status
)) {
700 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
705 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
706 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
707 if (!NT_STATUS_IS_OK(status
)) {
711 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
715 if (NT_STATUS_IS_OK(status
)) {
716 r
->out
.domain_is_ad
= true;
717 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
718 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
719 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
720 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->dns
.sid
);
721 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
724 if (!NT_STATUS_IS_OK(status
)) {
725 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
727 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
729 if (!NT_STATUS_IS_OK(status
)) {
733 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
734 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->account_domain
.sid
);
735 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
738 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
739 TALLOC_FREE(pipe_hnd
);
745 /****************************************************************
746 Do the domain join unsecure
747 ****************************************************************/
749 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
750 struct libnet_JoinCtx
*r
,
751 struct cli_state
*cli
)
753 struct rpc_pipe_client
*pipe_hnd
= NULL
;
754 unsigned char orig_trust_passwd_hash
[16];
755 unsigned char new_trust_passwd_hash
[16];
756 fstring trust_passwd
;
759 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
761 if (!NT_STATUS_IS_OK(status
)) {
765 if (!r
->in
.machine_password
) {
766 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
767 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
770 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
772 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
773 fstrcpy(trust_passwd
, r
->in
.admin_password
);
774 strlower_m(trust_passwd
);
777 * Machine names can be 15 characters, but the max length on
778 * a password is 14. --jerry
781 trust_passwd
[14] = '\0';
783 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
785 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
786 orig_trust_passwd_hash
,
787 r
->in
.machine_password
,
788 new_trust_passwd_hash
,
789 r
->in
.secure_channel_type
);
794 /****************************************************************
796 ****************************************************************/
798 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
799 struct libnet_JoinCtx
*r
,
800 struct cli_state
*cli
)
802 struct rpc_pipe_client
*pipe_hnd
= NULL
;
803 struct policy_handle sam_pol
, domain_pol
, user_pol
;
804 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
806 struct lsa_String lsa_acct_name
;
808 uint32_t acct_flags
= ACB_WSTRUST
;
809 struct samr_Ids user_rids
;
810 struct samr_Ids name_types
;
811 union samr_UserInfo user_info
;
813 struct samr_CryptPassword crypt_pwd
;
814 struct samr_CryptPasswordEx crypt_pwd_ex
;
816 ZERO_STRUCT(sam_pol
);
817 ZERO_STRUCT(domain_pol
);
818 ZERO_STRUCT(user_pol
);
820 switch (r
->in
.secure_channel_type
) {
822 acct_flags
= ACB_WSTRUST
;
825 acct_flags
= ACB_SVRTRUST
;
828 return NT_STATUS_INVALID_PARAMETER
;
831 if (!r
->in
.machine_password
) {
832 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
833 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
836 /* Open the domain */
838 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
840 if (!NT_STATUS_IS_OK(status
)) {
841 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
846 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
848 SAMR_ACCESS_ENUM_DOMAINS
849 | SAMR_ACCESS_LOOKUP_DOMAIN
,
851 if (!NT_STATUS_IS_OK(status
)) {
855 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
857 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
858 | SAMR_DOMAIN_ACCESS_CREATE_USER
859 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
862 if (!NT_STATUS_IS_OK(status
)) {
866 /* Create domain user */
868 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
869 strlower_m(acct_name
);
871 init_lsa_String(&lsa_acct_name
, acct_name
);
873 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
874 uint32_t access_desired
=
875 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
876 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
877 SAMR_USER_ACCESS_SET_PASSWORD
|
878 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
879 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
880 uint32_t access_granted
= 0;
882 DEBUG(10,("Creating account with desired access mask: %d\n",
885 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
893 if (!NT_STATUS_IS_OK(status
) &&
894 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
896 DEBUG(10,("Creation of workstation account failed: %s\n",
899 /* If NT_STATUS_ACCESS_DENIED then we have a valid
900 username/password combo but the user does not have
901 administrator access. */
903 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
904 libnet_join_set_error_string(mem_ctx
, r
,
905 "User specified does not have "
906 "administrator privileges");
912 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
913 if (!(r
->in
.join_flags
&
914 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
919 /* We *must* do this.... don't ask... */
921 if (NT_STATUS_IS_OK(status
)) {
922 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
926 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
932 if (!NT_STATUS_IS_OK(status
)) {
936 if (name_types
.ids
[0] != SID_NAME_USER
) {
937 DEBUG(0,("%s is not a user account (type=%d)\n",
938 acct_name
, name_types
.ids
[0]));
939 status
= NT_STATUS_INVALID_WORKSTATION
;
943 user_rid
= user_rids
.ids
[0];
945 /* Open handle on user */
947 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
949 SEC_FLAG_MAXIMUM_ALLOWED
,
952 if (!NT_STATUS_IS_OK(status
)) {
956 /* Fill in the additional account flags now */
958 acct_flags
|= ACB_PWNOEXP
;
959 if (r
->out
.domain_is_ad
) {
960 #if !defined(ENCTYPE_ARCFOUR_HMAC)
961 acct_flags
|= ACB_USE_DES_KEY_ONLY
;
966 /* Set account flags on machine account */
967 ZERO_STRUCT(user_info
.info16
);
968 user_info
.info16
.acct_flags
= acct_flags
;
970 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
975 if (!NT_STATUS_IS_OK(status
)) {
977 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
980 libnet_join_set_error_string(mem_ctx
, r
,
981 "Failed to set account flags for machine account (%s)\n",
986 /* Set password on machine account - first try level 26 */
988 init_samr_CryptPasswordEx(r
->in
.machine_password
,
989 &cli
->user_session_key
,
992 user_info
.info26
.password
= crypt_pwd_ex
;
993 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
995 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1000 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
1002 /* retry with level 24 */
1004 init_samr_CryptPassword(r
->in
.machine_password
,
1005 &cli
->user_session_key
,
1008 user_info
.info24
.password
= crypt_pwd
;
1009 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1011 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1017 if (!NT_STATUS_IS_OK(status
)) {
1019 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
1022 libnet_join_set_error_string(mem_ctx
, r
,
1023 "Failed to set password for machine account (%s)\n",
1028 status
= NT_STATUS_OK
;
1035 if (is_valid_policy_hnd(&sam_pol
)) {
1036 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1038 if (is_valid_policy_hnd(&domain_pol
)) {
1039 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1041 if (is_valid_policy_hnd(&user_pol
)) {
1042 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1044 TALLOC_FREE(pipe_hnd
);
1049 /****************************************************************
1050 ****************************************************************/
1052 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1053 const char *machine_name
,
1054 const char *dc_name
)
1056 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1057 struct cli_state
*cli
= NULL
;
1058 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1059 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1061 char *machine_password
= NULL
;
1062 char *machine_account
= NULL
;
1065 return NT_STATUS_INVALID_PARAMETER
;
1068 if (!secrets_init()) {
1069 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1072 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1074 if (!machine_password
) {
1075 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1078 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1079 SAFE_FREE(machine_password
);
1080 return NT_STATUS_NO_MEMORY
;
1083 status
= cli_full_connection(&cli
, NULL
,
1092 free(machine_account
);
1093 free(machine_password
);
1095 if (!NT_STATUS_IS_OK(status
)) {
1096 status
= cli_full_connection(&cli
, NULL
,
1107 if (!NT_STATUS_IS_OK(status
)) {
1111 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1112 &neg_flags
, &netlogon_pipe
);
1113 if (!NT_STATUS_IS_OK(status
)) {
1114 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1116 return NT_STATUS_OK
;
1119 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1120 "key from server %s for domain %s. Error was %s\n",
1121 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1126 if (!lp_client_schannel()) {
1128 return NT_STATUS_OK
;
1131 status
= cli_rpc_pipe_open_schannel_with_key(
1132 cli
, &ndr_table_netlogon
.syntax_id
, PIPE_AUTH_LEVEL_PRIVACY
,
1133 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1137 if (!NT_STATUS_IS_OK(status
)) {
1138 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1139 "on netlogon pipe to server %s for domain %s. "
1141 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1145 return NT_STATUS_OK
;
1148 /****************************************************************
1149 ****************************************************************/
1151 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1152 struct libnet_JoinCtx
*r
)
1156 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1159 if (!NT_STATUS_IS_OK(status
)) {
1160 libnet_join_set_error_string(mem_ctx
, r
,
1161 "failed to verify domain membership after joining: %s",
1162 get_friendly_nt_error_msg(status
));
1163 return WERR_SETUP_NOT_JOINED
;
1169 /****************************************************************
1170 ****************************************************************/
1172 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1173 struct libnet_UnjoinCtx
*r
)
1175 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1179 if (!secrets_delete_domain_sid(lp_workgroup())) {
1186 /****************************************************************
1187 ****************************************************************/
1189 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1190 struct libnet_UnjoinCtx
*r
)
1192 struct cli_state
*cli
= NULL
;
1193 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1194 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1195 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1198 struct lsa_String lsa_acct_name
;
1199 struct samr_Ids user_rids
;
1200 struct samr_Ids name_types
;
1201 union samr_UserInfo
*info
= NULL
;
1203 ZERO_STRUCT(sam_pol
);
1204 ZERO_STRUCT(domain_pol
);
1205 ZERO_STRUCT(user_pol
);
1207 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1208 r
->in
.admin_account
,
1209 r
->in
.admin_password
,
1212 if (!NT_STATUS_IS_OK(status
)) {
1216 /* Open the domain */
1218 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1220 if (!NT_STATUS_IS_OK(status
)) {
1221 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1222 nt_errstr(status
)));
1226 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1228 SEC_FLAG_MAXIMUM_ALLOWED
,
1230 if (!NT_STATUS_IS_OK(status
)) {
1234 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1236 SEC_FLAG_MAXIMUM_ALLOWED
,
1239 if (!NT_STATUS_IS_OK(status
)) {
1243 /* Create domain user */
1245 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1246 strlower_m(acct_name
);
1248 init_lsa_String(&lsa_acct_name
, acct_name
);
1250 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1257 if (!NT_STATUS_IS_OK(status
)) {
1261 if (name_types
.ids
[0] != SID_NAME_USER
) {
1262 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1263 name_types
.ids
[0]));
1264 status
= NT_STATUS_INVALID_WORKSTATION
;
1268 user_rid
= user_rids
.ids
[0];
1270 /* Open handle on user */
1272 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1274 SEC_FLAG_MAXIMUM_ALLOWED
,
1277 if (!NT_STATUS_IS_OK(status
)) {
1283 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1287 if (!NT_STATUS_IS_OK(status
)) {
1288 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1292 /* now disable and setuser info */
1294 info
->info16
.acct_flags
|= ACB_DISABLED
;
1296 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1301 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1305 if (is_valid_policy_hnd(&domain_pol
)) {
1306 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1308 if (is_valid_policy_hnd(&sam_pol
)) {
1309 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1311 TALLOC_FREE(pipe_hnd
);
1321 /****************************************************************
1322 ****************************************************************/
1324 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1327 struct smbconf_ctx
*ctx
;
1329 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1330 if (!W_ERROR_IS_OK(werr
)) {
1334 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1336 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1337 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1339 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1342 smbconf_delete_global_parameter(ctx
, "realm");
1346 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1347 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1349 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1350 r
->out
.netbios_domain_name
);
1351 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1353 if (r
->out
.domain_is_ad
) {
1354 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1355 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1357 werr
= smbconf_set_global_parameter(ctx
, "realm",
1358 r
->out
.dns_domain_name
);
1359 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1363 smbconf_shutdown(ctx
);
1367 /****************************************************************
1368 ****************************************************************/
1370 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1372 WERROR werr
= WERR_OK
;
1373 struct smbconf_ctx
*ctx
;
1375 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1376 if (!W_ERROR_IS_OK(werr
)) {
1380 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1382 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1383 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1385 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1386 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1388 smbconf_delete_global_parameter(ctx
, "realm");
1392 smbconf_shutdown(ctx
);
1396 /****************************************************************
1397 ****************************************************************/
1399 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1403 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1404 return r
->out
.result
;
1407 if (!r
->in
.modify_config
) {
1411 werr
= do_join_modify_vals_config(r
);
1412 if (!W_ERROR_IS_OK(werr
)) {
1416 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1418 r
->out
.modified_config
= true;
1419 r
->out
.result
= werr
;
1424 /****************************************************************
1425 ****************************************************************/
1427 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1431 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1432 return r
->out
.result
;
1435 if (!r
->in
.modify_config
) {
1439 werr
= do_unjoin_modify_vals_config(r
);
1440 if (!W_ERROR_IS_OK(werr
)) {
1444 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1446 r
->out
.modified_config
= true;
1447 r
->out
.result
= werr
;
1452 /****************************************************************
1453 ****************************************************************/
1455 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1456 const char *domain_str
,
1457 const char **domain_p
,
1460 char *domain
= NULL
;
1462 const char *p
= NULL
;
1464 if (!domain_str
|| !domain_p
|| !dc_p
) {
1468 p
= strchr_m(domain_str
, '\\');
1471 domain
= talloc_strndup(mem_ctx
, domain_str
,
1472 PTR_DIFF(p
, domain_str
));
1473 dc
= talloc_strdup(mem_ctx
, p
+1);
1478 domain
= talloc_strdup(mem_ctx
, domain_str
);
1494 /****************************************************************
1495 ****************************************************************/
1497 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1498 struct libnet_JoinCtx
*r
)
1500 if (!r
->in
.domain_name
) {
1501 libnet_join_set_error_string(mem_ctx
, r
,
1502 "No domain name defined");
1503 return WERR_INVALID_PARAM
;
1506 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1509 libnet_join_set_error_string(mem_ctx
, r
,
1510 "Failed to parse domain name");
1511 return WERR_INVALID_PARAM
;
1515 return WERR_SETUP_DOMAIN_CONTROLLER
;
1518 if (!secrets_init()) {
1519 libnet_join_set_error_string(mem_ctx
, r
,
1520 "Unable to open secrets database");
1521 return WERR_CAN_NOT_COMPLETE
;
1527 /****************************************************************
1528 ****************************************************************/
1530 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1534 /* Try adding dom admins to builtin\admins. Only log failures. */
1535 status
= create_builtin_administrators(domain_sid
);
1536 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1537 DEBUG(10,("Unable to auto-add domain administrators to "
1538 "BUILTIN\\Administrators during join because "
1539 "winbindd must be running."));
1540 } else if (!NT_STATUS_IS_OK(status
)) {
1541 DEBUG(5, ("Failed to auto-add domain administrators to "
1542 "BUILTIN\\Administrators during join: %s\n",
1543 nt_errstr(status
)));
1546 /* Try adding dom users to builtin\users. Only log failures. */
1547 status
= create_builtin_users(domain_sid
);
1548 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1549 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1550 "during join because winbindd must be running."));
1551 } else if (!NT_STATUS_IS_OK(status
)) {
1552 DEBUG(5, ("Failed to auto-add domain administrators to "
1553 "BUILTIN\\Administrators during join: %s\n",
1554 nt_errstr(status
)));
1558 /****************************************************************
1559 ****************************************************************/
1561 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1562 struct libnet_JoinCtx
*r
)
1566 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1567 return r
->out
.result
;
1570 werr
= do_JoinConfig(r
);
1571 if (!W_ERROR_IS_OK(werr
)) {
1575 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1579 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1580 if (r
->out
.dns_domain_name
) {
1581 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1585 if (r
->out
.domain_is_ad
&&
1586 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1587 ADS_STATUS ads_status
;
1589 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1590 if (!ADS_ERR_OK(ads_status
)) {
1591 return WERR_GENERAL_FAILURE
;
1594 #endif /* WITH_ADS */
1596 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1601 /****************************************************************
1602 ****************************************************************/
1604 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1606 const char *krb5_cc_env
= NULL
;
1609 ads_destroy(&r
->in
.ads
);
1612 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1613 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1614 unsetenv(KRB5_ENV_CCNAME
);
1620 /****************************************************************
1621 ****************************************************************/
1623 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1625 const char *krb5_cc_env
= NULL
;
1628 ads_destroy(&r
->in
.ads
);
1631 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1632 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1633 unsetenv(KRB5_ENV_CCNAME
);
1639 /****************************************************************
1640 ****************************************************************/
1642 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1643 struct libnet_JoinCtx
**r
)
1645 struct libnet_JoinCtx
*ctx
;
1646 const char *krb5_cc_env
= NULL
;
1648 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1653 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1655 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1656 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1658 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1659 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1660 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1661 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1662 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1665 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1672 /****************************************************************
1673 ****************************************************************/
1675 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1676 struct libnet_UnjoinCtx
**r
)
1678 struct libnet_UnjoinCtx
*ctx
;
1679 const char *krb5_cc_env
= NULL
;
1681 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1686 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1688 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1689 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1691 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1692 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1693 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1694 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1695 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1703 /****************************************************************
1704 ****************************************************************/
1706 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1707 struct libnet_JoinCtx
*r
)
1709 bool valid_security
= false;
1710 bool valid_workgroup
= false;
1711 bool valid_realm
= false;
1713 /* check if configuration is already set correctly */
1715 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1717 switch (r
->out
.domain_is_ad
) {
1719 valid_security
= (lp_security() == SEC_DOMAIN
);
1720 if (valid_workgroup
&& valid_security
) {
1721 /* nothing to be done */
1726 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1727 switch (lp_security()) {
1730 valid_security
= true;
1733 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1734 /* nothing to be done */
1740 /* check if we are supposed to manipulate configuration */
1742 if (!r
->in
.modify_config
) {
1744 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1746 if (!valid_workgroup
) {
1747 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1748 "\"workgroup\" set to '%s', should be '%s'",
1749 lp_workgroup(), r
->out
.netbios_domain_name
);
1750 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1754 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1755 "\"realm\" set to '%s', should be '%s'",
1756 lp_realm(), r
->out
.dns_domain_name
);
1757 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1760 if (!valid_security
) {
1761 const char *sec
= NULL
;
1762 switch (lp_security()) {
1763 case SEC_SHARE
: sec
= "share"; break;
1764 case SEC_USER
: sec
= "user"; break;
1765 case SEC_DOMAIN
: sec
= "domain"; break;
1766 case SEC_ADS
: sec
= "ads"; break;
1768 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1769 "\"security\" set to '%s', should be %s",
1770 sec
, r
->out
.domain_is_ad
?
1771 "either 'domain' or 'ads'" : "'domain'");
1772 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1775 libnet_join_set_error_string(mem_ctx
, r
,
1776 "Invalid configuration (%s) and configuration modification "
1777 "was not requested", wrong_conf
);
1778 return WERR_CAN_NOT_COMPLETE
;
1781 /* check if we are able to manipulate configuration */
1783 if (!lp_config_backend_is_registry()) {
1784 libnet_join_set_error_string(mem_ctx
, r
,
1785 "Configuration manipulation requested but not "
1786 "supported by backend");
1787 return WERR_NOT_SUPPORTED
;
1793 /****************************************************************
1794 ****************************************************************/
1796 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1797 struct libnet_JoinCtx
*r
)
1801 struct cli_state
*cli
= NULL
;
1803 ADS_STATUS ads_status
;
1804 #endif /* WITH_ADS */
1806 if (!r
->in
.dc_name
) {
1807 struct netr_DsRGetDCNameInfo
*info
;
1809 status
= dsgetdcname(mem_ctx
,
1814 DS_FORCE_REDISCOVERY
|
1815 DS_DIRECTORY_SERVICE_REQUIRED
|
1816 DS_WRITABLE_REQUIRED
|
1819 if (!NT_STATUS_IS_OK(status
)) {
1820 libnet_join_set_error_string(mem_ctx
, r
,
1821 "failed to find DC for domain %s",
1823 get_friendly_nt_error_msg(status
));
1824 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1827 dc
= strip_hostname(info
->dc_unc
);
1828 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1829 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1832 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1833 if (!NT_STATUS_IS_OK(status
)) {
1834 libnet_join_set_error_string(mem_ctx
, r
,
1835 "failed to lookup DC info for domain '%s' over rpc: %s",
1836 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1837 return ntstatus_to_werror(status
);
1840 werr
= libnet_join_check_config(mem_ctx
, r
);
1841 if (!W_ERROR_IS_OK(werr
)) {
1846 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
1847 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1849 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1850 if (!ADS_ERR_OK(ads_status
)) {
1851 return WERR_DEFAULT_JOIN_REQUIRED
;
1854 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1855 if (!ADS_ERR_OK(ads_status
)) {
1856 libnet_join_set_error_string(mem_ctx
, r
,
1857 "failed to precreate account in ou %s: %s",
1859 ads_errstr(ads_status
));
1860 return WERR_DEFAULT_JOIN_REQUIRED
;
1863 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1865 #endif /* WITH_ADS */
1867 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
1868 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
1869 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
1871 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1873 if (!NT_STATUS_IS_OK(status
)) {
1874 libnet_join_set_error_string(mem_ctx
, r
,
1875 "failed to join domain '%s' over rpc: %s",
1876 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1877 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1878 return WERR_SETUP_ALREADY_JOINED
;
1880 werr
= ntstatus_to_werror(status
);
1884 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1885 werr
= WERR_SETUP_NOT_JOINED
;
1899 /****************************************************************
1900 ****************************************************************/
1902 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
1903 struct libnet_JoinCtx
*r
)
1906 struct libnet_UnjoinCtx
*u
= NULL
;
1908 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
1909 if (!W_ERROR_IS_OK(werr
)) {
1913 u
->in
.debug
= r
->in
.debug
;
1914 u
->in
.dc_name
= r
->in
.dc_name
;
1915 u
->in
.domain_name
= r
->in
.domain_name
;
1916 u
->in
.admin_account
= r
->in
.admin_account
;
1917 u
->in
.admin_password
= r
->in
.admin_password
;
1918 u
->in
.modify_config
= r
->in
.modify_config
;
1919 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1920 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1922 werr
= libnet_Unjoin(mem_ctx
, u
);
1928 /****************************************************************
1929 ****************************************************************/
1931 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1932 struct libnet_JoinCtx
*r
)
1937 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1940 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1941 if (!W_ERROR_IS_OK(werr
)) {
1945 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1946 werr
= libnet_DomainJoin(mem_ctx
, r
);
1947 if (!W_ERROR_IS_OK(werr
)) {
1952 werr
= libnet_join_post_processing(mem_ctx
, r
);
1953 if (!W_ERROR_IS_OK(werr
)) {
1957 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1958 werr
= libnet_join_post_verify(mem_ctx
, r
);
1959 if (!W_ERROR_IS_OK(werr
)) {
1960 libnet_join_rollback(mem_ctx
, r
);
1965 r
->out
.result
= werr
;
1968 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1973 /****************************************************************
1974 ****************************************************************/
1976 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
1977 struct libnet_UnjoinCtx
*r
)
1981 if (!r
->in
.domain_sid
) {
1983 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
1984 libnet_unjoin_set_error_string(mem_ctx
, r
,
1985 "Unable to fetch domain sid: are we joined?");
1986 return WERR_SETUP_NOT_JOINED
;
1988 r
->in
.domain_sid
= sid_dup_talloc(mem_ctx
, &sid
);
1989 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
1992 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
1993 !r
->in
.delete_machine_account
) {
1994 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
1998 if (!r
->in
.dc_name
) {
1999 struct netr_DsRGetDCNameInfo
*info
;
2001 status
= dsgetdcname(mem_ctx
,
2006 DS_DIRECTORY_SERVICE_REQUIRED
|
2007 DS_WRITABLE_REQUIRED
|
2010 if (!NT_STATUS_IS_OK(status
)) {
2011 libnet_unjoin_set_error_string(mem_ctx
, r
,
2012 "failed to find DC for domain %s",
2014 get_friendly_nt_error_msg(status
));
2015 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
2018 dc
= strip_hostname(info
->dc_unc
);
2019 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2020 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2024 /* for net ads leave, try to delete the account. If it works,
2025 no sense in disabling. If it fails, we can still try to
2028 if (r
->in
.delete_machine_account
) {
2029 ADS_STATUS ads_status
;
2030 libnet_unjoin_connect_ads(mem_ctx
, r
);
2031 ads_status
= libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2032 if (!ADS_ERR_OK(ads_status
)) {
2033 libnet_unjoin_set_error_string(mem_ctx
, r
,
2034 "failed to remove machine account from AD: %s",
2035 ads_errstr(ads_status
));
2037 r
->out
.deleted_machine_account
= true;
2039 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
2040 r
->in
.ads
->server
.realm
);
2041 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2042 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2046 #endif /* WITH_ADS */
2048 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2050 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2051 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2052 if (!NT_STATUS_IS_OK(status
)) {
2053 libnet_unjoin_set_error_string(mem_ctx
, r
,
2054 "failed to disable machine account via rpc: %s",
2055 get_friendly_nt_error_msg(status
));
2056 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2057 return WERR_SETUP_NOT_JOINED
;
2059 return ntstatus_to_werror(status
);
2062 r
->out
.disabled_machine_account
= true;
2063 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
2064 r
->in
.ads
->server
.realm
);
2067 /* If disable succeeded or was not requested at all, we
2068 should be getting rid of our end of things */
2070 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2075 /****************************************************************
2076 ****************************************************************/
2078 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2079 struct libnet_UnjoinCtx
*r
)
2081 if (!r
->in
.domain_name
) {
2082 libnet_unjoin_set_error_string(mem_ctx
, r
,
2083 "No domain name defined");
2084 return WERR_INVALID_PARAM
;
2087 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2090 libnet_unjoin_set_error_string(mem_ctx
, r
,
2091 "Failed to parse domain name");
2092 return WERR_INVALID_PARAM
;
2096 return WERR_SETUP_DOMAIN_CONTROLLER
;
2099 if (!secrets_init()) {
2100 libnet_unjoin_set_error_string(mem_ctx
, r
,
2101 "Unable to open secrets database");
2102 return WERR_CAN_NOT_COMPLETE
;
2108 /****************************************************************
2109 ****************************************************************/
2111 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2112 struct libnet_UnjoinCtx
*r
)
2114 saf_delete(r
->out
.netbios_domain_name
);
2115 saf_delete(r
->out
.dns_domain_name
);
2117 return libnet_unjoin_config(r
);
2120 /****************************************************************
2121 ****************************************************************/
2123 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2124 struct libnet_UnjoinCtx
*r
)
2129 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2132 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2133 if (!W_ERROR_IS_OK(werr
)) {
2137 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2138 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2139 if (!W_ERROR_IS_OK(werr
)) {
2140 libnet_unjoin_config(r
);
2145 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2146 if (!W_ERROR_IS_OK(werr
)) {
2151 r
->out
.result
= werr
;
2154 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);