s3:configure: search fdatasync also in librt
[Samba/ekacnet.git] / source3 / libnet / libnet_join.c
blob5271c910ddafcf84766ca40f8aba1ee96aacf720
1 /*
2 * Unix SMB/CIFS implementation.
3 * libnet Join Support
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/>.
21 #include "includes.h"
22 #include "libnet/libnet.h"
23 #include "libcli/auth/libcli_auth.h"
24 #include "../librpc/gen_ndr/cli_samr.h"
25 #include "../librpc/gen_ndr/cli_lsa.h"
26 #include "../librpc/gen_ndr/cli_netlogon.h"
28 /****************************************************************
29 ****************************************************************/
31 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
32 do { \
33 char *str = NULL; \
34 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
35 DEBUG(1,("libnet_Join:\n%s", str)); \
36 TALLOC_FREE(str); \
37 } while (0)
39 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
40 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
41 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
42 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
44 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
45 do { \
46 char *str = NULL; \
47 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
48 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
49 TALLOC_FREE(str); \
50 } while (0)
52 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
53 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
54 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
55 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
57 /****************************************************************
58 ****************************************************************/
60 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
61 struct libnet_JoinCtx *r,
62 const char *format, ...)
64 va_list args;
66 if (r->out.error_string) {
67 return;
70 va_start(args, format);
71 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
72 va_end(args);
75 /****************************************************************
76 ****************************************************************/
78 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
79 struct libnet_UnjoinCtx *r,
80 const char *format, ...)
82 va_list args;
84 if (r->out.error_string) {
85 return;
88 va_start(args, format);
89 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
90 va_end(args);
93 #ifdef WITH_ADS
95 /****************************************************************
96 ****************************************************************/
98 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
99 const char *netbios_domain_name,
100 const char *dc_name,
101 const char *user_name,
102 const char *password,
103 ADS_STRUCT **ads)
105 ADS_STATUS status;
106 ADS_STRUCT *my_ads = NULL;
108 my_ads = ads_init(dns_domain_name,
109 netbios_domain_name,
110 dc_name);
111 if (!my_ads) {
112 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
115 if (user_name) {
116 SAFE_FREE(my_ads->auth.user_name);
117 my_ads->auth.user_name = SMB_STRDUP(user_name);
120 if (password) {
121 SAFE_FREE(my_ads->auth.password);
122 my_ads->auth.password = SMB_STRDUP(password);
125 status = ads_connect_user_creds(my_ads);
126 if (!ADS_ERR_OK(status)) {
127 ads_destroy(&my_ads);
128 return status;
131 *ads = my_ads;
132 return ADS_SUCCESS;
135 /****************************************************************
136 ****************************************************************/
138 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
139 struct libnet_JoinCtx *r)
141 ADS_STATUS status;
143 status = libnet_connect_ads(r->out.dns_domain_name,
144 r->out.netbios_domain_name,
145 r->in.dc_name,
146 r->in.admin_account,
147 r->in.admin_password,
148 &r->in.ads);
149 if (!ADS_ERR_OK(status)) {
150 libnet_join_set_error_string(mem_ctx, r,
151 "failed to connect to AD: %s",
152 ads_errstr(status));
153 return status;
156 if (!r->out.netbios_domain_name) {
157 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
158 r->in.ads->server.workgroup);
159 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
162 if (!r->out.dns_domain_name) {
163 r->out.dns_domain_name = talloc_strdup(mem_ctx,
164 r->in.ads->config.realm);
165 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
168 r->out.domain_is_ad = true;
170 return ADS_SUCCESS;
173 /****************************************************************
174 ****************************************************************/
176 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
177 struct libnet_UnjoinCtx *r)
179 ADS_STATUS status;
181 status = libnet_connect_ads(r->in.domain_name,
182 r->in.domain_name,
183 r->in.dc_name,
184 r->in.admin_account,
185 r->in.admin_password,
186 &r->in.ads);
187 if (!ADS_ERR_OK(status)) {
188 libnet_unjoin_set_error_string(mem_ctx, r,
189 "failed to connect to AD: %s",
190 ads_errstr(status));
193 return status;
196 /****************************************************************
197 join a domain using ADS (LDAP mods)
198 ****************************************************************/
200 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
201 struct libnet_JoinCtx *r)
203 ADS_STATUS status;
204 LDAPMessage *res = NULL;
205 const char *attrs[] = { "dn", NULL };
206 bool moved = false;
208 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
209 if (!ADS_ERR_OK(status)) {
210 return status;
213 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
214 if (!ADS_ERR_OK(status)) {
215 return status;
218 if (ads_count_replies(r->in.ads, res) != 1) {
219 ads_msgfree(r->in.ads, res);
220 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
223 ads_msgfree(r->in.ads, res);
225 /* Attempt to create the machine account and bail if this fails.
226 Assume that the admin wants exactly what they requested */
228 status = ads_create_machine_acct(r->in.ads,
229 r->in.machine_name,
230 r->in.account_ou);
232 if (ADS_ERR_OK(status)) {
233 DEBUG(1,("machine account creation created\n"));
234 return status;
235 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
236 (status.err.rc == LDAP_ALREADY_EXISTS)) {
237 status = ADS_SUCCESS;
240 if (!ADS_ERR_OK(status)) {
241 DEBUG(1,("machine account creation failed\n"));
242 return status;
245 status = ads_move_machine_acct(r->in.ads,
246 r->in.machine_name,
247 r->in.account_ou,
248 &moved);
249 if (!ADS_ERR_OK(status)) {
250 DEBUG(1,("failure to locate/move pre-existing "
251 "machine account\n"));
252 return status;
255 DEBUG(1,("The machine account %s the specified OU.\n",
256 moved ? "was moved into" : "already exists in"));
258 return status;
261 /****************************************************************
262 ****************************************************************/
264 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
265 struct libnet_UnjoinCtx *r)
267 ADS_STATUS status;
269 if (!r->in.ads) {
270 status = libnet_unjoin_connect_ads(mem_ctx, r);
271 if (!ADS_ERR_OK(status)) {
272 libnet_unjoin_set_error_string(mem_ctx, r,
273 "failed to connect to AD: %s",
274 ads_errstr(status));
275 return status;
279 status = ads_leave_realm(r->in.ads, r->in.machine_name);
280 if (!ADS_ERR_OK(status)) {
281 libnet_unjoin_set_error_string(mem_ctx, r,
282 "failed to leave realm: %s",
283 ads_errstr(status));
284 return status;
287 return ADS_SUCCESS;
290 /****************************************************************
291 ****************************************************************/
293 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
294 struct libnet_JoinCtx *r)
296 ADS_STATUS status;
297 LDAPMessage *res = NULL;
298 char *dn = NULL;
300 if (!r->in.machine_name) {
301 return ADS_ERROR(LDAP_NO_MEMORY);
304 status = ads_find_machine_acct(r->in.ads,
305 &res,
306 r->in.machine_name);
307 if (!ADS_ERR_OK(status)) {
308 return status;
311 if (ads_count_replies(r->in.ads, res) != 1) {
312 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
313 goto done;
316 dn = ads_get_dn(r->in.ads, mem_ctx, res);
317 if (!dn) {
318 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
319 goto done;
322 r->out.dn = talloc_strdup(mem_ctx, dn);
323 if (!r->out.dn) {
324 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
325 goto done;
328 done:
329 ads_msgfree(r->in.ads, res);
330 TALLOC_FREE(dn);
332 return status;
335 /****************************************************************
336 Set a machines dNSHostName and servicePrincipalName attributes
337 ****************************************************************/
339 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
340 struct libnet_JoinCtx *r)
342 ADS_STATUS status;
343 ADS_MODLIST mods;
344 fstring my_fqdn;
345 const char *spn_array[3] = {NULL, NULL, NULL};
346 char *spn = NULL;
348 /* Find our DN */
350 status = libnet_join_find_machine_acct(mem_ctx, r);
351 if (!ADS_ERR_OK(status)) {
352 return status;
355 /* Windows only creates HOST/shortname & HOST/fqdn. */
357 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
358 if (!spn) {
359 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
361 strupper_m(spn);
362 spn_array[0] = spn;
364 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
365 || (strchr(my_fqdn, '.') == NULL)) {
366 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
367 r->out.dns_domain_name);
370 strlower_m(my_fqdn);
372 if (!strequal(my_fqdn, r->in.machine_name)) {
373 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
374 if (!spn) {
375 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
377 spn_array[1] = spn;
380 mods = ads_init_mods(mem_ctx);
381 if (!mods) {
382 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
385 /* fields of primary importance */
387 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
388 if (!ADS_ERR_OK(status)) {
389 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
392 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
393 spn_array);
394 if (!ADS_ERR_OK(status)) {
395 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
398 return ads_gen_mod(r->in.ads, r->out.dn, mods);
401 /****************************************************************
402 ****************************************************************/
404 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
405 struct libnet_JoinCtx *r)
407 ADS_STATUS status;
408 ADS_MODLIST mods;
410 if (!r->in.create_upn) {
411 return ADS_SUCCESS;
414 /* Find our DN */
416 status = libnet_join_find_machine_acct(mem_ctx, r);
417 if (!ADS_ERR_OK(status)) {
418 return status;
421 if (!r->in.upn) {
422 r->in.upn = talloc_asprintf(mem_ctx,
423 "host/%s@%s",
424 r->in.machine_name,
425 r->out.dns_domain_name);
426 if (!r->in.upn) {
427 return ADS_ERROR(LDAP_NO_MEMORY);
431 /* now do the mods */
433 mods = ads_init_mods(mem_ctx);
434 if (!mods) {
435 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
438 /* fields of primary importance */
440 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
441 if (!ADS_ERR_OK(status)) {
442 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
445 return ads_gen_mod(r->in.ads, r->out.dn, mods);
449 /****************************************************************
450 ****************************************************************/
452 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
453 struct libnet_JoinCtx *r)
455 ADS_STATUS status;
456 ADS_MODLIST mods;
457 char *os_sp = NULL;
459 if (!r->in.os_name || !r->in.os_version ) {
460 return ADS_SUCCESS;
463 /* Find our DN */
465 status = libnet_join_find_machine_acct(mem_ctx, r);
466 if (!ADS_ERR_OK(status)) {
467 return status;
470 /* now do the mods */
472 mods = ads_init_mods(mem_ctx);
473 if (!mods) {
474 return ADS_ERROR(LDAP_NO_MEMORY);
477 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
478 if (!os_sp) {
479 return ADS_ERROR(LDAP_NO_MEMORY);
482 /* fields of primary importance */
484 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
485 r->in.os_name);
486 if (!ADS_ERR_OK(status)) {
487 return status;
490 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
491 r->in.os_version);
492 if (!ADS_ERR_OK(status)) {
493 return status;
496 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
497 os_sp);
498 if (!ADS_ERR_OK(status)) {
499 return status;
502 return ads_gen_mod(r->in.ads, r->out.dn, mods);
505 /****************************************************************
506 ****************************************************************/
508 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
509 struct libnet_JoinCtx *r)
511 if (!USE_SYSTEM_KEYTAB) {
512 return true;
515 if (ads_keytab_create_default(r->in.ads) != 0) {
516 return false;
519 return true;
522 /****************************************************************
523 ****************************************************************/
525 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
526 struct libnet_JoinCtx *r)
528 uint32_t domain_func;
529 ADS_STATUS status;
530 const char *salt = NULL;
531 char *std_salt = NULL;
533 status = ads_domain_func_level(r->in.ads, &domain_func);
534 if (!ADS_ERR_OK(status)) {
535 libnet_join_set_error_string(mem_ctx, r,
536 "failed to determine domain functional level: %s",
537 ads_errstr(status));
538 return false;
541 /* go ahead and setup the default salt */
543 std_salt = kerberos_standard_des_salt();
544 if (!std_salt) {
545 libnet_join_set_error_string(mem_ctx, r,
546 "failed to obtain standard DES salt");
547 return false;
550 salt = talloc_strdup(mem_ctx, std_salt);
551 if (!salt) {
552 return false;
555 SAFE_FREE(std_salt);
557 /* if it's a Windows functional domain, we have to look for the UPN */
559 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
560 char *upn;
562 upn = ads_get_upn(r->in.ads, mem_ctx,
563 r->in.machine_name);
564 if (upn) {
565 salt = talloc_strdup(mem_ctx, upn);
566 if (!salt) {
567 return false;
572 return kerberos_secrets_store_des_salt(salt);
575 /****************************************************************
576 ****************************************************************/
578 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
579 struct libnet_JoinCtx *r)
581 ADS_STATUS status;
583 if (!r->in.ads) {
584 status = libnet_join_connect_ads(mem_ctx, r);
585 if (!ADS_ERR_OK(status)) {
586 return status;
590 status = libnet_join_set_machine_spn(mem_ctx, r);
591 if (!ADS_ERR_OK(status)) {
592 libnet_join_set_error_string(mem_ctx, r,
593 "failed to set machine spn: %s",
594 ads_errstr(status));
595 return status;
598 status = libnet_join_set_os_attributes(mem_ctx, r);
599 if (!ADS_ERR_OK(status)) {
600 libnet_join_set_error_string(mem_ctx, r,
601 "failed to set machine os attributes: %s",
602 ads_errstr(status));
603 return status;
606 status = libnet_join_set_machine_upn(mem_ctx, r);
607 if (!ADS_ERR_OK(status)) {
608 libnet_join_set_error_string(mem_ctx, r,
609 "failed to set machine upn: %s",
610 ads_errstr(status));
611 return status;
614 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
615 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
618 if (!libnet_join_create_keytab(mem_ctx, r)) {
619 libnet_join_set_error_string(mem_ctx, r,
620 "failed to create kerberos keytab");
621 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
624 return ADS_SUCCESS;
626 #endif /* WITH_ADS */
628 /****************************************************************
629 Store the machine password and domain SID
630 ****************************************************************/
632 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
633 struct libnet_JoinCtx *r)
635 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
636 r->out.domain_sid))
638 DEBUG(1,("Failed to save domain sid\n"));
639 return false;
642 if (!secrets_store_machine_password(r->in.machine_password,
643 r->out.netbios_domain_name,
644 r->in.secure_channel_type))
646 DEBUG(1,("Failed to save machine password\n"));
647 return false;
650 return true;
653 /****************************************************************
654 Connect dc's IPC$ share
655 ****************************************************************/
657 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
658 const char *user,
659 const char *pass,
660 bool use_kerberos,
661 struct cli_state **cli)
663 int flags = 0;
665 if (use_kerberos) {
666 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
669 if (use_kerberos && pass) {
670 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
673 return cli_full_connection(cli, NULL,
675 NULL, 0,
676 "IPC$", "IPC",
677 user,
678 NULL,
679 pass,
680 flags,
681 Undefined, NULL);
684 /****************************************************************
685 Lookup domain dc's info
686 ****************************************************************/
688 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
689 struct libnet_JoinCtx *r,
690 struct cli_state **cli)
692 struct rpc_pipe_client *pipe_hnd = NULL;
693 struct policy_handle lsa_pol;
694 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
695 union lsa_PolicyInformation *info = NULL;
697 status = libnet_join_connect_dc_ipc(r->in.dc_name,
698 r->in.admin_account,
699 r->in.admin_password,
700 r->in.use_kerberos,
701 cli);
702 if (!NT_STATUS_IS_OK(status)) {
703 goto done;
706 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
707 &pipe_hnd);
708 if (!NT_STATUS_IS_OK(status)) {
709 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
710 nt_errstr(status)));
711 goto done;
714 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
715 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
716 if (!NT_STATUS_IS_OK(status)) {
717 goto done;
720 status = rpccli_lsa_QueryInfoPolicy2(pipe_hnd, mem_ctx,
721 &lsa_pol,
722 LSA_POLICY_INFO_DNS,
723 &info);
724 if (NT_STATUS_IS_OK(status)) {
725 r->out.domain_is_ad = true;
726 r->out.netbios_domain_name = info->dns.name.string;
727 r->out.dns_domain_name = info->dns.dns_domain.string;
728 r->out.forest_name = info->dns.dns_forest.string;
729 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->dns.sid);
730 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
733 if (!NT_STATUS_IS_OK(status)) {
734 status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
735 &lsa_pol,
736 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
737 &info);
738 if (!NT_STATUS_IS_OK(status)) {
739 goto done;
742 r->out.netbios_domain_name = info->account_domain.name.string;
743 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->account_domain.sid);
744 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
747 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
748 TALLOC_FREE(pipe_hnd);
750 done:
751 return status;
754 /****************************************************************
755 Do the domain join unsecure
756 ****************************************************************/
758 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
759 struct libnet_JoinCtx *r,
760 struct cli_state *cli)
762 struct rpc_pipe_client *pipe_hnd = NULL;
763 unsigned char orig_trust_passwd_hash[16];
764 unsigned char new_trust_passwd_hash[16];
765 fstring trust_passwd;
766 NTSTATUS status;
768 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
769 &pipe_hnd);
770 if (!NT_STATUS_IS_OK(status)) {
771 return status;
774 if (!r->in.machine_password) {
775 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
776 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
779 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
781 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
782 fstrcpy(trust_passwd, r->in.admin_password);
783 strlower_m(trust_passwd);
786 * Machine names can be 15 characters, but the max length on
787 * a password is 14. --jerry
790 trust_passwd[14] = '\0';
792 E_md4hash(trust_passwd, orig_trust_passwd_hash);
794 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
795 r->in.machine_name,
796 orig_trust_passwd_hash,
797 r->in.machine_password,
798 new_trust_passwd_hash,
799 r->in.secure_channel_type);
801 return status;
804 /****************************************************************
805 Do the domain join
806 ****************************************************************/
808 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
809 struct libnet_JoinCtx *r,
810 struct cli_state *cli)
812 struct rpc_pipe_client *pipe_hnd = NULL;
813 struct policy_handle sam_pol, domain_pol, user_pol;
814 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
815 char *acct_name;
816 struct lsa_String lsa_acct_name;
817 uint32_t user_rid;
818 uint32_t acct_flags = ACB_WSTRUST;
819 struct samr_Ids user_rids;
820 struct samr_Ids name_types;
821 union samr_UserInfo user_info;
823 struct samr_CryptPassword crypt_pwd;
824 struct samr_CryptPasswordEx crypt_pwd_ex;
826 ZERO_STRUCT(sam_pol);
827 ZERO_STRUCT(domain_pol);
828 ZERO_STRUCT(user_pol);
830 switch (r->in.secure_channel_type) {
831 case SEC_CHAN_WKSTA:
832 acct_flags = ACB_WSTRUST;
833 break;
834 case SEC_CHAN_BDC:
835 acct_flags = ACB_SVRTRUST;
836 break;
837 default:
838 return NT_STATUS_INVALID_PARAMETER;
841 if (!r->in.machine_password) {
842 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
843 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
846 /* Open the domain */
848 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
849 &pipe_hnd);
850 if (!NT_STATUS_IS_OK(status)) {
851 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
852 nt_errstr(status)));
853 goto done;
856 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
857 pipe_hnd->desthost,
858 SAMR_ACCESS_ENUM_DOMAINS
859 | SAMR_ACCESS_LOOKUP_DOMAIN,
860 &sam_pol);
861 if (!NT_STATUS_IS_OK(status)) {
862 goto done;
865 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
866 &sam_pol,
867 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
868 | SAMR_DOMAIN_ACCESS_CREATE_USER
869 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
870 r->out.domain_sid,
871 &domain_pol);
872 if (!NT_STATUS_IS_OK(status)) {
873 goto done;
876 /* Create domain user */
878 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
879 strlower_m(acct_name);
881 init_lsa_String(&lsa_acct_name, acct_name);
883 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
884 uint32_t access_desired =
885 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
886 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
887 SAMR_USER_ACCESS_SET_PASSWORD |
888 SAMR_USER_ACCESS_GET_ATTRIBUTES |
889 SAMR_USER_ACCESS_SET_ATTRIBUTES;
890 uint32_t access_granted = 0;
892 DEBUG(10,("Creating account with desired access mask: %d\n",
893 access_desired));
895 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
896 &domain_pol,
897 &lsa_acct_name,
898 acct_flags,
899 access_desired,
900 &user_pol,
901 &access_granted,
902 &user_rid);
903 if (!NT_STATUS_IS_OK(status) &&
904 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
906 DEBUG(10,("Creation of workstation account failed: %s\n",
907 nt_errstr(status)));
909 /* If NT_STATUS_ACCESS_DENIED then we have a valid
910 username/password combo but the user does not have
911 administrator access. */
913 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
914 libnet_join_set_error_string(mem_ctx, r,
915 "User specified does not have "
916 "administrator privileges");
919 goto done;
922 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
923 if (!(r->in.join_flags &
924 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
925 goto done;
929 /* We *must* do this.... don't ask... */
931 if (NT_STATUS_IS_OK(status)) {
932 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
936 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
937 &domain_pol,
939 &lsa_acct_name,
940 &user_rids,
941 &name_types);
942 if (!NT_STATUS_IS_OK(status)) {
943 goto done;
946 if (name_types.ids[0] != SID_NAME_USER) {
947 DEBUG(0,("%s is not a user account (type=%d)\n",
948 acct_name, name_types.ids[0]));
949 status = NT_STATUS_INVALID_WORKSTATION;
950 goto done;
953 user_rid = user_rids.ids[0];
955 /* Open handle on user */
957 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
958 &domain_pol,
959 SEC_FLAG_MAXIMUM_ALLOWED,
960 user_rid,
961 &user_pol);
962 if (!NT_STATUS_IS_OK(status)) {
963 goto done;
966 /* Fill in the additional account flags now */
968 acct_flags |= ACB_PWNOEXP;
969 if (r->out.domain_is_ad) {
970 #if !defined(ENCTYPE_ARCFOUR_HMAC)
971 acct_flags |= ACB_USE_DES_KEY_ONLY;
972 #endif
976 /* Set account flags on machine account */
977 ZERO_STRUCT(user_info.info16);
978 user_info.info16.acct_flags = acct_flags;
980 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
981 &user_pol,
983 &user_info);
985 if (!NT_STATUS_IS_OK(status)) {
987 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
988 &user_pol);
990 libnet_join_set_error_string(mem_ctx, r,
991 "Failed to set account flags for machine account (%s)\n",
992 nt_errstr(status));
993 goto done;
996 /* Set password on machine account - first try level 26 */
998 init_samr_CryptPasswordEx(r->in.machine_password,
999 &cli->user_session_key,
1000 &crypt_pwd_ex);
1002 user_info.info26.password = crypt_pwd_ex;
1003 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1005 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
1006 &user_pol,
1008 &user_info);
1010 if (NT_STATUS_EQUAL(status, NT_STATUS(DCERPC_FAULT_INVALID_TAG))) {
1012 /* retry with level 24 */
1014 init_samr_CryptPassword(r->in.machine_password,
1015 &cli->user_session_key,
1016 &crypt_pwd);
1018 user_info.info24.password = crypt_pwd;
1019 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1021 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
1022 &user_pol,
1024 &user_info);
1027 if (!NT_STATUS_IS_OK(status)) {
1029 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
1030 &user_pol);
1032 libnet_join_set_error_string(mem_ctx, r,
1033 "Failed to set password for machine account (%s)\n",
1034 nt_errstr(status));
1035 goto done;
1038 status = NT_STATUS_OK;
1040 done:
1041 if (!pipe_hnd) {
1042 return status;
1045 if (is_valid_policy_hnd(&sam_pol)) {
1046 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1048 if (is_valid_policy_hnd(&domain_pol)) {
1049 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1051 if (is_valid_policy_hnd(&user_pol)) {
1052 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1054 TALLOC_FREE(pipe_hnd);
1056 return status;
1059 /****************************************************************
1060 ****************************************************************/
1062 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1063 const char *machine_name,
1064 const char *dc_name)
1066 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1067 struct cli_state *cli = NULL;
1068 struct rpc_pipe_client *pipe_hnd = NULL;
1069 struct rpc_pipe_client *netlogon_pipe = NULL;
1070 NTSTATUS status;
1071 char *machine_password = NULL;
1072 char *machine_account = NULL;
1074 if (!dc_name) {
1075 return NT_STATUS_INVALID_PARAMETER;
1078 if (!secrets_init()) {
1079 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1082 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1083 NULL, NULL);
1084 if (!machine_password) {
1085 return NT_STATUS_NO_TRUST_LSA_SECRET;
1088 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1089 SAFE_FREE(machine_password);
1090 return NT_STATUS_NO_MEMORY;
1093 status = cli_full_connection(&cli, NULL,
1094 dc_name,
1095 NULL, 0,
1096 "IPC$", "IPC",
1097 machine_account,
1098 NULL,
1099 machine_password,
1101 Undefined, NULL);
1102 free(machine_account);
1103 free(machine_password);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 status = cli_full_connection(&cli, NULL,
1107 dc_name,
1108 NULL, 0,
1109 "IPC$", "IPC",
1111 NULL,
1114 Undefined, NULL);
1117 if (!NT_STATUS_IS_OK(status)) {
1118 return status;
1121 status = get_schannel_session_key(cli, netbios_domain_name,
1122 &neg_flags, &netlogon_pipe);
1123 if (!NT_STATUS_IS_OK(status)) {
1124 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1125 cli_shutdown(cli);
1126 return NT_STATUS_OK;
1129 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1130 "key from server %s for domain %s. Error was %s\n",
1131 cli->desthost, netbios_domain_name, nt_errstr(status)));
1132 cli_shutdown(cli);
1133 return status;
1136 if (!lp_client_schannel()) {
1137 cli_shutdown(cli);
1138 return NT_STATUS_OK;
1141 status = cli_rpc_pipe_open_schannel_with_key(
1142 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
1143 DCERPC_AUTH_LEVEL_PRIVACY,
1144 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1146 cli_shutdown(cli);
1148 if (!NT_STATUS_IS_OK(status)) {
1149 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1150 "on netlogon pipe to server %s for domain %s. "
1151 "Error was %s\n",
1152 cli->desthost, netbios_domain_name, nt_errstr(status)));
1153 return status;
1156 return NT_STATUS_OK;
1159 /****************************************************************
1160 ****************************************************************/
1162 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1163 struct libnet_JoinCtx *r)
1165 NTSTATUS status;
1167 status = libnet_join_ok(r->out.netbios_domain_name,
1168 r->in.machine_name,
1169 r->in.dc_name);
1170 if (!NT_STATUS_IS_OK(status)) {
1171 libnet_join_set_error_string(mem_ctx, r,
1172 "failed to verify domain membership after joining: %s",
1173 get_friendly_nt_error_msg(status));
1174 return WERR_SETUP_NOT_JOINED;
1177 return WERR_OK;
1180 /****************************************************************
1181 ****************************************************************/
1183 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1184 struct libnet_UnjoinCtx *r)
1186 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1187 return false;
1190 if (!secrets_delete_domain_sid(lp_workgroup())) {
1191 return false;
1194 return true;
1197 /****************************************************************
1198 ****************************************************************/
1200 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1201 struct libnet_UnjoinCtx *r)
1203 struct cli_state *cli = NULL;
1204 struct rpc_pipe_client *pipe_hnd = NULL;
1205 struct policy_handle sam_pol, domain_pol, user_pol;
1206 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1207 char *acct_name;
1208 uint32_t user_rid;
1209 struct lsa_String lsa_acct_name;
1210 struct samr_Ids user_rids;
1211 struct samr_Ids name_types;
1212 union samr_UserInfo *info = NULL;
1214 ZERO_STRUCT(sam_pol);
1215 ZERO_STRUCT(domain_pol);
1216 ZERO_STRUCT(user_pol);
1218 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1219 r->in.admin_account,
1220 r->in.admin_password,
1221 r->in.use_kerberos,
1222 &cli);
1223 if (!NT_STATUS_IS_OK(status)) {
1224 goto done;
1227 /* Open the domain */
1229 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1230 &pipe_hnd);
1231 if (!NT_STATUS_IS_OK(status)) {
1232 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1233 nt_errstr(status)));
1234 goto done;
1237 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1238 pipe_hnd->desthost,
1239 SEC_FLAG_MAXIMUM_ALLOWED,
1240 &sam_pol);
1241 if (!NT_STATUS_IS_OK(status)) {
1242 goto done;
1245 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1246 &sam_pol,
1247 SEC_FLAG_MAXIMUM_ALLOWED,
1248 r->in.domain_sid,
1249 &domain_pol);
1250 if (!NT_STATUS_IS_OK(status)) {
1251 goto done;
1254 /* Create domain user */
1256 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1257 strlower_m(acct_name);
1259 init_lsa_String(&lsa_acct_name, acct_name);
1261 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1262 &domain_pol,
1264 &lsa_acct_name,
1265 &user_rids,
1266 &name_types);
1268 if (!NT_STATUS_IS_OK(status)) {
1269 goto done;
1272 if (name_types.ids[0] != SID_NAME_USER) {
1273 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1274 name_types.ids[0]));
1275 status = NT_STATUS_INVALID_WORKSTATION;
1276 goto done;
1279 user_rid = user_rids.ids[0];
1281 /* Open handle on user */
1283 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1284 &domain_pol,
1285 SEC_FLAG_MAXIMUM_ALLOWED,
1286 user_rid,
1287 &user_pol);
1288 if (!NT_STATUS_IS_OK(status)) {
1289 goto done;
1292 /* Get user info */
1294 status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1295 &user_pol,
1297 &info);
1298 if (!NT_STATUS_IS_OK(status)) {
1299 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1300 goto done;
1303 /* now disable and setuser info */
1305 info->info16.acct_flags |= ACB_DISABLED;
1307 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1308 &user_pol,
1310 info);
1312 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1314 done:
1315 if (pipe_hnd) {
1316 if (is_valid_policy_hnd(&domain_pol)) {
1317 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1319 if (is_valid_policy_hnd(&sam_pol)) {
1320 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1322 TALLOC_FREE(pipe_hnd);
1325 if (cli) {
1326 cli_shutdown(cli);
1329 return status;
1332 /****************************************************************
1333 ****************************************************************/
1335 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1337 WERROR werr;
1338 struct smbconf_ctx *ctx;
1340 werr = smbconf_init_reg(r, &ctx, NULL);
1341 if (!W_ERROR_IS_OK(werr)) {
1342 goto done;
1345 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1347 werr = smbconf_set_global_parameter(ctx, "security", "user");
1348 W_ERROR_NOT_OK_GOTO_DONE(werr);
1350 werr = smbconf_set_global_parameter(ctx, "workgroup",
1351 r->in.domain_name);
1353 smbconf_delete_global_parameter(ctx, "realm");
1354 goto done;
1357 werr = smbconf_set_global_parameter(ctx, "security", "domain");
1358 W_ERROR_NOT_OK_GOTO_DONE(werr);
1360 werr = smbconf_set_global_parameter(ctx, "workgroup",
1361 r->out.netbios_domain_name);
1362 W_ERROR_NOT_OK_GOTO_DONE(werr);
1364 if (r->out.domain_is_ad) {
1365 werr = smbconf_set_global_parameter(ctx, "security", "ads");
1366 W_ERROR_NOT_OK_GOTO_DONE(werr);
1368 werr = smbconf_set_global_parameter(ctx, "realm",
1369 r->out.dns_domain_name);
1370 W_ERROR_NOT_OK_GOTO_DONE(werr);
1373 done:
1374 smbconf_shutdown(ctx);
1375 return werr;
1378 /****************************************************************
1379 ****************************************************************/
1381 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1383 WERROR werr = WERR_OK;
1384 struct smbconf_ctx *ctx;
1386 werr = smbconf_init_reg(r, &ctx, NULL);
1387 if (!W_ERROR_IS_OK(werr)) {
1388 goto done;
1391 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1393 werr = smbconf_set_global_parameter(ctx, "security", "user");
1394 W_ERROR_NOT_OK_GOTO_DONE(werr);
1396 werr = smbconf_delete_global_parameter(ctx, "workgroup");
1397 W_ERROR_NOT_OK_GOTO_DONE(werr);
1399 smbconf_delete_global_parameter(ctx, "realm");
1402 done:
1403 smbconf_shutdown(ctx);
1404 return werr;
1407 /****************************************************************
1408 ****************************************************************/
1410 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1412 WERROR werr;
1414 if (!W_ERROR_IS_OK(r->out.result)) {
1415 return r->out.result;
1418 if (!r->in.modify_config) {
1419 return WERR_OK;
1422 werr = do_join_modify_vals_config(r);
1423 if (!W_ERROR_IS_OK(werr)) {
1424 return werr;
1427 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1429 r->out.modified_config = true;
1430 r->out.result = werr;
1432 return werr;
1435 /****************************************************************
1436 ****************************************************************/
1438 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1440 WERROR werr;
1442 if (!W_ERROR_IS_OK(r->out.result)) {
1443 return r->out.result;
1446 if (!r->in.modify_config) {
1447 return WERR_OK;
1450 werr = do_unjoin_modify_vals_config(r);
1451 if (!W_ERROR_IS_OK(werr)) {
1452 return werr;
1455 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1457 r->out.modified_config = true;
1458 r->out.result = werr;
1460 return werr;
1463 /****************************************************************
1464 ****************************************************************/
1466 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1467 const char *domain_str,
1468 const char **domain_p,
1469 const char **dc_p)
1471 char *domain = NULL;
1472 char *dc = NULL;
1473 const char *p = NULL;
1475 if (!domain_str || !domain_p || !dc_p) {
1476 return false;
1479 p = strchr_m(domain_str, '\\');
1481 if (p != NULL) {
1482 domain = talloc_strndup(mem_ctx, domain_str,
1483 PTR_DIFF(p, domain_str));
1484 dc = talloc_strdup(mem_ctx, p+1);
1485 if (!dc) {
1486 return false;
1488 } else {
1489 domain = talloc_strdup(mem_ctx, domain_str);
1490 dc = NULL;
1492 if (!domain) {
1493 return false;
1496 *domain_p = domain;
1498 if (!*dc_p && dc) {
1499 *dc_p = dc;
1502 return true;
1505 /****************************************************************
1506 ****************************************************************/
1508 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1509 struct libnet_JoinCtx *r)
1511 if (!r->in.domain_name) {
1512 libnet_join_set_error_string(mem_ctx, r,
1513 "No domain name defined");
1514 return WERR_INVALID_PARAM;
1517 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1518 &r->in.domain_name,
1519 &r->in.dc_name)) {
1520 libnet_join_set_error_string(mem_ctx, r,
1521 "Failed to parse domain name");
1522 return WERR_INVALID_PARAM;
1525 if (IS_DC) {
1526 return WERR_SETUP_DOMAIN_CONTROLLER;
1529 if (!secrets_init()) {
1530 libnet_join_set_error_string(mem_ctx, r,
1531 "Unable to open secrets database");
1532 return WERR_CAN_NOT_COMPLETE;
1535 return WERR_OK;
1538 /****************************************************************
1539 ****************************************************************/
1541 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1543 NTSTATUS status;
1545 /* Try adding dom admins to builtin\admins. Only log failures. */
1546 status = create_builtin_administrators(domain_sid);
1547 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1548 DEBUG(10,("Unable to auto-add domain administrators to "
1549 "BUILTIN\\Administrators during join because "
1550 "winbindd must be running."));
1551 } else if (!NT_STATUS_IS_OK(status)) {
1552 DEBUG(5, ("Failed to auto-add domain administrators to "
1553 "BUILTIN\\Administrators during join: %s\n",
1554 nt_errstr(status)));
1557 /* Try adding dom users to builtin\users. Only log failures. */
1558 status = create_builtin_users(domain_sid);
1559 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1560 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1561 "during join because winbindd must be running."));
1562 } else if (!NT_STATUS_IS_OK(status)) {
1563 DEBUG(5, ("Failed to auto-add domain administrators to "
1564 "BUILTIN\\Administrators during join: %s\n",
1565 nt_errstr(status)));
1569 /****************************************************************
1570 ****************************************************************/
1572 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1573 struct libnet_JoinCtx *r)
1575 WERROR werr;
1577 if (!W_ERROR_IS_OK(r->out.result)) {
1578 return r->out.result;
1581 werr = do_JoinConfig(r);
1582 if (!W_ERROR_IS_OK(werr)) {
1583 return werr;
1586 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1587 return WERR_OK;
1590 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1591 if (r->out.dns_domain_name) {
1592 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1595 #ifdef WITH_ADS
1596 if (r->out.domain_is_ad &&
1597 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1598 ADS_STATUS ads_status;
1600 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1601 if (!ADS_ERR_OK(ads_status)) {
1602 return WERR_GENERAL_FAILURE;
1605 #endif /* WITH_ADS */
1607 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1609 return WERR_OK;
1612 /****************************************************************
1613 ****************************************************************/
1615 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1617 const char *krb5_cc_env = NULL;
1619 if (r->in.ads) {
1620 ads_destroy(&r->in.ads);
1623 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1624 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1625 unsetenv(KRB5_ENV_CCNAME);
1628 return 0;
1631 /****************************************************************
1632 ****************************************************************/
1634 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1636 const char *krb5_cc_env = NULL;
1638 if (r->in.ads) {
1639 ads_destroy(&r->in.ads);
1642 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1643 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1644 unsetenv(KRB5_ENV_CCNAME);
1647 return 0;
1650 /****************************************************************
1651 ****************************************************************/
1653 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1654 struct libnet_JoinCtx **r)
1656 struct libnet_JoinCtx *ctx;
1657 const char *krb5_cc_env = NULL;
1659 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1660 if (!ctx) {
1661 return WERR_NOMEM;
1664 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1666 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1667 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1669 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1670 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1671 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1672 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1673 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1676 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1678 *r = ctx;
1680 return WERR_OK;
1683 /****************************************************************
1684 ****************************************************************/
1686 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1687 struct libnet_UnjoinCtx **r)
1689 struct libnet_UnjoinCtx *ctx;
1690 const char *krb5_cc_env = NULL;
1692 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1693 if (!ctx) {
1694 return WERR_NOMEM;
1697 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1699 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1700 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1702 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1703 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1704 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1705 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1706 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1709 *r = ctx;
1711 return WERR_OK;
1714 /****************************************************************
1715 ****************************************************************/
1717 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1718 struct libnet_JoinCtx *r)
1720 bool valid_security = false;
1721 bool valid_workgroup = false;
1722 bool valid_realm = false;
1724 /* check if configuration is already set correctly */
1726 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1728 switch (r->out.domain_is_ad) {
1729 case false:
1730 valid_security = (lp_security() == SEC_DOMAIN);
1731 if (valid_workgroup && valid_security) {
1732 /* nothing to be done */
1733 return WERR_OK;
1735 break;
1736 case true:
1737 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1738 switch (lp_security()) {
1739 case SEC_DOMAIN:
1740 case SEC_ADS:
1741 valid_security = true;
1744 if (valid_workgroup && valid_realm && valid_security) {
1745 /* nothing to be done */
1746 return WERR_OK;
1748 break;
1751 /* check if we are supposed to manipulate configuration */
1753 if (!r->in.modify_config) {
1755 char *wrong_conf = talloc_strdup(mem_ctx, "");
1757 if (!valid_workgroup) {
1758 wrong_conf = talloc_asprintf_append(wrong_conf,
1759 "\"workgroup\" set to '%s', should be '%s'",
1760 lp_workgroup(), r->out.netbios_domain_name);
1761 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1764 if (!valid_realm) {
1765 wrong_conf = talloc_asprintf_append(wrong_conf,
1766 "\"realm\" set to '%s', should be '%s'",
1767 lp_realm(), r->out.dns_domain_name);
1768 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1771 if (!valid_security) {
1772 const char *sec = NULL;
1773 switch (lp_security()) {
1774 case SEC_SHARE: sec = "share"; break;
1775 case SEC_USER: sec = "user"; break;
1776 case SEC_DOMAIN: sec = "domain"; break;
1777 case SEC_ADS: sec = "ads"; break;
1779 wrong_conf = talloc_asprintf_append(wrong_conf,
1780 "\"security\" set to '%s', should be %s",
1781 sec, r->out.domain_is_ad ?
1782 "either 'domain' or 'ads'" : "'domain'");
1783 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1786 libnet_join_set_error_string(mem_ctx, r,
1787 "Invalid configuration (%s) and configuration modification "
1788 "was not requested", wrong_conf);
1789 return WERR_CAN_NOT_COMPLETE;
1792 /* check if we are able to manipulate configuration */
1794 if (!lp_config_backend_is_registry()) {
1795 libnet_join_set_error_string(mem_ctx, r,
1796 "Configuration manipulation requested but not "
1797 "supported by backend");
1798 return WERR_NOT_SUPPORTED;
1801 return WERR_OK;
1804 /****************************************************************
1805 ****************************************************************/
1807 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1808 struct libnet_JoinCtx *r)
1810 NTSTATUS status;
1811 WERROR werr;
1812 struct cli_state *cli = NULL;
1813 #ifdef WITH_ADS
1814 ADS_STATUS ads_status;
1815 #endif /* WITH_ADS */
1817 if (!r->in.dc_name) {
1818 struct netr_DsRGetDCNameInfo *info;
1819 const char *dc;
1820 status = dsgetdcname(mem_ctx,
1821 r->in.msg_ctx,
1822 r->in.domain_name,
1823 NULL,
1824 NULL,
1825 DS_FORCE_REDISCOVERY |
1826 DS_DIRECTORY_SERVICE_REQUIRED |
1827 DS_WRITABLE_REQUIRED |
1828 DS_RETURN_DNS_NAME,
1829 &info);
1830 if (!NT_STATUS_IS_OK(status)) {
1831 libnet_join_set_error_string(mem_ctx, r,
1832 "failed to find DC for domain %s",
1833 r->in.domain_name,
1834 get_friendly_nt_error_msg(status));
1835 return WERR_DCNOTFOUND;
1838 dc = strip_hostname(info->dc_unc);
1839 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1840 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1843 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1844 if (!NT_STATUS_IS_OK(status)) {
1845 libnet_join_set_error_string(mem_ctx, r,
1846 "failed to lookup DC info for domain '%s' over rpc: %s",
1847 r->in.domain_name, get_friendly_nt_error_msg(status));
1848 return ntstatus_to_werror(status);
1851 werr = libnet_join_check_config(mem_ctx, r);
1852 if (!W_ERROR_IS_OK(werr)) {
1853 goto done;
1856 #ifdef WITH_ADS
1857 if (r->out.domain_is_ad && r->in.account_ou &&
1858 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1860 ads_status = libnet_join_connect_ads(mem_ctx, r);
1861 if (!ADS_ERR_OK(ads_status)) {
1862 return WERR_DEFAULT_JOIN_REQUIRED;
1865 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1866 if (!ADS_ERR_OK(ads_status)) {
1867 libnet_join_set_error_string(mem_ctx, r,
1868 "failed to precreate account in ou %s: %s",
1869 r->in.account_ou,
1870 ads_errstr(ads_status));
1871 return WERR_DEFAULT_JOIN_REQUIRED;
1874 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1876 #endif /* WITH_ADS */
1878 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
1879 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
1880 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
1881 } else {
1882 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
1884 if (!NT_STATUS_IS_OK(status)) {
1885 libnet_join_set_error_string(mem_ctx, r,
1886 "failed to join domain '%s' over rpc: %s",
1887 r->in.domain_name, get_friendly_nt_error_msg(status));
1888 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1889 return WERR_SETUP_ALREADY_JOINED;
1891 werr = ntstatus_to_werror(status);
1892 goto done;
1895 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1896 werr = WERR_SETUP_NOT_JOINED;
1897 goto done;
1900 werr = WERR_OK;
1902 done:
1903 if (cli) {
1904 cli_shutdown(cli);
1907 return werr;
1910 /****************************************************************
1911 ****************************************************************/
1913 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
1914 struct libnet_JoinCtx *r)
1916 WERROR werr;
1917 struct libnet_UnjoinCtx *u = NULL;
1919 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
1920 if (!W_ERROR_IS_OK(werr)) {
1921 return werr;
1924 u->in.debug = r->in.debug;
1925 u->in.dc_name = r->in.dc_name;
1926 u->in.domain_name = r->in.domain_name;
1927 u->in.admin_account = r->in.admin_account;
1928 u->in.admin_password = r->in.admin_password;
1929 u->in.modify_config = r->in.modify_config;
1930 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
1931 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
1933 werr = libnet_Unjoin(mem_ctx, u);
1934 TALLOC_FREE(u);
1936 return werr;
1939 /****************************************************************
1940 ****************************************************************/
1942 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1943 struct libnet_JoinCtx *r)
1945 WERROR werr;
1947 if (r->in.debug) {
1948 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1951 werr = libnet_join_pre_processing(mem_ctx, r);
1952 if (!W_ERROR_IS_OK(werr)) {
1953 goto done;
1956 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1957 werr = libnet_DomainJoin(mem_ctx, r);
1958 if (!W_ERROR_IS_OK(werr)) {
1959 goto done;
1963 werr = libnet_join_post_processing(mem_ctx, r);
1964 if (!W_ERROR_IS_OK(werr)) {
1965 goto done;
1968 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1969 werr = libnet_join_post_verify(mem_ctx, r);
1970 if (!W_ERROR_IS_OK(werr)) {
1971 libnet_join_rollback(mem_ctx, r);
1975 done:
1976 r->out.result = werr;
1978 if (r->in.debug) {
1979 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1981 return werr;
1984 /****************************************************************
1985 ****************************************************************/
1987 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1988 struct libnet_UnjoinCtx *r)
1990 NTSTATUS status;
1992 if (!r->in.domain_sid) {
1993 struct dom_sid sid;
1994 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1995 libnet_unjoin_set_error_string(mem_ctx, r,
1996 "Unable to fetch domain sid: are we joined?");
1997 return WERR_SETUP_NOT_JOINED;
1999 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
2000 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2003 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2004 !r->in.delete_machine_account) {
2005 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2006 return WERR_OK;
2009 if (!r->in.dc_name) {
2010 struct netr_DsRGetDCNameInfo *info;
2011 const char *dc;
2012 status = dsgetdcname(mem_ctx,
2013 r->in.msg_ctx,
2014 r->in.domain_name,
2015 NULL,
2016 NULL,
2017 DS_DIRECTORY_SERVICE_REQUIRED |
2018 DS_WRITABLE_REQUIRED |
2019 DS_RETURN_DNS_NAME,
2020 &info);
2021 if (!NT_STATUS_IS_OK(status)) {
2022 libnet_unjoin_set_error_string(mem_ctx, r,
2023 "failed to find DC for domain %s",
2024 r->in.domain_name,
2025 get_friendly_nt_error_msg(status));
2026 return WERR_DCNOTFOUND;
2029 dc = strip_hostname(info->dc_unc);
2030 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2031 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2034 #ifdef WITH_ADS
2035 /* for net ads leave, try to delete the account. If it works,
2036 no sense in disabling. If it fails, we can still try to
2037 disable it. jmcd */
2039 if (r->in.delete_machine_account) {
2040 ADS_STATUS ads_status;
2041 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2042 if (ADS_ERR_OK(ads_status)) {
2043 /* dirty hack */
2044 r->out.dns_domain_name =
2045 talloc_strdup(mem_ctx,
2046 r->in.ads->server.realm);
2047 ads_status =
2048 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2050 if (!ADS_ERR_OK(ads_status)) {
2051 libnet_unjoin_set_error_string(mem_ctx, r,
2052 "failed to remove machine account from AD: %s",
2053 ads_errstr(ads_status));
2054 } else {
2055 r->out.deleted_machine_account = true;
2056 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2057 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2058 return WERR_OK;
2061 #endif /* WITH_ADS */
2063 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2064 "disable". */
2065 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2066 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2067 if (!NT_STATUS_IS_OK(status)) {
2068 libnet_unjoin_set_error_string(mem_ctx, r,
2069 "failed to disable machine account via rpc: %s",
2070 get_friendly_nt_error_msg(status));
2071 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2072 return WERR_SETUP_NOT_JOINED;
2074 return ntstatus_to_werror(status);
2077 r->out.disabled_machine_account = true;
2080 /* If disable succeeded or was not requested at all, we
2081 should be getting rid of our end of things */
2083 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2085 return WERR_OK;
2088 /****************************************************************
2089 ****************************************************************/
2091 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2092 struct libnet_UnjoinCtx *r)
2094 if (!r->in.domain_name) {
2095 libnet_unjoin_set_error_string(mem_ctx, r,
2096 "No domain name defined");
2097 return WERR_INVALID_PARAM;
2100 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2101 &r->in.domain_name,
2102 &r->in.dc_name)) {
2103 libnet_unjoin_set_error_string(mem_ctx, r,
2104 "Failed to parse domain name");
2105 return WERR_INVALID_PARAM;
2108 if (IS_DC) {
2109 return WERR_SETUP_DOMAIN_CONTROLLER;
2112 if (!secrets_init()) {
2113 libnet_unjoin_set_error_string(mem_ctx, r,
2114 "Unable to open secrets database");
2115 return WERR_CAN_NOT_COMPLETE;
2118 return WERR_OK;
2121 /****************************************************************
2122 ****************************************************************/
2124 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2125 struct libnet_UnjoinCtx *r)
2127 saf_delete(r->out.netbios_domain_name);
2128 saf_delete(r->out.dns_domain_name);
2130 return libnet_unjoin_config(r);
2133 /****************************************************************
2134 ****************************************************************/
2136 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2137 struct libnet_UnjoinCtx *r)
2139 WERROR werr;
2141 if (r->in.debug) {
2142 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2145 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2146 if (!W_ERROR_IS_OK(werr)) {
2147 goto done;
2150 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2151 werr = libnet_DomainUnjoin(mem_ctx, r);
2152 if (!W_ERROR_IS_OK(werr)) {
2153 libnet_unjoin_config(r);
2154 goto done;
2158 werr = libnet_unjoin_post_processing(mem_ctx, r);
2159 if (!W_ERROR_IS_OK(werr)) {
2160 goto done;
2163 done:
2164 r->out.result = werr;
2166 if (r->in.debug) {
2167 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2170 return werr;