2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Gerald (Jerry) Carter 2006
5 * Copyright (C) Guenther Deschner 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "librpc/gen_ndr/ndr_libnet_join.h"
24 #include "libnet/libnet_join.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_lsa_c.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/ndr_netlogon.h"
31 #include "rpc_client/cli_netlogon.h"
32 #include "lib/smbconf/smbconf.h"
33 #include "lib/smbconf/smbconf_reg.h"
34 #include "../libds/common/flags.h"
36 #include "rpc_client/init_lsa.h"
37 #include "rpc_client/cli_pipe.h"
39 #include "../libcli/security/security.h"
41 #include "libsmb/libsmb.h"
43 /****************************************************************
44 ****************************************************************/
46 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
49 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
50 DEBUG(1,("libnet_Join:\n%s", str)); \
54 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
55 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
56 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
57 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
59 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
62 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
63 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
67 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
68 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
69 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
70 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
72 /****************************************************************
73 ****************************************************************/
75 static void libnet_join_set_error_string(TALLOC_CTX
*mem_ctx
,
76 struct libnet_JoinCtx
*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
);
90 /****************************************************************
91 ****************************************************************/
93 static void libnet_unjoin_set_error_string(TALLOC_CTX
*mem_ctx
,
94 struct libnet_UnjoinCtx
*r
,
95 const char *format
, ...)
99 if (r
->out
.error_string
) {
103 va_start(args
, format
);
104 r
->out
.error_string
= talloc_vasprintf(mem_ctx
, format
, args
);
110 /****************************************************************
111 ****************************************************************/
113 static ADS_STATUS
libnet_connect_ads(const char *dns_domain_name
,
114 const char *netbios_domain_name
,
116 const char *user_name
,
117 const char *password
,
121 ADS_STRUCT
*my_ads
= NULL
;
124 my_ads
= ads_init(dns_domain_name
,
128 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
132 SAFE_FREE(my_ads
->auth
.user_name
);
133 my_ads
->auth
.user_name
= SMB_STRDUP(user_name
);
134 if ((cp
= strchr_m(my_ads
->auth
.user_name
, '@'))!=0) {
136 SAFE_FREE(my_ads
->auth
.realm
);
137 my_ads
->auth
.realm
= smb_xstrdup(cp
);
138 strupper_m(my_ads
->auth
.realm
);
143 SAFE_FREE(my_ads
->auth
.password
);
144 my_ads
->auth
.password
= SMB_STRDUP(password
);
147 status
= ads_connect_user_creds(my_ads
);
148 if (!ADS_ERR_OK(status
)) {
149 ads_destroy(&my_ads
);
157 /****************************************************************
158 ****************************************************************/
160 static ADS_STATUS
libnet_join_connect_ads(TALLOC_CTX
*mem_ctx
,
161 struct libnet_JoinCtx
*r
)
165 status
= libnet_connect_ads(r
->out
.dns_domain_name
,
166 r
->out
.netbios_domain_name
,
169 r
->in
.admin_password
,
171 if (!ADS_ERR_OK(status
)) {
172 libnet_join_set_error_string(mem_ctx
, r
,
173 "failed to connect to AD: %s",
178 if (!r
->out
.netbios_domain_name
) {
179 r
->out
.netbios_domain_name
= talloc_strdup(mem_ctx
,
180 r
->in
.ads
->server
.workgroup
);
181 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.netbios_domain_name
);
184 if (!r
->out
.dns_domain_name
) {
185 r
->out
.dns_domain_name
= talloc_strdup(mem_ctx
,
186 r
->in
.ads
->config
.realm
);
187 ADS_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
190 r
->out
.domain_is_ad
= true;
195 /****************************************************************
196 ****************************************************************/
198 static ADS_STATUS
libnet_unjoin_connect_ads(TALLOC_CTX
*mem_ctx
,
199 struct libnet_UnjoinCtx
*r
)
203 status
= libnet_connect_ads(r
->in
.domain_name
,
207 r
->in
.admin_password
,
209 if (!ADS_ERR_OK(status
)) {
210 libnet_unjoin_set_error_string(mem_ctx
, r
,
211 "failed to connect to AD: %s",
218 /****************************************************************
219 join a domain using ADS (LDAP mods)
220 ****************************************************************/
222 static ADS_STATUS
libnet_join_precreate_machine_acct(TALLOC_CTX
*mem_ctx
,
223 struct libnet_JoinCtx
*r
)
226 LDAPMessage
*res
= NULL
;
227 const char *attrs
[] = { "dn", NULL
};
230 status
= ads_check_ou_dn(mem_ctx
, r
->in
.ads
, &r
->in
.account_ou
);
231 if (!ADS_ERR_OK(status
)) {
235 status
= ads_search_dn(r
->in
.ads
, &res
, r
->in
.account_ou
, attrs
);
236 if (!ADS_ERR_OK(status
)) {
240 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
241 ads_msgfree(r
->in
.ads
, res
);
242 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT
);
245 ads_msgfree(r
->in
.ads
, res
);
247 /* Attempt to create the machine account and bail if this fails.
248 Assume that the admin wants exactly what they requested */
250 status
= ads_create_machine_acct(r
->in
.ads
,
254 if (ADS_ERR_OK(status
)) {
255 DEBUG(1,("machine account creation created\n"));
257 } else if ((status
.error_type
== ENUM_ADS_ERROR_LDAP
) &&
258 (status
.err
.rc
== LDAP_ALREADY_EXISTS
)) {
259 status
= ADS_SUCCESS
;
262 if (!ADS_ERR_OK(status
)) {
263 DEBUG(1,("machine account creation failed\n"));
267 status
= ads_move_machine_acct(r
->in
.ads
,
271 if (!ADS_ERR_OK(status
)) {
272 DEBUG(1,("failure to locate/move pre-existing "
273 "machine account\n"));
277 DEBUG(1,("The machine account %s the specified OU.\n",
278 moved
? "was moved into" : "already exists in"));
283 /****************************************************************
284 ****************************************************************/
286 static ADS_STATUS
libnet_unjoin_remove_machine_acct(TALLOC_CTX
*mem_ctx
,
287 struct libnet_UnjoinCtx
*r
)
292 status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
293 if (!ADS_ERR_OK(status
)) {
294 libnet_unjoin_set_error_string(mem_ctx
, r
,
295 "failed to connect to AD: %s",
301 status
= ads_leave_realm(r
->in
.ads
, r
->in
.machine_name
);
302 if (!ADS_ERR_OK(status
)) {
303 libnet_unjoin_set_error_string(mem_ctx
, r
,
304 "failed to leave realm: %s",
312 /****************************************************************
313 ****************************************************************/
315 static ADS_STATUS
libnet_join_find_machine_acct(TALLOC_CTX
*mem_ctx
,
316 struct libnet_JoinCtx
*r
)
319 LDAPMessage
*res
= NULL
;
322 if (!r
->in
.machine_name
) {
323 return ADS_ERROR(LDAP_NO_MEMORY
);
326 status
= ads_find_machine_acct(r
->in
.ads
,
329 if (!ADS_ERR_OK(status
)) {
333 if (ads_count_replies(r
->in
.ads
, res
) != 1) {
334 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
338 dn
= ads_get_dn(r
->in
.ads
, mem_ctx
, res
);
340 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
344 r
->out
.dn
= talloc_strdup(mem_ctx
, dn
);
346 status
= ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
351 ads_msgfree(r
->in
.ads
, res
);
357 /****************************************************************
358 Set a machines dNSHostName and servicePrincipalName attributes
359 ****************************************************************/
361 static ADS_STATUS
libnet_join_set_machine_spn(TALLOC_CTX
*mem_ctx
,
362 struct libnet_JoinCtx
*r
)
367 const char *spn_array
[3] = {NULL
, NULL
, NULL
};
372 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
373 if (!ADS_ERR_OK(status
)) {
377 /* Windows only creates HOST/shortname & HOST/fqdn. */
379 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", r
->in
.machine_name
);
381 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
386 if (!name_to_fqdn(my_fqdn
, r
->in
.machine_name
)
387 || (strchr(my_fqdn
, '.') == NULL
)) {
388 fstr_sprintf(my_fqdn
, "%s.%s", r
->in
.machine_name
,
389 r
->out
.dns_domain_name
);
394 if (!strequal(my_fqdn
, r
->in
.machine_name
)) {
395 spn
= talloc_asprintf(mem_ctx
, "HOST/%s", my_fqdn
);
397 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
402 mods
= ads_init_mods(mem_ctx
);
404 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
407 /* fields of primary importance */
409 status
= ads_mod_str(mem_ctx
, &mods
, "dNSHostName", my_fqdn
);
410 if (!ADS_ERR_OK(status
)) {
411 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
414 status
= ads_mod_strlist(mem_ctx
, &mods
, "servicePrincipalName",
416 if (!ADS_ERR_OK(status
)) {
417 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
420 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
423 /****************************************************************
424 ****************************************************************/
426 static ADS_STATUS
libnet_join_set_machine_upn(TALLOC_CTX
*mem_ctx
,
427 struct libnet_JoinCtx
*r
)
432 if (!r
->in
.create_upn
) {
438 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
439 if (!ADS_ERR_OK(status
)) {
444 r
->in
.upn
= talloc_asprintf(mem_ctx
,
447 r
->out
.dns_domain_name
);
449 return ADS_ERROR(LDAP_NO_MEMORY
);
453 /* now do the mods */
455 mods
= ads_init_mods(mem_ctx
);
457 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
460 /* fields of primary importance */
462 status
= ads_mod_str(mem_ctx
, &mods
, "userPrincipalName", r
->in
.upn
);
463 if (!ADS_ERR_OK(status
)) {
464 return ADS_ERROR_LDAP(LDAP_NO_MEMORY
);
467 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
471 /****************************************************************
472 ****************************************************************/
474 static ADS_STATUS
libnet_join_set_os_attributes(TALLOC_CTX
*mem_ctx
,
475 struct libnet_JoinCtx
*r
)
481 if (!r
->in
.os_name
|| !r
->in
.os_version
) {
487 status
= libnet_join_find_machine_acct(mem_ctx
, r
);
488 if (!ADS_ERR_OK(status
)) {
492 /* now do the mods */
494 mods
= ads_init_mods(mem_ctx
);
496 return ADS_ERROR(LDAP_NO_MEMORY
);
499 os_sp
= talloc_asprintf(mem_ctx
, "Samba %s", samba_version_string());
501 return ADS_ERROR(LDAP_NO_MEMORY
);
504 /* fields of primary importance */
506 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystem",
508 if (!ADS_ERR_OK(status
)) {
512 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemVersion",
514 if (!ADS_ERR_OK(status
)) {
518 status
= ads_mod_str(mem_ctx
, &mods
, "operatingSystemServicePack",
520 if (!ADS_ERR_OK(status
)) {
524 return ads_gen_mod(r
->in
.ads
, r
->out
.dn
, mods
);
527 /****************************************************************
528 ****************************************************************/
530 static bool libnet_join_create_keytab(TALLOC_CTX
*mem_ctx
,
531 struct libnet_JoinCtx
*r
)
533 if (!USE_SYSTEM_KEYTAB
) {
537 if (ads_keytab_create_default(r
->in
.ads
) != 0) {
544 /****************************************************************
545 ****************************************************************/
547 static bool libnet_join_derive_salting_principal(TALLOC_CTX
*mem_ctx
,
548 struct libnet_JoinCtx
*r
)
550 uint32_t domain_func
;
552 const char *salt
= NULL
;
553 char *std_salt
= NULL
;
555 status
= ads_domain_func_level(r
->in
.ads
, &domain_func
);
556 if (!ADS_ERR_OK(status
)) {
557 libnet_join_set_error_string(mem_ctx
, r
,
558 "failed to determine domain functional level: %s",
563 /* go ahead and setup the default salt */
565 std_salt
= kerberos_standard_des_salt();
567 libnet_join_set_error_string(mem_ctx
, r
,
568 "failed to obtain standard DES salt");
572 salt
= talloc_strdup(mem_ctx
, std_salt
);
579 /* if it's a Windows functional domain, we have to look for the UPN */
581 if (domain_func
== DS_DOMAIN_FUNCTION_2000
) {
584 upn
= ads_get_upn(r
->in
.ads
, mem_ctx
,
587 salt
= talloc_strdup(mem_ctx
, upn
);
594 return kerberos_secrets_store_des_salt(salt
);
597 /****************************************************************
598 ****************************************************************/
600 static ADS_STATUS
libnet_join_post_processing_ads(TALLOC_CTX
*mem_ctx
,
601 struct libnet_JoinCtx
*r
)
606 status
= libnet_join_connect_ads(mem_ctx
, r
);
607 if (!ADS_ERR_OK(status
)) {
612 status
= libnet_join_set_machine_spn(mem_ctx
, r
);
613 if (!ADS_ERR_OK(status
)) {
614 libnet_join_set_error_string(mem_ctx
, r
,
615 "failed to set machine spn: %s",
620 status
= libnet_join_set_os_attributes(mem_ctx
, r
);
621 if (!ADS_ERR_OK(status
)) {
622 libnet_join_set_error_string(mem_ctx
, r
,
623 "failed to set machine os attributes: %s",
628 status
= libnet_join_set_machine_upn(mem_ctx
, r
);
629 if (!ADS_ERR_OK(status
)) {
630 libnet_join_set_error_string(mem_ctx
, r
,
631 "failed to set machine upn: %s",
636 if (!libnet_join_derive_salting_principal(mem_ctx
, r
)) {
637 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
640 if (!libnet_join_create_keytab(mem_ctx
, r
)) {
641 libnet_join_set_error_string(mem_ctx
, r
,
642 "failed to create kerberos keytab");
643 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
648 #endif /* HAVE_ADS */
650 /****************************************************************
651 Store the machine password and domain SID
652 ****************************************************************/
654 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX
*mem_ctx
,
655 struct libnet_JoinCtx
*r
)
657 if (!secrets_store_domain_sid(r
->out
.netbios_domain_name
,
660 DEBUG(1,("Failed to save domain sid\n"));
664 if (!secrets_store_machine_password(r
->in
.machine_password
,
665 r
->out
.netbios_domain_name
,
666 r
->in
.secure_channel_type
))
668 DEBUG(1,("Failed to save machine password\n"));
675 /****************************************************************
676 Connect dc's IPC$ share
677 ****************************************************************/
679 static NTSTATUS
libnet_join_connect_dc_ipc(const char *dc
,
683 struct cli_state
**cli
)
688 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
691 if (use_kerberos
&& pass
) {
692 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
695 return cli_full_connection(cli
, NULL
,
706 /****************************************************************
707 Lookup domain dc's info
708 ****************************************************************/
710 static NTSTATUS
libnet_join_lookup_dc_rpc(TALLOC_CTX
*mem_ctx
,
711 struct libnet_JoinCtx
*r
,
712 struct cli_state
**cli
)
714 struct rpc_pipe_client
*pipe_hnd
= NULL
;
715 struct policy_handle lsa_pol
;
716 NTSTATUS status
, result
;
717 union lsa_PolicyInformation
*info
= NULL
;
718 struct dcerpc_binding_handle
*b
;
720 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
722 r
->in
.admin_password
,
725 if (!NT_STATUS_IS_OK(status
)) {
729 status
= cli_rpc_pipe_open_noauth(*cli
, &ndr_table_lsarpc
.syntax_id
,
731 if (!NT_STATUS_IS_OK(status
)) {
732 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
737 b
= pipe_hnd
->binding_handle
;
739 status
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
740 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
741 if (!NT_STATUS_IS_OK(status
)) {
745 status
= dcerpc_lsa_QueryInfoPolicy2(b
, mem_ctx
,
750 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
751 r
->out
.domain_is_ad
= true;
752 r
->out
.netbios_domain_name
= info
->dns
.name
.string
;
753 r
->out
.dns_domain_name
= info
->dns
.dns_domain
.string
;
754 r
->out
.forest_name
= info
->dns
.dns_forest
.string
;
755 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->dns
.sid
);
756 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
759 if (!NT_STATUS_IS_OK(status
)) {
760 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
762 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
765 if (!NT_STATUS_IS_OK(status
)) {
768 if (!NT_STATUS_IS_OK(result
)) {
773 r
->out
.netbios_domain_name
= info
->account_domain
.name
.string
;
774 r
->out
.domain_sid
= dom_sid_dup(mem_ctx
, info
->account_domain
.sid
);
775 NT_STATUS_HAVE_NO_MEMORY(r
->out
.domain_sid
);
778 dcerpc_lsa_Close(b
, mem_ctx
, &lsa_pol
, &result
);
779 TALLOC_FREE(pipe_hnd
);
785 /****************************************************************
786 Do the domain join unsecure
787 ****************************************************************/
789 static NTSTATUS
libnet_join_joindomain_rpc_unsecure(TALLOC_CTX
*mem_ctx
,
790 struct libnet_JoinCtx
*r
,
791 struct cli_state
*cli
)
793 struct rpc_pipe_client
*pipe_hnd
= NULL
;
794 unsigned char orig_trust_passwd_hash
[16];
795 unsigned char new_trust_passwd_hash
[16];
796 fstring trust_passwd
;
799 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
801 if (!NT_STATUS_IS_OK(status
)) {
805 if (!r
->in
.machine_password
) {
806 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
807 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
810 E_md4hash(r
->in
.machine_password
, new_trust_passwd_hash
);
812 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
813 fstrcpy(trust_passwd
, r
->in
.admin_password
);
814 strlower_m(trust_passwd
);
817 * Machine names can be 15 characters, but the max length on
818 * a password is 14. --jerry
821 trust_passwd
[14] = '\0';
823 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
825 status
= rpccli_netlogon_set_trust_password(pipe_hnd
, mem_ctx
,
827 orig_trust_passwd_hash
,
828 r
->in
.machine_password
,
829 new_trust_passwd_hash
,
830 r
->in
.secure_channel_type
);
835 /****************************************************************
837 ****************************************************************/
839 static NTSTATUS
libnet_join_joindomain_rpc(TALLOC_CTX
*mem_ctx
,
840 struct libnet_JoinCtx
*r
,
841 struct cli_state
*cli
)
843 struct rpc_pipe_client
*pipe_hnd
= NULL
;
844 struct policy_handle sam_pol
, domain_pol
, user_pol
;
845 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
847 struct lsa_String lsa_acct_name
;
849 uint32_t acct_flags
= ACB_WSTRUST
;
850 struct samr_Ids user_rids
;
851 struct samr_Ids name_types
;
852 union samr_UserInfo user_info
;
853 struct dcerpc_binding_handle
*b
= NULL
;
855 struct samr_CryptPassword crypt_pwd
;
856 struct samr_CryptPasswordEx crypt_pwd_ex
;
858 ZERO_STRUCT(sam_pol
);
859 ZERO_STRUCT(domain_pol
);
860 ZERO_STRUCT(user_pol
);
862 switch (r
->in
.secure_channel_type
) {
864 acct_flags
= ACB_WSTRUST
;
867 acct_flags
= ACB_SVRTRUST
;
870 return NT_STATUS_INVALID_PARAMETER
;
873 if (!r
->in
.machine_password
) {
874 r
->in
.machine_password
= generate_random_str(mem_ctx
, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH
);
875 NT_STATUS_HAVE_NO_MEMORY(r
->in
.machine_password
);
878 /* Open the domain */
880 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
882 if (!NT_STATUS_IS_OK(status
)) {
883 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
888 b
= pipe_hnd
->binding_handle
;
890 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
892 SAMR_ACCESS_ENUM_DOMAINS
893 | SAMR_ACCESS_LOOKUP_DOMAIN
,
896 if (!NT_STATUS_IS_OK(status
)) {
899 if (!NT_STATUS_IS_OK(result
)) {
904 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
906 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
907 | SAMR_DOMAIN_ACCESS_CREATE_USER
908 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT
,
912 if (!NT_STATUS_IS_OK(status
)) {
915 if (!NT_STATUS_IS_OK(result
)) {
920 /* Create domain user */
922 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
923 strlower_m(acct_name
);
925 init_lsa_String(&lsa_acct_name
, acct_name
);
927 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
) {
928 uint32_t access_desired
=
929 SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
930 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
931 SAMR_USER_ACCESS_SET_PASSWORD
|
932 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
933 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
934 uint32_t access_granted
= 0;
936 DEBUG(10,("Creating account with desired access mask: %d\n",
939 status
= dcerpc_samr_CreateUser2(b
, mem_ctx
,
948 if (!NT_STATUS_IS_OK(status
)) {
953 if (!NT_STATUS_IS_OK(status
) &&
954 !NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
956 DEBUG(10,("Creation of workstation account failed: %s\n",
959 /* If NT_STATUS_ACCESS_DENIED then we have a valid
960 username/password combo but the user does not have
961 administrator access. */
963 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
964 libnet_join_set_error_string(mem_ctx
, r
,
965 "User specified does not have "
966 "administrator privileges");
972 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
973 if (!(r
->in
.join_flags
&
974 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED
)) {
979 /* We *must* do this.... don't ask... */
981 if (NT_STATUS_IS_OK(status
)) {
982 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
986 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
993 if (!NT_STATUS_IS_OK(status
)) {
996 if (!NT_STATUS_IS_OK(result
)) {
1001 if (name_types
.ids
[0] != SID_NAME_USER
) {
1002 DEBUG(0,("%s is not a user account (type=%d)\n",
1003 acct_name
, name_types
.ids
[0]));
1004 status
= NT_STATUS_INVALID_WORKSTATION
;
1008 user_rid
= user_rids
.ids
[0];
1010 /* Open handle on user */
1012 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1014 SEC_FLAG_MAXIMUM_ALLOWED
,
1018 if (!NT_STATUS_IS_OK(status
)) {
1021 if (!NT_STATUS_IS_OK(result
)) {
1026 /* Fill in the additional account flags now */
1028 acct_flags
|= ACB_PWNOEXP
;
1030 /* Set account flags on machine account */
1031 ZERO_STRUCT(user_info
.info16
);
1032 user_info
.info16
.acct_flags
= acct_flags
;
1034 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1039 if (!NT_STATUS_IS_OK(status
)) {
1040 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1044 libnet_join_set_error_string(mem_ctx
, r
,
1045 "Failed to set account flags for machine account (%s)\n",
1050 if (!NT_STATUS_IS_OK(result
)) {
1053 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1057 libnet_join_set_error_string(mem_ctx
, r
,
1058 "Failed to set account flags for machine account (%s)\n",
1063 /* Set password on machine account - first try level 26 */
1065 init_samr_CryptPasswordEx(r
->in
.machine_password
,
1066 &cli
->user_session_key
,
1069 user_info
.info26
.password
= crypt_pwd_ex
;
1070 user_info
.info26
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1072 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1078 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
1080 /* retry with level 24 */
1082 init_samr_CryptPassword(r
->in
.machine_password
,
1083 &cli
->user_session_key
,
1086 user_info
.info24
.password
= crypt_pwd
;
1087 user_info
.info24
.password_expired
= PASS_DONT_CHANGE_AT_NEXT_LOGON
;
1089 status
= dcerpc_samr_SetUserInfo2(b
, mem_ctx
,
1096 if (!NT_STATUS_IS_OK(status
)) {
1098 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1102 libnet_join_set_error_string(mem_ctx
, r
,
1103 "Failed to set password for machine account (%s)\n",
1107 if (!NT_STATUS_IS_OK(result
)) {
1110 dcerpc_samr_DeleteUser(b
, mem_ctx
,
1114 libnet_join_set_error_string(mem_ctx
, r
,
1115 "Failed to set password for machine account (%s)\n",
1120 status
= NT_STATUS_OK
;
1127 if (is_valid_policy_hnd(&sam_pol
)) {
1128 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1130 if (is_valid_policy_hnd(&domain_pol
)) {
1131 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1133 if (is_valid_policy_hnd(&user_pol
)) {
1134 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1136 TALLOC_FREE(pipe_hnd
);
1141 /****************************************************************
1142 ****************************************************************/
1144 NTSTATUS
libnet_join_ok(const char *netbios_domain_name
,
1145 const char *machine_name
,
1146 const char *dc_name
)
1148 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
1149 struct cli_state
*cli
= NULL
;
1150 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1151 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1153 char *machine_password
= NULL
;
1154 char *machine_account
= NULL
;
1157 return NT_STATUS_INVALID_PARAMETER
;
1160 if (!secrets_init()) {
1161 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1164 machine_password
= secrets_fetch_machine_password(netbios_domain_name
,
1166 if (!machine_password
) {
1167 return NT_STATUS_NO_TRUST_LSA_SECRET
;
1170 if (asprintf(&machine_account
, "%s$", machine_name
) == -1) {
1171 SAFE_FREE(machine_password
);
1172 return NT_STATUS_NO_MEMORY
;
1175 status
= cli_full_connection(&cli
, NULL
,
1184 free(machine_account
);
1185 free(machine_password
);
1187 if (!NT_STATUS_IS_OK(status
)) {
1188 status
= cli_full_connection(&cli
, NULL
,
1199 if (!NT_STATUS_IS_OK(status
)) {
1203 status
= get_schannel_session_key(cli
, netbios_domain_name
,
1204 &neg_flags
, &netlogon_pipe
);
1205 if (!NT_STATUS_IS_OK(status
)) {
1206 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_NETWORK_RESPONSE
)) {
1208 return NT_STATUS_OK
;
1211 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1212 "key from server %s for domain %s. Error was %s\n",
1213 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1218 if (!lp_client_schannel()) {
1220 return NT_STATUS_OK
;
1223 status
= cli_rpc_pipe_open_schannel_with_key(
1224 cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
1225 DCERPC_AUTH_LEVEL_PRIVACY
,
1226 netbios_domain_name
, &netlogon_pipe
->dc
, &pipe_hnd
);
1230 if (!NT_STATUS_IS_OK(status
)) {
1231 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1232 "on netlogon pipe to server %s for domain %s. "
1234 cli
->desthost
, netbios_domain_name
, nt_errstr(status
)));
1238 return NT_STATUS_OK
;
1241 /****************************************************************
1242 ****************************************************************/
1244 static WERROR
libnet_join_post_verify(TALLOC_CTX
*mem_ctx
,
1245 struct libnet_JoinCtx
*r
)
1249 status
= libnet_join_ok(r
->out
.netbios_domain_name
,
1252 if (!NT_STATUS_IS_OK(status
)) {
1253 libnet_join_set_error_string(mem_ctx
, r
,
1254 "failed to verify domain membership after joining: %s",
1255 get_friendly_nt_error_msg(status
));
1256 return WERR_SETUP_NOT_JOINED
;
1262 /****************************************************************
1263 ****************************************************************/
1265 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX
*mem_ctx
,
1266 struct libnet_UnjoinCtx
*r
)
1268 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1272 if (!secrets_delete_domain_sid(lp_workgroup())) {
1279 /****************************************************************
1280 ****************************************************************/
1282 static NTSTATUS
libnet_join_unjoindomain_rpc(TALLOC_CTX
*mem_ctx
,
1283 struct libnet_UnjoinCtx
*r
)
1285 struct cli_state
*cli
= NULL
;
1286 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1287 struct policy_handle sam_pol
, domain_pol
, user_pol
;
1288 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
, result
;
1291 struct lsa_String lsa_acct_name
;
1292 struct samr_Ids user_rids
;
1293 struct samr_Ids name_types
;
1294 union samr_UserInfo
*info
= NULL
;
1295 struct dcerpc_binding_handle
*b
= NULL
;
1297 ZERO_STRUCT(sam_pol
);
1298 ZERO_STRUCT(domain_pol
);
1299 ZERO_STRUCT(user_pol
);
1301 status
= libnet_join_connect_dc_ipc(r
->in
.dc_name
,
1302 r
->in
.admin_account
,
1303 r
->in
.admin_password
,
1306 if (!NT_STATUS_IS_OK(status
)) {
1310 /* Open the domain */
1312 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
1314 if (!NT_STATUS_IS_OK(status
)) {
1315 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1316 nt_errstr(status
)));
1320 b
= pipe_hnd
->binding_handle
;
1322 status
= dcerpc_samr_Connect2(b
, mem_ctx
,
1324 SEC_FLAG_MAXIMUM_ALLOWED
,
1327 if (!NT_STATUS_IS_OK(status
)) {
1330 if (!NT_STATUS_IS_OK(result
)) {
1335 status
= dcerpc_samr_OpenDomain(b
, mem_ctx
,
1337 SEC_FLAG_MAXIMUM_ALLOWED
,
1341 if (!NT_STATUS_IS_OK(status
)) {
1344 if (!NT_STATUS_IS_OK(result
)) {
1349 /* Create domain user */
1351 acct_name
= talloc_asprintf(mem_ctx
, "%s$", r
->in
.machine_name
);
1352 strlower_m(acct_name
);
1354 init_lsa_String(&lsa_acct_name
, acct_name
);
1356 status
= dcerpc_samr_LookupNames(b
, mem_ctx
,
1364 if (!NT_STATUS_IS_OK(status
)) {
1367 if (!NT_STATUS_IS_OK(result
)) {
1372 if (name_types
.ids
[0] != SID_NAME_USER
) {
1373 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name
,
1374 name_types
.ids
[0]));
1375 status
= NT_STATUS_INVALID_WORKSTATION
;
1379 user_rid
= user_rids
.ids
[0];
1381 /* Open handle on user */
1383 status
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1385 SEC_FLAG_MAXIMUM_ALLOWED
,
1389 if (!NT_STATUS_IS_OK(status
)) {
1392 if (!NT_STATUS_IS_OK(result
)) {
1399 status
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1404 if (!NT_STATUS_IS_OK(status
)) {
1405 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1408 if (!NT_STATUS_IS_OK(result
)) {
1410 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1414 /* now disable and setuser info */
1416 info
->info16
.acct_flags
|= ACB_DISABLED
;
1418 status
= dcerpc_samr_SetUserInfo(b
, mem_ctx
,
1423 if (!NT_STATUS_IS_OK(status
)) {
1424 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1427 if (!NT_STATUS_IS_OK(result
)) {
1429 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1433 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result
);
1436 if (pipe_hnd
&& b
) {
1437 if (is_valid_policy_hnd(&domain_pol
)) {
1438 dcerpc_samr_Close(b
, mem_ctx
, &domain_pol
, &result
);
1440 if (is_valid_policy_hnd(&sam_pol
)) {
1441 dcerpc_samr_Close(b
, mem_ctx
, &sam_pol
, &result
);
1443 TALLOC_FREE(pipe_hnd
);
1453 /****************************************************************
1454 ****************************************************************/
1456 static WERROR
do_join_modify_vals_config(struct libnet_JoinCtx
*r
)
1458 WERROR werr
= WERR_OK
;
1460 struct smbconf_ctx
*ctx
;
1462 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1463 if (!SBC_ERROR_IS_OK(err
)) {
1464 werr
= WERR_NO_SUCH_SERVICE
;
1468 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1470 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1471 if (!SBC_ERROR_IS_OK(err
)) {
1472 werr
= WERR_NO_SUCH_SERVICE
;
1476 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1478 if (!SBC_ERROR_IS_OK(err
)) {
1479 werr
= WERR_NO_SUCH_SERVICE
;
1483 smbconf_delete_global_parameter(ctx
, "realm");
1487 err
= smbconf_set_global_parameter(ctx
, "security", "domain");
1488 if (!SBC_ERROR_IS_OK(err
)) {
1489 werr
= WERR_NO_SUCH_SERVICE
;
1493 err
= smbconf_set_global_parameter(ctx
, "workgroup",
1494 r
->out
.netbios_domain_name
);
1495 if (!SBC_ERROR_IS_OK(err
)) {
1496 werr
= WERR_NO_SUCH_SERVICE
;
1500 if (r
->out
.domain_is_ad
) {
1501 err
= smbconf_set_global_parameter(ctx
, "security", "ads");
1502 if (!SBC_ERROR_IS_OK(err
)) {
1503 werr
= WERR_NO_SUCH_SERVICE
;
1507 err
= smbconf_set_global_parameter(ctx
, "realm",
1508 r
->out
.dns_domain_name
);
1509 if (!SBC_ERROR_IS_OK(err
)) {
1510 werr
= WERR_NO_SUCH_SERVICE
;
1516 smbconf_shutdown(ctx
);
1520 /****************************************************************
1521 ****************************************************************/
1523 static WERROR
do_unjoin_modify_vals_config(struct libnet_UnjoinCtx
*r
)
1525 WERROR werr
= WERR_OK
;
1527 struct smbconf_ctx
*ctx
;
1529 err
= smbconf_init_reg(r
, &ctx
, NULL
);
1530 if (!SBC_ERROR_IS_OK(err
)) {
1531 werr
= WERR_NO_SUCH_SERVICE
;
1535 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
1537 err
= smbconf_set_global_parameter(ctx
, "security", "user");
1538 if (!SBC_ERROR_IS_OK(err
)) {
1539 werr
= WERR_NO_SUCH_SERVICE
;
1543 err
= smbconf_delete_global_parameter(ctx
, "workgroup");
1544 if (!SBC_ERROR_IS_OK(err
)) {
1545 werr
= WERR_NO_SUCH_SERVICE
;
1549 smbconf_delete_global_parameter(ctx
, "realm");
1553 smbconf_shutdown(ctx
);
1557 /****************************************************************
1558 ****************************************************************/
1560 static WERROR
do_JoinConfig(struct libnet_JoinCtx
*r
)
1564 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1565 return r
->out
.result
;
1568 if (!r
->in
.modify_config
) {
1572 werr
= do_join_modify_vals_config(r
);
1573 if (!W_ERROR_IS_OK(werr
)) {
1577 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1579 r
->out
.modified_config
= true;
1580 r
->out
.result
= werr
;
1585 /****************************************************************
1586 ****************************************************************/
1588 static WERROR
libnet_unjoin_config(struct libnet_UnjoinCtx
*r
)
1592 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1593 return r
->out
.result
;
1596 if (!r
->in
.modify_config
) {
1600 werr
= do_unjoin_modify_vals_config(r
);
1601 if (!W_ERROR_IS_OK(werr
)) {
1605 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1607 r
->out
.modified_config
= true;
1608 r
->out
.result
= werr
;
1613 /****************************************************************
1614 ****************************************************************/
1616 static bool libnet_parse_domain_dc(TALLOC_CTX
*mem_ctx
,
1617 const char *domain_str
,
1618 const char **domain_p
,
1621 char *domain
= NULL
;
1623 const char *p
= NULL
;
1625 if (!domain_str
|| !domain_p
|| !dc_p
) {
1629 p
= strchr_m(domain_str
, '\\');
1632 domain
= talloc_strndup(mem_ctx
, domain_str
,
1633 PTR_DIFF(p
, domain_str
));
1634 dc
= talloc_strdup(mem_ctx
, p
+1);
1639 domain
= talloc_strdup(mem_ctx
, domain_str
);
1655 /****************************************************************
1656 ****************************************************************/
1658 static WERROR
libnet_join_pre_processing(TALLOC_CTX
*mem_ctx
,
1659 struct libnet_JoinCtx
*r
)
1661 if (!r
->in
.domain_name
) {
1662 libnet_join_set_error_string(mem_ctx
, r
,
1663 "No domain name defined");
1664 return WERR_INVALID_PARAM
;
1667 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
1670 libnet_join_set_error_string(mem_ctx
, r
,
1671 "Failed to parse domain name");
1672 return WERR_INVALID_PARAM
;
1676 return WERR_SETUP_DOMAIN_CONTROLLER
;
1679 if (!secrets_init()) {
1680 libnet_join_set_error_string(mem_ctx
, r
,
1681 "Unable to open secrets database");
1682 return WERR_CAN_NOT_COMPLETE
;
1688 /****************************************************************
1689 ****************************************************************/
1691 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid
*domain_sid
)
1695 /* Try adding dom admins to builtin\admins. Only log failures. */
1696 status
= create_builtin_administrators(domain_sid
);
1697 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1698 DEBUG(10,("Unable to auto-add domain administrators to "
1699 "BUILTIN\\Administrators during join because "
1700 "winbindd must be running."));
1701 } else if (!NT_STATUS_IS_OK(status
)) {
1702 DEBUG(5, ("Failed to auto-add domain administrators to "
1703 "BUILTIN\\Administrators during join: %s\n",
1704 nt_errstr(status
)));
1707 /* Try adding dom users to builtin\users. Only log failures. */
1708 status
= create_builtin_users(domain_sid
);
1709 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
1710 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1711 "during join because winbindd must be running."));
1712 } else if (!NT_STATUS_IS_OK(status
)) {
1713 DEBUG(5, ("Failed to auto-add domain administrators to "
1714 "BUILTIN\\Administrators during join: %s\n",
1715 nt_errstr(status
)));
1719 /****************************************************************
1720 ****************************************************************/
1722 static WERROR
libnet_join_post_processing(TALLOC_CTX
*mem_ctx
,
1723 struct libnet_JoinCtx
*r
)
1727 if (!W_ERROR_IS_OK(r
->out
.result
)) {
1728 return r
->out
.result
;
1731 werr
= do_JoinConfig(r
);
1732 if (!W_ERROR_IS_OK(werr
)) {
1736 if (!(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
)) {
1740 saf_join_store(r
->out
.netbios_domain_name
, r
->in
.dc_name
);
1741 if (r
->out
.dns_domain_name
) {
1742 saf_join_store(r
->out
.dns_domain_name
, r
->in
.dc_name
);
1746 if (r
->out
.domain_is_ad
&&
1747 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
1748 ADS_STATUS ads_status
;
1750 ads_status
= libnet_join_post_processing_ads(mem_ctx
, r
);
1751 if (!ADS_ERR_OK(ads_status
)) {
1752 return WERR_GENERAL_FAILURE
;
1755 #endif /* HAVE_ADS */
1757 libnet_join_add_dom_rids_to_builtins(r
->out
.domain_sid
);
1762 /****************************************************************
1763 ****************************************************************/
1765 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx
*r
)
1767 const char *krb5_cc_env
= NULL
;
1770 ads_destroy(&r
->in
.ads
);
1773 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1774 if (krb5_cc_env
&& strcasecmp_m(krb5_cc_env
, "MEMORY:libnetjoin")) {
1775 unsetenv(KRB5_ENV_CCNAME
);
1781 /****************************************************************
1782 ****************************************************************/
1784 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx
*r
)
1786 const char *krb5_cc_env
= NULL
;
1789 ads_destroy(&r
->in
.ads
);
1792 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1793 if (krb5_cc_env
&& strcasecmp_m(krb5_cc_env
, "MEMORY:libnetjoin")) {
1794 unsetenv(KRB5_ENV_CCNAME
);
1800 /****************************************************************
1801 ****************************************************************/
1803 WERROR
libnet_init_JoinCtx(TALLOC_CTX
*mem_ctx
,
1804 struct libnet_JoinCtx
**r
)
1806 struct libnet_JoinCtx
*ctx
;
1807 const char *krb5_cc_env
= NULL
;
1809 ctx
= talloc_zero(mem_ctx
, struct libnet_JoinCtx
);
1814 talloc_set_destructor(ctx
, libnet_destroy_JoinCtx
);
1816 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
1817 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1819 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1820 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1821 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1822 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1823 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1826 ctx
->in
.secure_channel_type
= SEC_CHAN_WKSTA
;
1833 /****************************************************************
1834 ****************************************************************/
1836 WERROR
libnet_init_UnjoinCtx(TALLOC_CTX
*mem_ctx
,
1837 struct libnet_UnjoinCtx
**r
)
1839 struct libnet_UnjoinCtx
*ctx
;
1840 const char *krb5_cc_env
= NULL
;
1842 ctx
= talloc_zero(mem_ctx
, struct libnet_UnjoinCtx
);
1847 talloc_set_destructor(ctx
, libnet_destroy_UnjoinCtx
);
1849 ctx
->in
.machine_name
= talloc_strdup(mem_ctx
, lp_netbios_name());
1850 W_ERROR_HAVE_NO_MEMORY(ctx
->in
.machine_name
);
1852 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
1853 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
1854 krb5_cc_env
= talloc_strdup(mem_ctx
, "MEMORY:libnetjoin");
1855 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env
);
1856 setenv(KRB5_ENV_CCNAME
, krb5_cc_env
, 1);
1864 /****************************************************************
1865 ****************************************************************/
1867 static WERROR
libnet_join_check_config(TALLOC_CTX
*mem_ctx
,
1868 struct libnet_JoinCtx
*r
)
1870 bool valid_security
= false;
1871 bool valid_workgroup
= false;
1872 bool valid_realm
= false;
1874 /* check if configuration is already set correctly */
1876 valid_workgroup
= strequal(lp_workgroup(), r
->out
.netbios_domain_name
);
1878 switch (r
->out
.domain_is_ad
) {
1880 valid_security
= (lp_security() == SEC_DOMAIN
);
1881 if (valid_workgroup
&& valid_security
) {
1882 /* nothing to be done */
1887 valid_realm
= strequal(lp_realm(), r
->out
.dns_domain_name
);
1888 switch (lp_security()) {
1891 valid_security
= true;
1894 if (valid_workgroup
&& valid_realm
&& valid_security
) {
1895 /* nothing to be done */
1901 /* check if we are supposed to manipulate configuration */
1903 if (!r
->in
.modify_config
) {
1905 char *wrong_conf
= talloc_strdup(mem_ctx
, "");
1907 if (!valid_workgroup
) {
1908 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1909 "\"workgroup\" set to '%s', should be '%s'",
1910 lp_workgroup(), r
->out
.netbios_domain_name
);
1911 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1915 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1916 "\"realm\" set to '%s', should be '%s'",
1917 lp_realm(), r
->out
.dns_domain_name
);
1918 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1921 if (!valid_security
) {
1922 const char *sec
= NULL
;
1923 switch (lp_security()) {
1924 case SEC_SHARE
: sec
= "share"; break;
1925 case SEC_USER
: sec
= "user"; break;
1926 case SEC_DOMAIN
: sec
= "domain"; break;
1927 case SEC_ADS
: sec
= "ads"; break;
1929 wrong_conf
= talloc_asprintf_append(wrong_conf
,
1930 "\"security\" set to '%s', should be %s",
1931 sec
, r
->out
.domain_is_ad
?
1932 "either 'domain' or 'ads'" : "'domain'");
1933 W_ERROR_HAVE_NO_MEMORY(wrong_conf
);
1936 libnet_join_set_error_string(mem_ctx
, r
,
1937 "Invalid configuration (%s) and configuration modification "
1938 "was not requested", wrong_conf
);
1939 return WERR_CAN_NOT_COMPLETE
;
1942 /* check if we are able to manipulate configuration */
1944 if (!lp_config_backend_is_registry()) {
1945 libnet_join_set_error_string(mem_ctx
, r
,
1946 "Configuration manipulation requested but not "
1947 "supported by backend");
1948 return WERR_NOT_SUPPORTED
;
1954 /****************************************************************
1955 ****************************************************************/
1957 static WERROR
libnet_DomainJoin(TALLOC_CTX
*mem_ctx
,
1958 struct libnet_JoinCtx
*r
)
1962 struct cli_state
*cli
= NULL
;
1964 ADS_STATUS ads_status
;
1965 #endif /* HAVE_ADS */
1967 if (!r
->in
.dc_name
) {
1968 struct netr_DsRGetDCNameInfo
*info
;
1970 status
= dsgetdcname(mem_ctx
,
1975 DS_FORCE_REDISCOVERY
|
1976 DS_DIRECTORY_SERVICE_REQUIRED
|
1977 DS_WRITABLE_REQUIRED
|
1980 if (!NT_STATUS_IS_OK(status
)) {
1981 libnet_join_set_error_string(mem_ctx
, r
,
1982 "failed to find DC for domain %s",
1984 get_friendly_nt_error_msg(status
));
1985 return WERR_DCNOTFOUND
;
1988 dc
= strip_hostname(info
->dc_unc
);
1989 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
1990 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
1993 status
= libnet_join_lookup_dc_rpc(mem_ctx
, r
, &cli
);
1994 if (!NT_STATUS_IS_OK(status
)) {
1995 libnet_join_set_error_string(mem_ctx
, r
,
1996 "failed to lookup DC info for domain '%s' over rpc: %s",
1997 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
1998 return ntstatus_to_werror(status
);
2001 werr
= libnet_join_check_config(mem_ctx
, r
);
2002 if (!W_ERROR_IS_OK(werr
)) {
2008 create_local_private_krb5_conf_for_domain(
2009 r
->out
.dns_domain_name
, r
->out
.netbios_domain_name
,
2010 NULL
, &cli
->dest_ss
, cli
->desthost
);
2012 if (r
->out
.domain_is_ad
&& r
->in
.account_ou
&&
2013 !(r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
)) {
2015 ads_status
= libnet_join_connect_ads(mem_ctx
, r
);
2016 if (!ADS_ERR_OK(ads_status
)) {
2017 return WERR_DEFAULT_JOIN_REQUIRED
;
2020 ads_status
= libnet_join_precreate_machine_acct(mem_ctx
, r
);
2021 if (!ADS_ERR_OK(ads_status
)) {
2022 libnet_join_set_error_string(mem_ctx
, r
,
2023 "failed to precreate account in ou %s: %s",
2025 ads_errstr(ads_status
));
2026 return WERR_DEFAULT_JOIN_REQUIRED
;
2029 r
->in
.join_flags
&= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE
;
2031 #endif /* HAVE_ADS */
2033 if ((r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_UNSECURE
) &&
2034 (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED
)) {
2035 status
= libnet_join_joindomain_rpc_unsecure(mem_ctx
, r
, cli
);
2037 status
= libnet_join_joindomain_rpc(mem_ctx
, r
, cli
);
2039 if (!NT_STATUS_IS_OK(status
)) {
2040 libnet_join_set_error_string(mem_ctx
, r
,
2041 "failed to join domain '%s' over rpc: %s",
2042 r
->in
.domain_name
, get_friendly_nt_error_msg(status
));
2043 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
2044 return WERR_SETUP_ALREADY_JOINED
;
2046 werr
= ntstatus_to_werror(status
);
2050 if (!libnet_join_joindomain_store_secrets(mem_ctx
, r
)) {
2051 werr
= WERR_SETUP_NOT_JOINED
;
2065 /****************************************************************
2066 ****************************************************************/
2068 static WERROR
libnet_join_rollback(TALLOC_CTX
*mem_ctx
,
2069 struct libnet_JoinCtx
*r
)
2072 struct libnet_UnjoinCtx
*u
= NULL
;
2074 werr
= libnet_init_UnjoinCtx(mem_ctx
, &u
);
2075 if (!W_ERROR_IS_OK(werr
)) {
2079 u
->in
.debug
= r
->in
.debug
;
2080 u
->in
.dc_name
= r
->in
.dc_name
;
2081 u
->in
.domain_name
= r
->in
.domain_name
;
2082 u
->in
.admin_account
= r
->in
.admin_account
;
2083 u
->in
.admin_password
= r
->in
.admin_password
;
2084 u
->in
.modify_config
= r
->in
.modify_config
;
2085 u
->in
.unjoin_flags
= WKSSVC_JOIN_FLAGS_JOIN_TYPE
|
2086 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
;
2088 werr
= libnet_Unjoin(mem_ctx
, u
);
2094 /****************************************************************
2095 ****************************************************************/
2097 WERROR
libnet_Join(TALLOC_CTX
*mem_ctx
,
2098 struct libnet_JoinCtx
*r
)
2103 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx
, r
);
2106 ZERO_STRUCT(r
->out
);
2108 werr
= libnet_join_pre_processing(mem_ctx
, r
);
2109 if (!W_ERROR_IS_OK(werr
)) {
2113 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2114 werr
= libnet_DomainJoin(mem_ctx
, r
);
2115 if (!W_ERROR_IS_OK(werr
)) {
2120 werr
= libnet_join_post_processing(mem_ctx
, r
);
2121 if (!W_ERROR_IS_OK(werr
)) {
2125 if (r
->in
.join_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2126 werr
= libnet_join_post_verify(mem_ctx
, r
);
2127 if (!W_ERROR_IS_OK(werr
)) {
2128 libnet_join_rollback(mem_ctx
, r
);
2133 r
->out
.result
= werr
;
2136 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx
, r
);
2141 /****************************************************************
2142 ****************************************************************/
2144 static WERROR
libnet_DomainUnjoin(TALLOC_CTX
*mem_ctx
,
2145 struct libnet_UnjoinCtx
*r
)
2149 if (!r
->in
.domain_sid
) {
2151 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid
)) {
2152 libnet_unjoin_set_error_string(mem_ctx
, r
,
2153 "Unable to fetch domain sid: are we joined?");
2154 return WERR_SETUP_NOT_JOINED
;
2156 r
->in
.domain_sid
= dom_sid_dup(mem_ctx
, &sid
);
2157 W_ERROR_HAVE_NO_MEMORY(r
->in
.domain_sid
);
2160 if (!(r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) &&
2161 !r
->in
.delete_machine_account
) {
2162 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2166 if (!r
->in
.dc_name
) {
2167 struct netr_DsRGetDCNameInfo
*info
;
2169 status
= dsgetdcname(mem_ctx
,
2174 DS_DIRECTORY_SERVICE_REQUIRED
|
2175 DS_WRITABLE_REQUIRED
|
2178 if (!NT_STATUS_IS_OK(status
)) {
2179 libnet_unjoin_set_error_string(mem_ctx
, r
,
2180 "failed to find DC for domain %s",
2182 get_friendly_nt_error_msg(status
));
2183 return WERR_DCNOTFOUND
;
2186 dc
= strip_hostname(info
->dc_unc
);
2187 r
->in
.dc_name
= talloc_strdup(mem_ctx
, dc
);
2188 W_ERROR_HAVE_NO_MEMORY(r
->in
.dc_name
);
2192 /* for net ads leave, try to delete the account. If it works,
2193 no sense in disabling. If it fails, we can still try to
2196 if (r
->in
.delete_machine_account
) {
2197 ADS_STATUS ads_status
;
2198 ads_status
= libnet_unjoin_connect_ads(mem_ctx
, r
);
2199 if (ADS_ERR_OK(ads_status
)) {
2201 r
->out
.dns_domain_name
=
2202 talloc_strdup(mem_ctx
,
2203 r
->in
.ads
->server
.realm
);
2205 libnet_unjoin_remove_machine_acct(mem_ctx
, r
);
2207 if (!ADS_ERR_OK(ads_status
)) {
2208 libnet_unjoin_set_error_string(mem_ctx
, r
,
2209 "failed to remove machine account from AD: %s",
2210 ads_errstr(ads_status
));
2212 r
->out
.deleted_machine_account
= true;
2213 W_ERROR_HAVE_NO_MEMORY(r
->out
.dns_domain_name
);
2214 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2218 #endif /* HAVE_ADS */
2220 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2222 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE
) {
2223 status
= libnet_join_unjoindomain_rpc(mem_ctx
, r
);
2224 if (!NT_STATUS_IS_OK(status
)) {
2225 libnet_unjoin_set_error_string(mem_ctx
, r
,
2226 "failed to disable machine account via rpc: %s",
2227 get_friendly_nt_error_msg(status
));
2228 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
2229 return WERR_SETUP_NOT_JOINED
;
2231 return ntstatus_to_werror(status
);
2234 r
->out
.disabled_machine_account
= true;
2237 /* If disable succeeded or was not requested at all, we
2238 should be getting rid of our end of things */
2240 libnet_join_unjoindomain_remove_secrets(mem_ctx
, r
);
2245 /****************************************************************
2246 ****************************************************************/
2248 static WERROR
libnet_unjoin_pre_processing(TALLOC_CTX
*mem_ctx
,
2249 struct libnet_UnjoinCtx
*r
)
2251 if (!r
->in
.domain_name
) {
2252 libnet_unjoin_set_error_string(mem_ctx
, r
,
2253 "No domain name defined");
2254 return WERR_INVALID_PARAM
;
2257 if (!libnet_parse_domain_dc(mem_ctx
, r
->in
.domain_name
,
2260 libnet_unjoin_set_error_string(mem_ctx
, r
,
2261 "Failed to parse domain name");
2262 return WERR_INVALID_PARAM
;
2266 return WERR_SETUP_DOMAIN_CONTROLLER
;
2269 if (!secrets_init()) {
2270 libnet_unjoin_set_error_string(mem_ctx
, r
,
2271 "Unable to open secrets database");
2272 return WERR_CAN_NOT_COMPLETE
;
2278 /****************************************************************
2279 ****************************************************************/
2281 static WERROR
libnet_unjoin_post_processing(TALLOC_CTX
*mem_ctx
,
2282 struct libnet_UnjoinCtx
*r
)
2284 saf_delete(r
->out
.netbios_domain_name
);
2285 saf_delete(r
->out
.dns_domain_name
);
2287 return libnet_unjoin_config(r
);
2290 /****************************************************************
2291 ****************************************************************/
2293 WERROR
libnet_Unjoin(TALLOC_CTX
*mem_ctx
,
2294 struct libnet_UnjoinCtx
*r
)
2299 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx
, r
);
2302 werr
= libnet_unjoin_pre_processing(mem_ctx
, r
);
2303 if (!W_ERROR_IS_OK(werr
)) {
2307 if (r
->in
.unjoin_flags
& WKSSVC_JOIN_FLAGS_JOIN_TYPE
) {
2308 werr
= libnet_DomainUnjoin(mem_ctx
, r
);
2309 if (!W_ERROR_IS_OK(werr
)) {
2310 libnet_unjoin_config(r
);
2315 werr
= libnet_unjoin_post_processing(mem_ctx
, r
);
2316 if (!W_ERROR_IS_OK(werr
)) {
2321 r
->out
.result
= werr
;
2324 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx
, r
);