Merge tag 'samba-4.1.3' into v4-1-test
[Samba.git] / source3 / libnet / libnet_join.c
blob14183853bdcd1b2310f03819e3459d47c1d67d68
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 const char *realm = r->out.dns_domain_name;
454 /* in case we are about to generate a keytab during the join
455 * make sure the default upn we create is usable with kinit -k.
456 * gd */
458 if (USE_KERBEROS_KEYTAB) {
459 realm = talloc_strdup_upper(mem_ctx,
460 r->out.dns_domain_name);
463 if (!realm) {
464 return ADS_ERROR(LDAP_NO_MEMORY);
467 r->in.upn = talloc_asprintf(mem_ctx,
468 "host/%s@%s",
469 r->in.machine_name,
470 realm);
471 if (!r->in.upn) {
472 return ADS_ERROR(LDAP_NO_MEMORY);
476 /* now do the mods */
478 mods = ads_init_mods(mem_ctx);
479 if (!mods) {
480 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
483 /* fields of primary importance */
485 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
486 if (!ADS_ERR_OK(status)) {
487 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
490 return ads_gen_mod(r->in.ads, r->out.dn, mods);
494 /****************************************************************
495 ****************************************************************/
497 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
498 struct libnet_JoinCtx *r)
500 ADS_STATUS status;
501 ADS_MODLIST mods;
502 char *os_sp = NULL;
504 if (!r->in.os_name || !r->in.os_version ) {
505 return ADS_SUCCESS;
508 /* Find our DN */
510 status = libnet_join_find_machine_acct(mem_ctx, r);
511 if (!ADS_ERR_OK(status)) {
512 return status;
515 /* now do the mods */
517 mods = ads_init_mods(mem_ctx);
518 if (!mods) {
519 return ADS_ERROR(LDAP_NO_MEMORY);
522 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
523 if (!os_sp) {
524 return ADS_ERROR(LDAP_NO_MEMORY);
527 /* fields of primary importance */
529 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
530 r->in.os_name);
531 if (!ADS_ERR_OK(status)) {
532 return status;
535 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
536 r->in.os_version);
537 if (!ADS_ERR_OK(status)) {
538 return status;
541 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
542 os_sp);
543 if (!ADS_ERR_OK(status)) {
544 return status;
547 return ads_gen_mod(r->in.ads, r->out.dn, mods);
550 /****************************************************************
551 ****************************************************************/
553 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
554 struct libnet_JoinCtx *r)
556 if (!USE_SYSTEM_KEYTAB) {
557 return true;
560 if (ads_keytab_create_default(r->in.ads) != 0) {
561 return false;
564 return true;
567 /****************************************************************
568 ****************************************************************/
570 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
571 struct libnet_JoinCtx *r)
573 uint32_t domain_func;
574 ADS_STATUS status;
575 const char *salt = NULL;
576 char *std_salt = NULL;
578 status = ads_domain_func_level(r->in.ads, &domain_func);
579 if (!ADS_ERR_OK(status)) {
580 libnet_join_set_error_string(mem_ctx, r,
581 "failed to determine domain functional level: %s",
582 ads_errstr(status));
583 return false;
586 /* go ahead and setup the default salt */
588 std_salt = kerberos_standard_des_salt();
589 if (!std_salt) {
590 libnet_join_set_error_string(mem_ctx, r,
591 "failed to obtain standard DES salt");
592 return false;
595 salt = talloc_strdup(mem_ctx, std_salt);
596 if (!salt) {
597 return false;
600 SAFE_FREE(std_salt);
602 /* if it's a Windows functional domain, we have to look for the UPN */
604 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
605 char *upn;
607 upn = ads_get_upn(r->in.ads, mem_ctx,
608 r->in.machine_name);
609 if (upn) {
610 salt = talloc_strdup(mem_ctx, upn);
611 if (!salt) {
612 return false;
617 return kerberos_secrets_store_des_salt(salt);
620 /****************************************************************
621 ****************************************************************/
623 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
624 struct libnet_JoinCtx *r)
626 ADS_STATUS status;
628 if (!r->in.ads) {
629 status = libnet_join_connect_ads(mem_ctx, r);
630 if (!ADS_ERR_OK(status)) {
631 return status;
635 status = libnet_join_set_machine_spn(mem_ctx, r);
636 if (!ADS_ERR_OK(status)) {
637 libnet_join_set_error_string(mem_ctx, r,
638 "failed to set machine spn: %s",
639 ads_errstr(status));
640 return status;
643 status = libnet_join_set_os_attributes(mem_ctx, r);
644 if (!ADS_ERR_OK(status)) {
645 libnet_join_set_error_string(mem_ctx, r,
646 "failed to set machine os attributes: %s",
647 ads_errstr(status));
648 return status;
651 status = libnet_join_set_machine_upn(mem_ctx, r);
652 if (!ADS_ERR_OK(status)) {
653 libnet_join_set_error_string(mem_ctx, r,
654 "failed to set machine upn: %s",
655 ads_errstr(status));
656 return status;
659 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
660 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
663 if (!libnet_join_create_keytab(mem_ctx, r)) {
664 libnet_join_set_error_string(mem_ctx, r,
665 "failed to create kerberos keytab");
666 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
669 return ADS_SUCCESS;
671 #endif /* HAVE_ADS */
673 /****************************************************************
674 Store the machine password and domain SID
675 ****************************************************************/
677 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
678 struct libnet_JoinCtx *r)
680 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
681 r->out.domain_sid))
683 DEBUG(1,("Failed to save domain sid\n"));
684 return false;
687 if (!secrets_store_machine_password(r->in.machine_password,
688 r->out.netbios_domain_name,
689 r->in.secure_channel_type))
691 DEBUG(1,("Failed to save machine password\n"));
692 return false;
695 return true;
698 /****************************************************************
699 Connect dc's IPC$ share
700 ****************************************************************/
702 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
703 const char *user,
704 const char *pass,
705 bool use_kerberos,
706 struct cli_state **cli)
708 int flags = 0;
710 if (use_kerberos) {
711 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
714 if (use_kerberos && pass) {
715 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
718 return cli_full_connection(cli, NULL,
720 NULL, 0,
721 "IPC$", "IPC",
722 user,
723 NULL,
724 pass,
725 flags,
726 SMB_SIGNING_DEFAULT);
729 /****************************************************************
730 Lookup domain dc's info
731 ****************************************************************/
733 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
734 struct libnet_JoinCtx *r,
735 struct cli_state **cli)
737 struct rpc_pipe_client *pipe_hnd = NULL;
738 struct policy_handle lsa_pol;
739 NTSTATUS status, result;
740 union lsa_PolicyInformation *info = NULL;
741 struct dcerpc_binding_handle *b;
743 status = libnet_join_connect_dc_ipc(r->in.dc_name,
744 r->in.admin_account,
745 r->in.admin_password,
746 r->in.use_kerberos,
747 cli);
748 if (!NT_STATUS_IS_OK(status)) {
749 goto done;
752 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
753 &pipe_hnd);
754 if (!NT_STATUS_IS_OK(status)) {
755 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
756 nt_errstr(status)));
757 goto done;
760 b = pipe_hnd->binding_handle;
762 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
763 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
764 if (!NT_STATUS_IS_OK(status)) {
765 goto done;
768 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
769 &lsa_pol,
770 LSA_POLICY_INFO_DNS,
771 &info,
772 &result);
773 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
774 r->out.domain_is_ad = true;
775 r->out.netbios_domain_name = info->dns.name.string;
776 r->out.dns_domain_name = info->dns.dns_domain.string;
777 r->out.forest_name = info->dns.dns_forest.string;
778 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
779 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
782 if (!NT_STATUS_IS_OK(status)) {
783 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
784 &lsa_pol,
785 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
786 &info,
787 &result);
788 if (!NT_STATUS_IS_OK(status)) {
789 goto done;
791 if (!NT_STATUS_IS_OK(result)) {
792 status = result;
793 goto done;
796 r->out.netbios_domain_name = info->account_domain.name.string;
797 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
798 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
801 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
802 TALLOC_FREE(pipe_hnd);
804 done:
805 return status;
808 /****************************************************************
809 Do the domain join unsecure
810 ****************************************************************/
812 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
813 struct libnet_JoinCtx *r,
814 struct cli_state *cli)
816 struct rpc_pipe_client *pipe_hnd = NULL;
817 unsigned char orig_trust_passwd_hash[16];
818 unsigned char new_trust_passwd_hash[16];
819 fstring trust_passwd;
820 NTSTATUS status;
822 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
823 &pipe_hnd);
824 if (!NT_STATUS_IS_OK(status)) {
825 return status;
828 if (!r->in.machine_password) {
829 r->in.machine_password = generate_random_password(mem_ctx,
830 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
831 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
832 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
835 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
837 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
838 fstrcpy(trust_passwd, r->in.admin_password);
839 if (!strlower_m(trust_passwd)) {
840 return NT_STATUS_INVALID_PARAMETER;
844 * Machine names can be 15 characters, but the max length on
845 * a password is 14. --jerry
848 trust_passwd[14] = '\0';
850 E_md4hash(trust_passwd, orig_trust_passwd_hash);
852 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
853 r->in.machine_name,
854 orig_trust_passwd_hash,
855 r->in.machine_password,
856 new_trust_passwd_hash,
857 r->in.secure_channel_type);
859 return status;
862 /****************************************************************
863 Do the domain join
864 ****************************************************************/
866 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
867 struct libnet_JoinCtx *r,
868 struct cli_state *cli)
870 struct rpc_pipe_client *pipe_hnd = NULL;
871 struct policy_handle sam_pol, domain_pol, user_pol;
872 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
873 char *acct_name;
874 struct lsa_String lsa_acct_name;
875 uint32_t user_rid;
876 uint32_t acct_flags = ACB_WSTRUST;
877 struct samr_Ids user_rids;
878 struct samr_Ids name_types;
879 union samr_UserInfo user_info;
880 struct dcerpc_binding_handle *b = NULL;
881 unsigned int old_timeout = 0;
883 DATA_BLOB session_key = data_blob_null;
884 struct samr_CryptPassword crypt_pwd;
885 struct samr_CryptPasswordEx crypt_pwd_ex;
887 ZERO_STRUCT(sam_pol);
888 ZERO_STRUCT(domain_pol);
889 ZERO_STRUCT(user_pol);
891 switch (r->in.secure_channel_type) {
892 case SEC_CHAN_WKSTA:
893 acct_flags = ACB_WSTRUST;
894 break;
895 case SEC_CHAN_BDC:
896 acct_flags = ACB_SVRTRUST;
897 break;
898 default:
899 return NT_STATUS_INVALID_PARAMETER;
902 if (!r->in.machine_password) {
903 r->in.machine_password = generate_random_password(mem_ctx,
904 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH,
905 DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
906 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
909 /* Open the domain */
911 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
912 &pipe_hnd);
913 if (!NT_STATUS_IS_OK(status)) {
914 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
915 nt_errstr(status)));
916 goto done;
919 b = pipe_hnd->binding_handle;
921 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
922 if (!NT_STATUS_IS_OK(status)) {
923 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
924 nt_errstr(status)));
925 goto done;
928 status = dcerpc_samr_Connect2(b, mem_ctx,
929 pipe_hnd->desthost,
930 SAMR_ACCESS_ENUM_DOMAINS
931 | SAMR_ACCESS_LOOKUP_DOMAIN,
932 &sam_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 status = dcerpc_samr_OpenDomain(b, mem_ctx,
943 &sam_pol,
944 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
945 | SAMR_DOMAIN_ACCESS_CREATE_USER
946 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
947 r->out.domain_sid,
948 &domain_pol,
949 &result);
950 if (!NT_STATUS_IS_OK(status)) {
951 goto done;
953 if (!NT_STATUS_IS_OK(result)) {
954 status = result;
955 goto done;
958 /* Create domain user */
960 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
961 if (!strlower_m(acct_name)) {
962 status = NT_STATUS_INVALID_PARAMETER;
963 goto done;
966 init_lsa_String(&lsa_acct_name, acct_name);
968 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
969 uint32_t access_desired =
970 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
971 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
972 SAMR_USER_ACCESS_SET_PASSWORD |
973 SAMR_USER_ACCESS_GET_ATTRIBUTES |
974 SAMR_USER_ACCESS_SET_ATTRIBUTES;
975 uint32_t access_granted = 0;
977 DEBUG(10,("Creating account with desired access mask: %d\n",
978 access_desired));
980 status = dcerpc_samr_CreateUser2(b, mem_ctx,
981 &domain_pol,
982 &lsa_acct_name,
983 acct_flags,
984 access_desired,
985 &user_pol,
986 &access_granted,
987 &user_rid,
988 &result);
989 if (!NT_STATUS_IS_OK(status)) {
990 goto done;
993 status = result;
994 if (!NT_STATUS_IS_OK(status) &&
995 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
997 DEBUG(10,("Creation of workstation account failed: %s\n",
998 nt_errstr(status)));
1000 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1001 username/password combo but the user does not have
1002 administrator access. */
1004 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1005 libnet_join_set_error_string(mem_ctx, r,
1006 "User specified does not have "
1007 "administrator privileges");
1010 goto done;
1013 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1014 if (!(r->in.join_flags &
1015 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1016 goto done;
1020 /* We *must* do this.... don't ask... */
1022 if (NT_STATUS_IS_OK(status)) {
1023 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1027 status = dcerpc_samr_LookupNames(b, mem_ctx,
1028 &domain_pol,
1030 &lsa_acct_name,
1031 &user_rids,
1032 &name_types,
1033 &result);
1034 if (!NT_STATUS_IS_OK(status)) {
1035 goto done;
1037 if (!NT_STATUS_IS_OK(result)) {
1038 status = result;
1039 goto done;
1041 if (user_rids.count != 1) {
1042 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1043 goto done;
1045 if (name_types.count != 1) {
1046 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1047 goto done;
1050 if (name_types.ids[0] != SID_NAME_USER) {
1051 DEBUG(0,("%s is not a user account (type=%d)\n",
1052 acct_name, name_types.ids[0]));
1053 status = NT_STATUS_INVALID_WORKSTATION;
1054 goto done;
1057 user_rid = user_rids.ids[0];
1059 /* Open handle on user */
1061 status = dcerpc_samr_OpenUser(b, mem_ctx,
1062 &domain_pol,
1063 SEC_FLAG_MAXIMUM_ALLOWED,
1064 user_rid,
1065 &user_pol,
1066 &result);
1067 if (!NT_STATUS_IS_OK(status)) {
1068 goto done;
1070 if (!NT_STATUS_IS_OK(result)) {
1071 status = result;
1072 goto done;
1075 /* Fill in the additional account flags now */
1077 acct_flags |= ACB_PWNOEXP;
1079 /* Set account flags on machine account */
1080 ZERO_STRUCT(user_info.info16);
1081 user_info.info16.acct_flags = acct_flags;
1083 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1084 &user_pol,
1086 &user_info,
1087 &result);
1088 if (!NT_STATUS_IS_OK(status)) {
1089 dcerpc_samr_DeleteUser(b, mem_ctx,
1090 &user_pol,
1091 &result);
1093 libnet_join_set_error_string(mem_ctx, r,
1094 "Failed to set account flags for machine account (%s)\n",
1095 nt_errstr(status));
1096 goto done;
1099 if (!NT_STATUS_IS_OK(result)) {
1100 status = result;
1102 dcerpc_samr_DeleteUser(b, mem_ctx,
1103 &user_pol,
1104 &result);
1106 libnet_join_set_error_string(mem_ctx, r,
1107 "Failed to set account flags for machine account (%s)\n",
1108 nt_errstr(status));
1109 goto done;
1112 /* Set password on machine account - first try level 26 */
1115 * increase the timeout as password filter modules on the DC
1116 * might delay the operation for a significant amount of time
1118 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1120 init_samr_CryptPasswordEx(r->in.machine_password,
1121 &session_key,
1122 &crypt_pwd_ex);
1124 user_info.info26.password = crypt_pwd_ex;
1125 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1127 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1128 &user_pol,
1130 &user_info,
1131 &result);
1133 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1135 /* retry with level 24 */
1137 init_samr_CryptPassword(r->in.machine_password,
1138 &session_key,
1139 &crypt_pwd);
1141 user_info.info24.password = crypt_pwd;
1142 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1144 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1145 &user_pol,
1147 &user_info,
1148 &result);
1151 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1153 if (!NT_STATUS_IS_OK(status)) {
1155 dcerpc_samr_DeleteUser(b, mem_ctx,
1156 &user_pol,
1157 &result);
1159 libnet_join_set_error_string(mem_ctx, r,
1160 "Failed to set password for machine account (%s)\n",
1161 nt_errstr(status));
1162 goto done;
1164 if (!NT_STATUS_IS_OK(result)) {
1165 status = result;
1167 dcerpc_samr_DeleteUser(b, mem_ctx,
1168 &user_pol,
1169 &result);
1171 libnet_join_set_error_string(mem_ctx, r,
1172 "Failed to set password for machine account (%s)\n",
1173 nt_errstr(status));
1174 goto done;
1177 status = NT_STATUS_OK;
1179 done:
1180 if (!pipe_hnd) {
1181 return status;
1184 data_blob_clear_free(&session_key);
1186 if (is_valid_policy_hnd(&sam_pol)) {
1187 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1189 if (is_valid_policy_hnd(&domain_pol)) {
1190 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1192 if (is_valid_policy_hnd(&user_pol)) {
1193 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1195 TALLOC_FREE(pipe_hnd);
1197 return status;
1200 /****************************************************************
1201 ****************************************************************/
1203 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1204 const char *machine_name,
1205 const char *dc_name,
1206 const bool use_kerberos)
1208 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1209 struct cli_state *cli = NULL;
1210 struct rpc_pipe_client *pipe_hnd = NULL;
1211 struct rpc_pipe_client *netlogon_pipe = NULL;
1212 NTSTATUS status;
1213 char *machine_password = NULL;
1214 char *machine_account = NULL;
1215 int flags = 0;
1217 if (!dc_name) {
1218 return NT_STATUS_INVALID_PARAMETER;
1221 if (!secrets_init()) {
1222 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1225 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1226 NULL, NULL);
1227 if (!machine_password) {
1228 return NT_STATUS_NO_TRUST_LSA_SECRET;
1231 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1232 SAFE_FREE(machine_password);
1233 return NT_STATUS_NO_MEMORY;
1236 if (use_kerberos) {
1237 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1240 status = cli_full_connection(&cli, NULL,
1241 dc_name,
1242 NULL, 0,
1243 "IPC$", "IPC",
1244 machine_account,
1245 NULL,
1246 machine_password,
1247 flags,
1248 SMB_SIGNING_DEFAULT);
1249 free(machine_account);
1250 free(machine_password);
1252 if (!NT_STATUS_IS_OK(status)) {
1253 status = cli_full_connection(&cli, NULL,
1254 dc_name,
1255 NULL, 0,
1256 "IPC$", "IPC",
1258 NULL,
1261 SMB_SIGNING_DEFAULT);
1264 if (!NT_STATUS_IS_OK(status)) {
1265 return status;
1268 status = get_schannel_session_key(cli, netbios_domain_name,
1269 &neg_flags, &netlogon_pipe);
1270 if (!NT_STATUS_IS_OK(status)) {
1271 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1272 cli_shutdown(cli);
1273 return NT_STATUS_OK;
1276 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1277 "key from server %s for domain %s. Error was %s\n",
1278 smbXcli_conn_remote_name(cli->conn),
1279 netbios_domain_name, nt_errstr(status)));
1280 cli_shutdown(cli);
1281 return status;
1284 if (!lp_client_schannel()) {
1285 cli_shutdown(cli);
1286 return NT_STATUS_OK;
1289 status = cli_rpc_pipe_open_schannel_with_key(
1290 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
1291 DCERPC_AUTH_LEVEL_PRIVACY,
1292 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1294 cli_shutdown(cli);
1296 if (!NT_STATUS_IS_OK(status)) {
1297 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1298 "on netlogon pipe to server %s for domain %s. "
1299 "Error was %s\n",
1300 smbXcli_conn_remote_name(cli->conn),
1301 netbios_domain_name, nt_errstr(status)));
1302 return status;
1305 return NT_STATUS_OK;
1308 /****************************************************************
1309 ****************************************************************/
1311 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1312 struct libnet_JoinCtx *r)
1314 NTSTATUS status;
1316 status = libnet_join_ok(r->out.netbios_domain_name,
1317 r->in.machine_name,
1318 r->in.dc_name,
1319 r->in.use_kerberos);
1320 if (!NT_STATUS_IS_OK(status)) {
1321 libnet_join_set_error_string(mem_ctx, r,
1322 "failed to verify domain membership after joining: %s",
1323 get_friendly_nt_error_msg(status));
1324 return WERR_SETUP_NOT_JOINED;
1327 return WERR_OK;
1330 /****************************************************************
1331 ****************************************************************/
1333 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1334 struct libnet_UnjoinCtx *r)
1336 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1337 return false;
1340 if (!secrets_delete_domain_sid(lp_workgroup())) {
1341 return false;
1344 return true;
1347 /****************************************************************
1348 ****************************************************************/
1350 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1351 struct libnet_UnjoinCtx *r)
1353 struct cli_state *cli = NULL;
1354 struct rpc_pipe_client *pipe_hnd = NULL;
1355 struct policy_handle sam_pol, domain_pol, user_pol;
1356 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1357 char *acct_name;
1358 uint32_t user_rid;
1359 struct lsa_String lsa_acct_name;
1360 struct samr_Ids user_rids;
1361 struct samr_Ids name_types;
1362 union samr_UserInfo *info = NULL;
1363 struct dcerpc_binding_handle *b = NULL;
1365 ZERO_STRUCT(sam_pol);
1366 ZERO_STRUCT(domain_pol);
1367 ZERO_STRUCT(user_pol);
1369 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1370 r->in.admin_account,
1371 r->in.admin_password,
1372 r->in.use_kerberos,
1373 &cli);
1374 if (!NT_STATUS_IS_OK(status)) {
1375 goto done;
1378 /* Open the domain */
1380 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1381 &pipe_hnd);
1382 if (!NT_STATUS_IS_OK(status)) {
1383 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1384 nt_errstr(status)));
1385 goto done;
1388 b = pipe_hnd->binding_handle;
1390 status = dcerpc_samr_Connect2(b, mem_ctx,
1391 pipe_hnd->desthost,
1392 SEC_FLAG_MAXIMUM_ALLOWED,
1393 &sam_pol,
1394 &result);
1395 if (!NT_STATUS_IS_OK(status)) {
1396 goto done;
1398 if (!NT_STATUS_IS_OK(result)) {
1399 status = result;
1400 goto done;
1403 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1404 &sam_pol,
1405 SEC_FLAG_MAXIMUM_ALLOWED,
1406 r->in.domain_sid,
1407 &domain_pol,
1408 &result);
1409 if (!NT_STATUS_IS_OK(status)) {
1410 goto done;
1412 if (!NT_STATUS_IS_OK(result)) {
1413 status = result;
1414 goto done;
1417 /* Create domain user */
1419 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1420 if (!strlower_m(acct_name)) {
1421 status = NT_STATUS_INVALID_PARAMETER;
1422 goto done;
1425 init_lsa_String(&lsa_acct_name, acct_name);
1427 status = dcerpc_samr_LookupNames(b, mem_ctx,
1428 &domain_pol,
1430 &lsa_acct_name,
1431 &user_rids,
1432 &name_types,
1433 &result);
1435 if (!NT_STATUS_IS_OK(status)) {
1436 goto done;
1438 if (!NT_STATUS_IS_OK(result)) {
1439 status = result;
1440 goto done;
1442 if (user_rids.count != 1) {
1443 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1444 goto done;
1446 if (name_types.count != 1) {
1447 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1448 goto done;
1451 if (name_types.ids[0] != SID_NAME_USER) {
1452 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1453 name_types.ids[0]));
1454 status = NT_STATUS_INVALID_WORKSTATION;
1455 goto done;
1458 user_rid = user_rids.ids[0];
1460 /* Open handle on user */
1462 status = dcerpc_samr_OpenUser(b, mem_ctx,
1463 &domain_pol,
1464 SEC_FLAG_MAXIMUM_ALLOWED,
1465 user_rid,
1466 &user_pol,
1467 &result);
1468 if (!NT_STATUS_IS_OK(status)) {
1469 goto done;
1471 if (!NT_STATUS_IS_OK(result)) {
1472 status = result;
1473 goto done;
1476 /* Get user info */
1478 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1479 &user_pol,
1481 &info,
1482 &result);
1483 if (!NT_STATUS_IS_OK(status)) {
1484 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1485 goto done;
1487 if (!NT_STATUS_IS_OK(result)) {
1488 status = result;
1489 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1490 goto done;
1493 /* now disable and setuser info */
1495 info->info16.acct_flags |= ACB_DISABLED;
1497 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1498 &user_pol,
1500 info,
1501 &result);
1502 if (!NT_STATUS_IS_OK(status)) {
1503 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1504 goto done;
1506 if (!NT_STATUS_IS_OK(result)) {
1507 status = result;
1508 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1509 goto done;
1511 status = result;
1512 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1514 done:
1515 if (pipe_hnd && b) {
1516 if (is_valid_policy_hnd(&domain_pol)) {
1517 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1519 if (is_valid_policy_hnd(&sam_pol)) {
1520 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1522 TALLOC_FREE(pipe_hnd);
1525 if (cli) {
1526 cli_shutdown(cli);
1529 return status;
1532 /****************************************************************
1533 ****************************************************************/
1535 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1537 WERROR werr = WERR_OK;
1538 sbcErr err;
1539 struct smbconf_ctx *ctx;
1541 err = smbconf_init_reg(r, &ctx, NULL);
1542 if (!SBC_ERROR_IS_OK(err)) {
1543 werr = WERR_NO_SUCH_SERVICE;
1544 goto done;
1547 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1549 err = smbconf_set_global_parameter(ctx, "security", "user");
1550 if (!SBC_ERROR_IS_OK(err)) {
1551 werr = WERR_NO_SUCH_SERVICE;
1552 goto done;
1555 err = smbconf_set_global_parameter(ctx, "workgroup",
1556 r->in.domain_name);
1557 if (!SBC_ERROR_IS_OK(err)) {
1558 werr = WERR_NO_SUCH_SERVICE;
1559 goto done;
1562 smbconf_delete_global_parameter(ctx, "realm");
1563 goto done;
1566 err = smbconf_set_global_parameter(ctx, "security", "domain");
1567 if (!SBC_ERROR_IS_OK(err)) {
1568 werr = WERR_NO_SUCH_SERVICE;
1569 goto done;
1572 err = smbconf_set_global_parameter(ctx, "workgroup",
1573 r->out.netbios_domain_name);
1574 if (!SBC_ERROR_IS_OK(err)) {
1575 werr = WERR_NO_SUCH_SERVICE;
1576 goto done;
1579 if (r->out.domain_is_ad) {
1580 err = smbconf_set_global_parameter(ctx, "security", "ads");
1581 if (!SBC_ERROR_IS_OK(err)) {
1582 werr = WERR_NO_SUCH_SERVICE;
1583 goto done;
1586 err = smbconf_set_global_parameter(ctx, "realm",
1587 r->out.dns_domain_name);
1588 if (!SBC_ERROR_IS_OK(err)) {
1589 werr = WERR_NO_SUCH_SERVICE;
1590 goto done;
1594 done:
1595 smbconf_shutdown(ctx);
1596 return werr;
1599 /****************************************************************
1600 ****************************************************************/
1602 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1604 WERROR werr = WERR_OK;
1605 sbcErr err;
1606 struct smbconf_ctx *ctx;
1608 err = smbconf_init_reg(r, &ctx, NULL);
1609 if (!SBC_ERROR_IS_OK(err)) {
1610 werr = WERR_NO_SUCH_SERVICE;
1611 goto done;
1614 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1616 err = smbconf_set_global_parameter(ctx, "security", "user");
1617 if (!SBC_ERROR_IS_OK(err)) {
1618 werr = WERR_NO_SUCH_SERVICE;
1619 goto done;
1622 err = smbconf_delete_global_parameter(ctx, "workgroup");
1623 if (!SBC_ERROR_IS_OK(err)) {
1624 werr = WERR_NO_SUCH_SERVICE;
1625 goto done;
1628 smbconf_delete_global_parameter(ctx, "realm");
1631 done:
1632 smbconf_shutdown(ctx);
1633 return werr;
1636 /****************************************************************
1637 ****************************************************************/
1639 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1641 WERROR werr;
1643 if (!W_ERROR_IS_OK(r->out.result)) {
1644 return r->out.result;
1647 if (!r->in.modify_config) {
1648 return WERR_OK;
1651 werr = do_join_modify_vals_config(r);
1652 if (!W_ERROR_IS_OK(werr)) {
1653 return werr;
1656 lp_load_global(get_dyn_CONFIGFILE());
1658 r->out.modified_config = true;
1659 r->out.result = werr;
1661 return werr;
1664 /****************************************************************
1665 ****************************************************************/
1667 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1669 WERROR werr;
1671 if (!W_ERROR_IS_OK(r->out.result)) {
1672 return r->out.result;
1675 if (!r->in.modify_config) {
1676 return WERR_OK;
1679 werr = do_unjoin_modify_vals_config(r);
1680 if (!W_ERROR_IS_OK(werr)) {
1681 return werr;
1684 lp_load_global(get_dyn_CONFIGFILE());
1686 r->out.modified_config = true;
1687 r->out.result = werr;
1689 return werr;
1692 /****************************************************************
1693 ****************************************************************/
1695 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1696 const char *domain_str,
1697 const char **domain_p,
1698 const char **dc_p)
1700 char *domain = NULL;
1701 char *dc = NULL;
1702 const char *p = NULL;
1704 if (!domain_str || !domain_p || !dc_p) {
1705 return false;
1708 p = strchr_m(domain_str, '\\');
1710 if (p != NULL) {
1711 domain = talloc_strndup(mem_ctx, domain_str,
1712 PTR_DIFF(p, domain_str));
1713 dc = talloc_strdup(mem_ctx, p+1);
1714 if (!dc) {
1715 return false;
1717 } else {
1718 domain = talloc_strdup(mem_ctx, domain_str);
1719 dc = NULL;
1721 if (!domain) {
1722 return false;
1725 *domain_p = domain;
1727 if (!*dc_p && dc) {
1728 *dc_p = dc;
1731 return true;
1734 /****************************************************************
1735 ****************************************************************/
1737 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1738 struct libnet_JoinCtx *r)
1740 if (!r->in.domain_name) {
1741 libnet_join_set_error_string(mem_ctx, r,
1742 "No domain name defined");
1743 return WERR_INVALID_PARAM;
1746 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1747 &r->in.domain_name,
1748 &r->in.dc_name)) {
1749 libnet_join_set_error_string(mem_ctx, r,
1750 "Failed to parse domain name");
1751 return WERR_INVALID_PARAM;
1754 if (IS_DC) {
1755 return WERR_SETUP_DOMAIN_CONTROLLER;
1758 if (!secrets_init()) {
1759 libnet_join_set_error_string(mem_ctx, r,
1760 "Unable to open secrets database");
1761 return WERR_CAN_NOT_COMPLETE;
1764 return WERR_OK;
1767 /****************************************************************
1768 ****************************************************************/
1770 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1772 NTSTATUS status;
1774 /* Try adding dom admins to builtin\admins. Only log failures. */
1775 status = create_builtin_administrators(domain_sid);
1776 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1777 DEBUG(10,("Unable to auto-add domain administrators to "
1778 "BUILTIN\\Administrators during join because "
1779 "winbindd must be running.\n"));
1780 } else if (!NT_STATUS_IS_OK(status)) {
1781 DEBUG(5, ("Failed to auto-add domain administrators to "
1782 "BUILTIN\\Administrators during join: %s\n",
1783 nt_errstr(status)));
1786 /* Try adding dom users to builtin\users. Only log failures. */
1787 status = create_builtin_users(domain_sid);
1788 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1789 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1790 "during join because winbindd must be running.\n"));
1791 } else if (!NT_STATUS_IS_OK(status)) {
1792 DEBUG(5, ("Failed to auto-add domain administrators to "
1793 "BUILTIN\\Administrators during join: %s\n",
1794 nt_errstr(status)));
1798 /****************************************************************
1799 ****************************************************************/
1801 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1802 struct libnet_JoinCtx *r)
1804 WERROR werr;
1806 if (!W_ERROR_IS_OK(r->out.result)) {
1807 return r->out.result;
1810 werr = do_JoinConfig(r);
1811 if (!W_ERROR_IS_OK(werr)) {
1812 return werr;
1815 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1816 return WERR_OK;
1819 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1820 if (r->out.dns_domain_name) {
1821 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1824 #ifdef HAVE_ADS
1825 if (r->out.domain_is_ad &&
1826 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1827 ADS_STATUS ads_status;
1829 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1830 if (!ADS_ERR_OK(ads_status)) {
1831 return WERR_GENERAL_FAILURE;
1834 #endif /* HAVE_ADS */
1836 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1838 return WERR_OK;
1841 /****************************************************************
1842 ****************************************************************/
1844 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1846 if (r->in.ads) {
1847 ads_destroy(&r->in.ads);
1850 return 0;
1853 /****************************************************************
1854 ****************************************************************/
1856 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1858 if (r->in.ads) {
1859 ads_destroy(&r->in.ads);
1862 return 0;
1865 /****************************************************************
1866 ****************************************************************/
1868 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1869 struct libnet_JoinCtx **r)
1871 struct libnet_JoinCtx *ctx;
1873 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1874 if (!ctx) {
1875 return WERR_NOMEM;
1878 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1880 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1881 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1883 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1885 *r = ctx;
1887 return WERR_OK;
1890 /****************************************************************
1891 ****************************************************************/
1893 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1894 struct libnet_UnjoinCtx **r)
1896 struct libnet_UnjoinCtx *ctx;
1898 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1899 if (!ctx) {
1900 return WERR_NOMEM;
1903 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1905 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1906 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1908 *r = ctx;
1910 return WERR_OK;
1913 /****************************************************************
1914 ****************************************************************/
1916 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1917 struct libnet_JoinCtx *r)
1919 bool valid_security = false;
1920 bool valid_workgroup = false;
1921 bool valid_realm = false;
1923 /* check if configuration is already set correctly */
1925 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1927 switch (r->out.domain_is_ad) {
1928 case false:
1929 valid_security = (lp_security() == SEC_DOMAIN);
1930 if (valid_workgroup && valid_security) {
1931 /* nothing to be done */
1932 return WERR_OK;
1934 break;
1935 case true:
1936 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1937 switch (lp_security()) {
1938 case SEC_DOMAIN:
1939 case SEC_ADS:
1940 valid_security = true;
1943 if (valid_workgroup && valid_realm && valid_security) {
1944 /* nothing to be done */
1945 return WERR_OK;
1947 break;
1950 /* check if we are supposed to manipulate configuration */
1952 if (!r->in.modify_config) {
1954 char *wrong_conf = talloc_strdup(mem_ctx, "");
1956 if (!valid_workgroup) {
1957 wrong_conf = talloc_asprintf_append(wrong_conf,
1958 "\"workgroup\" set to '%s', should be '%s'",
1959 lp_workgroup(), r->out.netbios_domain_name);
1960 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1963 if (!valid_realm) {
1964 wrong_conf = talloc_asprintf_append(wrong_conf,
1965 "\"realm\" set to '%s', should be '%s'",
1966 lp_realm(), r->out.dns_domain_name);
1967 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1970 if (!valid_security) {
1971 const char *sec = NULL;
1972 switch (lp_security()) {
1973 case SEC_USER: sec = "user"; break;
1974 case SEC_DOMAIN: sec = "domain"; break;
1975 case SEC_ADS: sec = "ads"; break;
1977 wrong_conf = talloc_asprintf_append(wrong_conf,
1978 "\"security\" set to '%s', should be %s",
1979 sec, r->out.domain_is_ad ?
1980 "either 'domain' or 'ads'" : "'domain'");
1981 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1984 libnet_join_set_error_string(mem_ctx, r,
1985 "Invalid configuration (%s) and configuration modification "
1986 "was not requested", wrong_conf);
1987 return WERR_CAN_NOT_COMPLETE;
1990 /* check if we are able to manipulate configuration */
1992 if (!lp_config_backend_is_registry()) {
1993 libnet_join_set_error_string(mem_ctx, r,
1994 "Configuration manipulation requested but not "
1995 "supported by backend");
1996 return WERR_NOT_SUPPORTED;
1999 return WERR_OK;
2002 /****************************************************************
2003 ****************************************************************/
2005 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2006 struct libnet_JoinCtx *r)
2008 NTSTATUS status;
2009 WERROR werr;
2010 struct cli_state *cli = NULL;
2011 #ifdef HAVE_ADS
2012 ADS_STATUS ads_status;
2013 #endif /* HAVE_ADS */
2015 if (!r->in.dc_name) {
2016 struct netr_DsRGetDCNameInfo *info;
2017 const char *dc;
2018 status = dsgetdcname(mem_ctx,
2019 r->in.msg_ctx,
2020 r->in.domain_name,
2021 NULL,
2022 NULL,
2023 DS_FORCE_REDISCOVERY |
2024 DS_DIRECTORY_SERVICE_REQUIRED |
2025 DS_WRITABLE_REQUIRED |
2026 DS_RETURN_DNS_NAME,
2027 &info);
2028 if (!NT_STATUS_IS_OK(status)) {
2029 libnet_join_set_error_string(mem_ctx, r,
2030 "failed to find DC for domain %s",
2031 r->in.domain_name,
2032 get_friendly_nt_error_msg(status));
2033 return WERR_DCNOTFOUND;
2036 dc = strip_hostname(info->dc_unc);
2037 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2038 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2041 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2042 if (!NT_STATUS_IS_OK(status)) {
2043 libnet_join_set_error_string(mem_ctx, r,
2044 "failed to lookup DC info for domain '%s' over rpc: %s",
2045 r->in.domain_name, get_friendly_nt_error_msg(status));
2046 return ntstatus_to_werror(status);
2049 werr = libnet_join_check_config(mem_ctx, r);
2050 if (!W_ERROR_IS_OK(werr)) {
2051 goto done;
2054 #ifdef HAVE_ADS
2056 create_local_private_krb5_conf_for_domain(
2057 r->out.dns_domain_name, r->out.netbios_domain_name,
2058 NULL, smbXcli_conn_remote_sockaddr(cli->conn),
2059 smbXcli_conn_remote_name(cli->conn));
2061 if (r->out.domain_is_ad && r->in.account_ou &&
2062 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2064 ads_status = libnet_join_connect_ads(mem_ctx, r);
2065 if (!ADS_ERR_OK(ads_status)) {
2066 return WERR_DEFAULT_JOIN_REQUIRED;
2069 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2070 if (!ADS_ERR_OK(ads_status)) {
2071 libnet_join_set_error_string(mem_ctx, r,
2072 "failed to precreate account in ou %s: %s",
2073 r->in.account_ou,
2074 ads_errstr(ads_status));
2075 return WERR_DEFAULT_JOIN_REQUIRED;
2078 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2080 #endif /* HAVE_ADS */
2082 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2083 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2084 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2085 } else {
2086 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2088 if (!NT_STATUS_IS_OK(status)) {
2089 libnet_join_set_error_string(mem_ctx, r,
2090 "failed to join domain '%s' over rpc: %s",
2091 r->in.domain_name, get_friendly_nt_error_msg(status));
2092 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2093 return WERR_SETUP_ALREADY_JOINED;
2095 werr = ntstatus_to_werror(status);
2096 goto done;
2099 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2100 werr = WERR_SETUP_NOT_JOINED;
2101 goto done;
2104 werr = WERR_OK;
2106 done:
2107 if (cli) {
2108 cli_shutdown(cli);
2111 return werr;
2114 /****************************************************************
2115 ****************************************************************/
2117 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2118 struct libnet_JoinCtx *r)
2120 WERROR werr;
2121 struct libnet_UnjoinCtx *u = NULL;
2123 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2124 if (!W_ERROR_IS_OK(werr)) {
2125 return werr;
2128 u->in.debug = r->in.debug;
2129 u->in.dc_name = r->in.dc_name;
2130 u->in.domain_name = r->in.domain_name;
2131 u->in.admin_account = r->in.admin_account;
2132 u->in.admin_password = r->in.admin_password;
2133 u->in.modify_config = r->in.modify_config;
2134 u->in.use_kerberos = r->in.use_kerberos;
2135 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2136 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2138 werr = libnet_Unjoin(mem_ctx, u);
2139 TALLOC_FREE(u);
2141 return werr;
2144 /****************************************************************
2145 ****************************************************************/
2147 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2148 struct libnet_JoinCtx *r)
2150 WERROR werr;
2152 if (r->in.debug) {
2153 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2156 ZERO_STRUCT(r->out);
2158 werr = libnet_join_pre_processing(mem_ctx, r);
2159 if (!W_ERROR_IS_OK(werr)) {
2160 goto done;
2163 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2164 werr = libnet_DomainJoin(mem_ctx, r);
2165 if (!W_ERROR_IS_OK(werr)) {
2166 goto done;
2170 werr = libnet_join_post_processing(mem_ctx, r);
2171 if (!W_ERROR_IS_OK(werr)) {
2172 goto done;
2175 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2176 werr = libnet_join_post_verify(mem_ctx, r);
2177 if (!W_ERROR_IS_OK(werr)) {
2178 libnet_join_rollback(mem_ctx, r);
2182 done:
2183 r->out.result = werr;
2185 if (r->in.debug) {
2186 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2188 return werr;
2191 /****************************************************************
2192 ****************************************************************/
2194 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2195 struct libnet_UnjoinCtx *r)
2197 NTSTATUS status;
2199 if (!r->in.domain_sid) {
2200 struct dom_sid sid;
2201 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2202 libnet_unjoin_set_error_string(mem_ctx, r,
2203 "Unable to fetch domain sid: are we joined?");
2204 return WERR_SETUP_NOT_JOINED;
2206 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2207 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2210 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2211 !r->in.delete_machine_account) {
2212 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2213 return WERR_OK;
2216 if (!r->in.dc_name) {
2217 struct netr_DsRGetDCNameInfo *info;
2218 const char *dc;
2219 status = dsgetdcname(mem_ctx,
2220 r->in.msg_ctx,
2221 r->in.domain_name,
2222 NULL,
2223 NULL,
2224 DS_DIRECTORY_SERVICE_REQUIRED |
2225 DS_WRITABLE_REQUIRED |
2226 DS_RETURN_DNS_NAME,
2227 &info);
2228 if (!NT_STATUS_IS_OK(status)) {
2229 libnet_unjoin_set_error_string(mem_ctx, r,
2230 "failed to find DC for domain %s",
2231 r->in.domain_name,
2232 get_friendly_nt_error_msg(status));
2233 return WERR_DCNOTFOUND;
2236 dc = strip_hostname(info->dc_unc);
2237 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2238 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2241 #ifdef HAVE_ADS
2242 /* for net ads leave, try to delete the account. If it works,
2243 no sense in disabling. If it fails, we can still try to
2244 disable it. jmcd */
2246 if (r->in.delete_machine_account) {
2247 ADS_STATUS ads_status;
2248 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2249 if (ADS_ERR_OK(ads_status)) {
2250 /* dirty hack */
2251 r->out.dns_domain_name =
2252 talloc_strdup(mem_ctx,
2253 r->in.ads->server.realm);
2254 ads_status =
2255 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2257 if (!ADS_ERR_OK(ads_status)) {
2258 libnet_unjoin_set_error_string(mem_ctx, r,
2259 "failed to remove machine account from AD: %s",
2260 ads_errstr(ads_status));
2261 } else {
2262 r->out.deleted_machine_account = true;
2263 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2264 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2265 return WERR_OK;
2268 #endif /* HAVE_ADS */
2270 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2271 "disable". */
2272 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2273 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2274 if (!NT_STATUS_IS_OK(status)) {
2275 libnet_unjoin_set_error_string(mem_ctx, r,
2276 "failed to disable machine account via rpc: %s",
2277 get_friendly_nt_error_msg(status));
2278 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2279 return WERR_SETUP_NOT_JOINED;
2281 return ntstatus_to_werror(status);
2284 r->out.disabled_machine_account = true;
2287 /* If disable succeeded or was not requested at all, we
2288 should be getting rid of our end of things */
2290 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2292 return WERR_OK;
2295 /****************************************************************
2296 ****************************************************************/
2298 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2299 struct libnet_UnjoinCtx *r)
2301 if (!r->in.domain_name) {
2302 libnet_unjoin_set_error_string(mem_ctx, r,
2303 "No domain name defined");
2304 return WERR_INVALID_PARAM;
2307 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2308 &r->in.domain_name,
2309 &r->in.dc_name)) {
2310 libnet_unjoin_set_error_string(mem_ctx, r,
2311 "Failed to parse domain name");
2312 return WERR_INVALID_PARAM;
2315 if (IS_DC) {
2316 return WERR_SETUP_DOMAIN_CONTROLLER;
2319 if (!secrets_init()) {
2320 libnet_unjoin_set_error_string(mem_ctx, r,
2321 "Unable to open secrets database");
2322 return WERR_CAN_NOT_COMPLETE;
2325 return WERR_OK;
2328 /****************************************************************
2329 ****************************************************************/
2331 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2332 struct libnet_UnjoinCtx *r)
2334 saf_delete(r->out.netbios_domain_name);
2335 saf_delete(r->out.dns_domain_name);
2337 return libnet_unjoin_config(r);
2340 /****************************************************************
2341 ****************************************************************/
2343 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2344 struct libnet_UnjoinCtx *r)
2346 WERROR werr;
2348 if (r->in.debug) {
2349 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2352 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2353 if (!W_ERROR_IS_OK(werr)) {
2354 goto done;
2357 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2358 werr = libnet_DomainUnjoin(mem_ctx, r);
2359 if (!W_ERROR_IS_OK(werr)) {
2360 libnet_unjoin_config(r);
2361 goto done;
2365 werr = libnet_unjoin_post_processing(mem_ctx, r);
2366 if (!W_ERROR_IS_OK(werr)) {
2367 goto done;
2370 done:
2371 r->out.result = werr;
2373 if (r->in.debug) {
2374 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2377 return werr;