CVE-2013-4408:s3:Ensure LookupNames replies arrays are range checked.
[Samba.git] / source3 / libnet / libnet_join.c
blob22e667518e656b1af23d013f1a71b3c915a8d352
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 "ads.h"
23 #include "librpc/gen_ndr/ndr_libnet_join.h"
24 #include "libnet/libnet_join.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_lsa_c.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/ndr_netlogon.h"
31 #include "rpc_client/cli_netlogon.h"
32 #include "lib/smbconf/smbconf.h"
33 #include "lib/smbconf/smbconf_reg.h"
34 #include "../libds/common/flags.h"
35 #include "secrets.h"
36 #include "rpc_client/init_lsa.h"
37 #include "rpc_client/cli_pipe.h"
38 #include "../libcli/security/security.h"
39 #include "passdb.h"
40 #include "libsmb/libsmb.h"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/param/loadparm.h"
44 /****************************************************************
45 ****************************************************************/
47 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
48 do { \
49 char *str = NULL; \
50 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
51 DEBUG(1,("libnet_Join:\n%s", str)); \
52 TALLOC_FREE(str); \
53 } while (0)
55 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
56 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
57 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
58 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
60 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
61 do { \
62 char *str = NULL; \
63 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
64 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
65 TALLOC_FREE(str); \
66 } while (0)
68 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
69 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
70 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
71 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
73 /****************************************************************
74 ****************************************************************/
76 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
77 struct libnet_JoinCtx *r,
78 const char *format, ...)
80 va_list args;
82 if (r->out.error_string) {
83 return;
86 va_start(args, format);
87 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
88 va_end(args);
91 /****************************************************************
92 ****************************************************************/
94 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
95 struct libnet_UnjoinCtx *r,
96 const char *format, ...)
98 va_list args;
100 if (r->out.error_string) {
101 return;
104 va_start(args, format);
105 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
106 va_end(args);
109 #ifdef HAVE_ADS
111 /****************************************************************
112 ****************************************************************/
114 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
115 const char *netbios_domain_name,
116 const char *dc_name,
117 const char *user_name,
118 const char *password,
119 ADS_STRUCT **ads)
121 ADS_STATUS status;
122 ADS_STRUCT *my_ads = NULL;
123 char *cp;
125 my_ads = ads_init(dns_domain_name,
126 netbios_domain_name,
127 dc_name);
128 if (!my_ads) {
129 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
132 if (user_name) {
133 SAFE_FREE(my_ads->auth.user_name);
134 my_ads->auth.user_name = SMB_STRDUP(user_name);
135 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
136 *cp++ = '\0';
137 SAFE_FREE(my_ads->auth.realm);
138 my_ads->auth.realm = smb_xstrdup(cp);
139 if (!strupper_m(my_ads->auth.realm)) {
140 ads_destroy(&my_ads);
141 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
146 if (password) {
147 SAFE_FREE(my_ads->auth.password);
148 my_ads->auth.password = SMB_STRDUP(password);
151 status = ads_connect_user_creds(my_ads);
152 if (!ADS_ERR_OK(status)) {
153 ads_destroy(&my_ads);
154 return status;
157 *ads = my_ads;
158 return ADS_SUCCESS;
161 /****************************************************************
162 ****************************************************************/
164 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
165 struct libnet_JoinCtx *r)
167 ADS_STATUS status;
169 status = libnet_connect_ads(r->out.dns_domain_name,
170 r->out.netbios_domain_name,
171 r->in.dc_name,
172 r->in.admin_account,
173 r->in.admin_password,
174 &r->in.ads);
175 if (!ADS_ERR_OK(status)) {
176 libnet_join_set_error_string(mem_ctx, r,
177 "failed to connect to AD: %s",
178 ads_errstr(status));
179 return status;
182 if (!r->out.netbios_domain_name) {
183 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
184 r->in.ads->server.workgroup);
185 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
188 if (!r->out.dns_domain_name) {
189 r->out.dns_domain_name = talloc_strdup(mem_ctx,
190 r->in.ads->config.realm);
191 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
194 r->out.domain_is_ad = true;
196 return ADS_SUCCESS;
199 /****************************************************************
200 ****************************************************************/
202 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
203 struct libnet_UnjoinCtx *r)
205 ADS_STATUS status;
207 status = libnet_connect_ads(r->in.domain_name,
208 r->in.domain_name,
209 r->in.dc_name,
210 r->in.admin_account,
211 r->in.admin_password,
212 &r->in.ads);
213 if (!ADS_ERR_OK(status)) {
214 libnet_unjoin_set_error_string(mem_ctx, r,
215 "failed to connect to AD: %s",
216 ads_errstr(status));
219 return status;
222 /****************************************************************
223 join a domain using ADS (LDAP mods)
224 ****************************************************************/
226 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
227 struct libnet_JoinCtx *r)
229 ADS_STATUS status;
230 LDAPMessage *res = NULL;
231 const char *attrs[] = { "dn", NULL };
232 bool moved = false;
234 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
235 if (!ADS_ERR_OK(status)) {
236 return status;
239 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
240 if (!ADS_ERR_OK(status)) {
241 return status;
244 if (ads_count_replies(r->in.ads, res) != 1) {
245 ads_msgfree(r->in.ads, res);
246 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
249 ads_msgfree(r->in.ads, res);
251 /* Attempt to create the machine account and bail if this fails.
252 Assume that the admin wants exactly what they requested */
254 status = ads_create_machine_acct(r->in.ads,
255 r->in.machine_name,
256 r->in.account_ou);
258 if (ADS_ERR_OK(status)) {
259 DEBUG(1,("machine account creation created\n"));
260 return status;
261 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
262 (status.err.rc == LDAP_ALREADY_EXISTS)) {
263 status = ADS_SUCCESS;
266 if (!ADS_ERR_OK(status)) {
267 DEBUG(1,("machine account creation failed\n"));
268 return status;
271 status = ads_move_machine_acct(r->in.ads,
272 r->in.machine_name,
273 r->in.account_ou,
274 &moved);
275 if (!ADS_ERR_OK(status)) {
276 DEBUG(1,("failure to locate/move pre-existing "
277 "machine account\n"));
278 return status;
281 DEBUG(1,("The machine account %s the specified OU.\n",
282 moved ? "was moved into" : "already exists in"));
284 return status;
287 /****************************************************************
288 ****************************************************************/
290 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
291 struct libnet_UnjoinCtx *r)
293 ADS_STATUS status;
295 if (!r->in.ads) {
296 status = libnet_unjoin_connect_ads(mem_ctx, r);
297 if (!ADS_ERR_OK(status)) {
298 libnet_unjoin_set_error_string(mem_ctx, r,
299 "failed to connect to AD: %s",
300 ads_errstr(status));
301 return status;
305 status = ads_leave_realm(r->in.ads, r->in.machine_name);
306 if (!ADS_ERR_OK(status)) {
307 libnet_unjoin_set_error_string(mem_ctx, r,
308 "failed to leave realm: %s",
309 ads_errstr(status));
310 return status;
313 return ADS_SUCCESS;
316 /****************************************************************
317 ****************************************************************/
319 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
320 struct libnet_JoinCtx *r)
322 ADS_STATUS status;
323 LDAPMessage *res = NULL;
324 char *dn = NULL;
326 if (!r->in.machine_name) {
327 return ADS_ERROR(LDAP_NO_MEMORY);
330 status = ads_find_machine_acct(r->in.ads,
331 &res,
332 r->in.machine_name);
333 if (!ADS_ERR_OK(status)) {
334 return status;
337 if (ads_count_replies(r->in.ads, res) != 1) {
338 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
339 goto done;
342 dn = ads_get_dn(r->in.ads, mem_ctx, res);
343 if (!dn) {
344 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
345 goto done;
348 r->out.dn = talloc_strdup(mem_ctx, dn);
349 if (!r->out.dn) {
350 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
351 goto done;
354 done:
355 ads_msgfree(r->in.ads, res);
356 TALLOC_FREE(dn);
358 return status;
361 /****************************************************************
362 Set a machines dNSHostName and servicePrincipalName attributes
363 ****************************************************************/
365 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
366 struct libnet_JoinCtx *r)
368 ADS_STATUS status;
369 ADS_MODLIST mods;
370 fstring my_fqdn;
371 const char *spn_array[3] = {NULL, NULL, NULL};
372 char *spn = NULL;
374 /* Find our DN */
376 status = libnet_join_find_machine_acct(mem_ctx, r);
377 if (!ADS_ERR_OK(status)) {
378 return status;
381 /* Windows only creates HOST/shortname & HOST/fqdn. */
383 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
384 if (!spn) {
385 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
387 if (!strupper_m(spn)) {
388 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
390 spn_array[0] = spn;
392 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
393 || (strchr(my_fqdn, '.') == NULL)) {
394 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
395 r->out.dns_domain_name);
398 if (!strlower_m(my_fqdn)) {
399 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
402 if (!strequal(my_fqdn, r->in.machine_name)) {
403 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
404 if (!spn) {
405 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
407 spn_array[1] = spn;
410 mods = ads_init_mods(mem_ctx);
411 if (!mods) {
412 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
415 /* fields of primary importance */
417 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
418 if (!ADS_ERR_OK(status)) {
419 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
422 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
423 spn_array);
424 if (!ADS_ERR_OK(status)) {
425 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
428 return ads_gen_mod(r->in.ads, r->out.dn, mods);
431 /****************************************************************
432 ****************************************************************/
434 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
435 struct libnet_JoinCtx *r)
437 ADS_STATUS status;
438 ADS_MODLIST mods;
440 if (!r->in.create_upn) {
441 return ADS_SUCCESS;
444 /* Find our DN */
446 status = libnet_join_find_machine_acct(mem_ctx, r);
447 if (!ADS_ERR_OK(status)) {
448 return status;
451 if (!r->in.upn) {
452 r->in.upn = talloc_asprintf(mem_ctx,
453 "host/%s@%s",
454 r->in.machine_name,
455 r->out.dns_domain_name);
456 if (!r->in.upn) {
457 return ADS_ERROR(LDAP_NO_MEMORY);
461 /* now do the mods */
463 mods = ads_init_mods(mem_ctx);
464 if (!mods) {
465 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
468 /* fields of primary importance */
470 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
471 if (!ADS_ERR_OK(status)) {
472 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
475 return ads_gen_mod(r->in.ads, r->out.dn, mods);
479 /****************************************************************
480 ****************************************************************/
482 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
483 struct libnet_JoinCtx *r)
485 ADS_STATUS status;
486 ADS_MODLIST mods;
487 char *os_sp = NULL;
489 if (!r->in.os_name || !r->in.os_version ) {
490 return ADS_SUCCESS;
493 /* Find our DN */
495 status = libnet_join_find_machine_acct(mem_ctx, r);
496 if (!ADS_ERR_OK(status)) {
497 return status;
500 /* now do the mods */
502 mods = ads_init_mods(mem_ctx);
503 if (!mods) {
504 return ADS_ERROR(LDAP_NO_MEMORY);
507 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
508 if (!os_sp) {
509 return ADS_ERROR(LDAP_NO_MEMORY);
512 /* fields of primary importance */
514 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
515 r->in.os_name);
516 if (!ADS_ERR_OK(status)) {
517 return status;
520 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
521 r->in.os_version);
522 if (!ADS_ERR_OK(status)) {
523 return status;
526 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
527 os_sp);
528 if (!ADS_ERR_OK(status)) {
529 return status;
532 return ads_gen_mod(r->in.ads, r->out.dn, mods);
535 /****************************************************************
536 ****************************************************************/
538 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
539 struct libnet_JoinCtx *r)
541 if (!USE_SYSTEM_KEYTAB) {
542 return true;
545 if (ads_keytab_create_default(r->in.ads) != 0) {
546 return false;
549 return true;
552 /****************************************************************
553 ****************************************************************/
555 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
556 struct libnet_JoinCtx *r)
558 uint32_t domain_func;
559 ADS_STATUS status;
560 const char *salt = NULL;
561 char *std_salt = NULL;
563 status = ads_domain_func_level(r->in.ads, &domain_func);
564 if (!ADS_ERR_OK(status)) {
565 libnet_join_set_error_string(mem_ctx, r,
566 "failed to determine domain functional level: %s",
567 ads_errstr(status));
568 return false;
571 /* go ahead and setup the default salt */
573 std_salt = kerberos_standard_des_salt();
574 if (!std_salt) {
575 libnet_join_set_error_string(mem_ctx, r,
576 "failed to obtain standard DES salt");
577 return false;
580 salt = talloc_strdup(mem_ctx, std_salt);
581 if (!salt) {
582 return false;
585 SAFE_FREE(std_salt);
587 /* if it's a Windows functional domain, we have to look for the UPN */
589 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
590 char *upn;
592 upn = ads_get_upn(r->in.ads, mem_ctx,
593 r->in.machine_name);
594 if (upn) {
595 salt = talloc_strdup(mem_ctx, upn);
596 if (!salt) {
597 return false;
602 return kerberos_secrets_store_des_salt(salt);
605 /****************************************************************
606 ****************************************************************/
608 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
609 struct libnet_JoinCtx *r)
611 ADS_STATUS status;
613 if (!r->in.ads) {
614 status = libnet_join_connect_ads(mem_ctx, r);
615 if (!ADS_ERR_OK(status)) {
616 return status;
620 status = libnet_join_set_machine_spn(mem_ctx, r);
621 if (!ADS_ERR_OK(status)) {
622 libnet_join_set_error_string(mem_ctx, r,
623 "failed to set machine spn: %s",
624 ads_errstr(status));
625 return status;
628 status = libnet_join_set_os_attributes(mem_ctx, r);
629 if (!ADS_ERR_OK(status)) {
630 libnet_join_set_error_string(mem_ctx, r,
631 "failed to set machine os attributes: %s",
632 ads_errstr(status));
633 return status;
636 status = libnet_join_set_machine_upn(mem_ctx, r);
637 if (!ADS_ERR_OK(status)) {
638 libnet_join_set_error_string(mem_ctx, r,
639 "failed to set machine upn: %s",
640 ads_errstr(status));
641 return status;
644 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
645 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
648 if (!libnet_join_create_keytab(mem_ctx, r)) {
649 libnet_join_set_error_string(mem_ctx, r,
650 "failed to create kerberos keytab");
651 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
654 return ADS_SUCCESS;
656 #endif /* HAVE_ADS */
658 /****************************************************************
659 Store the machine password and domain SID
660 ****************************************************************/
662 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
663 struct libnet_JoinCtx *r)
665 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
666 r->out.domain_sid))
668 DEBUG(1,("Failed to save domain sid\n"));
669 return false;
672 if (!secrets_store_machine_password(r->in.machine_password,
673 r->out.netbios_domain_name,
674 r->in.secure_channel_type))
676 DEBUG(1,("Failed to save machine password\n"));
677 return false;
680 return true;
683 /****************************************************************
684 Connect dc's IPC$ share
685 ****************************************************************/
687 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
688 const char *user,
689 const char *pass,
690 bool use_kerberos,
691 struct cli_state **cli)
693 int flags = 0;
695 if (use_kerberos) {
696 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
699 if (use_kerberos && pass) {
700 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
703 return cli_full_connection(cli, NULL,
705 NULL, 0,
706 "IPC$", "IPC",
707 user,
708 NULL,
709 pass,
710 flags,
711 SMB_SIGNING_DEFAULT);
714 /****************************************************************
715 Lookup domain dc's info
716 ****************************************************************/
718 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
719 struct libnet_JoinCtx *r,
720 struct cli_state **cli)
722 struct rpc_pipe_client *pipe_hnd = NULL;
723 struct policy_handle lsa_pol;
724 NTSTATUS status, result;
725 union lsa_PolicyInformation *info = NULL;
726 struct dcerpc_binding_handle *b;
728 status = libnet_join_connect_dc_ipc(r->in.dc_name,
729 r->in.admin_account,
730 r->in.admin_password,
731 r->in.use_kerberos,
732 cli);
733 if (!NT_STATUS_IS_OK(status)) {
734 goto done;
737 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
738 &pipe_hnd);
739 if (!NT_STATUS_IS_OK(status)) {
740 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
741 nt_errstr(status)));
742 goto done;
745 b = pipe_hnd->binding_handle;
747 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
748 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
749 if (!NT_STATUS_IS_OK(status)) {
750 goto done;
753 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
754 &lsa_pol,
755 LSA_POLICY_INFO_DNS,
756 &info,
757 &result);
758 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
759 r->out.domain_is_ad = true;
760 r->out.netbios_domain_name = info->dns.name.string;
761 r->out.dns_domain_name = info->dns.dns_domain.string;
762 r->out.forest_name = info->dns.dns_forest.string;
763 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
764 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
767 if (!NT_STATUS_IS_OK(status)) {
768 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
769 &lsa_pol,
770 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
771 &info,
772 &result);
773 if (!NT_STATUS_IS_OK(status)) {
774 goto done;
776 if (!NT_STATUS_IS_OK(result)) {
777 status = result;
778 goto done;
781 r->out.netbios_domain_name = info->account_domain.name.string;
782 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
783 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
786 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
787 TALLOC_FREE(pipe_hnd);
789 done:
790 return status;
793 /****************************************************************
794 Do the domain join unsecure
795 ****************************************************************/
797 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
798 struct libnet_JoinCtx *r,
799 struct cli_state *cli)
801 struct rpc_pipe_client *pipe_hnd = NULL;
802 unsigned char orig_trust_passwd_hash[16];
803 unsigned char new_trust_passwd_hash[16];
804 fstring trust_passwd;
805 NTSTATUS status;
807 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
808 &pipe_hnd);
809 if (!NT_STATUS_IS_OK(status)) {
810 return status;
813 if (!r->in.machine_password) {
814 r->in.machine_password = generate_random_password(mem_ctx,
815 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
816 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
817 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
820 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
822 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
823 fstrcpy(trust_passwd, r->in.admin_password);
824 if (!strlower_m(trust_passwd)) {
825 return NT_STATUS_INVALID_PARAMETER;
829 * Machine names can be 15 characters, but the max length on
830 * a password is 14. --jerry
833 trust_passwd[14] = '\0';
835 E_md4hash(trust_passwd, orig_trust_passwd_hash);
837 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
838 r->in.machine_name,
839 orig_trust_passwd_hash,
840 r->in.machine_password,
841 new_trust_passwd_hash,
842 r->in.secure_channel_type);
844 return status;
847 /****************************************************************
848 Do the domain join
849 ****************************************************************/
851 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
852 struct libnet_JoinCtx *r,
853 struct cli_state *cli)
855 struct rpc_pipe_client *pipe_hnd = NULL;
856 struct policy_handle sam_pol, domain_pol, user_pol;
857 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
858 char *acct_name;
859 struct lsa_String lsa_acct_name;
860 uint32_t user_rid;
861 uint32_t acct_flags = ACB_WSTRUST;
862 struct samr_Ids user_rids;
863 struct samr_Ids name_types;
864 union samr_UserInfo user_info;
865 struct dcerpc_binding_handle *b = NULL;
867 DATA_BLOB session_key = data_blob_null;
868 struct samr_CryptPassword crypt_pwd;
869 struct samr_CryptPasswordEx crypt_pwd_ex;
871 ZERO_STRUCT(sam_pol);
872 ZERO_STRUCT(domain_pol);
873 ZERO_STRUCT(user_pol);
875 switch (r->in.secure_channel_type) {
876 case SEC_CHAN_WKSTA:
877 acct_flags = ACB_WSTRUST;
878 break;
879 case SEC_CHAN_BDC:
880 acct_flags = ACB_SVRTRUST;
881 break;
882 default:
883 return NT_STATUS_INVALID_PARAMETER;
886 if (!r->in.machine_password) {
887 r->in.machine_password = generate_random_password(mem_ctx,
888 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
889 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
890 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
893 /* Open the domain */
895 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
896 &pipe_hnd);
897 if (!NT_STATUS_IS_OK(status)) {
898 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
899 nt_errstr(status)));
900 goto done;
903 b = pipe_hnd->binding_handle;
905 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
906 if (!NT_STATUS_IS_OK(status)) {
907 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
908 nt_errstr(status)));
909 goto done;
912 status = dcerpc_samr_Connect2(b, mem_ctx,
913 pipe_hnd->desthost,
914 SAMR_ACCESS_ENUM_DOMAINS
915 | SAMR_ACCESS_LOOKUP_DOMAIN,
916 &sam_pol,
917 &result);
918 if (!NT_STATUS_IS_OK(status)) {
919 goto done;
921 if (!NT_STATUS_IS_OK(result)) {
922 status = result;
923 goto done;
926 status = dcerpc_samr_OpenDomain(b, mem_ctx,
927 &sam_pol,
928 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
929 | SAMR_DOMAIN_ACCESS_CREATE_USER
930 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
931 r->out.domain_sid,
932 &domain_pol,
933 &result);
934 if (!NT_STATUS_IS_OK(status)) {
935 goto done;
937 if (!NT_STATUS_IS_OK(result)) {
938 status = result;
939 goto done;
942 /* Create domain user */
944 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
945 if (!strlower_m(acct_name)) {
946 status = NT_STATUS_INVALID_PARAMETER;
947 goto done;
950 init_lsa_String(&lsa_acct_name, acct_name);
952 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
953 uint32_t access_desired =
954 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
955 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
956 SAMR_USER_ACCESS_SET_PASSWORD |
957 SAMR_USER_ACCESS_GET_ATTRIBUTES |
958 SAMR_USER_ACCESS_SET_ATTRIBUTES;
959 uint32_t access_granted = 0;
961 DEBUG(10,("Creating account with desired access mask: %d\n",
962 access_desired));
964 status = dcerpc_samr_CreateUser2(b, mem_ctx,
965 &domain_pol,
966 &lsa_acct_name,
967 acct_flags,
968 access_desired,
969 &user_pol,
970 &access_granted,
971 &user_rid,
972 &result);
973 if (!NT_STATUS_IS_OK(status)) {
974 goto done;
977 status = result;
978 if (!NT_STATUS_IS_OK(status) &&
979 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
981 DEBUG(10,("Creation of workstation account failed: %s\n",
982 nt_errstr(status)));
984 /* If NT_STATUS_ACCESS_DENIED then we have a valid
985 username/password combo but the user does not have
986 administrator access. */
988 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
989 libnet_join_set_error_string(mem_ctx, r,
990 "User specified does not have "
991 "administrator privileges");
994 goto done;
997 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
998 if (!(r->in.join_flags &
999 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1000 goto done;
1004 /* We *must* do this.... don't ask... */
1006 if (NT_STATUS_IS_OK(status)) {
1007 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1011 status = dcerpc_samr_LookupNames(b, mem_ctx,
1012 &domain_pol,
1014 &lsa_acct_name,
1015 &user_rids,
1016 &name_types,
1017 &result);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 goto done;
1021 if (!NT_STATUS_IS_OK(result)) {
1022 status = result;
1023 goto done;
1025 if (user_rids.count != 1) {
1026 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1027 goto done;
1029 if (name_types.count != 1) {
1030 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1031 goto done;
1034 if (name_types.ids[0] != SID_NAME_USER) {
1035 DEBUG(0,("%s is not a user account (type=%d)\n",
1036 acct_name, name_types.ids[0]));
1037 status = NT_STATUS_INVALID_WORKSTATION;
1038 goto done;
1041 user_rid = user_rids.ids[0];
1043 /* Open handle on user */
1045 status = dcerpc_samr_OpenUser(b, mem_ctx,
1046 &domain_pol,
1047 SEC_FLAG_MAXIMUM_ALLOWED,
1048 user_rid,
1049 &user_pol,
1050 &result);
1051 if (!NT_STATUS_IS_OK(status)) {
1052 goto done;
1054 if (!NT_STATUS_IS_OK(result)) {
1055 status = result;
1056 goto done;
1059 /* Fill in the additional account flags now */
1061 acct_flags |= ACB_PWNOEXP;
1063 /* Set account flags on machine account */
1064 ZERO_STRUCT(user_info.info16);
1065 user_info.info16.acct_flags = acct_flags;
1067 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1068 &user_pol,
1070 &user_info,
1071 &result);
1072 if (!NT_STATUS_IS_OK(status)) {
1073 dcerpc_samr_DeleteUser(b, mem_ctx,
1074 &user_pol,
1075 &result);
1077 libnet_join_set_error_string(mem_ctx, r,
1078 "Failed to set account flags for machine account (%s)\n",
1079 nt_errstr(status));
1080 goto done;
1083 if (!NT_STATUS_IS_OK(result)) {
1084 status = result;
1086 dcerpc_samr_DeleteUser(b, mem_ctx,
1087 &user_pol,
1088 &result);
1090 libnet_join_set_error_string(mem_ctx, r,
1091 "Failed to set account flags for machine account (%s)\n",
1092 nt_errstr(status));
1093 goto done;
1096 /* Set password on machine account - first try level 26 */
1098 init_samr_CryptPasswordEx(r->in.machine_password,
1099 &session_key,
1100 &crypt_pwd_ex);
1102 user_info.info26.password = crypt_pwd_ex;
1103 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1105 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1106 &user_pol,
1108 &user_info,
1109 &result);
1111 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1113 /* retry with level 24 */
1115 init_samr_CryptPassword(r->in.machine_password,
1116 &session_key,
1117 &crypt_pwd);
1119 user_info.info24.password = crypt_pwd;
1120 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1122 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1123 &user_pol,
1125 &user_info,
1126 &result);
1129 if (!NT_STATUS_IS_OK(status)) {
1131 dcerpc_samr_DeleteUser(b, mem_ctx,
1132 &user_pol,
1133 &result);
1135 libnet_join_set_error_string(mem_ctx, r,
1136 "Failed to set password for machine account (%s)\n",
1137 nt_errstr(status));
1138 goto done;
1140 if (!NT_STATUS_IS_OK(result)) {
1141 status = result;
1143 dcerpc_samr_DeleteUser(b, mem_ctx,
1144 &user_pol,
1145 &result);
1147 libnet_join_set_error_string(mem_ctx, r,
1148 "Failed to set password for machine account (%s)\n",
1149 nt_errstr(status));
1150 goto done;
1153 status = NT_STATUS_OK;
1155 done:
1156 if (!pipe_hnd) {
1157 return status;
1160 data_blob_clear_free(&session_key);
1162 if (is_valid_policy_hnd(&sam_pol)) {
1163 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1165 if (is_valid_policy_hnd(&domain_pol)) {
1166 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1168 if (is_valid_policy_hnd(&user_pol)) {
1169 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1171 TALLOC_FREE(pipe_hnd);
1173 return status;
1176 /****************************************************************
1177 ****************************************************************/
1179 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1180 const char *machine_name,
1181 const char *dc_name,
1182 const bool use_kerberos)
1184 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1185 struct cli_state *cli = NULL;
1186 struct rpc_pipe_client *pipe_hnd = NULL;
1187 struct rpc_pipe_client *netlogon_pipe = NULL;
1188 NTSTATUS status;
1189 char *machine_password = NULL;
1190 char *machine_account = NULL;
1191 int flags = 0;
1193 if (!dc_name) {
1194 return NT_STATUS_INVALID_PARAMETER;
1197 if (!secrets_init()) {
1198 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1201 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1202 NULL, NULL);
1203 if (!machine_password) {
1204 return NT_STATUS_NO_TRUST_LSA_SECRET;
1207 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1208 SAFE_FREE(machine_password);
1209 return NT_STATUS_NO_MEMORY;
1212 if (use_kerberos) {
1213 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1216 status = cli_full_connection(&cli, NULL,
1217 dc_name,
1218 NULL, 0,
1219 "IPC$", "IPC",
1220 machine_account,
1221 NULL,
1222 machine_password,
1223 flags,
1224 SMB_SIGNING_DEFAULT);
1225 free(machine_account);
1226 free(machine_password);
1228 if (!NT_STATUS_IS_OK(status)) {
1229 status = cli_full_connection(&cli, NULL,
1230 dc_name,
1231 NULL, 0,
1232 "IPC$", "IPC",
1234 NULL,
1237 SMB_SIGNING_DEFAULT);
1240 if (!NT_STATUS_IS_OK(status)) {
1241 return status;
1244 status = get_schannel_session_key(cli, netbios_domain_name,
1245 &neg_flags, &netlogon_pipe);
1246 if (!NT_STATUS_IS_OK(status)) {
1247 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1248 cli_shutdown(cli);
1249 return NT_STATUS_OK;
1252 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1253 "key from server %s for domain %s. Error was %s\n",
1254 smbXcli_conn_remote_name(cli->conn),
1255 netbios_domain_name, nt_errstr(status)));
1256 cli_shutdown(cli);
1257 return status;
1260 if (!lp_client_schannel()) {
1261 cli_shutdown(cli);
1262 return NT_STATUS_OK;
1265 status = cli_rpc_pipe_open_schannel_with_key(
1266 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
1267 DCERPC_AUTH_LEVEL_PRIVACY,
1268 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1270 cli_shutdown(cli);
1272 if (!NT_STATUS_IS_OK(status)) {
1273 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1274 "on netlogon pipe to server %s for domain %s. "
1275 "Error was %s\n",
1276 smbXcli_conn_remote_name(cli->conn),
1277 netbios_domain_name, nt_errstr(status)));
1278 return status;
1281 return NT_STATUS_OK;
1284 /****************************************************************
1285 ****************************************************************/
1287 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1288 struct libnet_JoinCtx *r)
1290 NTSTATUS status;
1292 status = libnet_join_ok(r->out.netbios_domain_name,
1293 r->in.machine_name,
1294 r->in.dc_name,
1295 r->in.use_kerberos);
1296 if (!NT_STATUS_IS_OK(status)) {
1297 libnet_join_set_error_string(mem_ctx, r,
1298 "failed to verify domain membership after joining: %s",
1299 get_friendly_nt_error_msg(status));
1300 return WERR_SETUP_NOT_JOINED;
1303 return WERR_OK;
1306 /****************************************************************
1307 ****************************************************************/
1309 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1310 struct libnet_UnjoinCtx *r)
1312 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1313 return false;
1316 if (!secrets_delete_domain_sid(lp_workgroup())) {
1317 return false;
1320 return true;
1323 /****************************************************************
1324 ****************************************************************/
1326 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1327 struct libnet_UnjoinCtx *r)
1329 struct cli_state *cli = NULL;
1330 struct rpc_pipe_client *pipe_hnd = NULL;
1331 struct policy_handle sam_pol, domain_pol, user_pol;
1332 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1333 char *acct_name;
1334 uint32_t user_rid;
1335 struct lsa_String lsa_acct_name;
1336 struct samr_Ids user_rids;
1337 struct samr_Ids name_types;
1338 union samr_UserInfo *info = NULL;
1339 struct dcerpc_binding_handle *b = NULL;
1341 ZERO_STRUCT(sam_pol);
1342 ZERO_STRUCT(domain_pol);
1343 ZERO_STRUCT(user_pol);
1345 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1346 r->in.admin_account,
1347 r->in.admin_password,
1348 r->in.use_kerberos,
1349 &cli);
1350 if (!NT_STATUS_IS_OK(status)) {
1351 goto done;
1354 /* Open the domain */
1356 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1357 &pipe_hnd);
1358 if (!NT_STATUS_IS_OK(status)) {
1359 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1360 nt_errstr(status)));
1361 goto done;
1364 b = pipe_hnd->binding_handle;
1366 status = dcerpc_samr_Connect2(b, mem_ctx,
1367 pipe_hnd->desthost,
1368 SEC_FLAG_MAXIMUM_ALLOWED,
1369 &sam_pol,
1370 &result);
1371 if (!NT_STATUS_IS_OK(status)) {
1372 goto done;
1374 if (!NT_STATUS_IS_OK(result)) {
1375 status = result;
1376 goto done;
1379 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1380 &sam_pol,
1381 SEC_FLAG_MAXIMUM_ALLOWED,
1382 r->in.domain_sid,
1383 &domain_pol,
1384 &result);
1385 if (!NT_STATUS_IS_OK(status)) {
1386 goto done;
1388 if (!NT_STATUS_IS_OK(result)) {
1389 status = result;
1390 goto done;
1393 /* Create domain user */
1395 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1396 if (!strlower_m(acct_name)) {
1397 status = NT_STATUS_INVALID_PARAMETER;
1398 goto done;
1401 init_lsa_String(&lsa_acct_name, acct_name);
1403 status = dcerpc_samr_LookupNames(b, mem_ctx,
1404 &domain_pol,
1406 &lsa_acct_name,
1407 &user_rids,
1408 &name_types,
1409 &result);
1411 if (!NT_STATUS_IS_OK(status)) {
1412 goto done;
1414 if (!NT_STATUS_IS_OK(result)) {
1415 status = result;
1416 goto done;
1418 if (user_rids.count != 1) {
1419 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1420 goto done;
1422 if (name_types.count != 1) {
1423 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1424 goto done;
1427 if (name_types.ids[0] != SID_NAME_USER) {
1428 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1429 name_types.ids[0]));
1430 status = NT_STATUS_INVALID_WORKSTATION;
1431 goto done;
1434 user_rid = user_rids.ids[0];
1436 /* Open handle on user */
1438 status = dcerpc_samr_OpenUser(b, mem_ctx,
1439 &domain_pol,
1440 SEC_FLAG_MAXIMUM_ALLOWED,
1441 user_rid,
1442 &user_pol,
1443 &result);
1444 if (!NT_STATUS_IS_OK(status)) {
1445 goto done;
1447 if (!NT_STATUS_IS_OK(result)) {
1448 status = result;
1449 goto done;
1452 /* Get user info */
1454 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1455 &user_pol,
1457 &info,
1458 &result);
1459 if (!NT_STATUS_IS_OK(status)) {
1460 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1461 goto done;
1463 if (!NT_STATUS_IS_OK(result)) {
1464 status = result;
1465 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1466 goto done;
1469 /* now disable and setuser info */
1471 info->info16.acct_flags |= ACB_DISABLED;
1473 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1474 &user_pol,
1476 info,
1477 &result);
1478 if (!NT_STATUS_IS_OK(status)) {
1479 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1480 goto done;
1482 if (!NT_STATUS_IS_OK(result)) {
1483 status = result;
1484 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1485 goto done;
1487 status = result;
1488 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1490 done:
1491 if (pipe_hnd && b) {
1492 if (is_valid_policy_hnd(&domain_pol)) {
1493 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1495 if (is_valid_policy_hnd(&sam_pol)) {
1496 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1498 TALLOC_FREE(pipe_hnd);
1501 if (cli) {
1502 cli_shutdown(cli);
1505 return status;
1508 /****************************************************************
1509 ****************************************************************/
1511 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1513 WERROR werr = WERR_OK;
1514 sbcErr err;
1515 struct smbconf_ctx *ctx;
1517 err = smbconf_init_reg(r, &ctx, NULL);
1518 if (!SBC_ERROR_IS_OK(err)) {
1519 werr = WERR_NO_SUCH_SERVICE;
1520 goto done;
1523 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1525 err = smbconf_set_global_parameter(ctx, "security", "user");
1526 if (!SBC_ERROR_IS_OK(err)) {
1527 werr = WERR_NO_SUCH_SERVICE;
1528 goto done;
1531 err = smbconf_set_global_parameter(ctx, "workgroup",
1532 r->in.domain_name);
1533 if (!SBC_ERROR_IS_OK(err)) {
1534 werr = WERR_NO_SUCH_SERVICE;
1535 goto done;
1538 smbconf_delete_global_parameter(ctx, "realm");
1539 goto done;
1542 err = smbconf_set_global_parameter(ctx, "security", "domain");
1543 if (!SBC_ERROR_IS_OK(err)) {
1544 werr = WERR_NO_SUCH_SERVICE;
1545 goto done;
1548 err = smbconf_set_global_parameter(ctx, "workgroup",
1549 r->out.netbios_domain_name);
1550 if (!SBC_ERROR_IS_OK(err)) {
1551 werr = WERR_NO_SUCH_SERVICE;
1552 goto done;
1555 if (r->out.domain_is_ad) {
1556 err = smbconf_set_global_parameter(ctx, "security", "ads");
1557 if (!SBC_ERROR_IS_OK(err)) {
1558 werr = WERR_NO_SUCH_SERVICE;
1559 goto done;
1562 err = smbconf_set_global_parameter(ctx, "realm",
1563 r->out.dns_domain_name);
1564 if (!SBC_ERROR_IS_OK(err)) {
1565 werr = WERR_NO_SUCH_SERVICE;
1566 goto done;
1570 done:
1571 smbconf_shutdown(ctx);
1572 return werr;
1575 /****************************************************************
1576 ****************************************************************/
1578 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1580 WERROR werr = WERR_OK;
1581 sbcErr err;
1582 struct smbconf_ctx *ctx;
1584 err = smbconf_init_reg(r, &ctx, NULL);
1585 if (!SBC_ERROR_IS_OK(err)) {
1586 werr = WERR_NO_SUCH_SERVICE;
1587 goto done;
1590 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1592 err = smbconf_set_global_parameter(ctx, "security", "user");
1593 if (!SBC_ERROR_IS_OK(err)) {
1594 werr = WERR_NO_SUCH_SERVICE;
1595 goto done;
1598 err = smbconf_delete_global_parameter(ctx, "workgroup");
1599 if (!SBC_ERROR_IS_OK(err)) {
1600 werr = WERR_NO_SUCH_SERVICE;
1601 goto done;
1604 smbconf_delete_global_parameter(ctx, "realm");
1607 done:
1608 smbconf_shutdown(ctx);
1609 return werr;
1612 /****************************************************************
1613 ****************************************************************/
1615 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1617 WERROR werr;
1619 if (!W_ERROR_IS_OK(r->out.result)) {
1620 return r->out.result;
1623 if (!r->in.modify_config) {
1624 return WERR_OK;
1627 werr = do_join_modify_vals_config(r);
1628 if (!W_ERROR_IS_OK(werr)) {
1629 return werr;
1632 lp_load_global(get_dyn_CONFIGFILE());
1634 r->out.modified_config = true;
1635 r->out.result = werr;
1637 return werr;
1640 /****************************************************************
1641 ****************************************************************/
1643 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1645 WERROR werr;
1647 if (!W_ERROR_IS_OK(r->out.result)) {
1648 return r->out.result;
1651 if (!r->in.modify_config) {
1652 return WERR_OK;
1655 werr = do_unjoin_modify_vals_config(r);
1656 if (!W_ERROR_IS_OK(werr)) {
1657 return werr;
1660 lp_load_global(get_dyn_CONFIGFILE());
1662 r->out.modified_config = true;
1663 r->out.result = werr;
1665 return werr;
1668 /****************************************************************
1669 ****************************************************************/
1671 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1672 const char *domain_str,
1673 const char **domain_p,
1674 const char **dc_p)
1676 char *domain = NULL;
1677 char *dc = NULL;
1678 const char *p = NULL;
1680 if (!domain_str || !domain_p || !dc_p) {
1681 return false;
1684 p = strchr_m(domain_str, '\\');
1686 if (p != NULL) {
1687 domain = talloc_strndup(mem_ctx, domain_str,
1688 PTR_DIFF(p, domain_str));
1689 dc = talloc_strdup(mem_ctx, p+1);
1690 if (!dc) {
1691 return false;
1693 } else {
1694 domain = talloc_strdup(mem_ctx, domain_str);
1695 dc = NULL;
1697 if (!domain) {
1698 return false;
1701 *domain_p = domain;
1703 if (!*dc_p && dc) {
1704 *dc_p = dc;
1707 return true;
1710 /****************************************************************
1711 ****************************************************************/
1713 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1714 struct libnet_JoinCtx *r)
1716 if (!r->in.domain_name) {
1717 libnet_join_set_error_string(mem_ctx, r,
1718 "No domain name defined");
1719 return WERR_INVALID_PARAM;
1722 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1723 &r->in.domain_name,
1724 &r->in.dc_name)) {
1725 libnet_join_set_error_string(mem_ctx, r,
1726 "Failed to parse domain name");
1727 return WERR_INVALID_PARAM;
1730 if (IS_DC) {
1731 return WERR_SETUP_DOMAIN_CONTROLLER;
1734 if (!secrets_init()) {
1735 libnet_join_set_error_string(mem_ctx, r,
1736 "Unable to open secrets database");
1737 return WERR_CAN_NOT_COMPLETE;
1740 return WERR_OK;
1743 /****************************************************************
1744 ****************************************************************/
1746 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1748 NTSTATUS status;
1750 /* Try adding dom admins to builtin\admins. Only log failures. */
1751 status = create_builtin_administrators(domain_sid);
1752 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1753 DEBUG(10,("Unable to auto-add domain administrators to "
1754 "BUILTIN\\Administrators during join because "
1755 "winbindd must be running.\n"));
1756 } else if (!NT_STATUS_IS_OK(status)) {
1757 DEBUG(5, ("Failed to auto-add domain administrators to "
1758 "BUILTIN\\Administrators during join: %s\n",
1759 nt_errstr(status)));
1762 /* Try adding dom users to builtin\users. Only log failures. */
1763 status = create_builtin_users(domain_sid);
1764 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1765 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1766 "during join because winbindd must be running.\n"));
1767 } else if (!NT_STATUS_IS_OK(status)) {
1768 DEBUG(5, ("Failed to auto-add domain administrators to "
1769 "BUILTIN\\Administrators during join: %s\n",
1770 nt_errstr(status)));
1774 /****************************************************************
1775 ****************************************************************/
1777 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1778 struct libnet_JoinCtx *r)
1780 WERROR werr;
1782 if (!W_ERROR_IS_OK(r->out.result)) {
1783 return r->out.result;
1786 werr = do_JoinConfig(r);
1787 if (!W_ERROR_IS_OK(werr)) {
1788 return werr;
1791 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1792 return WERR_OK;
1795 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1796 if (r->out.dns_domain_name) {
1797 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1800 #ifdef HAVE_ADS
1801 if (r->out.domain_is_ad &&
1802 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1803 ADS_STATUS ads_status;
1805 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1806 if (!ADS_ERR_OK(ads_status)) {
1807 return WERR_GENERAL_FAILURE;
1810 #endif /* HAVE_ADS */
1812 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1814 return WERR_OK;
1817 /****************************************************************
1818 ****************************************************************/
1820 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1822 if (r->in.ads) {
1823 ads_destroy(&r->in.ads);
1826 return 0;
1829 /****************************************************************
1830 ****************************************************************/
1832 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1834 if (r->in.ads) {
1835 ads_destroy(&r->in.ads);
1838 return 0;
1841 /****************************************************************
1842 ****************************************************************/
1844 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1845 struct libnet_JoinCtx **r)
1847 struct libnet_JoinCtx *ctx;
1849 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1850 if (!ctx) {
1851 return WERR_NOMEM;
1854 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1856 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1857 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1859 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1861 *r = ctx;
1863 return WERR_OK;
1866 /****************************************************************
1867 ****************************************************************/
1869 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1870 struct libnet_UnjoinCtx **r)
1872 struct libnet_UnjoinCtx *ctx;
1874 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1875 if (!ctx) {
1876 return WERR_NOMEM;
1879 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1881 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1882 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1884 *r = ctx;
1886 return WERR_OK;
1889 /****************************************************************
1890 ****************************************************************/
1892 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1893 struct libnet_JoinCtx *r)
1895 bool valid_security = false;
1896 bool valid_workgroup = false;
1897 bool valid_realm = false;
1899 /* check if configuration is already set correctly */
1901 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1903 switch (r->out.domain_is_ad) {
1904 case false:
1905 valid_security = (lp_security() == SEC_DOMAIN);
1906 if (valid_workgroup && valid_security) {
1907 /* nothing to be done */
1908 return WERR_OK;
1910 break;
1911 case true:
1912 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1913 switch (lp_security()) {
1914 case SEC_DOMAIN:
1915 case SEC_ADS:
1916 valid_security = true;
1919 if (valid_workgroup && valid_realm && valid_security) {
1920 /* nothing to be done */
1921 return WERR_OK;
1923 break;
1926 /* check if we are supposed to manipulate configuration */
1928 if (!r->in.modify_config) {
1930 char *wrong_conf = talloc_strdup(mem_ctx, "");
1932 if (!valid_workgroup) {
1933 wrong_conf = talloc_asprintf_append(wrong_conf,
1934 "\"workgroup\" set to '%s', should be '%s'",
1935 lp_workgroup(), r->out.netbios_domain_name);
1936 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1939 if (!valid_realm) {
1940 wrong_conf = talloc_asprintf_append(wrong_conf,
1941 "\"realm\" set to '%s', should be '%s'",
1942 lp_realm(), r->out.dns_domain_name);
1943 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1946 if (!valid_security) {
1947 const char *sec = NULL;
1948 switch (lp_security()) {
1949 case SEC_USER: sec = "user"; break;
1950 case SEC_DOMAIN: sec = "domain"; break;
1951 case SEC_ADS: sec = "ads"; break;
1953 wrong_conf = talloc_asprintf_append(wrong_conf,
1954 "\"security\" set to '%s', should be %s",
1955 sec, r->out.domain_is_ad ?
1956 "either 'domain' or 'ads'" : "'domain'");
1957 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1960 libnet_join_set_error_string(mem_ctx, r,
1961 "Invalid configuration (%s) and configuration modification "
1962 "was not requested", wrong_conf);
1963 return WERR_CAN_NOT_COMPLETE;
1966 /* check if we are able to manipulate configuration */
1968 if (!lp_config_backend_is_registry()) {
1969 libnet_join_set_error_string(mem_ctx, r,
1970 "Configuration manipulation requested but not "
1971 "supported by backend");
1972 return WERR_NOT_SUPPORTED;
1975 return WERR_OK;
1978 /****************************************************************
1979 ****************************************************************/
1981 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1982 struct libnet_JoinCtx *r)
1984 NTSTATUS status;
1985 WERROR werr;
1986 struct cli_state *cli = NULL;
1987 #ifdef HAVE_ADS
1988 ADS_STATUS ads_status;
1989 #endif /* HAVE_ADS */
1991 if (!r->in.dc_name) {
1992 struct netr_DsRGetDCNameInfo *info;
1993 const char *dc;
1994 status = dsgetdcname(mem_ctx,
1995 r->in.msg_ctx,
1996 r->in.domain_name,
1997 NULL,
1998 NULL,
1999 DS_FORCE_REDISCOVERY |
2000 DS_DIRECTORY_SERVICE_REQUIRED |
2001 DS_WRITABLE_REQUIRED |
2002 DS_RETURN_DNS_NAME,
2003 &info);
2004 if (!NT_STATUS_IS_OK(status)) {
2005 libnet_join_set_error_string(mem_ctx, r,
2006 "failed to find DC for domain %s",
2007 r->in.domain_name,
2008 get_friendly_nt_error_msg(status));
2009 return WERR_DCNOTFOUND;
2012 dc = strip_hostname(info->dc_unc);
2013 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2014 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2017 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2018 if (!NT_STATUS_IS_OK(status)) {
2019 libnet_join_set_error_string(mem_ctx, r,
2020 "failed to lookup DC info for domain '%s' over rpc: %s",
2021 r->in.domain_name, get_friendly_nt_error_msg(status));
2022 return ntstatus_to_werror(status);
2025 werr = libnet_join_check_config(mem_ctx, r);
2026 if (!W_ERROR_IS_OK(werr)) {
2027 goto done;
2030 #ifdef HAVE_ADS
2032 create_local_private_krb5_conf_for_domain(
2033 r->out.dns_domain_name, r->out.netbios_domain_name,
2034 NULL, smbXcli_conn_remote_sockaddr(cli->conn),
2035 smbXcli_conn_remote_name(cli->conn));
2037 if (r->out.domain_is_ad && r->in.account_ou &&
2038 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2040 ads_status = libnet_join_connect_ads(mem_ctx, r);
2041 if (!ADS_ERR_OK(ads_status)) {
2042 return WERR_DEFAULT_JOIN_REQUIRED;
2045 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2046 if (!ADS_ERR_OK(ads_status)) {
2047 libnet_join_set_error_string(mem_ctx, r,
2048 "failed to precreate account in ou %s: %s",
2049 r->in.account_ou,
2050 ads_errstr(ads_status));
2051 return WERR_DEFAULT_JOIN_REQUIRED;
2054 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2056 #endif /* HAVE_ADS */
2058 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2059 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2060 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2061 } else {
2062 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2064 if (!NT_STATUS_IS_OK(status)) {
2065 libnet_join_set_error_string(mem_ctx, r,
2066 "failed to join domain '%s' over rpc: %s",
2067 r->in.domain_name, get_friendly_nt_error_msg(status));
2068 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2069 return WERR_SETUP_ALREADY_JOINED;
2071 werr = ntstatus_to_werror(status);
2072 goto done;
2075 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2076 werr = WERR_SETUP_NOT_JOINED;
2077 goto done;
2080 werr = WERR_OK;
2082 done:
2083 if (cli) {
2084 cli_shutdown(cli);
2087 return werr;
2090 /****************************************************************
2091 ****************************************************************/
2093 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2094 struct libnet_JoinCtx *r)
2096 WERROR werr;
2097 struct libnet_UnjoinCtx *u = NULL;
2099 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2100 if (!W_ERROR_IS_OK(werr)) {
2101 return werr;
2104 u->in.debug = r->in.debug;
2105 u->in.dc_name = r->in.dc_name;
2106 u->in.domain_name = r->in.domain_name;
2107 u->in.admin_account = r->in.admin_account;
2108 u->in.admin_password = r->in.admin_password;
2109 u->in.modify_config = r->in.modify_config;
2110 u->in.use_kerberos = r->in.use_kerberos;
2111 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2112 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2114 werr = libnet_Unjoin(mem_ctx, u);
2115 TALLOC_FREE(u);
2117 return werr;
2120 /****************************************************************
2121 ****************************************************************/
2123 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2124 struct libnet_JoinCtx *r)
2126 WERROR werr;
2128 if (r->in.debug) {
2129 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2132 ZERO_STRUCT(r->out);
2134 werr = libnet_join_pre_processing(mem_ctx, r);
2135 if (!W_ERROR_IS_OK(werr)) {
2136 goto done;
2139 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2140 werr = libnet_DomainJoin(mem_ctx, r);
2141 if (!W_ERROR_IS_OK(werr)) {
2142 goto done;
2146 werr = libnet_join_post_processing(mem_ctx, r);
2147 if (!W_ERROR_IS_OK(werr)) {
2148 goto done;
2151 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2152 werr = libnet_join_post_verify(mem_ctx, r);
2153 if (!W_ERROR_IS_OK(werr)) {
2154 libnet_join_rollback(mem_ctx, r);
2158 done:
2159 r->out.result = werr;
2161 if (r->in.debug) {
2162 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2164 return werr;
2167 /****************************************************************
2168 ****************************************************************/
2170 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2171 struct libnet_UnjoinCtx *r)
2173 NTSTATUS status;
2175 if (!r->in.domain_sid) {
2176 struct dom_sid sid;
2177 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2178 libnet_unjoin_set_error_string(mem_ctx, r,
2179 "Unable to fetch domain sid: are we joined?");
2180 return WERR_SETUP_NOT_JOINED;
2182 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2183 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2186 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2187 !r->in.delete_machine_account) {
2188 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2189 return WERR_OK;
2192 if (!r->in.dc_name) {
2193 struct netr_DsRGetDCNameInfo *info;
2194 const char *dc;
2195 status = dsgetdcname(mem_ctx,
2196 r->in.msg_ctx,
2197 r->in.domain_name,
2198 NULL,
2199 NULL,
2200 DS_DIRECTORY_SERVICE_REQUIRED |
2201 DS_WRITABLE_REQUIRED |
2202 DS_RETURN_DNS_NAME,
2203 &info);
2204 if (!NT_STATUS_IS_OK(status)) {
2205 libnet_unjoin_set_error_string(mem_ctx, r,
2206 "failed to find DC for domain %s",
2207 r->in.domain_name,
2208 get_friendly_nt_error_msg(status));
2209 return WERR_DCNOTFOUND;
2212 dc = strip_hostname(info->dc_unc);
2213 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2214 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2217 #ifdef HAVE_ADS
2218 /* for net ads leave, try to delete the account. If it works,
2219 no sense in disabling. If it fails, we can still try to
2220 disable it. jmcd */
2222 if (r->in.delete_machine_account) {
2223 ADS_STATUS ads_status;
2224 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2225 if (ADS_ERR_OK(ads_status)) {
2226 /* dirty hack */
2227 r->out.dns_domain_name =
2228 talloc_strdup(mem_ctx,
2229 r->in.ads->server.realm);
2230 ads_status =
2231 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2233 if (!ADS_ERR_OK(ads_status)) {
2234 libnet_unjoin_set_error_string(mem_ctx, r,
2235 "failed to remove machine account from AD: %s",
2236 ads_errstr(ads_status));
2237 } else {
2238 r->out.deleted_machine_account = true;
2239 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2240 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2241 return WERR_OK;
2244 #endif /* HAVE_ADS */
2246 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2247 "disable". */
2248 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2249 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2250 if (!NT_STATUS_IS_OK(status)) {
2251 libnet_unjoin_set_error_string(mem_ctx, r,
2252 "failed to disable machine account via rpc: %s",
2253 get_friendly_nt_error_msg(status));
2254 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2255 return WERR_SETUP_NOT_JOINED;
2257 return ntstatus_to_werror(status);
2260 r->out.disabled_machine_account = true;
2263 /* If disable succeeded or was not requested at all, we
2264 should be getting rid of our end of things */
2266 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2268 return WERR_OK;
2271 /****************************************************************
2272 ****************************************************************/
2274 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2275 struct libnet_UnjoinCtx *r)
2277 if (!r->in.domain_name) {
2278 libnet_unjoin_set_error_string(mem_ctx, r,
2279 "No domain name defined");
2280 return WERR_INVALID_PARAM;
2283 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2284 &r->in.domain_name,
2285 &r->in.dc_name)) {
2286 libnet_unjoin_set_error_string(mem_ctx, r,
2287 "Failed to parse domain name");
2288 return WERR_INVALID_PARAM;
2291 if (IS_DC) {
2292 return WERR_SETUP_DOMAIN_CONTROLLER;
2295 if (!secrets_init()) {
2296 libnet_unjoin_set_error_string(mem_ctx, r,
2297 "Unable to open secrets database");
2298 return WERR_CAN_NOT_COMPLETE;
2301 return WERR_OK;
2304 /****************************************************************
2305 ****************************************************************/
2307 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2308 struct libnet_UnjoinCtx *r)
2310 saf_delete(r->out.netbios_domain_name);
2311 saf_delete(r->out.dns_domain_name);
2313 return libnet_unjoin_config(r);
2316 /****************************************************************
2317 ****************************************************************/
2319 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2320 struct libnet_UnjoinCtx *r)
2322 WERROR werr;
2324 if (r->in.debug) {
2325 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2328 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2329 if (!W_ERROR_IS_OK(werr)) {
2330 goto done;
2333 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2334 werr = libnet_DomainUnjoin(mem_ctx, r);
2335 if (!W_ERROR_IS_OK(werr)) {
2336 libnet_unjoin_config(r);
2337 goto done;
2341 werr = libnet_unjoin_post_processing(mem_ctx, r);
2342 if (!W_ERROR_IS_OK(werr)) {
2343 goto done;
2346 done:
2347 r->out.result = werr;
2349 if (r->in.debug) {
2350 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2353 return werr;