libnetjoin: Merge in comments, debugs and missing code from original join code.
[Samba/gebeck_regimport.git] / source3 / libnet / libnet_join.c
blob38d98221b4374664fb54841de169e72805c1be63
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"
24 /****************************************************************
25 ****************************************************************/
27 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
28 do { \
29 char *str = NULL; \
30 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
31 DEBUG(1,("libnet_Join:\n%s", str)); \
32 talloc_free(str); \
33 } while (0)
35 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
36 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
37 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
38 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
40 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
41 do { \
42 char *str = NULL; \
43 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
44 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
45 talloc_free(str); \
46 } while (0)
48 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
49 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
50 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
51 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
53 #define W_ERROR_NOT_OK_GOTO_DONE(x) do { \
54 if (!W_ERROR_IS_OK(x)) {\
55 goto done;\
57 } while (0)
59 /****************************************************************
60 ****************************************************************/
62 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
63 struct libnet_JoinCtx *r,
64 const char *format, ...)
66 va_list args;
68 if (r->out.error_string) {
69 return;
72 va_start(args, format);
73 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
74 va_end(args);
77 /****************************************************************
78 ****************************************************************/
80 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
81 struct libnet_UnjoinCtx *r,
82 const char *format, ...)
84 va_list args;
86 if (r->out.error_string) {
87 return;
90 va_start(args, format);
91 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
92 va_end(args);
95 #ifdef WITH_ADS
97 /****************************************************************
98 ****************************************************************/
100 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
101 const char *netbios_domain_name,
102 const char *dc_name,
103 const char *user_name,
104 const char *password,
105 ADS_STRUCT **ads)
107 ADS_STATUS status;
108 ADS_STRUCT *my_ads = NULL;
110 my_ads = ads_init(dns_domain_name,
111 netbios_domain_name,
112 dc_name);
113 if (!my_ads) {
114 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
117 if (user_name) {
118 SAFE_FREE(my_ads->auth.user_name);
119 my_ads->auth.user_name = SMB_STRDUP(user_name);
122 if (password) {
123 SAFE_FREE(my_ads->auth.password);
124 my_ads->auth.password = SMB_STRDUP(password);
127 status = ads_connect(my_ads);
128 if (!ADS_ERR_OK(status)) {
129 ads_destroy(&my_ads);
130 return status;
133 *ads = my_ads;
134 return ADS_SUCCESS;
137 /****************************************************************
138 ****************************************************************/
140 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
141 struct libnet_JoinCtx *r)
143 ADS_STATUS status;
145 status = libnet_connect_ads(r->in.domain_name,
146 r->in.domain_name,
147 r->in.dc_name,
148 r->in.admin_account,
149 r->in.admin_password,
150 &r->in.ads);
151 if (!ADS_ERR_OK(status)) {
152 libnet_join_set_error_string(mem_ctx, r,
153 "failed to connect to AD: %s",
154 ads_errstr(status));
155 return status;
158 if (!r->out.netbios_domain_name) {
159 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
160 r->in.ads->server.workgroup);
161 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
164 if (!r->out.dns_domain_name) {
165 r->out.dns_domain_name = talloc_strdup(mem_ctx,
166 r->in.ads->config.realm);
167 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
170 r->out.domain_is_ad = true;
172 return ADS_SUCCESS;
175 /****************************************************************
176 ****************************************************************/
178 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
179 struct libnet_UnjoinCtx *r)
181 ADS_STATUS status;
183 status = libnet_connect_ads(r->in.domain_name,
184 r->in.domain_name,
185 r->in.dc_name,
186 r->in.admin_account,
187 r->in.admin_password,
188 &r->in.ads);
189 if (!ADS_ERR_OK(status)) {
190 libnet_unjoin_set_error_string(mem_ctx, r,
191 "failed to connect to AD: %s",
192 ads_errstr(status));
195 return status;
198 /****************************************************************
199 join a domain using ADS (LDAP mods)
200 ****************************************************************/
202 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
203 struct libnet_JoinCtx *r)
205 ADS_STATUS status;
206 LDAPMessage *res = NULL;
207 const char *attrs[] = { "dn", NULL };
208 bool moved = false;
210 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
211 if (!ADS_ERR_OK(status)) {
212 return status;
215 if (ads_count_replies(r->in.ads, res) != 1) {
216 ads_msgfree(r->in.ads, res);
217 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
220 ads_msgfree(r->in.ads, res);
222 /* Attempt to create the machine account and bail if this fails.
223 Assume that the admin wants exactly what they requested */
225 status = ads_create_machine_acct(r->in.ads,
226 r->in.machine_name,
227 r->in.account_ou);
229 if (ADS_ERR_OK(status)) {
230 DEBUG(1,("machine account creation created\n"));
231 return status;
232 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
233 (status.err.rc == LDAP_ALREADY_EXISTS)) {
234 status = ADS_SUCCESS;
237 if (!ADS_ERR_OK(status)) {
238 DEBUG(1,("machine account creation failed\n"));
239 return status;
242 status = ads_move_machine_acct(r->in.ads,
243 r->in.machine_name,
244 r->in.account_ou,
245 &moved);
246 if (!ADS_ERR_OK(status)) {
247 DEBUG(1,("failure to locate/move pre-existing "
248 "machine account\n"));
249 return status;
252 DEBUG(1,("The machine account %s the specified OU.\n",
253 moved ? "was moved into" : "already exists in"));
255 return status;
258 /****************************************************************
259 ****************************************************************/
261 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
262 struct libnet_UnjoinCtx *r)
264 ADS_STATUS status;
266 if (!r->in.ads) {
267 status = libnet_unjoin_connect_ads(mem_ctx, r);
268 if (!ADS_ERR_OK(status)) {
269 return status;
273 status = ads_leave_realm(r->in.ads, r->in.machine_name);
274 if (!ADS_ERR_OK(status)) {
275 libnet_unjoin_set_error_string(mem_ctx, r,
276 "failed to leave realm: %s",
277 ads_errstr(status));
278 return status;
281 return ADS_SUCCESS;
284 /****************************************************************
285 ****************************************************************/
287 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
288 struct libnet_JoinCtx *r)
290 ADS_STATUS status;
291 LDAPMessage *res = NULL;
292 char *dn = NULL;
294 if (!r->in.machine_name) {
295 return ADS_ERROR(LDAP_NO_MEMORY);
298 status = ads_find_machine_acct(r->in.ads,
299 &res,
300 r->in.machine_name);
301 if (!ADS_ERR_OK(status)) {
302 return status;
305 if (ads_count_replies(r->in.ads, res) != 1) {
306 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
307 goto done;
310 dn = ads_get_dn(r->in.ads, res);
311 if (!dn) {
312 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
313 goto done;
316 r->out.dn = talloc_strdup(mem_ctx, dn);
317 if (!r->out.dn) {
318 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
319 goto done;
322 done:
323 ads_msgfree(r->in.ads, res);
324 ads_memfree(r->in.ads, dn);
326 return status;
329 /****************************************************************
330 Set a machines dNSHostName and servicePrincipalName attributes
331 ****************************************************************/
333 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
334 struct libnet_JoinCtx *r)
336 ADS_STATUS status;
337 ADS_MODLIST mods;
338 fstring my_fqdn;
339 const char *spn_array[3] = {NULL, NULL, NULL};
340 char *spn = NULL;
342 /* Find our DN */
344 status = libnet_join_find_machine_acct(mem_ctx, r);
345 if (!ADS_ERR_OK(status)) {
346 return status;
349 /* Windows only creates HOST/shortname & HOST/fqdn. */
351 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
352 if (!spn) {
353 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
355 strupper_m(spn);
356 spn_array[0] = spn;
358 if (name_to_fqdn(my_fqdn, r->in.machine_name) &&
359 !strequal(my_fqdn, r->in.machine_name)) {
361 strlower_m(my_fqdn);
362 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
363 if (!spn) {
364 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
366 spn_array[1] = spn;
369 mods = ads_init_mods(mem_ctx);
370 if (!mods) {
371 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
374 /* fields of primary importance */
376 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
377 if (!ADS_ERR_OK(status)) {
378 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
381 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
382 spn_array);
383 if (!ADS_ERR_OK(status)) {
384 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
387 return ads_gen_mod(r->in.ads, r->out.dn, mods);
390 /****************************************************************
391 ****************************************************************/
393 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
394 struct libnet_JoinCtx *r)
396 ADS_STATUS status;
397 ADS_MODLIST mods;
399 if (!r->in.create_upn) {
400 return ADS_SUCCESS;
403 /* Find our DN */
405 status = libnet_join_find_machine_acct(mem_ctx, r);
406 if (!ADS_ERR_OK(status)) {
407 return status;
410 if (!r->in.upn) {
411 r->in.upn = talloc_asprintf(mem_ctx,
412 "host/%s@%s",
413 r->in.machine_name,
414 r->out.dns_domain_name);
415 if (!r->in.upn) {
416 return ADS_ERROR(LDAP_NO_MEMORY);
420 /* now do the mods */
422 mods = ads_init_mods(mem_ctx);
423 if (!mods) {
424 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
427 /* fields of primary importance */
429 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
430 if (!ADS_ERR_OK(status)) {
431 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
434 return ads_gen_mod(r->in.ads, r->out.dn, mods);
438 /****************************************************************
439 ****************************************************************/
441 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
442 struct libnet_JoinCtx *r)
444 ADS_STATUS status;
445 ADS_MODLIST mods;
446 char *os_sp = NULL;
448 if (!r->in.os_name || !r->in.os_version ) {
449 return ADS_SUCCESS;
452 /* Find our DN */
454 status = libnet_join_find_machine_acct(mem_ctx, r);
455 if (!ADS_ERR_OK(status)) {
456 return status;
459 /* now do the mods */
461 mods = ads_init_mods(mem_ctx);
462 if (!mods) {
463 return ADS_ERROR(LDAP_NO_MEMORY);
466 os_sp = talloc_asprintf(mem_ctx, "Samba %s", SAMBA_VERSION_STRING);
467 if (!os_sp) {
468 return ADS_ERROR(LDAP_NO_MEMORY);
471 /* fields of primary importance */
473 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
474 r->in.os_name);
475 if (!ADS_ERR_OK(status)) {
476 return status;
479 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
480 r->in.os_version);
481 if (!ADS_ERR_OK(status)) {
482 return status;
485 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
486 os_sp);
487 if (!ADS_ERR_OK(status)) {
488 return status;
491 return ads_gen_mod(r->in.ads, r->out.dn, mods);
494 /****************************************************************
495 ****************************************************************/
497 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
498 struct libnet_JoinCtx *r)
500 if (!lp_use_kerberos_keytab()) {
501 return true;
504 if (!ads_keytab_create_default(r->in.ads)) {
505 return false;
508 return true;
511 /****************************************************************
512 ****************************************************************/
514 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
515 struct libnet_JoinCtx *r)
517 uint32_t domain_func;
518 ADS_STATUS status;
519 const char *salt = NULL;
520 char *std_salt = NULL;
522 status = ads_domain_func_level(r->in.ads, &domain_func);
523 if (!ADS_ERR_OK(status)) {
524 libnet_join_set_error_string(mem_ctx, r,
525 "failed to determine domain functional level: %s",
526 ads_errstr(status));
527 return false;
530 /* go ahead and setup the default salt */
532 std_salt = kerberos_standard_des_salt();
533 if (!std_salt) {
534 libnet_join_set_error_string(mem_ctx, r,
535 "failed to obtain standard DES salt");
536 return false;
539 salt = talloc_strdup(mem_ctx, std_salt);
540 if (!salt) {
541 return false;
544 SAFE_FREE(std_salt);
546 /* if it's a Windows functional domain, we have to look for the UPN */
548 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
549 char *upn;
551 upn = ads_get_upn(r->in.ads, mem_ctx,
552 r->in.machine_name);
553 if (upn) {
554 salt = talloc_strdup(mem_ctx, upn);
555 if (!salt) {
556 return false;
561 return kerberos_secrets_store_des_salt(salt);
564 /****************************************************************
565 ****************************************************************/
567 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
568 struct libnet_JoinCtx *r)
570 ADS_STATUS status;
572 if (!r->in.ads) {
573 status = libnet_join_connect_ads(mem_ctx, r);
574 if (!ADS_ERR_OK(status)) {
575 return status;
579 status = libnet_join_set_machine_spn(mem_ctx, r);
580 if (!ADS_ERR_OK(status)) {
581 libnet_join_set_error_string(mem_ctx, r,
582 "failed to set machine spn: %s",
583 ads_errstr(status));
584 return status;
587 status = libnet_join_set_os_attributes(mem_ctx, r);
588 if (!ADS_ERR_OK(status)) {
589 libnet_join_set_error_string(mem_ctx, r,
590 "failed to set machine os attributes: %s",
591 ads_errstr(status));
592 return status;
595 status = libnet_join_set_machine_upn(mem_ctx, r);
596 if (!ADS_ERR_OK(status)) {
597 libnet_join_set_error_string(mem_ctx, r,
598 "failed to set machine upn: %s",
599 ads_errstr(status));
600 return status;
603 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
604 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
607 if (!libnet_join_create_keytab(mem_ctx, r)) {
608 libnet_join_set_error_string(mem_ctx, r,
609 "failed to create kerberos keytab");
610 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
613 return ADS_SUCCESS;
615 #endif /* WITH_ADS */
617 /****************************************************************
618 Store the machine password and domain SID
619 ****************************************************************/
621 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
622 struct libnet_JoinCtx *r)
624 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
625 r->out.domain_sid))
627 DEBUG(1,("Failed to save domain sid\n"));
628 return false;
631 if (!secrets_store_machine_password(r->in.machine_password,
632 r->out.netbios_domain_name,
633 SEC_CHAN_WKSTA))
635 DEBUG(1,("Failed to save machine password\n"));
636 return false;
639 return true;
642 /****************************************************************
643 Do the domain join
644 ****************************************************************/
646 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
647 struct libnet_JoinCtx *r)
649 struct cli_state *cli = NULL;
650 struct rpc_pipe_client *pipe_hnd = NULL;
651 POLICY_HND sam_pol, domain_pol, user_pol, lsa_pol;
652 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
653 char *acct_name;
654 struct lsa_String lsa_acct_name;
655 uint32 user_rid;
656 uint32 acb_info = ACB_WSTRUST;
657 uchar pwbuf[532];
658 struct MD5Context md5ctx;
659 uchar md5buffer[16];
660 DATA_BLOB digested_session_key;
661 uchar md4_trust_password[16];
662 union lsa_PolicyInformation *info = NULL;
663 struct samr_Ids user_rids;
664 struct samr_Ids name_types;
665 union samr_UserInfo user_info;
667 if (!r->in.machine_password) {
668 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
669 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
672 status = cli_full_connection(&cli, NULL,
673 r->in.dc_name,
674 NULL, 0,
675 "IPC$", "IPC",
676 r->in.admin_account,
677 NULL,
678 r->in.admin_password,
680 Undefined, NULL);
682 if (!NT_STATUS_IS_OK(status)) {
683 goto done;
686 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
687 if (!pipe_hnd) {
688 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
689 nt_errstr(status)));
690 goto done;
693 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
694 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
695 if (!NT_STATUS_IS_OK(status)) {
696 goto done;
699 status = rpccli_lsa_QueryInfoPolicy2(pipe_hnd, mem_ctx,
700 &lsa_pol,
701 LSA_POLICY_INFO_DNS,
702 &info);
703 if (NT_STATUS_IS_OK(status)) {
704 r->out.domain_is_ad = true;
705 r->out.netbios_domain_name = info->dns.name.string;
706 r->out.dns_domain_name = info->dns.dns_domain.string;
707 r->out.domain_sid = info->dns.sid;
710 if (!NT_STATUS_IS_OK(status)) {
711 status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
712 &lsa_pol,
713 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
714 &info);
715 if (!NT_STATUS_IS_OK(status)) {
716 goto done;
719 r->out.netbios_domain_name = info->account_domain.name.string;
720 r->out.domain_sid = info->account_domain.sid;
723 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
724 cli_rpc_pipe_close(pipe_hnd);
726 /* Open the domain */
728 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
729 if (!pipe_hnd) {
730 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
731 nt_errstr(status)));
732 goto done;
735 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
736 pipe_hnd->cli->desthost,
737 SEC_RIGHTS_MAXIMUM_ALLOWED,
738 &sam_pol);
739 if (!NT_STATUS_IS_OK(status)) {
740 goto done;
743 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
744 &sam_pol,
745 SEC_RIGHTS_MAXIMUM_ALLOWED,
746 r->out.domain_sid,
747 &domain_pol);
748 if (!NT_STATUS_IS_OK(status)) {
749 goto done;
752 /* Create domain user */
754 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
755 strlower_m(acct_name);
757 init_lsa_String(&lsa_acct_name, acct_name);
759 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
760 uint32_t acct_flags =
761 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
762 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
763 SAMR_USER_ACCESS_SET_PASSWORD |
764 SAMR_USER_ACCESS_GET_ATTRIBUTES |
765 SAMR_USER_ACCESS_SET_ATTRIBUTES;
766 uint32_t access_granted = 0;
768 /* Don't try to set any acb_info flags other than ACB_WSTRUST */
770 DEBUG(10,("Creating account with flags: %d\n", acct_flags));
772 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
773 &domain_pol,
774 &lsa_acct_name,
775 ACB_WSTRUST,
776 acct_flags,
777 &user_pol,
778 &access_granted,
779 &user_rid);
780 if (!NT_STATUS_IS_OK(status) &&
781 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
783 DEBUG(10,("Creation of workstation account failed: %s\n",
784 nt_errstr(status)));
786 /* If NT_STATUS_ACCESS_DENIED then we have a valid
787 username/password combo but the user does not have
788 administrator access. */
790 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
791 libnet_join_set_error_string(mem_ctx, r,
792 "User specified does not have "
793 "administrator privileges");
796 return status;
799 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
800 if (!(r->in.join_flags &
801 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
802 goto done;
806 /* We *must* do this.... don't ask... */
808 if (NT_STATUS_IS_OK(status)) {
809 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
813 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
814 &domain_pol,
816 &lsa_acct_name,
817 &user_rids,
818 &name_types);
819 if (!NT_STATUS_IS_OK(status)) {
820 goto done;
823 if (name_types.ids[0] != SID_NAME_USER) {
824 DEBUG(0,("%s is not a user account (type=%d)\n",
825 acct_name, name_types.ids[0]));
826 status = NT_STATUS_INVALID_WORKSTATION;
827 goto done;
830 user_rid = user_rids.ids[0];
832 /* Open handle on user */
834 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
835 &domain_pol,
836 SEC_RIGHTS_MAXIMUM_ALLOWED,
837 user_rid,
838 &user_pol);
839 if (!NT_STATUS_IS_OK(status)) {
840 goto done;
843 /* Create a random machine account password and generate the hash */
845 E_md4hash(r->in.machine_password, md4_trust_password);
846 encode_pw_buffer(pwbuf, r->in.machine_password, STR_UNICODE);
848 generate_random_buffer((uint8*)md5buffer, sizeof(md5buffer));
849 digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
851 MD5Init(&md5ctx);
852 MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
853 MD5Update(&md5ctx, cli->user_session_key.data,
854 cli->user_session_key.length);
855 MD5Final(digested_session_key.data, &md5ctx);
857 SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
858 memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
860 /* Fill in the additional account flags now */
862 acb_info |= ACB_PWNOEXP;
863 if (r->out.domain_is_ad) {
864 #if !defined(ENCTYPE_ARCFOUR_HMAC)
865 acb_info |= ACB_USE_DES_KEY_ONLY;
866 #endif
870 /* Set password and account flags on machine account */
872 ZERO_STRUCT(user_info.info25);
874 user_info.info25.info.fields_present = ACCT_NT_PWD_SET |
875 ACCT_LM_PWD_SET |
876 SAMR_FIELD_ACCT_FLAGS;
877 user_info.info25.info.acct_flags = acb_info;
878 memcpy(&user_info.info25.password.data, pwbuf, sizeof(pwbuf));
880 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
881 &user_pol,
883 &user_info);
884 if (!NT_STATUS_IS_OK(status)) {
885 libnet_join_set_error_string(mem_ctx, r,
886 "Failed to set password for machine account (%s)\n",
887 nt_errstr(status));
888 goto done;
891 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
892 cli_rpc_pipe_close(pipe_hnd);
894 status = NT_STATUS_OK;
895 done:
896 if (cli) {
897 cli_shutdown(cli);
900 return status;
903 /****************************************************************
904 ****************************************************************/
906 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
907 const char *machine_name,
908 const char *dc_name)
910 uint32_t neg_flags = NETLOGON_NEG_AUTH2_FLAGS |
911 NETLOGON_NEG_SCHANNEL;
912 /* FIXME: NETLOGON_NEG_SELECT_AUTH2_FLAGS */
913 struct cli_state *cli = NULL;
914 struct rpc_pipe_client *pipe_hnd = NULL;
915 struct rpc_pipe_client *netlogon_pipe = NULL;
916 NTSTATUS status;
917 char *machine_password = NULL;
918 char *machine_account = NULL;
920 if (!dc_name) {
921 return NT_STATUS_INVALID_PARAMETER;
924 if (!secrets_init()) {
925 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
928 machine_password = secrets_fetch_machine_password(netbios_domain_name,
929 NULL, NULL);
930 if (!machine_password) {
931 return NT_STATUS_NO_TRUST_LSA_SECRET;
934 asprintf(&machine_account, "%s$", machine_name);
935 if (!machine_account) {
936 SAFE_FREE(machine_password);
937 return NT_STATUS_NO_MEMORY;
940 status = cli_full_connection(&cli, NULL,
941 dc_name,
942 NULL, 0,
943 "IPC$", "IPC",
944 machine_account,
945 NULL,
946 machine_password,
948 Undefined, NULL);
949 free(machine_account);
950 free(machine_password);
952 if (!NT_STATUS_IS_OK(status)) {
953 status = cli_full_connection(&cli, NULL,
954 dc_name,
955 NULL, 0,
956 "IPC$", "IPC",
958 NULL,
961 Undefined, NULL);
964 if (!NT_STATUS_IS_OK(status)) {
965 return status;
968 netlogon_pipe = get_schannel_session_key(cli,
969 netbios_domain_name,
970 &neg_flags, &status);
971 if (!netlogon_pipe) {
972 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
973 cli_shutdown(cli);
974 return NT_STATUS_OK;
977 DEBUG(0,("libnet_join_ok: failed to get schannel session "
978 "key from server %s for domain %s. Error was %s\n",
979 cli->desthost, netbios_domain_name, nt_errstr(status)));
980 cli_shutdown(cli);
981 return status;
984 if (!lp_client_schannel()) {
985 cli_shutdown(cli);
986 return NT_STATUS_OK;
989 pipe_hnd = cli_rpc_pipe_open_schannel_with_key(cli, PI_NETLOGON,
990 PIPE_AUTH_LEVEL_PRIVACY,
991 netbios_domain_name,
992 netlogon_pipe->dc,
993 &status);
995 cli_shutdown(cli);
997 if (!pipe_hnd) {
998 DEBUG(0,("libnet_join_ok: failed to open schannel session "
999 "on netlogon pipe to server %s for domain %s. "
1000 "Error was %s\n",
1001 cli->desthost, netbios_domain_name, nt_errstr(status)));
1002 return status;
1005 return NT_STATUS_OK;
1008 /****************************************************************
1009 ****************************************************************/
1011 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1012 struct libnet_JoinCtx *r)
1014 NTSTATUS status;
1016 status = libnet_join_ok(r->out.netbios_domain_name,
1017 r->in.machine_name,
1018 r->in.dc_name);
1019 if (!NT_STATUS_IS_OK(status)) {
1020 libnet_join_set_error_string(mem_ctx, r,
1021 "failed to verify domain membership after joining: %s",
1022 get_friendly_nt_error_msg(status));
1023 return WERR_SETUP_NOT_JOINED;
1026 return WERR_OK;
1029 /****************************************************************
1030 ****************************************************************/
1032 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1033 struct libnet_UnjoinCtx *r)
1035 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1036 return false;
1039 if (!secrets_delete_domain_sid(lp_workgroup())) {
1040 return false;
1043 return true;
1046 /****************************************************************
1047 ****************************************************************/
1049 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1050 struct libnet_UnjoinCtx *r)
1052 struct cli_state *cli = NULL;
1053 struct rpc_pipe_client *pipe_hnd = NULL;
1054 POLICY_HND sam_pol, domain_pol, user_pol;
1055 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1056 char *acct_name;
1057 uint32 user_rid;
1058 struct lsa_String lsa_acct_name;
1059 struct samr_Ids user_rids;
1060 struct samr_Ids name_types;
1061 union samr_UserInfo *info = NULL;
1063 status = cli_full_connection(&cli, NULL,
1064 r->in.dc_name,
1065 NULL, 0,
1066 "IPC$", "IPC",
1067 r->in.admin_account,
1068 NULL,
1069 r->in.admin_password,
1070 0, Undefined, NULL);
1072 if (!NT_STATUS_IS_OK(status)) {
1073 goto done;
1076 /* Open the domain */
1078 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
1079 if (!pipe_hnd) {
1080 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1081 nt_errstr(status)));
1082 goto done;
1085 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1086 pipe_hnd->cli->desthost,
1087 SEC_RIGHTS_MAXIMUM_ALLOWED,
1088 &sam_pol);
1089 if (!NT_STATUS_IS_OK(status)) {
1090 goto done;
1093 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1094 &sam_pol,
1095 SEC_RIGHTS_MAXIMUM_ALLOWED,
1096 r->in.domain_sid,
1097 &domain_pol);
1098 if (!NT_STATUS_IS_OK(status)) {
1099 goto done;
1102 /* Create domain user */
1104 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1105 strlower_m(acct_name);
1107 init_lsa_String(&lsa_acct_name, acct_name);
1109 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1110 &domain_pol,
1112 &lsa_acct_name,
1113 &user_rids,
1114 &name_types);
1116 if (!NT_STATUS_IS_OK(status)) {
1117 goto done;
1120 if (name_types.ids[0] != SID_NAME_USER) {
1121 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1122 name_types.ids[0]));
1123 status = NT_STATUS_INVALID_WORKSTATION;
1124 goto done;
1127 user_rid = user_rids.ids[0];
1129 /* Open handle on user */
1131 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1132 &domain_pol,
1133 SEC_RIGHTS_MAXIMUM_ALLOWED,
1134 user_rid,
1135 &user_pol);
1136 if (!NT_STATUS_IS_OK(status)) {
1137 goto done;
1140 /* Get user info */
1142 status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1143 &user_pol,
1145 &info);
1146 if (!NT_STATUS_IS_OK(status)) {
1147 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1148 goto done;
1151 /* now disable and setuser info */
1153 info->info16.acct_flags |= ACB_DISABLED;
1155 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1156 &user_pol,
1158 info);
1160 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1162 done:
1163 if (pipe_hnd) {
1164 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1165 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1166 cli_rpc_pipe_close(pipe_hnd);
1169 if (cli) {
1170 cli_shutdown(cli);
1173 return status;
1176 /****************************************************************
1177 ****************************************************************/
1179 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1181 WERROR werr;
1182 struct libnet_conf_ctx *ctx;
1184 werr = libnet_conf_open(r, &ctx);
1185 if (!W_ERROR_IS_OK(werr)) {
1186 goto done;
1189 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1191 werr = libnet_conf_set_global_parameter(ctx, "security", "user");
1192 W_ERROR_NOT_OK_GOTO_DONE(werr);
1194 werr = libnet_conf_set_global_parameter(ctx, "workgroup",
1195 r->in.domain_name);
1196 goto done;
1199 werr = libnet_conf_set_global_parameter(ctx, "security", "domain");
1200 W_ERROR_NOT_OK_GOTO_DONE(werr);
1202 werr = libnet_conf_set_global_parameter(ctx, "workgroup",
1203 r->out.netbios_domain_name);
1204 W_ERROR_NOT_OK_GOTO_DONE(werr);
1206 if (r->out.domain_is_ad) {
1207 werr = libnet_conf_set_global_parameter(ctx, "security", "ads");
1208 W_ERROR_NOT_OK_GOTO_DONE(werr);
1210 werr = libnet_conf_set_global_parameter(ctx, "realm",
1211 r->out.dns_domain_name);
1212 W_ERROR_NOT_OK_GOTO_DONE(werr);
1215 done:
1216 libnet_conf_close(ctx);
1217 return werr;
1220 /****************************************************************
1221 ****************************************************************/
1223 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1225 WERROR werr = WERR_OK;
1226 struct libnet_conf_ctx *ctx;
1228 werr = libnet_conf_open(r, &ctx);
1229 if (!W_ERROR_IS_OK(werr)) {
1230 goto done;
1233 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1235 werr = libnet_conf_set_global_parameter(ctx, "security", "user");
1236 W_ERROR_NOT_OK_GOTO_DONE(werr);
1237 libnet_conf_delete_global_parameter(ctx, "realm");
1240 done:
1241 libnet_conf_close(ctx);
1242 return werr;
1245 /****************************************************************
1246 ****************************************************************/
1248 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1250 WERROR werr;
1252 if (!W_ERROR_IS_OK(r->out.result)) {
1253 return r->out.result;
1256 if (!r->in.modify_config) {
1257 return WERR_OK;
1260 werr = do_join_modify_vals_config(r);
1261 if (!W_ERROR_IS_OK(werr)) {
1262 return werr;
1265 r->out.modified_config = true;
1266 r->out.result = werr;
1268 return werr;
1271 /****************************************************************
1272 ****************************************************************/
1274 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1276 WERROR werr;
1278 if (!W_ERROR_IS_OK(r->out.result)) {
1279 return r->out.result;
1282 if (!r->in.modify_config) {
1283 return WERR_OK;
1286 werr = do_unjoin_modify_vals_config(r);
1287 if (!W_ERROR_IS_OK(werr)) {
1288 return werr;
1291 r->out.modified_config = true;
1292 r->out.result = werr;
1294 return werr;
1297 /****************************************************************
1298 ****************************************************************/
1300 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1301 struct libnet_JoinCtx *r)
1303 if (!r->in.domain_name) {
1304 libnet_join_set_error_string(mem_ctx, r,
1305 "No domain name defined");
1306 return WERR_INVALID_PARAM;
1309 if (r->in.modify_config && !lp_config_backend_is_registry()) {
1310 libnet_join_set_error_string(mem_ctx, r,
1311 "Configuration manipulation requested but not "
1312 "supported by backend");
1313 return WERR_NOT_SUPPORTED;
1316 if (IS_DC) {
1317 return WERR_SETUP_DOMAIN_CONTROLLER;
1320 if (!secrets_init()) {
1321 libnet_join_set_error_string(mem_ctx, r,
1322 "Unable to open secrets database");
1323 return WERR_CAN_NOT_COMPLETE;
1326 return WERR_OK;
1329 /****************************************************************
1330 ****************************************************************/
1332 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1333 struct libnet_JoinCtx *r)
1335 WERROR werr;
1337 if (!W_ERROR_IS_OK(r->out.result)) {
1338 return r->out.result;
1341 werr = do_JoinConfig(r);
1342 if (!W_ERROR_IS_OK(werr)) {
1343 return werr;
1346 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1347 saf_store(r->in.domain_name, r->in.dc_name);
1350 return WERR_OK;
1353 /****************************************************************
1354 ****************************************************************/
1356 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1358 if (r->in.ads) {
1359 ads_destroy(&r->in.ads);
1362 return 0;
1365 /****************************************************************
1366 ****************************************************************/
1368 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1370 if (r->in.ads) {
1371 ads_destroy(&r->in.ads);
1374 return 0;
1377 /****************************************************************
1378 ****************************************************************/
1380 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1381 struct libnet_JoinCtx **r)
1383 struct libnet_JoinCtx *ctx;
1385 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1386 if (!ctx) {
1387 return WERR_NOMEM;
1390 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1392 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1393 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1395 *r = ctx;
1397 return WERR_OK;
1400 /****************************************************************
1401 ****************************************************************/
1403 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1404 struct libnet_UnjoinCtx **r)
1406 struct libnet_UnjoinCtx *ctx;
1408 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1409 if (!ctx) {
1410 return WERR_NOMEM;
1413 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1415 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1416 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1418 *r = ctx;
1420 return WERR_OK;
1423 /****************************************************************
1424 ****************************************************************/
1426 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1427 struct libnet_JoinCtx *r)
1429 NTSTATUS status;
1430 #ifdef WITH_ADS
1431 ADS_STATUS ads_status;
1432 #endif /* WITH_ADS */
1434 if (!r->in.dc_name) {
1435 struct netr_DsRGetDCNameInfo *info;
1436 status = dsgetdcname(mem_ctx,
1437 r->in.domain_name,
1438 NULL,
1439 NULL,
1440 DS_DIRECTORY_SERVICE_REQUIRED |
1441 DS_WRITABLE_REQUIRED |
1442 DS_RETURN_DNS_NAME,
1443 &info);
1444 if (!NT_STATUS_IS_OK(status)) {
1445 libnet_join_set_error_string(mem_ctx, r,
1446 "failed to find DC for domain %s",
1447 r->in.domain_name,
1448 get_friendly_nt_error_msg(status));
1449 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1452 r->in.dc_name = talloc_strdup(mem_ctx,
1453 info->dc_unc);
1454 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1457 #ifdef WITH_ADS
1458 if (r->in.account_ou) {
1460 ads_status = libnet_join_connect_ads(mem_ctx, r);
1461 if (!ADS_ERR_OK(ads_status)) {
1462 return WERR_DEFAULT_JOIN_REQUIRED;
1465 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1466 if (!ADS_ERR_OK(ads_status)) {
1467 libnet_join_set_error_string(mem_ctx, r,
1468 "failed to precreate account in ou %s: %s",
1469 r->in.account_ou,
1470 ads_errstr(ads_status));
1471 return WERR_DEFAULT_JOIN_REQUIRED;
1474 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1476 #endif /* WITH_ADS */
1478 status = libnet_join_joindomain_rpc(mem_ctx, r);
1479 if (!NT_STATUS_IS_OK(status)) {
1480 libnet_join_set_error_string(mem_ctx, r,
1481 "failed to join domain over rpc: %s",
1482 get_friendly_nt_error_msg(status));
1483 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1484 return WERR_SETUP_ALREADY_JOINED;
1486 return ntstatus_to_werror(status);
1489 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1490 return WERR_SETUP_NOT_JOINED;
1493 #ifdef WITH_ADS
1494 if (r->out.domain_is_ad) {
1495 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1496 if (!ADS_ERR_OK(ads_status)) {
1497 return WERR_GENERAL_FAILURE;
1500 #endif /* WITH_ADS */
1502 return WERR_OK;
1505 /****************************************************************
1506 ****************************************************************/
1508 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1509 struct libnet_JoinCtx *r)
1511 WERROR werr;
1513 if (r->in.debug) {
1514 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1517 werr = libnet_join_pre_processing(mem_ctx, r);
1518 if (!W_ERROR_IS_OK(werr)) {
1519 goto done;
1522 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1523 werr = libnet_DomainJoin(mem_ctx, r);
1524 if (!W_ERROR_IS_OK(werr)) {
1525 goto done;
1528 werr = libnet_join_post_verify(mem_ctx, r);
1529 if (!W_ERROR_IS_OK(werr)) {
1530 goto done;
1534 werr = libnet_join_post_processing(mem_ctx, r);
1535 if (!W_ERROR_IS_OK(werr)) {
1536 goto done;
1538 done:
1539 r->out.result = werr;
1541 if (r->in.debug) {
1542 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1544 return werr;
1547 /****************************************************************
1548 ****************************************************************/
1550 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1551 struct libnet_UnjoinCtx *r)
1553 NTSTATUS status;
1555 if (!r->in.domain_sid) {
1556 struct dom_sid sid;
1557 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1558 libnet_unjoin_set_error_string(mem_ctx, r,
1559 "Unable to fetch domain sid: are we joined?");
1560 return WERR_SETUP_NOT_JOINED;
1562 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
1563 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
1566 if (!r->in.dc_name) {
1567 struct netr_DsRGetDCNameInfo *info;
1568 status = dsgetdcname(mem_ctx,
1569 r->in.domain_name,
1570 NULL,
1571 NULL,
1572 DS_DIRECTORY_SERVICE_REQUIRED |
1573 DS_WRITABLE_REQUIRED |
1574 DS_RETURN_DNS_NAME,
1575 &info);
1576 if (!NT_STATUS_IS_OK(status)) {
1577 libnet_unjoin_set_error_string(mem_ctx, r,
1578 "failed to find DC for domain %s",
1579 r->in.domain_name,
1580 get_friendly_nt_error_msg(status));
1581 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1584 r->in.dc_name = talloc_strdup(mem_ctx,
1585 info->dc_unc);
1586 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1589 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
1590 if (!NT_STATUS_IS_OK(status)) {
1591 libnet_unjoin_set_error_string(mem_ctx, r,
1592 "failed to disable machine account via rpc: %s",
1593 get_friendly_nt_error_msg(status));
1594 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1595 return WERR_SETUP_NOT_JOINED;
1597 return ntstatus_to_werror(status);
1600 r->out.disabled_machine_account = true;
1602 #ifdef WITH_ADS
1603 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
1604 ADS_STATUS ads_status;
1605 libnet_unjoin_connect_ads(mem_ctx, r);
1606 ads_status = libnet_unjoin_remove_machine_acct(mem_ctx, r);
1607 if (!ADS_ERR_OK(ads_status)) {
1608 libnet_unjoin_set_error_string(mem_ctx, r,
1609 "failed to remove machine account from AD: %s",
1610 ads_errstr(ads_status));
1611 } else {
1612 r->out.deleted_machine_account = true;
1613 /* dirty hack */
1614 r->out.dns_domain_name = talloc_strdup(mem_ctx,
1615 r->in.ads->server.realm);
1616 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
1619 #endif /* WITH_ADS */
1621 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
1623 return WERR_OK;
1626 /****************************************************************
1627 ****************************************************************/
1629 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
1630 struct libnet_UnjoinCtx *r)
1632 if (!r->in.domain_name) {
1633 libnet_unjoin_set_error_string(mem_ctx, r,
1634 "No domain name defined");
1635 return WERR_INVALID_PARAM;
1638 if (r->in.modify_config && !lp_config_backend_is_registry()) {
1639 libnet_unjoin_set_error_string(mem_ctx, r,
1640 "Configuration manipulation requested but not "
1641 "supported by backend");
1642 return WERR_NOT_SUPPORTED;
1645 if (IS_DC) {
1646 return WERR_SETUP_DOMAIN_CONTROLLER;
1649 if (!secrets_init()) {
1650 libnet_unjoin_set_error_string(mem_ctx, r,
1651 "Unable to open secrets database");
1652 return WERR_CAN_NOT_COMPLETE;
1655 return WERR_OK;
1658 /****************************************************************
1659 ****************************************************************/
1661 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
1662 struct libnet_UnjoinCtx *r)
1664 saf_delete(r->out.netbios_domain_name);
1665 saf_delete(r->out.dns_domain_name);
1667 return libnet_unjoin_config(r);
1670 /****************************************************************
1671 ****************************************************************/
1673 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
1674 struct libnet_UnjoinCtx *r)
1676 WERROR werr;
1678 if (r->in.debug) {
1679 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
1682 werr = libnet_unjoin_pre_processing(mem_ctx, r);
1683 if (!W_ERROR_IS_OK(werr)) {
1684 goto done;
1687 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1688 werr = libnet_DomainUnjoin(mem_ctx, r);
1689 if (!W_ERROR_IS_OK(werr)) {
1690 libnet_unjoin_config(r);
1691 goto done;
1695 werr = libnet_unjoin_post_processing(mem_ctx, r);
1696 if (!W_ERROR_IS_OK(werr)) {
1697 goto done;
1700 done:
1701 r->out.result = werr;
1703 if (r->in.debug) {
1704 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
1707 return werr;