CVE-2020-25718 tests/krb5: Fix indentation
[Samba.git] / source3 / libnet / libnet_join.c
blob349386036065a05a200908082419362c650394cb
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 "libsmb/namequery.h"
24 #include "librpc/gen_ndr/ndr_libnet_join.h"
25 #include "libnet/libnet_join.h"
26 #include "libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/init_samr.h"
29 #include "../librpc/gen_ndr/ndr_lsa_c.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../librpc/gen_ndr/ndr_netlogon.h"
32 #include "rpc_client/cli_netlogon.h"
33 #include "lib/smbconf/smbconf.h"
34 #include "lib/smbconf/smbconf_reg.h"
35 #include "../libds/common/flags.h"
36 #include "secrets.h"
37 #include "rpc_client/init_lsa.h"
38 #include "rpc_client/cli_pipe.h"
39 #include "../libcli/security/security.h"
40 #include "passdb.h"
41 #include "libsmb/libsmb.h"
42 #include "../libcli/smb/smbXcli_base.h"
43 #include "lib/param/loadparm.h"
44 #include "libcli/auth/netlogon_creds_cli.h"
45 #include "auth/credentials/credentials.h"
46 #include "krb5_env.h"
47 #include "libsmb/dsgetdcname.h"
49 /****************************************************************
50 ****************************************************************/
52 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
53 do { \
54 char *str = NULL; \
55 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
56 DEBUG(1,("libnet_Join:\n%s", str)); \
57 TALLOC_FREE(str); \
58 } while (0)
60 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
61 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
62 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
63 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
65 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
66 do { \
67 char *str = NULL; \
68 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
69 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
70 TALLOC_FREE(str); \
71 } while (0)
73 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
74 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
75 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
76 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
78 /****************************************************************
79 ****************************************************************/
81 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
82 struct libnet_JoinCtx *r,
83 const char *format, ...)
84 PRINTF_ATTRIBUTE(3,4);
86 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
87 struct libnet_JoinCtx *r,
88 const char *format, ...)
90 va_list args;
92 if (r->out.error_string) {
93 return;
96 va_start(args, format);
97 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
98 va_end(args);
101 /****************************************************************
102 ****************************************************************/
104 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
105 struct libnet_UnjoinCtx *r,
106 const char *format, ...)
107 PRINTF_ATTRIBUTE(3,4);
109 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
110 struct libnet_UnjoinCtx *r,
111 const char *format, ...)
113 va_list args;
115 if (r->out.error_string) {
116 return;
119 va_start(args, format);
120 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
121 va_end(args);
124 #ifdef HAVE_ADS
126 /****************************************************************
127 ****************************************************************/
129 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
130 const char *netbios_domain_name,
131 const char *dc_name,
132 const char *user_name,
133 const char *password,
134 const char *ccname,
135 ADS_STRUCT **ads)
137 ADS_STATUS status;
138 ADS_STRUCT *my_ads = NULL;
139 char *cp;
141 my_ads = ads_init(dns_domain_name,
142 netbios_domain_name,
143 dc_name,
144 ADS_SASL_SEAL);
145 if (!my_ads) {
146 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
149 my_ads->auth.flags |= ADS_AUTH_ALLOW_NTLMSSP;
151 if (user_name) {
152 SAFE_FREE(my_ads->auth.user_name);
153 my_ads->auth.user_name = SMB_STRDUP(user_name);
154 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
155 *cp++ = '\0';
156 SAFE_FREE(my_ads->auth.realm);
157 my_ads->auth.realm = smb_xstrdup(cp);
158 if (!strupper_m(my_ads->auth.realm)) {
159 ads_destroy(&my_ads);
160 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
165 if (password) {
166 SAFE_FREE(my_ads->auth.password);
167 my_ads->auth.password = SMB_STRDUP(password);
170 if (ccname != NULL) {
171 SAFE_FREE(my_ads->auth.ccache_name);
172 my_ads->auth.ccache_name = SMB_STRDUP(ccname);
173 setenv(KRB5_ENV_CCNAME, my_ads->auth.ccache_name, 1);
176 status = ads_connect_user_creds(my_ads);
177 if (!ADS_ERR_OK(status)) {
178 ads_destroy(&my_ads);
179 return status;
182 *ads = my_ads;
183 return ADS_SUCCESS;
186 /****************************************************************
187 ****************************************************************/
189 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
190 struct libnet_JoinCtx *r,
191 bool use_machine_creds)
193 ADS_STATUS status;
194 const char *username;
195 const char *password;
196 const char *ccname = NULL;
198 if (use_machine_creds) {
199 if (r->in.machine_name == NULL ||
200 r->in.machine_password == NULL) {
201 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
203 username = talloc_asprintf(mem_ctx, "%s$",
204 r->in.machine_name);
205 if (username == NULL) {
206 return ADS_ERROR(LDAP_NO_MEMORY);
208 password = r->in.machine_password;
209 ccname = "MEMORY:libnet_join_machine_creds";
210 } else {
211 char *p = NULL;
213 username = r->in.admin_account;
215 p = strchr(r->in.admin_account, '@');
216 if (p == NULL) {
217 username = talloc_asprintf(mem_ctx, "%s@%s",
218 r->in.admin_account,
219 r->in.admin_domain);
221 if (username == NULL) {
222 return ADS_ERROR(LDAP_NO_MEMORY);
224 password = r->in.admin_password;
227 * when r->in.use_kerberos is set to allow "net ads join -k" we
228 * may not override the provided credential cache - gd
231 if (!r->in.use_kerberos) {
232 ccname = "MEMORY:libnet_join_user_creds";
236 status = libnet_connect_ads(r->out.dns_domain_name,
237 r->out.netbios_domain_name,
238 r->in.dc_name,
239 username,
240 password,
241 ccname,
242 &r->in.ads);
243 if (!ADS_ERR_OK(status)) {
244 libnet_join_set_error_string(mem_ctx, r,
245 "failed to connect to AD: %s",
246 ads_errstr(status));
247 return status;
250 if (!r->out.netbios_domain_name) {
251 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
252 r->in.ads->server.workgroup);
253 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
256 if (!r->out.dns_domain_name) {
257 r->out.dns_domain_name = talloc_strdup(mem_ctx,
258 r->in.ads->config.realm);
259 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
262 r->out.domain_is_ad = true;
264 return ADS_SUCCESS;
267 /****************************************************************
268 ****************************************************************/
270 static ADS_STATUS libnet_join_connect_ads_user(TALLOC_CTX *mem_ctx,
271 struct libnet_JoinCtx *r)
273 return libnet_join_connect_ads(mem_ctx, r, false);
276 /****************************************************************
277 ****************************************************************/
279 static ADS_STATUS libnet_join_connect_ads_machine(TALLOC_CTX *mem_ctx,
280 struct libnet_JoinCtx *r)
282 return libnet_join_connect_ads(mem_ctx, r, true);
285 /****************************************************************
286 ****************************************************************/
288 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
289 struct libnet_UnjoinCtx *r)
291 ADS_STATUS status;
293 status = libnet_connect_ads(r->in.domain_name,
294 r->in.domain_name,
295 r->in.dc_name,
296 r->in.admin_account,
297 r->in.admin_password,
298 NULL,
299 &r->in.ads);
300 if (!ADS_ERR_OK(status)) {
301 libnet_unjoin_set_error_string(mem_ctx, r,
302 "failed to connect to AD: %s",
303 ads_errstr(status));
306 return status;
309 /****************************************************************
310 join a domain using ADS (LDAP mods)
311 ****************************************************************/
313 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
314 struct libnet_JoinCtx *r)
316 ADS_STATUS status;
317 LDAPMessage *res = NULL;
318 const char *attrs[] = { "dn", NULL };
319 bool moved = false;
321 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
322 if (!ADS_ERR_OK(status)) {
323 return status;
326 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
327 if (!ADS_ERR_OK(status)) {
328 return status;
331 if (ads_count_replies(r->in.ads, res) != 1) {
332 ads_msgfree(r->in.ads, res);
333 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
336 ads_msgfree(r->in.ads, res);
338 /* Attempt to create the machine account and bail if this fails.
339 Assume that the admin wants exactly what they requested */
341 if (r->in.machine_password == NULL) {
342 r->in.machine_password =
343 trust_pw_new_value(mem_ctx,
344 r->in.secure_channel_type,
345 SEC_ADS);
346 if (r->in.machine_password == NULL) {
347 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
351 status = ads_create_machine_acct(r->in.ads,
352 r->in.machine_name,
353 r->in.machine_password,
354 r->in.account_ou,
355 r->in.desired_encryption_types,
356 r->out.dns_domain_name);
358 if (ADS_ERR_OK(status)) {
359 DBG_WARNING("Machine account successfully created\n");
360 return status;
361 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
362 (status.err.rc == LDAP_ALREADY_EXISTS)) {
363 status = ADS_SUCCESS;
366 if (!ADS_ERR_OK(status)) {
367 DBG_WARNING("Failed to create machine account\n");
368 return status;
371 status = ads_move_machine_acct(r->in.ads,
372 r->in.machine_name,
373 r->in.account_ou,
374 &moved);
375 if (!ADS_ERR_OK(status)) {
376 DEBUG(1,("failure to locate/move pre-existing "
377 "machine account\n"));
378 return status;
381 DEBUG(1,("The machine account %s the specified OU.\n",
382 moved ? "was moved into" : "already exists in"));
384 return status;
387 /****************************************************************
388 ****************************************************************/
390 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
391 struct libnet_UnjoinCtx *r)
393 ADS_STATUS status;
395 if (!r->in.ads) {
396 status = libnet_unjoin_connect_ads(mem_ctx, r);
397 if (!ADS_ERR_OK(status)) {
398 libnet_unjoin_set_error_string(mem_ctx, r,
399 "failed to connect to AD: %s",
400 ads_errstr(status));
401 return status;
405 status = ads_leave_realm(r->in.ads, r->in.machine_name);
406 if (!ADS_ERR_OK(status)) {
407 libnet_unjoin_set_error_string(mem_ctx, r,
408 "failed to leave realm: %s",
409 ads_errstr(status));
410 return status;
413 return ADS_SUCCESS;
416 /****************************************************************
417 ****************************************************************/
419 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
420 struct libnet_JoinCtx *r)
422 ADS_STATUS status;
423 LDAPMessage *res = NULL;
424 char *dn = NULL;
426 if (!r->in.machine_name) {
427 return ADS_ERROR(LDAP_NO_MEMORY);
430 status = ads_find_machine_acct(r->in.ads,
431 &res,
432 r->in.machine_name);
433 if (!ADS_ERR_OK(status)) {
434 return status;
437 if (ads_count_replies(r->in.ads, res) != 1) {
438 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
439 goto done;
442 dn = ads_get_dn(r->in.ads, mem_ctx, res);
443 if (!dn) {
444 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
445 goto done;
448 r->out.dn = talloc_strdup(mem_ctx, dn);
449 if (!r->out.dn) {
450 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
451 goto done;
454 if (!ads_pull_uint32(r->in.ads, res, "msDS-SupportedEncryptionTypes",
455 &r->out.set_encryption_types)) {
456 r->out.set_encryption_types = 0;
459 done:
460 ads_msgfree(r->in.ads, res);
461 TALLOC_FREE(dn);
463 return status;
466 static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
467 struct libnet_JoinCtx *r,
468 char ***spn_array,
469 size_t *num_spns)
471 ADS_STATUS status;
473 if (r->in.machine_name == NULL) {
474 return ADS_ERROR_SYSTEM(EINVAL);
477 status = ads_get_service_principal_names(mem_ctx,
478 r->in.ads,
479 r->in.machine_name,
480 spn_array,
481 num_spns);
483 return status;
486 static ADS_STATUS add_uniq_spn(TALLOC_CTX *mem_ctx, const char *spn,
487 const char ***array, size_t *num)
489 bool ok = ads_element_in_array(*array, *num, spn);
490 if (!ok) {
491 ok = add_string_to_array(mem_ctx, spn, array, num);
492 if (!ok) {
493 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
496 return ADS_SUCCESS;
499 /****************************************************************
500 Set a machines dNSHostName and servicePrincipalName attributes
501 ****************************************************************/
503 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
504 struct libnet_JoinCtx *r)
506 TALLOC_CTX *frame = talloc_stackframe();
507 ADS_STATUS status;
508 ADS_MODLIST mods;
509 fstring my_fqdn;
510 fstring my_alias;
511 const char **spn_array = NULL;
512 size_t num_spns = 0;
513 char *spn = NULL;
514 const char **netbios_aliases = NULL;
515 const char **addl_hostnames = NULL;
517 /* Find our DN */
519 status = libnet_join_find_machine_acct(mem_ctx, r);
520 if (!ADS_ERR_OK(status)) {
521 return status;
524 status = libnet_join_get_machine_spns(frame,
526 discard_const_p(char **, &spn_array),
527 &num_spns);
528 if (!ADS_ERR_OK(status)) {
529 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
532 /* Windows only creates HOST/shortname & HOST/fqdn. */
534 spn = talloc_asprintf(frame, "HOST/%s", r->in.machine_name);
535 if (spn == NULL) {
536 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
537 goto done;
539 if (!strupper_m(spn)) {
540 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
541 goto done;
544 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
545 if (!ADS_ERR_OK(status)) {
546 goto done;
549 if (r->in.dnshostname != NULL) {
550 fstr_sprintf(my_fqdn, "%s", r->in.dnshostname);
551 } else {
552 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
553 lp_dnsdomain());
556 if (!strlower_m(my_fqdn)) {
557 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
558 goto done;
561 spn = talloc_asprintf(frame, "HOST/%s", my_fqdn);
562 if (spn == NULL) {
563 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
564 goto done;
567 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
568 if (!ADS_ERR_OK(status)) {
569 goto done;
572 for (netbios_aliases = lp_netbios_aliases();
573 netbios_aliases != NULL && *netbios_aliases != NULL;
574 netbios_aliases++) {
576 * Add HOST/NETBIOSNAME
578 spn = talloc_asprintf(frame, "HOST/%s", *netbios_aliases);
579 if (spn == NULL) {
580 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
581 goto done;
583 if (!strupper_m(spn)) {
584 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
585 goto done;
588 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
589 if (!ADS_ERR_OK(status)) {
590 goto done;
594 * Add HOST/netbiosname.domainname
596 fstr_sprintf(my_alias, "%s.%s",
597 *netbios_aliases,
598 lp_dnsdomain());
600 spn = talloc_asprintf(frame, "HOST/%s", my_alias);
601 if (spn == NULL) {
602 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
603 goto done;
606 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
607 if (!ADS_ERR_OK(status)) {
608 goto done;
612 for (addl_hostnames = lp_additional_dns_hostnames();
613 addl_hostnames != NULL && *addl_hostnames != NULL;
614 addl_hostnames++) {
616 spn = talloc_asprintf(frame, "HOST/%s", *addl_hostnames);
617 if (spn == NULL) {
618 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
619 goto done;
622 status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
623 if (!ADS_ERR_OK(status)) {
624 goto done;
628 /* make sure to NULL terminate the array */
629 spn_array = talloc_realloc(frame, spn_array, const char *, num_spns + 1);
630 if (spn_array == NULL) {
631 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
632 goto done;
634 spn_array[num_spns] = NULL;
636 mods = ads_init_mods(mem_ctx);
637 if (!mods) {
638 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
639 goto done;
642 /* fields of primary importance */
644 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
645 if (!ADS_ERR_OK(status)) {
646 goto done;
649 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
650 spn_array);
651 if (!ADS_ERR_OK(status)) {
652 goto done;
655 addl_hostnames = lp_additional_dns_hostnames();
656 if (addl_hostnames != NULL && *addl_hostnames != NULL) {
657 status = ads_mod_strlist(mem_ctx, &mods,
658 "msDS-AdditionalDnsHostName",
659 addl_hostnames);
660 if (!ADS_ERR_OK(status)) {
661 goto done;
665 status = ads_gen_mod(r->in.ads, r->out.dn, mods);
667 done:
668 TALLOC_FREE(frame);
669 return status;
672 /****************************************************************
673 ****************************************************************/
675 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
676 struct libnet_JoinCtx *r)
678 ADS_STATUS status;
679 ADS_MODLIST mods;
681 if (!r->in.create_upn) {
682 return ADS_SUCCESS;
685 /* Find our DN */
687 status = libnet_join_find_machine_acct(mem_ctx, r);
688 if (!ADS_ERR_OK(status)) {
689 return status;
692 if (!r->in.upn) {
693 const char *realm = r->out.dns_domain_name;
695 /* in case we are about to generate a keytab during the join
696 * make sure the default upn we create is usable with kinit -k.
697 * gd */
699 if (USE_KERBEROS_KEYTAB) {
700 realm = talloc_strdup_upper(mem_ctx,
701 r->out.dns_domain_name);
704 if (!realm) {
705 return ADS_ERROR(LDAP_NO_MEMORY);
708 r->in.upn = talloc_asprintf(mem_ctx,
709 "host/%s@%s",
710 r->in.machine_name,
711 realm);
712 if (!r->in.upn) {
713 return ADS_ERROR(LDAP_NO_MEMORY);
717 /* now do the mods */
719 mods = ads_init_mods(mem_ctx);
720 if (!mods) {
721 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
724 /* fields of primary importance */
726 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
727 if (!ADS_ERR_OK(status)) {
728 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
731 return ads_gen_mod(r->in.ads, r->out.dn, mods);
735 /****************************************************************
736 ****************************************************************/
738 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
739 struct libnet_JoinCtx *r)
741 ADS_STATUS status;
742 ADS_MODLIST mods;
743 char *os_sp = NULL;
745 if (!r->in.os_name || !r->in.os_version ) {
746 return ADS_SUCCESS;
749 /* Find our DN */
751 status = libnet_join_find_machine_acct(mem_ctx, r);
752 if (!ADS_ERR_OK(status)) {
753 return status;
756 /* now do the mods */
758 mods = ads_init_mods(mem_ctx);
759 if (!mods) {
760 return ADS_ERROR(LDAP_NO_MEMORY);
763 if (r->in.os_servicepack) {
765 * if blank string then leave os_sp equal to NULL to force
766 * attribute delete (LDAP_MOD_DELETE)
768 if (!strequal(r->in.os_servicepack,"")) {
769 os_sp = talloc_strdup(mem_ctx, r->in.os_servicepack);
771 } else {
772 os_sp = talloc_asprintf(mem_ctx, "Samba %s",
773 samba_version_string());
775 if (!os_sp && !strequal(r->in.os_servicepack,"")) {
776 return ADS_ERROR(LDAP_NO_MEMORY);
779 /* fields of primary importance */
781 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
782 r->in.os_name);
783 if (!ADS_ERR_OK(status)) {
784 return status;
787 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
788 r->in.os_version);
789 if (!ADS_ERR_OK(status)) {
790 return status;
793 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
794 os_sp);
795 if (!ADS_ERR_OK(status)) {
796 return status;
799 return ads_gen_mod(r->in.ads, r->out.dn, mods);
802 /****************************************************************
803 ****************************************************************/
805 static ADS_STATUS libnet_join_set_etypes(TALLOC_CTX *mem_ctx,
806 struct libnet_JoinCtx *r)
808 ADS_STATUS status;
809 ADS_MODLIST mods;
810 const char *etype_list_str;
812 etype_list_str = talloc_asprintf(mem_ctx, "%d",
813 r->in.desired_encryption_types);
814 if (!etype_list_str) {
815 return ADS_ERROR(LDAP_NO_MEMORY);
818 /* Find our DN */
820 status = libnet_join_find_machine_acct(mem_ctx, r);
821 if (!ADS_ERR_OK(status)) {
822 return status;
825 if (r->in.desired_encryption_types == r->out.set_encryption_types) {
826 return ADS_SUCCESS;
829 /* now do the mods */
831 mods = ads_init_mods(mem_ctx);
832 if (!mods) {
833 return ADS_ERROR(LDAP_NO_MEMORY);
836 status = ads_mod_str(mem_ctx, &mods, "msDS-SupportedEncryptionTypes",
837 etype_list_str);
838 if (!ADS_ERR_OK(status)) {
839 return status;
842 status = ads_gen_mod(r->in.ads, r->out.dn, mods);
843 if (!ADS_ERR_OK(status)) {
844 return status;
847 r->out.set_encryption_types = r->in.desired_encryption_types;
849 return ADS_SUCCESS;
852 /****************************************************************
853 ****************************************************************/
855 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
856 struct libnet_JoinCtx *r)
858 if (!USE_SYSTEM_KEYTAB) {
859 return true;
862 if (ads_keytab_create_default(r->in.ads) != 0) {
863 return false;
866 return true;
869 /****************************************************************
870 ****************************************************************/
872 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
873 struct libnet_JoinCtx *r)
875 uint32_t domain_func;
876 ADS_STATUS status;
877 const char *salt = NULL;
878 char *std_salt = NULL;
880 status = ads_domain_func_level(r->in.ads, &domain_func);
881 if (!ADS_ERR_OK(status)) {
882 libnet_join_set_error_string(mem_ctx, r,
883 "failed to determine domain functional level: %s",
884 ads_errstr(status));
885 return false;
888 /* go ahead and setup the default salt */
890 std_salt = kerberos_standard_des_salt();
891 if (!std_salt) {
892 libnet_join_set_error_string(mem_ctx, r,
893 "failed to obtain standard DES salt");
894 return false;
897 salt = talloc_strdup(mem_ctx, std_salt);
898 if (!salt) {
899 return false;
902 SAFE_FREE(std_salt);
904 /* if it's a Windows functional domain, we have to look for the UPN */
906 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
907 char *upn;
909 upn = ads_get_upn(r->in.ads, mem_ctx,
910 r->in.machine_name);
911 if (upn) {
912 salt = talloc_strdup(mem_ctx, upn);
913 if (!salt) {
914 return false;
919 r->out.krb5_salt = salt;
920 return true;
923 /****************************************************************
924 ****************************************************************/
926 static ADS_STATUS libnet_join_post_processing_ads_modify(TALLOC_CTX *mem_ctx,
927 struct libnet_JoinCtx *r)
929 ADS_STATUS status;
930 bool need_etype_update = false;
932 if (!r->in.ads) {
933 status = libnet_join_connect_ads_user(mem_ctx, r);
934 if (!ADS_ERR_OK(status)) {
935 return status;
939 status = libnet_join_set_machine_spn(mem_ctx, r);
940 if (!ADS_ERR_OK(status)) {
941 libnet_join_set_error_string(mem_ctx, r,
942 "Failed to set machine spn: %s\n"
943 "Do you have sufficient permissions to create machine "
944 "accounts?",
945 ads_errstr(status));
946 return status;
949 status = libnet_join_set_os_attributes(mem_ctx, r);
950 if (!ADS_ERR_OK(status)) {
951 libnet_join_set_error_string(mem_ctx, r,
952 "failed to set machine os attributes: %s",
953 ads_errstr(status));
954 return status;
957 status = libnet_join_set_machine_upn(mem_ctx, r);
958 if (!ADS_ERR_OK(status)) {
959 libnet_join_set_error_string(mem_ctx, r,
960 "failed to set machine upn: %s",
961 ads_errstr(status));
962 return status;
965 status = libnet_join_find_machine_acct(mem_ctx, r);
966 if (!ADS_ERR_OK(status)) {
967 return status;
970 if (r->in.desired_encryption_types != r->out.set_encryption_types) {
971 uint32_t func_level = 0;
973 status = ads_domain_func_level(r->in.ads, &func_level);
974 if (!ADS_ERR_OK(status)) {
975 libnet_join_set_error_string(mem_ctx, r,
976 "failed to query domain controller functional level: %s",
977 ads_errstr(status));
978 return status;
981 if (func_level >= DS_DOMAIN_FUNCTION_2008) {
982 need_etype_update = true;
986 if (need_etype_update) {
988 * We need to reconnect as machine account in order
989 * to update msDS-SupportedEncryptionTypes reliable
992 if (r->in.ads->auth.ccache_name != NULL) {
993 ads_kdestroy(r->in.ads->auth.ccache_name);
994 r->in.ads->auth.ccache_name = NULL;
997 ads_destroy(&r->in.ads);
999 status = libnet_join_connect_ads_machine(mem_ctx, r);
1000 if (!ADS_ERR_OK(status)) {
1001 libnet_join_set_error_string(mem_ctx, r,
1002 "Failed to connect as machine account: %s",
1003 ads_errstr(status));
1004 return status;
1007 status = libnet_join_set_etypes(mem_ctx, r);
1008 if (!ADS_ERR_OK(status)) {
1009 libnet_join_set_error_string(mem_ctx, r,
1010 "failed to set machine kerberos encryption types: %s",
1011 ads_errstr(status));
1012 return status;
1016 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
1017 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1020 return ADS_SUCCESS;
1023 static ADS_STATUS libnet_join_post_processing_ads_sync(TALLOC_CTX *mem_ctx,
1024 struct libnet_JoinCtx *r)
1026 if (!libnet_join_create_keytab(mem_ctx, r)) {
1027 libnet_join_set_error_string(mem_ctx, r,
1028 "failed to create kerberos keytab");
1029 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1032 return ADS_SUCCESS;
1034 #endif /* HAVE_ADS */
1036 /****************************************************************
1037 Store the machine password and domain SID
1038 ****************************************************************/
1040 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
1041 struct libnet_JoinCtx *r)
1043 NTSTATUS status;
1045 status = secrets_store_JoinCtx(r);
1046 if (!NT_STATUS_IS_OK(status)) {
1047 DBG_ERR("secrets_store_JoinCtx() failed %s\n",
1048 nt_errstr(status));
1049 return false;
1052 return true;
1055 /****************************************************************
1056 Connect dc's IPC$ share
1057 ****************************************************************/
1059 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
1060 const char *user,
1061 const char *domain,
1062 const char *pass,
1063 bool use_kerberos,
1064 struct cli_state **cli)
1066 TALLOC_CTX *frame = talloc_stackframe();
1067 bool fallback_after_kerberos = false;
1068 bool use_ccache = false;
1069 bool pw_nt_hash = false;
1070 struct cli_credentials *creds = NULL;
1071 int flags = 0;
1072 NTSTATUS status;
1074 if (use_kerberos && pass) {
1075 fallback_after_kerberos = true;
1078 creds = cli_session_creds_init(frame,
1079 user,
1080 domain,
1081 NULL, /* realm (use default) */
1082 pass,
1083 use_kerberos,
1084 fallback_after_kerberos,
1085 use_ccache,
1086 pw_nt_hash);
1087 if (creds == NULL) {
1088 TALLOC_FREE(frame);
1089 return NT_STATUS_NO_MEMORY;
1092 status = cli_full_connection_creds(cli,
1093 NULL,
1095 NULL, 0,
1096 "IPC$", "IPC",
1097 creds,
1098 flags,
1099 SMB_SIGNING_IPC_DEFAULT);
1100 if (!NT_STATUS_IS_OK(status)) {
1101 TALLOC_FREE(frame);
1102 return status;
1105 TALLOC_FREE(frame);
1106 return NT_STATUS_OK;
1109 /****************************************************************
1110 Lookup domain dc's info
1111 ****************************************************************/
1113 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
1114 struct libnet_JoinCtx *r,
1115 struct cli_state **cli)
1117 struct rpc_pipe_client *pipe_hnd = NULL;
1118 struct policy_handle lsa_pol;
1119 NTSTATUS status, result;
1120 union lsa_PolicyInformation *info = NULL;
1121 struct dcerpc_binding_handle *b;
1122 const char *account = r->in.admin_account;
1123 const char *domain = r->in.admin_domain;
1124 const char *password = r->in.admin_password;
1125 bool use_kerberos = r->in.use_kerberos;
1127 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) {
1128 account = "";
1129 domain = "";
1130 password = NULL;
1131 use_kerberos = false;
1134 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1135 account,
1136 domain,
1137 password,
1138 use_kerberos,
1139 cli);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 goto done;
1144 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
1145 &pipe_hnd);
1146 if (!NT_STATUS_IS_OK(status)) {
1147 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
1148 nt_errstr(status)));
1149 goto done;
1152 b = pipe_hnd->binding_handle;
1154 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
1155 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1156 if (!NT_STATUS_IS_OK(status)) {
1157 goto done;
1160 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
1161 &lsa_pol,
1162 LSA_POLICY_INFO_DNS,
1163 &info,
1164 &result);
1165 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
1166 r->out.domain_is_ad = true;
1167 r->out.netbios_domain_name = info->dns.name.string;
1168 r->out.dns_domain_name = info->dns.dns_domain.string;
1169 r->out.forest_name = info->dns.dns_forest.string;
1170 r->out.domain_guid = info->dns.domain_guid;
1171 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
1172 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1175 if (!NT_STATUS_IS_OK(status)) {
1176 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
1177 &lsa_pol,
1178 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
1179 &info,
1180 &result);
1181 if (!NT_STATUS_IS_OK(status)) {
1182 goto done;
1184 if (!NT_STATUS_IS_OK(result)) {
1185 status = result;
1186 goto done;
1189 r->out.netbios_domain_name = info->account_domain.name.string;
1190 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
1191 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1194 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1195 TALLOC_FREE(pipe_hnd);
1197 done:
1198 return status;
1201 /****************************************************************
1202 Do the domain join unsecure
1203 ****************************************************************/
1205 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
1206 struct libnet_JoinCtx *r,
1207 struct cli_state *cli)
1209 TALLOC_CTX *frame = talloc_stackframe();
1210 struct rpc_pipe_client *authenticate_pipe = NULL;
1211 struct rpc_pipe_client *passwordset_pipe = NULL;
1212 struct cli_credentials *cli_creds;
1213 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1214 struct netlogon_creds_CredentialState *creds = NULL;
1215 uint32_t netlogon_flags = 0;
1216 size_t len = 0;
1217 bool ok;
1218 DATA_BLOB new_trust_blob = data_blob_null;
1219 NTSTATUS status;
1221 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
1222 &authenticate_pipe);
1223 if (!NT_STATUS_IS_OK(status)) {
1224 TALLOC_FREE(frame);
1225 return status;
1228 if (!r->in.machine_password) {
1229 int security = r->in.ads ? SEC_ADS : SEC_DOMAIN;
1231 r->in.machine_password = trust_pw_new_value(mem_ctx,
1232 r->in.secure_channel_type,
1233 security);
1234 if (r->in.machine_password == NULL) {
1235 TALLOC_FREE(frame);
1236 return NT_STATUS_NO_MEMORY;
1240 cli_creds = cli_credentials_init(talloc_tos());
1241 if (cli_creds == NULL) {
1242 TALLOC_FREE(frame);
1243 return NT_STATUS_NO_MEMORY;
1246 cli_credentials_set_username(cli_creds, r->out.account_name,
1247 CRED_SPECIFIED);
1248 cli_credentials_set_domain(cli_creds, r->in.domain_name,
1249 CRED_SPECIFIED);
1250 cli_credentials_set_realm(cli_creds, "", CRED_SPECIFIED);
1251 cli_credentials_set_secure_channel_type(cli_creds,
1252 r->in.secure_channel_type);
1254 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
1255 cli_credentials_set_password(cli_creds, r->in.admin_password,
1256 CRED_SPECIFIED);
1258 status = rpccli_create_netlogon_creds_ctx(
1259 cli_creds, authenticate_pipe->desthost, r->in.msg_ctx,
1260 frame, &netlogon_creds);
1261 if (!NT_STATUS_IS_OK(status)) {
1262 TALLOC_FREE(frame);
1263 return status;
1266 status = rpccli_setup_netlogon_creds(
1267 cli, NCACN_NP, netlogon_creds, true /* force_reauth */,
1268 cli_creds);
1269 if (!NT_STATUS_IS_OK(status)) {
1270 TALLOC_FREE(frame);
1271 return status;
1274 status = netlogon_creds_cli_get(netlogon_creds, frame, &creds);
1275 if (!NT_STATUS_IS_OK(status)) {
1276 TALLOC_FREE(frame);
1277 return status;
1280 netlogon_flags = creds->negotiate_flags;
1281 TALLOC_FREE(creds);
1283 if (netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC) {
1284 status = cli_rpc_pipe_open_schannel_with_creds(cli,
1285 &ndr_table_netlogon,
1286 NCACN_NP,
1287 netlogon_creds,
1288 &passwordset_pipe);
1289 if (!NT_STATUS_IS_OK(status)) {
1290 TALLOC_FREE(frame);
1291 return status;
1293 } else {
1294 passwordset_pipe = authenticate_pipe;
1297 len = strlen(r->in.machine_password);
1298 ok = convert_string_talloc(frame, CH_UNIX, CH_UTF16,
1299 r->in.machine_password, len,
1300 (void **)&new_trust_blob.data,
1301 &new_trust_blob.length);
1302 if (!ok) {
1303 status = NT_STATUS_UNMAPPABLE_CHARACTER;
1304 if (errno == ENOMEM) {
1305 status = NT_STATUS_NO_MEMORY;
1307 TALLOC_FREE(frame);
1308 return status;
1311 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
1312 passwordset_pipe->binding_handle,
1313 &new_trust_blob,
1314 NULL); /* new_version */
1315 if (!NT_STATUS_IS_OK(status)) {
1316 TALLOC_FREE(frame);
1317 return status;
1320 TALLOC_FREE(frame);
1321 return NT_STATUS_OK;
1324 /****************************************************************
1325 Do the domain join
1326 ****************************************************************/
1328 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
1329 struct libnet_JoinCtx *r,
1330 struct cli_state *cli)
1332 struct rpc_pipe_client *pipe_hnd = NULL;
1333 struct policy_handle sam_pol, domain_pol, user_pol;
1334 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1335 char *acct_name;
1336 struct lsa_String lsa_acct_name;
1337 uint32_t user_rid;
1338 uint32_t acct_flags = ACB_WSTRUST;
1339 struct samr_Ids user_rids;
1340 struct samr_Ids name_types;
1341 union samr_UserInfo user_info;
1342 struct dcerpc_binding_handle *b = NULL;
1343 unsigned int old_timeout = 0;
1345 DATA_BLOB session_key = data_blob_null;
1346 struct samr_CryptPassword crypt_pwd;
1347 struct samr_CryptPasswordEx crypt_pwd_ex;
1349 ZERO_STRUCT(sam_pol);
1350 ZERO_STRUCT(domain_pol);
1351 ZERO_STRUCT(user_pol);
1353 switch (r->in.secure_channel_type) {
1354 case SEC_CHAN_WKSTA:
1355 acct_flags = ACB_WSTRUST;
1356 break;
1357 case SEC_CHAN_BDC:
1358 acct_flags = ACB_SVRTRUST;
1359 break;
1360 default:
1361 return NT_STATUS_INVALID_PARAMETER;
1364 if (!r->in.machine_password) {
1365 int security = r->in.ads ? SEC_ADS : SEC_DOMAIN;
1367 r->in.machine_password = trust_pw_new_value(mem_ctx,
1368 r->in.secure_channel_type,
1369 security);
1370 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
1373 /* Open the domain */
1375 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1376 &pipe_hnd);
1377 if (!NT_STATUS_IS_OK(status)) {
1378 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1379 nt_errstr(status)));
1380 goto done;
1383 b = pipe_hnd->binding_handle;
1385 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1386 if (!NT_STATUS_IS_OK(status)) {
1387 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1388 nt_errstr(status)));
1389 goto done;
1392 status = dcerpc_samr_Connect2(b, mem_ctx,
1393 pipe_hnd->desthost,
1394 SAMR_ACCESS_ENUM_DOMAINS
1395 | SAMR_ACCESS_LOOKUP_DOMAIN,
1396 &sam_pol,
1397 &result);
1398 if (!NT_STATUS_IS_OK(status)) {
1399 goto done;
1401 if (!NT_STATUS_IS_OK(result)) {
1402 status = result;
1403 goto done;
1406 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1407 &sam_pol,
1408 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1409 | SAMR_DOMAIN_ACCESS_CREATE_USER
1410 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1411 r->out.domain_sid,
1412 &domain_pol,
1413 &result);
1414 if (!NT_STATUS_IS_OK(status)) {
1415 goto done;
1417 if (!NT_STATUS_IS_OK(result)) {
1418 status = result;
1419 goto done;
1422 /* Create domain user */
1424 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1425 if (!strlower_m(acct_name)) {
1426 status = NT_STATUS_INVALID_PARAMETER;
1427 goto done;
1430 init_lsa_String(&lsa_acct_name, acct_name);
1432 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1433 uint32_t access_desired =
1434 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1435 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1436 SAMR_USER_ACCESS_SET_PASSWORD |
1437 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1438 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1439 uint32_t access_granted = 0;
1441 DEBUG(10,("Creating account with desired access mask: %d\n",
1442 access_desired));
1444 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1445 &domain_pol,
1446 &lsa_acct_name,
1447 acct_flags,
1448 access_desired,
1449 &user_pol,
1450 &access_granted,
1451 &user_rid,
1452 &result);
1453 if (!NT_STATUS_IS_OK(status)) {
1454 goto done;
1457 status = result;
1458 if (!NT_STATUS_IS_OK(status) &&
1459 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1461 DEBUG(10,("Creation of workstation account failed: %s\n",
1462 nt_errstr(status)));
1464 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1465 username/password combo but the user does not have
1466 administrator access. */
1468 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1469 libnet_join_set_error_string(mem_ctx, r,
1470 "User specified does not have "
1471 "administrator privileges");
1474 goto done;
1477 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1478 if (!(r->in.join_flags &
1479 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1480 goto done;
1484 /* We *must* do this.... don't ask... */
1486 if (NT_STATUS_IS_OK(status)) {
1487 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1491 status = dcerpc_samr_LookupNames(b, mem_ctx,
1492 &domain_pol,
1494 &lsa_acct_name,
1495 &user_rids,
1496 &name_types,
1497 &result);
1498 if (!NT_STATUS_IS_OK(status)) {
1499 goto done;
1501 if (!NT_STATUS_IS_OK(result)) {
1502 status = result;
1503 goto done;
1505 if (user_rids.count != 1) {
1506 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1507 goto done;
1509 if (name_types.count != 1) {
1510 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1511 goto done;
1514 if (name_types.ids[0] != SID_NAME_USER) {
1515 DEBUG(0,("%s is not a user account (type=%d)\n",
1516 acct_name, name_types.ids[0]));
1517 status = NT_STATUS_INVALID_WORKSTATION;
1518 goto done;
1521 user_rid = user_rids.ids[0];
1523 /* Open handle on user */
1525 status = dcerpc_samr_OpenUser(b, mem_ctx,
1526 &domain_pol,
1527 SEC_FLAG_MAXIMUM_ALLOWED,
1528 user_rid,
1529 &user_pol,
1530 &result);
1531 if (!NT_STATUS_IS_OK(status)) {
1532 goto done;
1534 if (!NT_STATUS_IS_OK(result)) {
1535 status = result;
1536 goto done;
1539 /* Fill in the additional account flags now */
1541 acct_flags |= ACB_PWNOEXP;
1543 /* Set account flags on machine account */
1544 ZERO_STRUCT(user_info.info16);
1545 user_info.info16.acct_flags = acct_flags;
1547 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1548 &user_pol,
1549 UserControlInformation,
1550 &user_info,
1551 &result);
1552 if (!NT_STATUS_IS_OK(status)) {
1553 dcerpc_samr_DeleteUser(b, mem_ctx,
1554 &user_pol,
1555 &result);
1557 libnet_join_set_error_string(mem_ctx, r,
1558 "Failed to set account flags for machine account (%s)\n",
1559 nt_errstr(status));
1560 goto done;
1563 if (!NT_STATUS_IS_OK(result)) {
1564 status = result;
1566 dcerpc_samr_DeleteUser(b, mem_ctx,
1567 &user_pol,
1568 &result);
1570 libnet_join_set_error_string(mem_ctx, r,
1571 "Failed to set account flags for machine account (%s)\n",
1572 nt_errstr(status));
1573 goto done;
1576 /* Set password on machine account - first try level 26 */
1579 * increase the timeout as password filter modules on the DC
1580 * might delay the operation for a significant amount of time
1582 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1584 status = init_samr_CryptPasswordEx(r->in.machine_password,
1585 &session_key,
1586 &crypt_pwd_ex);
1587 if (!NT_STATUS_IS_OK(status)) {
1588 goto error;
1591 user_info.info26.password = crypt_pwd_ex;
1592 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1594 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1595 &user_pol,
1596 UserInternal5InformationNew,
1597 &user_info,
1598 &result);
1600 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1602 /* retry with level 24 */
1604 status = init_samr_CryptPassword(r->in.machine_password,
1605 &session_key,
1606 &crypt_pwd);
1607 if (!NT_STATUS_IS_OK(status)) {
1608 goto error;
1611 user_info.info24.password = crypt_pwd;
1612 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1614 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1615 &user_pol,
1616 UserInternal5Information,
1617 &user_info,
1618 &result);
1621 error:
1622 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1624 if (!NT_STATUS_IS_OK(status)) {
1626 dcerpc_samr_DeleteUser(b, mem_ctx,
1627 &user_pol,
1628 &result);
1630 libnet_join_set_error_string(mem_ctx, r,
1631 "Failed to set password for machine account (%s)\n",
1632 nt_errstr(status));
1633 goto done;
1635 if (!NT_STATUS_IS_OK(result)) {
1636 status = result;
1638 dcerpc_samr_DeleteUser(b, mem_ctx,
1639 &user_pol,
1640 &result);
1642 libnet_join_set_error_string(mem_ctx, r,
1643 "Failed to set password for machine account (%s)\n",
1644 nt_errstr(status));
1645 goto done;
1648 status = NT_STATUS_OK;
1650 done:
1651 if (!pipe_hnd) {
1652 return status;
1655 data_blob_clear_free(&session_key);
1657 if (is_valid_policy_hnd(&sam_pol)) {
1658 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1660 if (is_valid_policy_hnd(&domain_pol)) {
1661 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1663 if (is_valid_policy_hnd(&user_pol)) {
1664 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1666 TALLOC_FREE(pipe_hnd);
1668 return status;
1671 /****************************************************************
1672 ****************************************************************/
1674 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1675 const char *netbios_domain_name,
1676 const char *dc_name,
1677 const bool use_kerberos)
1679 TALLOC_CTX *frame = talloc_stackframe();
1680 struct cli_state *cli = NULL;
1681 struct rpc_pipe_client *netlogon_pipe = NULL;
1682 struct cli_credentials *cli_creds = NULL;
1683 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1684 struct netlogon_creds_CredentialState *creds = NULL;
1685 uint32_t netlogon_flags = 0;
1686 NTSTATUS status;
1687 int flags = 0;
1689 if (!dc_name) {
1690 TALLOC_FREE(frame);
1691 return NT_STATUS_INVALID_PARAMETER;
1694 if (!secrets_init()) {
1695 TALLOC_FREE(frame);
1696 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1699 status = pdb_get_trust_credentials(netbios_domain_name, NULL,
1700 frame, &cli_creds);
1701 if (!NT_STATUS_IS_OK(status)) {
1702 TALLOC_FREE(frame);
1703 return status;
1706 /* we don't want any old password */
1707 cli_credentials_set_old_password(cli_creds, NULL, CRED_SPECIFIED);
1709 if (use_kerberos) {
1710 cli_credentials_set_kerberos_state(cli_creds,
1711 CRED_MUST_USE_KERBEROS);
1714 status = cli_full_connection_creds(&cli, NULL,
1715 dc_name,
1716 NULL, 0,
1717 "IPC$", "IPC",
1718 cli_creds,
1719 flags,
1720 SMB_SIGNING_IPC_DEFAULT);
1722 if (!NT_STATUS_IS_OK(status)) {
1723 struct cli_credentials *anon_creds = NULL;
1725 anon_creds = cli_credentials_init_anon(frame);
1726 if (anon_creds == NULL) {
1727 TALLOC_FREE(frame);
1728 return NT_STATUS_NO_MEMORY;
1731 status = cli_full_connection_creds(&cli,
1732 NULL,
1733 dc_name,
1734 NULL, 0,
1735 "IPC$", "IPC",
1736 anon_creds,
1738 SMB_SIGNING_OFF);
1741 if (!NT_STATUS_IS_OK(status)) {
1742 TALLOC_FREE(frame);
1743 return status;
1746 status = rpccli_create_netlogon_creds_ctx(cli_creds,
1747 dc_name,
1748 msg_ctx,
1749 frame,
1750 &netlogon_creds);
1751 if (!NT_STATUS_IS_OK(status)) {
1752 cli_shutdown(cli);
1753 TALLOC_FREE(frame);
1754 return status;
1757 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
1758 netlogon_creds,
1759 true, /* force_reauth */
1760 cli_creds);
1761 if (!NT_STATUS_IS_OK(status)) {
1762 DEBUG(0,("connect_to_domain_password_server: "
1763 "unable to open the domain client session to "
1764 "machine %s. Flags[0x%08X] Error was : %s.\n",
1765 dc_name, (unsigned)netlogon_flags,
1766 nt_errstr(status)));
1767 cli_shutdown(cli);
1768 TALLOC_FREE(frame);
1769 return status;
1772 status = netlogon_creds_cli_get(netlogon_creds,
1773 talloc_tos(),
1774 &creds);
1775 if (!NT_STATUS_IS_OK(status)) {
1776 cli_shutdown(cli);
1777 TALLOC_FREE(frame);
1778 return status;
1780 netlogon_flags = creds->negotiate_flags;
1781 TALLOC_FREE(creds);
1783 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1784 cli_shutdown(cli);
1785 TALLOC_FREE(frame);
1786 return NT_STATUS_OK;
1789 status = cli_rpc_pipe_open_schannel_with_creds(
1790 cli, &ndr_table_netlogon, NCACN_NP,
1791 netlogon_creds, &netlogon_pipe);
1793 TALLOC_FREE(netlogon_pipe);
1795 if (!NT_STATUS_IS_OK(status)) {
1796 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1797 "on netlogon pipe to server %s for domain %s. "
1798 "Error was %s\n",
1799 smbXcli_conn_remote_name(cli->conn),
1800 netbios_domain_name, nt_errstr(status)));
1801 cli_shutdown(cli);
1802 TALLOC_FREE(frame);
1803 return status;
1806 cli_shutdown(cli);
1807 TALLOC_FREE(frame);
1808 return NT_STATUS_OK;
1811 /****************************************************************
1812 ****************************************************************/
1814 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1815 struct libnet_JoinCtx *r)
1817 NTSTATUS status;
1819 status = libnet_join_ok(r->in.msg_ctx,
1820 r->out.netbios_domain_name,
1821 r->in.dc_name,
1822 r->in.use_kerberos);
1823 if (!NT_STATUS_IS_OK(status)) {
1824 libnet_join_set_error_string(mem_ctx, r,
1825 "failed to verify domain membership after joining: %s",
1826 get_friendly_nt_error_msg(status));
1827 return WERR_NERR_SETUPNOTJOINED;
1830 return WERR_OK;
1833 /****************************************************************
1834 ****************************************************************/
1836 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1837 struct libnet_UnjoinCtx *r)
1840 * TODO: use values from 'struct libnet_UnjoinCtx' ?
1842 return secrets_delete_machine_password_ex(lp_workgroup(), lp_realm());
1845 /****************************************************************
1846 ****************************************************************/
1848 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1849 struct libnet_UnjoinCtx *r)
1851 struct cli_state *cli = NULL;
1852 struct rpc_pipe_client *pipe_hnd = NULL;
1853 struct policy_handle sam_pol, domain_pol, user_pol;
1854 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1855 char *acct_name;
1856 uint32_t user_rid;
1857 struct lsa_String lsa_acct_name;
1858 struct samr_Ids user_rids;
1859 struct samr_Ids name_types;
1860 union samr_UserInfo *info = NULL;
1861 struct dcerpc_binding_handle *b = NULL;
1863 ZERO_STRUCT(sam_pol);
1864 ZERO_STRUCT(domain_pol);
1865 ZERO_STRUCT(user_pol);
1867 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1868 r->in.admin_account,
1869 r->in.admin_domain,
1870 r->in.admin_password,
1871 r->in.use_kerberos,
1872 &cli);
1873 if (!NT_STATUS_IS_OK(status)) {
1874 goto done;
1877 /* Open the domain */
1879 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1880 &pipe_hnd);
1881 if (!NT_STATUS_IS_OK(status)) {
1882 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1883 nt_errstr(status)));
1884 goto done;
1887 b = pipe_hnd->binding_handle;
1889 status = dcerpc_samr_Connect2(b, mem_ctx,
1890 pipe_hnd->desthost,
1891 SEC_FLAG_MAXIMUM_ALLOWED,
1892 &sam_pol,
1893 &result);
1894 if (!NT_STATUS_IS_OK(status)) {
1895 goto done;
1897 if (!NT_STATUS_IS_OK(result)) {
1898 status = result;
1899 goto done;
1902 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1903 &sam_pol,
1904 SEC_FLAG_MAXIMUM_ALLOWED,
1905 r->in.domain_sid,
1906 &domain_pol,
1907 &result);
1908 if (!NT_STATUS_IS_OK(status)) {
1909 goto done;
1911 if (!NT_STATUS_IS_OK(result)) {
1912 status = result;
1913 goto done;
1916 /* Create domain user */
1918 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1919 if (!strlower_m(acct_name)) {
1920 status = NT_STATUS_INVALID_PARAMETER;
1921 goto done;
1924 init_lsa_String(&lsa_acct_name, acct_name);
1926 status = dcerpc_samr_LookupNames(b, mem_ctx,
1927 &domain_pol,
1929 &lsa_acct_name,
1930 &user_rids,
1931 &name_types,
1932 &result);
1934 if (!NT_STATUS_IS_OK(status)) {
1935 goto done;
1937 if (!NT_STATUS_IS_OK(result)) {
1938 status = result;
1939 goto done;
1941 if (user_rids.count != 1) {
1942 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1943 goto done;
1945 if (name_types.count != 1) {
1946 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1947 goto done;
1950 if (name_types.ids[0] != SID_NAME_USER) {
1951 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1952 name_types.ids[0]));
1953 status = NT_STATUS_INVALID_WORKSTATION;
1954 goto done;
1957 user_rid = user_rids.ids[0];
1959 /* Open handle on user */
1961 status = dcerpc_samr_OpenUser(b, mem_ctx,
1962 &domain_pol,
1963 SEC_FLAG_MAXIMUM_ALLOWED,
1964 user_rid,
1965 &user_pol,
1966 &result);
1967 if (!NT_STATUS_IS_OK(status)) {
1968 goto done;
1970 if (!NT_STATUS_IS_OK(result)) {
1971 status = result;
1972 goto done;
1975 /* Get user info */
1977 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1978 &user_pol,
1980 &info,
1981 &result);
1982 if (!NT_STATUS_IS_OK(status)) {
1983 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1984 goto done;
1986 if (!NT_STATUS_IS_OK(result)) {
1987 status = result;
1988 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1989 goto done;
1992 /* now disable and setuser info */
1994 info->info16.acct_flags |= ACB_DISABLED;
1996 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1997 &user_pol,
1999 info,
2000 &result);
2001 if (!NT_STATUS_IS_OK(status)) {
2002 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2003 goto done;
2005 if (!NT_STATUS_IS_OK(result)) {
2006 status = result;
2007 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2008 goto done;
2010 status = result;
2011 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2013 done:
2014 if (pipe_hnd && b) {
2015 if (is_valid_policy_hnd(&domain_pol)) {
2016 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2018 if (is_valid_policy_hnd(&sam_pol)) {
2019 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
2021 TALLOC_FREE(pipe_hnd);
2024 if (cli) {
2025 cli_shutdown(cli);
2028 return status;
2031 /****************************************************************
2032 ****************************************************************/
2034 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
2036 WERROR werr = WERR_OK;
2037 sbcErr err;
2038 struct smbconf_ctx *ctx;
2040 err = smbconf_init_reg(r, &ctx, NULL);
2041 if (!SBC_ERROR_IS_OK(err)) {
2042 werr = WERR_SERVICE_DOES_NOT_EXIST;
2043 goto done;
2046 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2048 err = smbconf_set_global_parameter(ctx, "security", "user");
2049 if (!SBC_ERROR_IS_OK(err)) {
2050 werr = WERR_SERVICE_DOES_NOT_EXIST;
2051 goto done;
2054 err = smbconf_set_global_parameter(ctx, "workgroup",
2055 r->in.domain_name);
2056 if (!SBC_ERROR_IS_OK(err)) {
2057 werr = WERR_SERVICE_DOES_NOT_EXIST;
2058 goto done;
2061 smbconf_delete_global_parameter(ctx, "realm");
2062 goto done;
2065 err = smbconf_set_global_parameter(ctx, "security", "domain");
2066 if (!SBC_ERROR_IS_OK(err)) {
2067 werr = WERR_SERVICE_DOES_NOT_EXIST;
2068 goto done;
2071 err = smbconf_set_global_parameter(ctx, "workgroup",
2072 r->out.netbios_domain_name);
2073 if (!SBC_ERROR_IS_OK(err)) {
2074 werr = WERR_SERVICE_DOES_NOT_EXIST;
2075 goto done;
2078 if (r->out.domain_is_ad) {
2079 err = smbconf_set_global_parameter(ctx, "security", "ads");
2080 if (!SBC_ERROR_IS_OK(err)) {
2081 werr = WERR_SERVICE_DOES_NOT_EXIST;
2082 goto done;
2085 err = smbconf_set_global_parameter(ctx, "realm",
2086 r->out.dns_domain_name);
2087 if (!SBC_ERROR_IS_OK(err)) {
2088 werr = WERR_SERVICE_DOES_NOT_EXIST;
2089 goto done;
2093 done:
2094 smbconf_shutdown(ctx);
2095 return werr;
2098 /****************************************************************
2099 ****************************************************************/
2101 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
2103 WERROR werr = WERR_OK;
2104 sbcErr err;
2105 struct smbconf_ctx *ctx;
2107 err = smbconf_init_reg(r, &ctx, NULL);
2108 if (!SBC_ERROR_IS_OK(err)) {
2109 werr = WERR_SERVICE_DOES_NOT_EXIST;
2110 goto done;
2113 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2115 err = smbconf_set_global_parameter(ctx, "security", "user");
2116 if (!SBC_ERROR_IS_OK(err)) {
2117 werr = WERR_SERVICE_DOES_NOT_EXIST;
2118 goto done;
2121 err = smbconf_delete_global_parameter(ctx, "workgroup");
2122 if (!SBC_ERROR_IS_OK(err)) {
2123 werr = WERR_SERVICE_DOES_NOT_EXIST;
2124 goto done;
2127 smbconf_delete_global_parameter(ctx, "realm");
2130 done:
2131 smbconf_shutdown(ctx);
2132 return werr;
2135 /****************************************************************
2136 ****************************************************************/
2138 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
2140 WERROR werr;
2142 if (!W_ERROR_IS_OK(r->out.result)) {
2143 return r->out.result;
2146 if (!r->in.modify_config) {
2147 return WERR_OK;
2150 werr = do_join_modify_vals_config(r);
2151 if (!W_ERROR_IS_OK(werr)) {
2152 return werr;
2155 lp_load_global(get_dyn_CONFIGFILE());
2157 r->out.modified_config = true;
2158 r->out.result = werr;
2160 return werr;
2163 /****************************************************************
2164 ****************************************************************/
2166 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
2168 WERROR werr;
2170 if (!W_ERROR_IS_OK(r->out.result)) {
2171 return r->out.result;
2174 if (!r->in.modify_config) {
2175 return WERR_OK;
2178 werr = do_unjoin_modify_vals_config(r);
2179 if (!W_ERROR_IS_OK(werr)) {
2180 return werr;
2183 lp_load_global(get_dyn_CONFIGFILE());
2185 r->out.modified_config = true;
2186 r->out.result = werr;
2188 return werr;
2191 /****************************************************************
2192 ****************************************************************/
2194 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
2195 const char *domain_str,
2196 const char **domain_p,
2197 const char **dc_p)
2199 char *domain = NULL;
2200 char *dc = NULL;
2201 const char *p = NULL;
2203 if (!domain_str || !domain_p || !dc_p) {
2204 return false;
2207 p = strchr_m(domain_str, '\\');
2209 if (p != NULL) {
2210 domain = talloc_strndup(mem_ctx, domain_str,
2211 PTR_DIFF(p, domain_str));
2212 dc = talloc_strdup(mem_ctx, p+1);
2213 if (!dc) {
2214 return false;
2216 } else {
2217 domain = talloc_strdup(mem_ctx, domain_str);
2218 dc = NULL;
2220 if (!domain) {
2221 return false;
2224 *domain_p = domain;
2226 if (!*dc_p && dc) {
2227 *dc_p = dc;
2230 return true;
2233 /****************************************************************
2234 ****************************************************************/
2236 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
2237 struct libnet_JoinCtx *r)
2239 if (!r->in.domain_name) {
2240 libnet_join_set_error_string(mem_ctx, r,
2241 "No domain name defined");
2242 return WERR_INVALID_PARAMETER;
2245 if (strlen(r->in.machine_name) > 15) {
2246 libnet_join_set_error_string(mem_ctx, r,
2247 "Our netbios name can be at most 15 chars long, "
2248 "\"%s\" is %u chars long\n",
2249 r->in.machine_name,
2250 (unsigned int)strlen(r->in.machine_name));
2251 return WERR_INVALID_PARAMETER;
2254 r->out.account_name = talloc_asprintf(mem_ctx, "%s$",
2255 r->in.machine_name);
2256 if (r->out.account_name == NULL) {
2257 libnet_join_set_error_string(mem_ctx, r,
2258 "Unable to construct r->out.account_name");
2259 return WERR_NOT_ENOUGH_MEMORY;
2262 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2263 &r->in.domain_name,
2264 &r->in.dc_name)) {
2265 libnet_join_set_error_string(mem_ctx, r,
2266 "Failed to parse domain name");
2267 return WERR_INVALID_PARAMETER;
2270 if (!r->in.admin_domain) {
2271 char *admin_domain = NULL;
2272 char *admin_account = NULL;
2273 bool ok;
2275 ok = split_domain_user(mem_ctx,
2276 r->in.admin_account,
2277 &admin_domain,
2278 &admin_account);
2279 if (!ok) {
2280 return WERR_NOT_ENOUGH_MEMORY;
2283 if (admin_domain != NULL) {
2284 r->in.admin_domain = admin_domain;
2285 } else {
2286 r->in.admin_domain = r->in.domain_name;
2288 r->in.admin_account = admin_account;
2291 if (!secrets_init()) {
2292 libnet_join_set_error_string(mem_ctx, r,
2293 "Unable to open secrets database");
2294 return WERR_CAN_NOT_COMPLETE;
2297 return WERR_OK;
2300 /****************************************************************
2301 ****************************************************************/
2303 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
2305 NTSTATUS status;
2307 /* Try adding dom admins to builtin\admins. Only log failures. */
2308 status = create_builtin_administrators(domain_sid);
2309 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2310 DEBUG(10,("Unable to auto-add domain administrators to "
2311 "BUILTIN\\Administrators during join because "
2312 "winbindd must be running.\n"));
2313 } else if (!NT_STATUS_IS_OK(status)) {
2314 DEBUG(5, ("Failed to auto-add domain administrators to "
2315 "BUILTIN\\Administrators during join: %s\n",
2316 nt_errstr(status)));
2319 /* Try adding dom users to builtin\users. Only log failures. */
2320 status = create_builtin_users(domain_sid);
2321 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2322 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2323 "during join because winbindd must be running.\n"));
2324 } else if (!NT_STATUS_IS_OK(status)) {
2325 DEBUG(5, ("Failed to auto-add domain administrators to "
2326 "BUILTIN\\Administrators during join: %s\n",
2327 nt_errstr(status)));
2330 /* Try adding dom guests to builtin\guests. Only log failures. */
2331 status = create_builtin_guests(domain_sid);
2332 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2333 DEBUG(10,("Unable to auto-add domain guests to "
2334 "BUILTIN\\Guests during join because "
2335 "winbindd must be running.\n"));
2336 } else if (!NT_STATUS_IS_OK(status)) {
2337 DEBUG(5, ("Failed to auto-add domain guests to "
2338 "BUILTIN\\Guests during join: %s\n",
2339 nt_errstr(status)));
2343 /****************************************************************
2344 ****************************************************************/
2346 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
2347 struct libnet_JoinCtx *r)
2349 WERROR werr;
2351 if (!W_ERROR_IS_OK(r->out.result)) {
2352 return r->out.result;
2355 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2356 werr = do_JoinConfig(r);
2357 if (!W_ERROR_IS_OK(werr)) {
2358 return werr;
2361 return WERR_OK;
2364 #ifdef HAVE_ADS
2365 if (r->out.domain_is_ad &&
2366 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2367 ADS_STATUS ads_status;
2369 ads_status = libnet_join_post_processing_ads_modify(mem_ctx, r);
2370 if (!ADS_ERR_OK(ads_status)) {
2371 return WERR_GEN_FAILURE;
2374 #endif /* HAVE_ADS */
2376 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
2377 if (r->out.dns_domain_name) {
2378 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
2381 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2382 return WERR_NERR_SETUPNOTJOINED;
2385 werr = do_JoinConfig(r);
2386 if (!W_ERROR_IS_OK(werr)) {
2387 return werr;
2390 #ifdef HAVE_ADS
2391 if (r->out.domain_is_ad &&
2392 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2393 ADS_STATUS ads_status;
2395 ads_status = libnet_join_post_processing_ads_sync(mem_ctx, r);
2396 if (!ADS_ERR_OK(ads_status)) {
2397 return WERR_GEN_FAILURE;
2400 #endif /* HAVE_ADS */
2402 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
2404 return WERR_OK;
2407 /****************************************************************
2408 ****************************************************************/
2410 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
2412 if (r->in.ads) {
2413 ads_destroy(&r->in.ads);
2416 return 0;
2419 /****************************************************************
2420 ****************************************************************/
2422 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2424 if (r->in.ads) {
2425 ads_destroy(&r->in.ads);
2428 return 0;
2431 /****************************************************************
2432 ****************************************************************/
2434 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2435 struct libnet_JoinCtx **r)
2437 struct libnet_JoinCtx *ctx;
2439 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2440 if (!ctx) {
2441 return WERR_NOT_ENOUGH_MEMORY;
2444 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2446 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2447 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2449 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2451 ctx->in.desired_encryption_types = ENC_CRC32 |
2452 ENC_RSA_MD5 |
2453 ENC_RC4_HMAC_MD5;
2454 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2455 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES128;
2456 #endif
2457 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2458 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES256;
2459 #endif
2461 *r = ctx;
2463 return WERR_OK;
2466 /****************************************************************
2467 ****************************************************************/
2469 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2470 struct libnet_UnjoinCtx **r)
2472 struct libnet_UnjoinCtx *ctx;
2474 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2475 if (!ctx) {
2476 return WERR_NOT_ENOUGH_MEMORY;
2479 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2481 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2482 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2484 *r = ctx;
2486 return WERR_OK;
2489 /****************************************************************
2490 ****************************************************************/
2492 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2493 struct libnet_JoinCtx *r)
2495 bool valid_security = false;
2496 bool valid_workgroup = false;
2497 bool valid_realm = false;
2498 bool ignored_realm = false;
2500 /* check if configuration is already set correctly */
2502 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2504 switch (r->out.domain_is_ad) {
2505 case false:
2506 valid_security = (lp_security() == SEC_DOMAIN)
2507 || (lp_server_role() == ROLE_DOMAIN_PDC)
2508 || (lp_server_role() == ROLE_DOMAIN_BDC);
2509 if (valid_workgroup && valid_security) {
2510 /* nothing to be done */
2511 return WERR_OK;
2513 break;
2514 case true:
2515 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2516 switch (lp_security()) {
2517 case SEC_DOMAIN:
2518 if (!valid_realm && lp_winbind_rpc_only()) {
2519 valid_realm = true;
2520 ignored_realm = true;
2523 FALL_THROUGH;
2524 case SEC_ADS:
2525 valid_security = true;
2528 if (valid_workgroup && valid_realm && valid_security) {
2529 if (ignored_realm && !r->in.modify_config)
2531 libnet_join_set_error_string(mem_ctx, r,
2532 "Warning: ignoring realm when "
2533 "joining AD domain with "
2534 "'security=domain' and "
2535 "'winbind rpc only = yes'. "
2536 "(realm set to '%s', "
2537 "should be '%s').", lp_realm(),
2538 r->out.dns_domain_name);
2540 /* nothing to be done */
2541 return WERR_OK;
2543 break;
2546 /* check if we are supposed to manipulate configuration */
2548 if (!r->in.modify_config) {
2550 char *wrong_conf = talloc_strdup(mem_ctx, "");
2552 if (!valid_workgroup) {
2553 wrong_conf = talloc_asprintf_append(wrong_conf,
2554 "\"workgroup\" set to '%s', should be '%s'",
2555 lp_workgroup(), r->out.netbios_domain_name);
2556 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2559 if (!valid_realm) {
2560 wrong_conf = talloc_asprintf_append(wrong_conf,
2561 "\"realm\" set to '%s', should be '%s'",
2562 lp_realm(), r->out.dns_domain_name);
2563 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2566 if (!valid_security) {
2567 const char *sec = NULL;
2568 switch (lp_security()) {
2569 case SEC_USER: sec = "user"; break;
2570 case SEC_DOMAIN: sec = "domain"; break;
2571 case SEC_ADS: sec = "ads"; break;
2573 wrong_conf = talloc_asprintf_append(wrong_conf,
2574 "\"security\" set to '%s', should be %s",
2575 sec, r->out.domain_is_ad ?
2576 "either 'domain' or 'ads'" : "'domain'");
2577 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2580 libnet_join_set_error_string(mem_ctx, r,
2581 "Invalid configuration (%s) and configuration modification "
2582 "was not requested", wrong_conf);
2583 return WERR_CAN_NOT_COMPLETE;
2586 /* check if we are able to manipulate configuration */
2588 if (!lp_config_backend_is_registry()) {
2589 libnet_join_set_error_string(mem_ctx, r,
2590 "Configuration manipulation requested but not "
2591 "supported by backend");
2592 return WERR_NOT_SUPPORTED;
2595 return WERR_OK;
2598 /****************************************************************
2599 ****************************************************************/
2601 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2602 struct libnet_JoinCtx *r)
2604 NTSTATUS status;
2605 WERROR werr;
2606 struct cli_state *cli = NULL;
2607 #ifdef HAVE_ADS
2608 ADS_STATUS ads_status;
2609 #endif /* HAVE_ADS */
2610 const char *pre_connect_realm = NULL;
2611 const char *numeric_dcip = NULL;
2612 const char *sitename = NULL;
2614 /* Before contacting a DC, we can securely know
2615 * the realm only if the user specifies it.
2617 if (r->in.use_kerberos &&
2618 r->in.domain_name_type == JoinDomNameTypeDNS) {
2619 pre_connect_realm = r->in.domain_name;
2622 if (!r->in.dc_name) {
2623 struct netr_DsRGetDCNameInfo *info;
2624 const char *dc;
2625 uint32_t name_type_flags = 0;
2626 if (r->in.domain_name_type == JoinDomNameTypeDNS) {
2627 name_type_flags = DS_IS_DNS_NAME;
2628 } else if (r->in.domain_name_type == JoinDomNameTypeNBT) {
2629 name_type_flags = DS_IS_FLAT_NAME;
2631 status = dsgetdcname(mem_ctx,
2632 r->in.msg_ctx,
2633 r->in.domain_name,
2634 NULL,
2635 NULL,
2636 DS_FORCE_REDISCOVERY |
2637 DS_DIRECTORY_SERVICE_REQUIRED |
2638 DS_WRITABLE_REQUIRED |
2639 DS_RETURN_DNS_NAME |
2640 name_type_flags,
2641 &info);
2642 if (!NT_STATUS_IS_OK(status)) {
2643 libnet_join_set_error_string(mem_ctx, r,
2644 "failed to find DC for domain %s - %s",
2645 r->in.domain_name,
2646 get_friendly_nt_error_msg(status));
2647 return WERR_NERR_DCNOTFOUND;
2650 dc = strip_hostname(info->dc_unc);
2651 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2652 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2654 if (info->dc_address == NULL || info->dc_address[0] != '\\' ||
2655 info->dc_address[1] != '\\') {
2656 DBG_ERR("ill-formed DC address '%s'\n",
2657 info->dc_address);
2658 return WERR_NERR_DCNOTFOUND;
2661 numeric_dcip = info->dc_address + 2;
2662 sitename = info->dc_site_name;
2663 /* info goes out of scope but the memory stays
2664 allocated on the talloc context */
2667 if (pre_connect_realm != NULL) {
2668 struct sockaddr_storage ss = {0};
2670 if (numeric_dcip != NULL) {
2671 if (!interpret_string_addr(&ss, numeric_dcip,
2672 AI_NUMERICHOST)) {
2673 DBG_ERR(
2674 "cannot parse IP address '%s' of DC '%s'\n",
2675 numeric_dcip, r->in.dc_name);
2676 return WERR_NERR_DCNOTFOUND;
2678 } else {
2679 if (!interpret_string_addr(&ss, r->in.dc_name, 0)) {
2680 DBG_WARNING(
2681 "cannot resolve IP address of DC '%s'\n",
2682 r->in.dc_name);
2683 return WERR_NERR_DCNOTFOUND;
2687 /* The domain parameter is only used as modifier
2688 * to krb5.conf file name. _JOIN_ is is not a valid
2689 * NetBIOS name so it cannot clash with another domain
2690 * -- Uri.
2692 create_local_private_krb5_conf_for_domain(pre_connect_realm,
2693 "_JOIN_",
2694 sitename,
2695 &ss);
2698 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2699 if (!NT_STATUS_IS_OK(status)) {
2700 libnet_join_set_error_string(mem_ctx, r,
2701 "failed to lookup DC info for domain '%s' over rpc: %s",
2702 r->in.domain_name, get_friendly_nt_error_msg(status));
2703 return ntstatus_to_werror(status);
2706 werr = libnet_join_check_config(mem_ctx, r);
2707 if (!W_ERROR_IS_OK(werr)) {
2708 goto done;
2711 #ifdef HAVE_ADS
2713 if (r->out.domain_is_ad) {
2714 create_local_private_krb5_conf_for_domain(
2715 r->out.dns_domain_name, r->out.netbios_domain_name,
2716 sitename, smbXcli_conn_remote_sockaddr(cli->conn));
2719 if (r->out.domain_is_ad &&
2720 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2722 const char *initial_account_ou = r->in.account_ou;
2725 * we want to create the msDS-SupportedEncryptionTypes attribute
2726 * as early as possible so always try an LDAP create as the user
2727 * first. We copy r->in.account_ou because it may be changed
2728 * during the machine pre-creation.
2731 ads_status = libnet_join_connect_ads_user(mem_ctx, r);
2732 if (!ADS_ERR_OK(ads_status)) {
2733 libnet_join_set_error_string(mem_ctx, r,
2734 "failed to connect to AD: %s",
2735 ads_errstr(ads_status));
2736 return WERR_NERR_DEFAULTJOINREQUIRED;
2739 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2740 if (ADS_ERR_OK(ads_status)) {
2743 * LDAP object creation succeeded.
2745 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2747 return WERR_OK;
2750 if (initial_account_ou != NULL) {
2751 libnet_join_set_error_string(mem_ctx, r,
2752 "failed to precreate account in ou %s: %s",
2753 r->in.account_ou,
2754 ads_errstr(ads_status));
2755 return WERR_NERR_DEFAULTJOINREQUIRED;
2758 DBG_INFO("Failed to pre-create account in OU %s: %s\n",
2759 r->in.account_ou, ads_errstr(ads_status));
2761 #endif /* HAVE_ADS */
2763 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2764 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2765 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2766 } else {
2767 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2769 if (!NT_STATUS_IS_OK(status)) {
2770 libnet_join_set_error_string(mem_ctx, r,
2771 "failed to join domain '%s' over rpc: %s",
2772 r->in.domain_name, get_friendly_nt_error_msg(status));
2773 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2774 return WERR_NERR_SETUPALREADYJOINED;
2776 werr = ntstatus_to_werror(status);
2777 goto done;
2780 werr = WERR_OK;
2782 done:
2783 if (cli) {
2784 cli_shutdown(cli);
2787 return werr;
2790 /****************************************************************
2791 ****************************************************************/
2793 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2794 struct libnet_JoinCtx *r)
2796 WERROR werr;
2797 struct libnet_UnjoinCtx *u = NULL;
2799 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2800 if (!W_ERROR_IS_OK(werr)) {
2801 return werr;
2804 u->in.debug = r->in.debug;
2805 u->in.dc_name = r->in.dc_name;
2806 u->in.domain_name = r->in.domain_name;
2807 u->in.admin_account = r->in.admin_account;
2808 u->in.admin_password = r->in.admin_password;
2809 u->in.modify_config = r->in.modify_config;
2810 u->in.use_kerberos = r->in.use_kerberos;
2811 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2812 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2814 werr = libnet_Unjoin(mem_ctx, u);
2815 TALLOC_FREE(u);
2817 return werr;
2820 /****************************************************************
2821 ****************************************************************/
2823 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2824 struct libnet_JoinCtx *r)
2826 WERROR werr;
2828 if (r->in.debug) {
2829 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2832 ZERO_STRUCT(r->out);
2834 werr = libnet_join_pre_processing(mem_ctx, r);
2835 if (!W_ERROR_IS_OK(werr)) {
2836 goto done;
2839 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2840 werr = libnet_DomainJoin(mem_ctx, r);
2841 if (!W_ERROR_IS_OK(werr)) {
2842 goto done;
2846 werr = libnet_join_post_processing(mem_ctx, r);
2847 if (!W_ERROR_IS_OK(werr)) {
2848 goto done;
2851 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2852 werr = libnet_join_post_verify(mem_ctx, r);
2853 if (!W_ERROR_IS_OK(werr)) {
2854 libnet_join_rollback(mem_ctx, r);
2858 done:
2859 r->out.result = werr;
2861 if (r->in.debug) {
2862 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2864 return werr;
2867 /****************************************************************
2868 ****************************************************************/
2870 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2871 struct libnet_UnjoinCtx *r)
2873 NTSTATUS status;
2875 if (!r->in.domain_sid) {
2876 struct dom_sid sid;
2877 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2878 libnet_unjoin_set_error_string(mem_ctx, r,
2879 "Unable to fetch domain sid: are we joined?");
2880 return WERR_NERR_SETUPNOTJOINED;
2882 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2883 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2886 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2887 !r->in.delete_machine_account) {
2888 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2889 return WERR_OK;
2892 if (!r->in.dc_name) {
2893 struct netr_DsRGetDCNameInfo *info;
2894 const char *dc;
2895 status = dsgetdcname(mem_ctx,
2896 r->in.msg_ctx,
2897 r->in.domain_name,
2898 NULL,
2899 NULL,
2900 DS_DIRECTORY_SERVICE_REQUIRED |
2901 DS_WRITABLE_REQUIRED |
2902 DS_RETURN_DNS_NAME,
2903 &info);
2904 if (!NT_STATUS_IS_OK(status)) {
2905 libnet_unjoin_set_error_string(mem_ctx, r,
2906 "failed to find DC for domain %s - %s",
2907 r->in.domain_name,
2908 get_friendly_nt_error_msg(status));
2909 return WERR_NERR_DCNOTFOUND;
2912 dc = strip_hostname(info->dc_unc);
2913 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2914 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2917 #ifdef HAVE_ADS
2918 /* for net ads leave, try to delete the account. If it works,
2919 no sense in disabling. If it fails, we can still try to
2920 disable it. jmcd */
2922 if (r->in.delete_machine_account) {
2923 ADS_STATUS ads_status;
2924 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2925 if (ADS_ERR_OK(ads_status)) {
2926 /* dirty hack */
2927 r->out.dns_domain_name =
2928 talloc_strdup(mem_ctx,
2929 r->in.ads->server.realm);
2930 ads_status =
2931 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2933 if (!ADS_ERR_OK(ads_status)) {
2934 libnet_unjoin_set_error_string(mem_ctx, r,
2935 "failed to remove machine account from AD: %s",
2936 ads_errstr(ads_status));
2937 } else {
2938 r->out.deleted_machine_account = true;
2939 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2940 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2941 return WERR_OK;
2944 #endif /* HAVE_ADS */
2946 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2947 "disable". */
2948 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2949 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2950 if (!NT_STATUS_IS_OK(status)) {
2951 libnet_unjoin_set_error_string(mem_ctx, r,
2952 "failed to disable machine account via rpc: %s",
2953 get_friendly_nt_error_msg(status));
2954 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2955 return WERR_NERR_SETUPNOTJOINED;
2957 return ntstatus_to_werror(status);
2960 r->out.dns_domain_name = talloc_strdup(mem_ctx,
2961 r->in.domain_name);
2962 r->out.disabled_machine_account = true;
2965 /* If disable succeeded or was not requested at all, we
2966 should be getting rid of our end of things */
2968 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2970 return WERR_OK;
2973 /****************************************************************
2974 ****************************************************************/
2976 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2977 struct libnet_UnjoinCtx *r)
2979 if (!r->in.domain_name) {
2980 libnet_unjoin_set_error_string(mem_ctx, r,
2981 "No domain name defined");
2982 return WERR_INVALID_PARAMETER;
2985 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2986 &r->in.domain_name,
2987 &r->in.dc_name)) {
2988 libnet_unjoin_set_error_string(mem_ctx, r,
2989 "Failed to parse domain name");
2990 return WERR_INVALID_PARAMETER;
2993 if (IS_DC) {
2994 return WERR_NERR_SETUPDOMAINCONTROLLER;
2997 if (!r->in.admin_domain) {
2998 char *admin_domain = NULL;
2999 char *admin_account = NULL;
3000 bool ok;
3002 ok = split_domain_user(mem_ctx,
3003 r->in.admin_account,
3004 &admin_domain,
3005 &admin_account);
3006 if (!ok) {
3007 return WERR_NOT_ENOUGH_MEMORY;
3010 if (admin_domain != NULL) {
3011 r->in.admin_domain = admin_domain;
3012 } else {
3013 r->in.admin_domain = r->in.domain_name;
3015 r->in.admin_account = admin_account;
3018 if (!secrets_init()) {
3019 libnet_unjoin_set_error_string(mem_ctx, r,
3020 "Unable to open secrets database");
3021 return WERR_CAN_NOT_COMPLETE;
3024 return WERR_OK;
3027 /****************************************************************
3028 ****************************************************************/
3030 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
3031 struct libnet_UnjoinCtx *r)
3033 saf_delete(r->out.netbios_domain_name);
3034 saf_delete(r->out.dns_domain_name);
3036 return libnet_unjoin_config(r);
3039 /****************************************************************
3040 ****************************************************************/
3042 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
3043 struct libnet_UnjoinCtx *r)
3045 WERROR werr;
3047 if (r->in.debug) {
3048 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
3051 werr = libnet_unjoin_pre_processing(mem_ctx, r);
3052 if (!W_ERROR_IS_OK(werr)) {
3053 goto done;
3056 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
3057 werr = libnet_DomainUnjoin(mem_ctx, r);
3058 if (!W_ERROR_IS_OK(werr)) {
3059 libnet_unjoin_config(r);
3060 goto done;
3064 werr = libnet_unjoin_post_processing(mem_ctx, r);
3065 if (!W_ERROR_IS_OK(werr)) {
3066 goto done;
3069 done:
3070 r->out.result = werr;
3072 if (r->in.debug) {
3073 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
3076 return werr;