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 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
268 if (!ADS_ERR_OK(status
)) {
269 libnet_unjoin_set_error_string(mem_ctx
, r
,
270 "failed to connect to AD: %s",
276 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
277 if (!ADS_ERR_OK(status
)) {
278 libnet_unjoin_set_error_string(mem_ctx
, r
,
279 "failed to leave realm: %s",
287 /****************************************************************
288 ****************************************************************/
290 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
291 struct libnet_JoinCtx
*r
)
294 LDAPMessage
*res
= NULL
;
297 if (!r
->in
.machine_name
) {
298 return ADS_ERROR(LDAP_NO_MEMORY
);
301 status
= ads_find_machine_acct(r
->in
.ads
,
304 if (!ADS_ERR_OK(status
)) {
308 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
309 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
313 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
315 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
319 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
321 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
326 ads_msgfree(r
->in
.ads
, res
);
332 /****************************************************************
333 Set a machines dNSHostName and servicePrincipalName attributes
334 ****************************************************************/
336 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
337 struct libnet_JoinCtx
*r
)
342 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
347 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
348 if (!ADS_ERR_OK(status
)) {
352 /* Windows only creates HOST/shortname & HOST/fqdn. */
354 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
356 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
361 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
362 || (strchr(my_fqdn
, '.') == NULL
)) {
363 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
364 r
->out
.dns_domain_name
);
369 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
370 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
372 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
377 mods
= ads_init_mods(mem_ctx
);
379 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
382 /* fields of primary importance */
384 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
385 if (!ADS_ERR_OK(status
)) {
386 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
389 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
391 if (!ADS_ERR_OK(status
)) {
392 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
395 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
398 /****************************************************************
399 ****************************************************************/
401 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
402 struct libnet_JoinCtx
*r
)
407 if (!r
->in
.create_upn
) {
413 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
414 if (!ADS_ERR_OK(status
)) {
419 r
->in
.upn
= talloc_asprintf(mem_ctx
,
422 r
->out
.dns_domain_name
);
424 return ADS_ERROR(LDAP_NO_MEMORY
);
428 /* now do the mods */
430 mods
= ads_init_mods(mem_ctx
);
432 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
435 /* fields of primary importance */
437 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
438 if (!ADS_ERR_OK(status
)) {
439 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
442 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
446 /****************************************************************
447 ****************************************************************/
449 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
450 struct libnet_JoinCtx
*r
)
456 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
462 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
463 if (!ADS_ERR_OK(status
)) {
467 /* now do the mods */
469 mods
= ads_init_mods(mem_ctx
);
471 return ADS_ERROR(LDAP_NO_MEMORY
);
474 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
476 return ADS_ERROR(LDAP_NO_MEMORY
);
479 /* fields of primary importance */
481 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
483 if (!ADS_ERR_OK(status
)) {
487 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
489 if (!ADS_ERR_OK(status
)) {
493 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
495 if (!ADS_ERR_OK(status
)) {
499 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
502 /****************************************************************
503 ****************************************************************/
505 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
506 struct libnet_JoinCtx
*r
)
508 if (!USE_SYSTEM_KEYTAB
) {
512 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
519 /****************************************************************
520 ****************************************************************/
522 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
523 struct libnet_JoinCtx
*r
)
525 uint32_t domain_func
;
527 const char *salt
= NULL
;
528 char *std_salt
= NULL
;
530 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
531 if (!ADS_ERR_OK(status
)) {
532 libnet_join_set_error_string(mem_ctx
, r
,
533 "failed to determine domain functional level: %s",
538 /* go ahead and setup the default salt */
540 std_salt
= kerberos_standard_des_salt();
542 libnet_join_set_error_string(mem_ctx
, r
,
543 "failed to obtain standard DES salt");
547 salt
= talloc_strdup(mem_ctx
, std_salt
);
554 /* if it's a Windows functional domain, we have to look for the UPN */
556 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
559 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
562 salt
= talloc_strdup(mem_ctx
, upn
);
569 return kerberos_secrets_store_des_salt(salt
);
572 /****************************************************************
573 ****************************************************************/
575 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
576 struct libnet_JoinCtx
*r
)
581 status
= libnet_join_connect_ads(mem_ctx
, r
);
582 if (!ADS_ERR_OK(status
)) {
587 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
588 if (!ADS_ERR_OK(status
)) {
589 libnet_join_set_error_string(mem_ctx
, r
,
590 "failed to set machine spn: %s",
595 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
596 if (!ADS_ERR_OK(status
)) {
597 libnet_join_set_error_string(mem_ctx
, r
,
598 "failed to set machine os attributes: %s",
603 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
604 if (!ADS_ERR_OK(status
)) {
605 libnet_join_set_error_string(mem_ctx
, r
,
606 "failed to set machine upn: %s",
611 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
612 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
615 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
616 libnet_join_set_error_string(mem_ctx
, r
,
617 "failed to create kerberos keytab");
618 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
623 #endif /* WITH_ADS */
625 /****************************************************************
626 Store the machine password and domain SID
627 ****************************************************************/
629 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
630 struct libnet_JoinCtx
*r
)
632 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
635 DEBUG(1,("Failed to save domain sid\n"));
639 if (!secrets_store_machine_password(r
->in
.machine_password
,
640 r
->out
.netbios_domain_name
,
641 r
->in
.secure_channel_type
))
643 DEBUG(1,("Failed to save machine password\n"));
650 /****************************************************************
651 Connect dc's IPC$ share
652 ****************************************************************/
654 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
658 struct cli_state
**cli
)
663 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
666 if (use_kerberos
&& pass
) {
667 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
670 return cli_full_connection(cli
, NULL
,
681 /****************************************************************
682 Lookup domain dc's info
683 ****************************************************************/
685 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
686 struct libnet_JoinCtx
*r
,
687 struct cli_state
**cli
)
689 struct rpc_pipe_client
*pipe_hnd
= NULL
;
690 struct policy_handle lsa_pol
;
691 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
692 union lsa_PolicyInformation
*info
= NULL
;
694 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
696 r
->in
.admin_password
,
699 if (!NT_STATUS_IS_OK(status
)) {
703 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
705 if (!NT_STATUS_IS_OK(status
)) {
706 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
711 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
712 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
713 if (!NT_STATUS_IS_OK(status
)) {
717 status
= rpccli_lsa_QueryInfoPolicy2(pipe_hnd
, mem_ctx
,
721 if (NT_STATUS_IS_OK(status
)) {
722 r
->out
.domain_is_ad
= true;
723 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
724 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
725 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
726 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->dns
.sid
);
727 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
730 if (!NT_STATUS_IS_OK(status
)) {
731 status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
733 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
735 if (!NT_STATUS_IS_OK(status
)) {
739 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
740 r
->out
.domain_sid
= sid_dup_talloc(mem_ctx
, info
->account_domain
.sid
);
741 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
744 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
745 TALLOC_FREE(pipe_hnd
);
751 /****************************************************************
752 Do the domain join unsecure
753 ****************************************************************/
755 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
756 struct libnet_JoinCtx
*r
,
757 struct cli_state
*cli
)
759 struct rpc_pipe_client
*pipe_hnd
= NULL
;
760 unsigned char orig_trust_passwd_hash
[16];
761 unsigned char new_trust_passwd_hash
[16];
762 fstring trust_passwd
;
765 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
767 if (!NT_STATUS_IS_OK(status
)) {
771 if (!r
->in
.machine_password
) {
772 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
773 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
776 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
778 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
779 fstrcpy(trust_passwd
, r
->in
.admin_password
);
780 strlower_m(trust_passwd
);
783 * Machine names can be 15 characters, but the max length on
784 * a password is 14. --jerry
787 trust_passwd
[14] = '\0';
789 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
791 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
792 orig_trust_passwd_hash
,
793 r
->in
.machine_password
,
794 new_trust_passwd_hash
,
795 r
->in
.secure_channel_type
);
800 /****************************************************************
802 ****************************************************************/
804 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
805 struct libnet_JoinCtx
*r
,
806 struct cli_state
*cli
)
808 struct rpc_pipe_client
*pipe_hnd
= NULL
;
809 struct policy_handle sam_pol
, domain_pol
, user_pol
;
810 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
812 struct lsa_String lsa_acct_name
;
814 uint32_t acct_flags
= ACB_WSTRUST
;
815 struct samr_Ids user_rids
;
816 struct samr_Ids name_types
;
817 union samr_UserInfo user_info
;
819 struct samr_CryptPassword crypt_pwd
;
820 struct samr_CryptPasswordEx crypt_pwd_ex
;
822 ZERO_STRUCT(sam_pol
);
823 ZERO_STRUCT(domain_pol
);
824 ZERO_STRUCT(user_pol
);
826 switch (r
->in
.secure_channel_type
) {
828 acct_flags
= ACB_WSTRUST
;
831 acct_flags
= ACB_SVRTRUST
;
834 return NT_STATUS_INVALID_PARAMETER
;
837 if (!r
->in
.machine_password
) {
838 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
839 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
842 /* Open the domain */
844 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
846 if (!NT_STATUS_IS_OK(status
)) {
847 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
852 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
854 SAMR_ACCESS_ENUM_DOMAINS
855 | SAMR_ACCESS_LOOKUP_DOMAIN
,
857 if (!NT_STATUS_IS_OK(status
)) {
861 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
863 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
864 | SAMR_DOMAIN_ACCESS_CREATE_USER
865 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
868 if (!NT_STATUS_IS_OK(status
)) {
872 /* Create domain user */
874 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
875 strlower_m(acct_name
);
877 init_lsa_String(&lsa_acct_name
, acct_name
);
879 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
880 uint32_t access_desired
=
881 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
882 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
883 SAMR_USER_ACCESS_SET_PASSWORD
|
884 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
885 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
886 uint32_t access_granted
= 0;
888 DEBUG(10,("Creating account with desired access mask: %d\n",
891 status
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
899 if (!NT_STATUS_IS_OK(status
) &&
900 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
902 DEBUG(10,("Creation of workstation account failed: %s\n",
905 /* If NT_STATUS_ACCESS_DENIED then we have a valid
906 username/password combo but the user does not have
907 administrator access. */
909 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
910 libnet_join_set_error_string(mem_ctx
, r
,
911 "User specified does not have "
912 "administrator privileges");
918 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
919 if (!(r
->in
.join_flags
&
920 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
925 /* We *must* do this.... don't ask... */
927 if (NT_STATUS_IS_OK(status
)) {
928 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
932 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
938 if (!NT_STATUS_IS_OK(status
)) {
942 if (name_types
.ids
[0] != SID_NAME_USER
) {
943 DEBUG(0,("%s is not a user account (type=%d)\n",
944 acct_name
, name_types
.ids
[0]));
945 status
= NT_STATUS_INVALID_WORKSTATION
;
949 user_rid
= user_rids
.ids
[0];
951 /* Open handle on user */
953 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
955 SEC_FLAG_MAXIMUM_ALLOWED
,
958 if (!NT_STATUS_IS_OK(status
)) {
962 /* Fill in the additional account flags now */
964 acct_flags
|= ACB_PWNOEXP
;
965 if (r
->out
.domain_is_ad
) {
966 #if !defined(ENCTYPE_ARCFOUR_HMAC)
967 acct_flags
|= ACB_USE_DES_KEY_ONLY
;
972 /* Set account flags on machine account */
973 ZERO_STRUCT(user_info
.info16
);
974 user_info
.info16
.acct_flags
= acct_flags
;
976 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
981 if (!NT_STATUS_IS_OK(status
)) {
983 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
986 libnet_join_set_error_string(mem_ctx
, r
,
987 "Failed to set account flags for machine account (%s)\n",
992 /* Set password on machine account - first try level 26 */
994 init_samr_CryptPasswordEx(r
->in
.machine_password
,
995 &cli
->user_session_key
,
998 user_info
.info26
.password
= crypt_pwd_ex
;
999 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1001 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1006 if (NT_STATUS_EQUAL(status
, NT_STATUS(DCERPC_FAULT_INVALID_TAG
))) {
1008 /* retry with level 24 */
1010 init_samr_CryptPassword(r
->in
.machine_password
,
1011 &cli
->user_session_key
,
1014 user_info
.info24
.password
= crypt_pwd
;
1015 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1017 status
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
1023 if (!NT_STATUS_IS_OK(status
)) {
1025 rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
1028 libnet_join_set_error_string(mem_ctx
, r
,
1029 "Failed to set password for machine account (%s)\n",
1034 status
= NT_STATUS_OK
;
1041 if (is_valid_policy_hnd(&sam_pol
)) {
1042 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1044 if (is_valid_policy_hnd(&domain_pol
)) {
1045 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1047 if (is_valid_policy_hnd(&user_pol
)) {
1048 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1050 TALLOC_FREE(pipe_hnd
);
1055 /****************************************************************
1056 ****************************************************************/
1058 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1059 const char *machine_name
,
1060 const char *dc_name
)
1062 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1063 struct cli_state
*cli
= NULL
;
1064 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1065 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1067 char *machine_password
= NULL
;
1068 char *machine_account
= NULL
;
1071 return NT_STATUS_INVALID_PARAMETER
;
1074 if (!secrets_init()) {
1075 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1078 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1080 if (!machine_password
) {
1081 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1084 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1085 SAFE_FREE(machine_password
);
1086 return NT_STATUS_NO_MEMORY
;
1089 status
= cli_full_connection(&cli
, NULL
,
1098 free(machine_account
);
1099 free(machine_password
);
1101 if (!NT_STATUS_IS_OK(status
)) {
1102 status
= cli_full_connection(&cli
, NULL
,
1113 if (!NT_STATUS_IS_OK(status
)) {
1117 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1118 &neg_flags
, &netlogon_pipe
);
1119 if (!NT_STATUS_IS_OK(status
)) {
1120 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1122 return NT_STATUS_OK
;
1125 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1126 "key from server %s for domain %s. Error was %s\n",
1127 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1132 if (!lp_client_schannel()) {
1134 return NT_STATUS_OK
;
1137 status
= cli_rpc_pipe_open_schannel_with_key(
1138 cli
, &ndr_table_netlogon
.syntax_id
, PIPE_AUTH_LEVEL_PRIVACY
,
1139 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1143 if (!NT_STATUS_IS_OK(status
)) {
1144 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1145 "on netlogon pipe to server %s for domain %s. "
1147 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1151 return NT_STATUS_OK
;
1154 /****************************************************************
1155 ****************************************************************/
1157 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1158 struct libnet_JoinCtx
*r
)
1162 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1165 if (!NT_STATUS_IS_OK(status
)) {
1166 libnet_join_set_error_string(mem_ctx
, r
,
1167 "failed to verify domain membership after joining: %s",
1168 get_friendly_nt_error_msg(status
));
1169 return WERR_SETUP_NOT_JOINED
;
1175 /****************************************************************
1176 ****************************************************************/
1178 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1179 struct libnet_UnjoinCtx
*r
)
1181 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1185 if (!secrets_delete_domain_sid(lp_workgroup())) {
1192 /****************************************************************
1193 ****************************************************************/
1195 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1196 struct libnet_UnjoinCtx
*r
)
1198 struct cli_state
*cli
= NULL
;
1199 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1200 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1201 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1204 struct lsa_String lsa_acct_name
;
1205 struct samr_Ids user_rids
;
1206 struct samr_Ids name_types
;
1207 union samr_UserInfo
*info
= NULL
;
1209 ZERO_STRUCT(sam_pol
);
1210 ZERO_STRUCT(domain_pol
);
1211 ZERO_STRUCT(user_pol
);
1213 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1214 r
->in
.admin_account
,
1215 r
->in
.admin_password
,
1218 if (!NT_STATUS_IS_OK(status
)) {
1222 /* Open the domain */
1224 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1226 if (!NT_STATUS_IS_OK(status
)) {
1227 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1228 nt_errstr(status
)));
1232 status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1234 SEC_FLAG_MAXIMUM_ALLOWED
,
1236 if (!NT_STATUS_IS_OK(status
)) {
1240 status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1242 SEC_FLAG_MAXIMUM_ALLOWED
,
1245 if (!NT_STATUS_IS_OK(status
)) {
1249 /* Create domain user */
1251 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1252 strlower_m(acct_name
);
1254 init_lsa_String(&lsa_acct_name
, acct_name
);
1256 status
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1263 if (!NT_STATUS_IS_OK(status
)) {
1267 if (name_types
.ids
[0] != SID_NAME_USER
) {
1268 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1269 name_types
.ids
[0]));
1270 status
= NT_STATUS_INVALID_WORKSTATION
;
1274 user_rid
= user_rids
.ids
[0];
1276 /* Open handle on user */
1278 status
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1280 SEC_FLAG_MAXIMUM_ALLOWED
,
1283 if (!NT_STATUS_IS_OK(status
)) {
1289 status
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1293 if (!NT_STATUS_IS_OK(status
)) {
1294 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1298 /* now disable and setuser info */
1300 info
->info16
.acct_flags
|= ACB_DISABLED
;
1302 status
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1307 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1311 if (is_valid_policy_hnd(&domain_pol
)) {
1312 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1314 if (is_valid_policy_hnd(&sam_pol
)) {
1315 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &sam_pol
);
1317 TALLOC_FREE(pipe_hnd
);
1327 /****************************************************************
1328 ****************************************************************/
1330 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1333 struct smbconf_ctx
*ctx
;
1335 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1336 if (!W_ERROR_IS_OK(werr
)) {
1340 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1342 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1343 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1345 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1348 smbconf_delete_global_parameter(ctx
, "realm");
1352 werr
= smbconf_set_global_parameter(ctx
, "security", "domain");
1353 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1355 werr
= smbconf_set_global_parameter(ctx
, "workgroup",
1356 r
->out
.netbios_domain_name
);
1357 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1359 if (r
->out
.domain_is_ad
) {
1360 werr
= smbconf_set_global_parameter(ctx
, "security", "ads");
1361 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1363 werr
= smbconf_set_global_parameter(ctx
, "realm",
1364 r
->out
.dns_domain_name
);
1365 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1369 smbconf_shutdown(ctx
);
1373 /****************************************************************
1374 ****************************************************************/
1376 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1378 WERROR werr
= WERR_OK
;
1379 struct smbconf_ctx
*ctx
;
1381 werr
= smbconf_init_reg(r
, &ctx
, NULL
);
1382 if (!W_ERROR_IS_OK(werr
)) {
1386 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1388 werr
= smbconf_set_global_parameter(ctx
, "security", "user");
1389 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1391 werr
= smbconf_delete_global_parameter(ctx
, "workgroup");
1392 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1394 smbconf_delete_global_parameter(ctx
, "realm");
1398 smbconf_shutdown(ctx
);
1402 /****************************************************************
1403 ****************************************************************/
1405 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1409 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1410 return r
->out
.result
;
1413 if (!r
->in
.modify_config
) {
1417 werr
= do_join_modify_vals_config(r
);
1418 if (!W_ERROR_IS_OK(werr
)) {
1422 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1424 r
->out
.modified_config
= true;
1425 r
->out
.result
= werr
;
1430 /****************************************************************
1431 ****************************************************************/
1433 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1437 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1438 return r
->out
.result
;
1441 if (!r
->in
.modify_config
) {
1445 werr
= do_unjoin_modify_vals_config(r
);
1446 if (!W_ERROR_IS_OK(werr
)) {
1450 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1452 r
->out
.modified_config
= true;
1453 r
->out
.result
= werr
;
1458 /****************************************************************
1459 ****************************************************************/
1461 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1462 const char *domain_str
,
1463 const char **domain_p
,
1466 char *domain
= NULL
;
1468 const char *p
= NULL
;
1470 if (!domain_str
|| !domain_p
|| !dc_p
) {
1474 p
= strchr_m(domain_str
, '\\');
1477 domain
= talloc_strndup(mem_ctx
, domain_str
,
1478 PTR_DIFF(p
, domain_str
));
1479 dc
= talloc_strdup(mem_ctx
, p
+1);
1484 domain
= talloc_strdup(mem_ctx
, domain_str
);
1500 /****************************************************************
1501 ****************************************************************/
1503 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1504 struct libnet_JoinCtx
*r
)
1506 if (!r
->in
.domain_name
) {
1507 libnet_join_set_error_string(mem_ctx
, r
,
1508 "No domain name defined");
1509 return WERR_INVALID_PARAM
;
1512 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1515 libnet_join_set_error_string(mem_ctx
, r
,
1516 "Failed to parse domain name");
1517 return WERR_INVALID_PARAM
;
1521 return WERR_SETUP_DOMAIN_CONTROLLER
;
1524 if (!secrets_init()) {
1525 libnet_join_set_error_string(mem_ctx
, r
,
1526 "Unable to open secrets database");
1527 return WERR_CAN_NOT_COMPLETE
;
1533 /****************************************************************
1534 ****************************************************************/
1536 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1540 /* Try adding dom admins to builtin\admins. Only log failures. */
1541 status
= create_builtin_administrators(domain_sid
);
1542 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1543 DEBUG(10,("Unable to auto-add domain administrators to "
1544 "BUILTIN\\Administrators during join because "
1545 "winbindd must be running."));
1546 } else if (!NT_STATUS_IS_OK(status
)) {
1547 DEBUG(5, ("Failed to auto-add domain administrators to "
1548 "BUILTIN\\Administrators during join: %s\n",
1549 nt_errstr(status
)));
1552 /* Try adding dom users to builtin\users. Only log failures. */
1553 status
= create_builtin_users(domain_sid
);
1554 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1555 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1556 "during join because winbindd must be running."));
1557 } else if (!NT_STATUS_IS_OK(status
)) {
1558 DEBUG(5, ("Failed to auto-add domain administrators to "
1559 "BUILTIN\\Administrators during join: %s\n",
1560 nt_errstr(status
)));
1564 /****************************************************************
1565 ****************************************************************/
1567 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1568 struct libnet_JoinCtx
*r
)
1572 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1573 return r
->out
.result
;
1576 werr
= do_JoinConfig(r
);
1577 if (!W_ERROR_IS_OK(werr
)) {
1581 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1585 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1586 if (r
->out
.dns_domain_name
) {
1587 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1591 if (r
->out
.domain_is_ad
&&
1592 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1593 ADS_STATUS ads_status
;
1595 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1596 if (!ADS_ERR_OK(ads_status
)) {
1597 return WERR_GENERAL_FAILURE
;
1600 #endif /* WITH_ADS */
1602 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1607 /****************************************************************
1608 ****************************************************************/
1610 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1612 const char *krb5_cc_env
= NULL
;
1615 ads_destroy(&r
->in
.ads
);
1618 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1619 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1620 unsetenv(KRB5_ENV_CCNAME
);
1626 /****************************************************************
1627 ****************************************************************/
1629 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1631 const char *krb5_cc_env
= NULL
;
1634 ads_destroy(&r
->in
.ads
);
1637 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1638 if (krb5_cc_env
&& StrCaseCmp(krb5_cc_env
, "MEMORY:libnetjoin")) {
1639 unsetenv(KRB5_ENV_CCNAME
);
1645 /****************************************************************
1646 ****************************************************************/
1648 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1649 struct libnet_JoinCtx
**r
)
1651 struct libnet_JoinCtx
*ctx
;
1652 const char *krb5_cc_env
= NULL
;
1654 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1659 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1661 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1662 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1664 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1665 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1666 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1667 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1668 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1671 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1678 /****************************************************************
1679 ****************************************************************/
1681 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1682 struct libnet_UnjoinCtx
**r
)
1684 struct libnet_UnjoinCtx
*ctx
;
1685 const char *krb5_cc_env
= NULL
;
1687 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1692 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1694 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, global_myname());
1695 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1697 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1698 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1699 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1700 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1701 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1709 /****************************************************************
1710 ****************************************************************/
1712 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1713 struct libnet_JoinCtx
*r
)
1715 bool valid_security
= false;
1716 bool valid_workgroup
= false;
1717 bool valid_realm
= false;
1719 /* check if configuration is already set correctly */
1721 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1723 switch (r
->out
.domain_is_ad
) {
1725 valid_security
= (lp_security() == SEC_DOMAIN
);
1726 if (valid_workgroup
&& valid_security
) {
1727 /* nothing to be done */
1732 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1733 switch (lp_security()) {
1736 valid_security
= true;
1739 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1740 /* nothing to be done */
1746 /* check if we are supposed to manipulate configuration */
1748 if (!r
->in
.modify_config
) {
1750 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1752 if (!valid_workgroup
) {
1753 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1754 "\"workgroup\" set to '%s', should be '%s'",
1755 lp_workgroup(), r
->out
.netbios_domain_name
);
1756 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1760 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1761 "\"realm\" set to '%s', should be '%s'",
1762 lp_realm(), r
->out
.dns_domain_name
);
1763 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1766 if (!valid_security
) {
1767 const char *sec
= NULL
;
1768 switch (lp_security()) {
1769 case SEC_SHARE
: sec
= "share"; break;
1770 case SEC_USER
: sec
= "user"; break;
1771 case SEC_DOMAIN
: sec
= "domain"; break;
1772 case SEC_ADS
: sec
= "ads"; break;
1774 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1775 "\"security\" set to '%s', should be %s",
1776 sec
, r
->out
.domain_is_ad
?
1777 "either 'domain' or 'ads'" : "'domain'");
1778 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1781 libnet_join_set_error_string(mem_ctx
, r
,
1782 "Invalid configuration (%s) and configuration modification "
1783 "was not requested", wrong_conf
);
1784 return WERR_CAN_NOT_COMPLETE
;
1787 /* check if we are able to manipulate configuration */
1789 if (!lp_config_backend_is_registry()) {
1790 libnet_join_set_error_string(mem_ctx
, r
,
1791 "Configuration manipulation requested but not "
1792 "supported by backend");
1793 return WERR_NOT_SUPPORTED
;
1799 /****************************************************************
1800 ****************************************************************/
1802 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1803 struct libnet_JoinCtx
*r
)
1807 struct cli_state
*cli
= NULL
;
1809 ADS_STATUS ads_status
;
1810 #endif /* WITH_ADS */
1812 if (!r
->in
.dc_name
) {
1813 struct netr_DsRGetDCNameInfo
*info
;
1815 status
= dsgetdcname(mem_ctx
,
1820 DS_FORCE_REDISCOVERY
|
1821 DS_DIRECTORY_SERVICE_REQUIRED
|
1822 DS_WRITABLE_REQUIRED
|
1825 if (!NT_STATUS_IS_OK(status
)) {
1826 libnet_join_set_error_string(mem_ctx
, r
,
1827 "failed to find DC for domain %s",
1829 get_friendly_nt_error_msg(status
));
1830 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
1833 dc
= strip_hostname(info
->dc_unc
);
1834 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1835 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1838 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1839 if (!NT_STATUS_IS_OK(status
)) {
1840 libnet_join_set_error_string(mem_ctx
, r
,
1841 "failed to lookup DC info for domain '%s' over rpc: %s",
1842 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1843 return ntstatus_to_werror(status
);
1846 werr
= libnet_join_check_config(mem_ctx
, r
);
1847 if (!W_ERROR_IS_OK(werr
)) {
1852 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
1853 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1855 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
1856 if (!ADS_ERR_OK(ads_status
)) {
1857 return WERR_DEFAULT_JOIN_REQUIRED
;
1860 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
1861 if (!ADS_ERR_OK(ads_status
)) {
1862 libnet_join_set_error_string(mem_ctx
, r
,
1863 "failed to precreate account in ou %s: %s",
1865 ads_errstr(ads_status
));
1866 return WERR_DEFAULT_JOIN_REQUIRED
;
1869 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
1871 #endif /* WITH_ADS */
1873 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
1874 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
1875 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
1877 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
1879 if (!NT_STATUS_IS_OK(status
)) {
1880 libnet_join_set_error_string(mem_ctx
, r
,
1881 "failed to join domain '%s' over rpc: %s",
1882 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1883 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
1884 return WERR_SETUP_ALREADY_JOINED
;
1886 werr
= ntstatus_to_werror(status
);
1890 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
1891 werr
= WERR_SETUP_NOT_JOINED
;
1905 /****************************************************************
1906 ****************************************************************/
1908 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
1909 struct libnet_JoinCtx
*r
)
1912 struct libnet_UnjoinCtx
*u
= NULL
;
1914 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
1915 if (!W_ERROR_IS_OK(werr
)) {
1919 u
->in
.debug
= r
->in
.debug
;
1920 u
->in
.dc_name
= r
->in
.dc_name
;
1921 u
->in
.domain_name
= r
->in
.domain_name
;
1922 u
->in
.admin_account
= r
->in
.admin_account
;
1923 u
->in
.admin_password
= r
->in
.admin_password
;
1924 u
->in
.modify_config
= r
->in
.modify_config
;
1925 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
1926 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
1928 werr
= libnet_Unjoin(mem_ctx
, u
);
1934 /****************************************************************
1935 ****************************************************************/
1937 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
1938 struct libnet_JoinCtx
*r
)
1943 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
1946 werr
= libnet_join_pre_processing(mem_ctx
, r
);
1947 if (!W_ERROR_IS_OK(werr
)) {
1951 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1952 werr
= libnet_DomainJoin(mem_ctx
, r
);
1953 if (!W_ERROR_IS_OK(werr
)) {
1958 werr
= libnet_join_post_processing(mem_ctx
, r
);
1959 if (!W_ERROR_IS_OK(werr
)) {
1963 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1964 werr
= libnet_join_post_verify(mem_ctx
, r
);
1965 if (!W_ERROR_IS_OK(werr
)) {
1966 libnet_join_rollback(mem_ctx
, r
);
1971 r
->out
.result
= werr
;
1974 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
1979 /****************************************************************
1980 ****************************************************************/
1982 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
1983 struct libnet_UnjoinCtx
*r
)
1987 if (!r
->in
.domain_sid
) {
1989 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
1990 libnet_unjoin_set_error_string(mem_ctx
, r
,
1991 "Unable to fetch domain sid: are we joined?");
1992 return WERR_SETUP_NOT_JOINED
;
1994 r
->in
.domain_sid
= sid_dup_talloc(mem_ctx
, &sid
);
1995 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
1998 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
1999 !r
->in
.delete_machine_account
) {
2000 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2004 if (!r
->in
.dc_name
) {
2005 struct netr_DsRGetDCNameInfo
*info
;
2007 status
= dsgetdcname(mem_ctx
,
2012 DS_DIRECTORY_SERVICE_REQUIRED
|
2013 DS_WRITABLE_REQUIRED
|
2016 if (!NT_STATUS_IS_OK(status
)) {
2017 libnet_unjoin_set_error_string(mem_ctx
, r
,
2018 "failed to find DC for domain %s",
2020 get_friendly_nt_error_msg(status
));
2021 return WERR_DOMAIN_CONTROLLER_NOT_FOUND
;
2024 dc
= strip_hostname(info
->dc_unc
);
2025 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2026 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2030 /* for net ads leave, try to delete the account. If it works,
2031 no sense in disabling. If it fails, we can still try to
2034 if (r
->in
.delete_machine_account
) {
2035 ADS_STATUS ads_status
;
2036 libnet_unjoin_connect_ads(mem_ctx
, r
);
2037 ads_status
= libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2038 if (!ADS_ERR_OK(ads_status
)) {
2039 libnet_unjoin_set_error_string(mem_ctx
, r
,
2040 "failed to remove machine account from AD: %s",
2041 ads_errstr(ads_status
));
2043 r
->out
.deleted_machine_account
= true;
2045 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
2046 r
->in
.ads
->server
.realm
);
2047 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2048 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2052 #endif /* WITH_ADS */
2054 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2056 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2057 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2058 if (!NT_STATUS_IS_OK(status
)) {
2059 libnet_unjoin_set_error_string(mem_ctx
, r
,
2060 "failed to disable machine account via rpc: %s",
2061 get_friendly_nt_error_msg(status
));
2062 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2063 return WERR_SETUP_NOT_JOINED
;
2065 return ntstatus_to_werror(status
);
2068 r
->out
.disabled_machine_account
= true;
2069 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
2070 r
->in
.ads
->server
.realm
);
2073 /* If disable succeeded or was not requested at all, we
2074 should be getting rid of our end of things */
2076 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2081 /****************************************************************
2082 ****************************************************************/
2084 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2085 struct libnet_UnjoinCtx
*r
)
2087 if (!r
->in
.domain_name
) {
2088 libnet_unjoin_set_error_string(mem_ctx
, r
,
2089 "No domain name defined");
2090 return WERR_INVALID_PARAM
;
2093 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2096 libnet_unjoin_set_error_string(mem_ctx
, r
,
2097 "Failed to parse domain name");
2098 return WERR_INVALID_PARAM
;
2102 return WERR_SETUP_DOMAIN_CONTROLLER
;
2105 if (!secrets_init()) {
2106 libnet_unjoin_set_error_string(mem_ctx
, r
,
2107 "Unable to open secrets database");
2108 return WERR_CAN_NOT_COMPLETE
;
2114 /****************************************************************
2115 ****************************************************************/
2117 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2118 struct libnet_UnjoinCtx
*r
)
2120 saf_delete(r
->out
.netbios_domain_name
);
2121 saf_delete(r
->out
.dns_domain_name
);
2123 return libnet_unjoin_config(r
);
2126 /****************************************************************
2127 ****************************************************************/
2129 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2130 struct libnet_UnjoinCtx
*r
)
2135 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2138 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2139 if (!W_ERROR_IS_OK(werr
)) {
2143 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2144 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2145 if (!W_ERROR_IS_OK(werr
)) {
2146 libnet_unjoin_config(r
);
2151 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2152 if (!W_ERROR_IS_OK(werr
)) {
2157 r
->out
.result
= werr
;
2160 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);