s3:libnet: make use of secrets_store_JoinCtx()
[Samba.git] / source3 / libnet / libnet_join.c
blob591c177b44badfd78424622192ecb2b356b2762f
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"
43 #include "libcli/auth/netlogon_creds_cli.h"
44 #include "auth/credentials/credentials.h"
45 #include "krb5_env.h"
47 /****************************************************************
48 ****************************************************************/
50 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
51 do { \
52 char *str = NULL; \
53 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
54 DEBUG(1,("libnet_Join:\n%s", str)); \
55 TALLOC_FREE(str); \
56 } while (0)
58 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
59 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
60 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
61 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
63 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
64 do { \
65 char *str = NULL; \
66 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
67 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
68 TALLOC_FREE(str); \
69 } while (0)
71 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
72 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
73 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
74 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
76 /****************************************************************
77 ****************************************************************/
79 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
80 struct libnet_JoinCtx *r,
81 const char *format, ...)
82 PRINTF_ATTRIBUTE(3,4);
84 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
85 struct libnet_JoinCtx *r,
86 const char *format, ...)
88 va_list args;
90 if (r->out.error_string) {
91 return;
94 va_start(args, format);
95 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
96 va_end(args);
99 /****************************************************************
100 ****************************************************************/
102 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
103 struct libnet_UnjoinCtx *r,
104 const char *format, ...)
105 PRINTF_ATTRIBUTE(3,4);
107 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
108 struct libnet_UnjoinCtx *r,
109 const char *format, ...)
111 va_list args;
113 if (r->out.error_string) {
114 return;
117 va_start(args, format);
118 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
119 va_end(args);
122 #ifdef HAVE_ADS
124 /****************************************************************
125 ****************************************************************/
127 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
128 const char *netbios_domain_name,
129 const char *dc_name,
130 const char *user_name,
131 const char *password,
132 const char *ccname,
133 ADS_STRUCT **ads)
135 ADS_STATUS status;
136 ADS_STRUCT *my_ads = NULL;
137 char *cp;
139 my_ads = ads_init(dns_domain_name,
140 netbios_domain_name,
141 dc_name);
142 if (!my_ads) {
143 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
146 if (user_name) {
147 SAFE_FREE(my_ads->auth.user_name);
148 my_ads->auth.user_name = SMB_STRDUP(user_name);
149 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
150 *cp++ = '\0';
151 SAFE_FREE(my_ads->auth.realm);
152 my_ads->auth.realm = smb_xstrdup(cp);
153 if (!strupper_m(my_ads->auth.realm)) {
154 ads_destroy(&my_ads);
155 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
160 if (password) {
161 SAFE_FREE(my_ads->auth.password);
162 my_ads->auth.password = SMB_STRDUP(password);
165 if (ccname != NULL) {
166 SAFE_FREE(my_ads->auth.ccache_name);
167 my_ads->auth.ccache_name = SMB_STRDUP(ccname);
168 setenv(KRB5_ENV_CCNAME, my_ads->auth.ccache_name, 1);
171 status = ads_connect_user_creds(my_ads);
172 if (!ADS_ERR_OK(status)) {
173 ads_destroy(&my_ads);
174 return status;
177 *ads = my_ads;
178 return ADS_SUCCESS;
181 /****************************************************************
182 ****************************************************************/
184 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
185 struct libnet_JoinCtx *r,
186 bool use_machine_creds)
188 ADS_STATUS status;
189 const char *username;
190 const char *password;
191 const char *ccname = NULL;
193 if (use_machine_creds) {
194 if (r->in.machine_name == NULL ||
195 r->in.machine_password == NULL) {
196 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
198 username = talloc_asprintf(mem_ctx, "%s$",
199 r->in.machine_name);
200 if (username == NULL) {
201 return ADS_ERROR(LDAP_NO_MEMORY);
203 password = r->in.machine_password;
204 ccname = "MEMORY:libnet_join_machine_creds";
205 } else {
206 username = r->in.admin_account;
207 password = r->in.admin_password;
210 * when r->in.use_kerberos is set to allow "net ads join -k" we
211 * may not override the provided credential cache - gd
214 if (!r->in.use_kerberos) {
215 ccname = "MEMORY:libnet_join_user_creds";
219 status = libnet_connect_ads(r->out.dns_domain_name,
220 r->out.netbios_domain_name,
221 r->in.dc_name,
222 username,
223 password,
224 ccname,
225 &r->in.ads);
226 if (!ADS_ERR_OK(status)) {
227 libnet_join_set_error_string(mem_ctx, r,
228 "failed to connect to AD: %s",
229 ads_errstr(status));
230 return status;
233 if (!r->out.netbios_domain_name) {
234 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
235 r->in.ads->server.workgroup);
236 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
239 if (!r->out.dns_domain_name) {
240 r->out.dns_domain_name = talloc_strdup(mem_ctx,
241 r->in.ads->config.realm);
242 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
245 r->out.domain_is_ad = true;
247 return ADS_SUCCESS;
250 /****************************************************************
251 ****************************************************************/
253 static ADS_STATUS libnet_join_connect_ads_user(TALLOC_CTX *mem_ctx,
254 struct libnet_JoinCtx *r)
256 return libnet_join_connect_ads(mem_ctx, r, false);
259 /****************************************************************
260 ****************************************************************/
262 static ADS_STATUS libnet_join_connect_ads_machine(TALLOC_CTX *mem_ctx,
263 struct libnet_JoinCtx *r)
265 return libnet_join_connect_ads(mem_ctx, r, true);
268 /****************************************************************
269 ****************************************************************/
271 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
272 struct libnet_UnjoinCtx *r)
274 ADS_STATUS status;
276 status = libnet_connect_ads(r->in.domain_name,
277 r->in.domain_name,
278 r->in.dc_name,
279 r->in.admin_account,
280 r->in.admin_password,
281 NULL,
282 &r->in.ads);
283 if (!ADS_ERR_OK(status)) {
284 libnet_unjoin_set_error_string(mem_ctx, r,
285 "failed to connect to AD: %s",
286 ads_errstr(status));
289 return status;
292 /****************************************************************
293 join a domain using ADS (LDAP mods)
294 ****************************************************************/
296 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
297 struct libnet_JoinCtx *r)
299 ADS_STATUS status;
300 LDAPMessage *res = NULL;
301 const char *attrs[] = { "dn", NULL };
302 bool moved = false;
304 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
305 if (!ADS_ERR_OK(status)) {
306 return status;
309 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
310 if (!ADS_ERR_OK(status)) {
311 return status;
314 if (ads_count_replies(r->in.ads, res) != 1) {
315 ads_msgfree(r->in.ads, res);
316 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
319 ads_msgfree(r->in.ads, res);
321 /* Attempt to create the machine account and bail if this fails.
322 Assume that the admin wants exactly what they requested */
324 status = ads_create_machine_acct(r->in.ads,
325 r->in.machine_name,
326 r->in.account_ou,
327 r->in.desired_encryption_types);
329 if (ADS_ERR_OK(status)) {
330 DEBUG(1,("machine account creation created\n"));
331 return status;
332 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
333 (status.err.rc == LDAP_ALREADY_EXISTS)) {
334 status = ADS_SUCCESS;
337 if (!ADS_ERR_OK(status)) {
338 DEBUG(1,("machine account creation failed\n"));
339 return status;
342 status = ads_move_machine_acct(r->in.ads,
343 r->in.machine_name,
344 r->in.account_ou,
345 &moved);
346 if (!ADS_ERR_OK(status)) {
347 DEBUG(1,("failure to locate/move pre-existing "
348 "machine account\n"));
349 return status;
352 DEBUG(1,("The machine account %s the specified OU.\n",
353 moved ? "was moved into" : "already exists in"));
355 return status;
358 /****************************************************************
359 ****************************************************************/
361 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
362 struct libnet_UnjoinCtx *r)
364 ADS_STATUS status;
366 if (!r->in.ads) {
367 status = libnet_unjoin_connect_ads(mem_ctx, r);
368 if (!ADS_ERR_OK(status)) {
369 libnet_unjoin_set_error_string(mem_ctx, r,
370 "failed to connect to AD: %s",
371 ads_errstr(status));
372 return status;
376 status = ads_leave_realm(r->in.ads, r->in.machine_name);
377 if (!ADS_ERR_OK(status)) {
378 libnet_unjoin_set_error_string(mem_ctx, r,
379 "failed to leave realm: %s",
380 ads_errstr(status));
381 return status;
384 return ADS_SUCCESS;
387 /****************************************************************
388 ****************************************************************/
390 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
391 struct libnet_JoinCtx *r)
393 ADS_STATUS status;
394 LDAPMessage *res = NULL;
395 char *dn = NULL;
397 if (!r->in.machine_name) {
398 return ADS_ERROR(LDAP_NO_MEMORY);
401 status = ads_find_machine_acct(r->in.ads,
402 &res,
403 r->in.machine_name);
404 if (!ADS_ERR_OK(status)) {
405 return status;
408 if (ads_count_replies(r->in.ads, res) != 1) {
409 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
410 goto done;
413 dn = ads_get_dn(r->in.ads, mem_ctx, res);
414 if (!dn) {
415 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
416 goto done;
419 r->out.dn = talloc_strdup(mem_ctx, dn);
420 if (!r->out.dn) {
421 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
422 goto done;
425 if (!ads_pull_uint32(r->in.ads, res, "msDS-SupportedEncryptionTypes",
426 &r->out.set_encryption_types)) {
427 r->out.set_encryption_types = 0;
430 done:
431 ads_msgfree(r->in.ads, res);
432 TALLOC_FREE(dn);
434 return status;
437 static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
438 struct libnet_JoinCtx *r,
439 char ***spn_array,
440 size_t *num_spns)
442 ADS_STATUS status;
444 if (r->in.machine_name == NULL) {
445 return ADS_ERROR_SYSTEM(EINVAL);
448 status = ads_get_service_principal_names(mem_ctx,
449 r->in.ads,
450 r->in.machine_name,
451 spn_array,
452 num_spns);
454 return status;
457 /****************************************************************
458 Set a machines dNSHostName and servicePrincipalName attributes
459 ****************************************************************/
461 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
462 struct libnet_JoinCtx *r)
464 ADS_STATUS status;
465 ADS_MODLIST mods;
466 fstring my_fqdn;
467 const char **spn_array = NULL;
468 size_t num_spns = 0;
469 char *spn = NULL;
470 bool ok;
471 const char **netbios_aliases = NULL;
473 /* Find our DN */
475 status = libnet_join_find_machine_acct(mem_ctx, r);
476 if (!ADS_ERR_OK(status)) {
477 return status;
480 status = libnet_join_get_machine_spns(mem_ctx,
482 discard_const_p(char **, &spn_array),
483 &num_spns);
484 if (!ADS_ERR_OK(status)) {
485 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
488 /* Windows only creates HOST/shortname & HOST/fqdn. */
490 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
491 if (!spn) {
492 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
494 if (!strupper_m(spn)) {
495 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
498 ok = ads_element_in_array(spn_array, num_spns, spn);
499 if (!ok) {
500 ok = add_string_to_array(spn_array, spn,
501 &spn_array, &num_spns);
502 if (!ok) {
503 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
507 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
508 || (strchr(my_fqdn, '.') == NULL)) {
509 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
510 r->out.dns_domain_name);
513 if (!strlower_m(my_fqdn)) {
514 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
517 if (!strequal(my_fqdn, r->in.machine_name)) {
518 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
519 if (!spn) {
520 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
523 ok = ads_element_in_array(spn_array, num_spns, spn);
524 if (!ok) {
525 ok = add_string_to_array(spn_array, spn,
526 &spn_array, &num_spns);
527 if (!ok) {
528 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
533 netbios_aliases = lp_netbios_aliases();
534 if (netbios_aliases != NULL) {
535 for (; *netbios_aliases != NULL; netbios_aliases++) {
537 * Add HOST/NETBIOSNAME
539 spn = talloc_asprintf(mem_ctx, "HOST/%s", *netbios_aliases);
540 if (spn == NULL) {
541 TALLOC_FREE(spn);
542 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
544 if (!strupper_m(spn)) {
545 TALLOC_FREE(spn);
546 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
549 ok = ads_element_in_array(spn_array, num_spns, spn);
550 if (ok) {
551 TALLOC_FREE(spn);
552 continue;
554 ok = add_string_to_array(spn_array, spn,
555 &spn_array, &num_spns);
556 if (!ok) {
557 TALLOC_FREE(spn);
558 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
560 TALLOC_FREE(spn);
563 * Add HOST/netbiosname.domainname
565 if (r->out.dns_domain_name == NULL) {
566 continue;
568 fstr_sprintf(my_fqdn, "%s.%s",
569 *netbios_aliases,
570 r->out.dns_domain_name);
572 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
573 if (spn == NULL) {
574 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
577 ok = ads_element_in_array(spn_array, num_spns, spn);
578 if (ok) {
579 TALLOC_FREE(spn);
580 continue;
582 ok = add_string_to_array(spn_array, spn,
583 &spn_array, &num_spns);
584 if (!ok) {
585 TALLOC_FREE(spn);
586 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
588 TALLOC_FREE(spn);
592 /* make sure to NULL terminate the array */
593 spn_array = talloc_realloc(mem_ctx, spn_array, const char *, num_spns + 1);
594 if (spn_array == NULL) {
595 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
597 spn_array[num_spns] = NULL;
599 mods = ads_init_mods(mem_ctx);
600 if (!mods) {
601 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
604 /* fields of primary importance */
606 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
607 if (!ADS_ERR_OK(status)) {
608 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
611 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
612 spn_array);
613 if (!ADS_ERR_OK(status)) {
614 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
617 return ads_gen_mod(r->in.ads, r->out.dn, mods);
620 /****************************************************************
621 ****************************************************************/
623 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
624 struct libnet_JoinCtx *r)
626 ADS_STATUS status;
627 ADS_MODLIST mods;
629 if (!r->in.create_upn) {
630 return ADS_SUCCESS;
633 /* Find our DN */
635 status = libnet_join_find_machine_acct(mem_ctx, r);
636 if (!ADS_ERR_OK(status)) {
637 return status;
640 if (!r->in.upn) {
641 const char *realm = r->out.dns_domain_name;
643 /* in case we are about to generate a keytab during the join
644 * make sure the default upn we create is usable with kinit -k.
645 * gd */
647 if (USE_KERBEROS_KEYTAB) {
648 realm = talloc_strdup_upper(mem_ctx,
649 r->out.dns_domain_name);
652 if (!realm) {
653 return ADS_ERROR(LDAP_NO_MEMORY);
656 r->in.upn = talloc_asprintf(mem_ctx,
657 "host/%s@%s",
658 r->in.machine_name,
659 realm);
660 if (!r->in.upn) {
661 return ADS_ERROR(LDAP_NO_MEMORY);
665 /* now do the mods */
667 mods = ads_init_mods(mem_ctx);
668 if (!mods) {
669 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
672 /* fields of primary importance */
674 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
675 if (!ADS_ERR_OK(status)) {
676 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
679 return ads_gen_mod(r->in.ads, r->out.dn, mods);
683 /****************************************************************
684 ****************************************************************/
686 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
687 struct libnet_JoinCtx *r)
689 ADS_STATUS status;
690 ADS_MODLIST mods;
691 char *os_sp = NULL;
693 if (!r->in.os_name || !r->in.os_version ) {
694 return ADS_SUCCESS;
697 /* Find our DN */
699 status = libnet_join_find_machine_acct(mem_ctx, r);
700 if (!ADS_ERR_OK(status)) {
701 return status;
704 /* now do the mods */
706 mods = ads_init_mods(mem_ctx);
707 if (!mods) {
708 return ADS_ERROR(LDAP_NO_MEMORY);
711 if (r->in.os_servicepack) {
713 * if blank string then leave os_sp equal to NULL to force
714 * attribute delete (LDAP_MOD_DELETE)
716 if (!strequal(r->in.os_servicepack,"")) {
717 os_sp = talloc_strdup(mem_ctx, r->in.os_servicepack);
719 } else {
720 os_sp = talloc_asprintf(mem_ctx, "Samba %s",
721 samba_version_string());
723 if (!os_sp && !strequal(r->in.os_servicepack,"")) {
724 return ADS_ERROR(LDAP_NO_MEMORY);
727 /* fields of primary importance */
729 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
730 r->in.os_name);
731 if (!ADS_ERR_OK(status)) {
732 return status;
735 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
736 r->in.os_version);
737 if (!ADS_ERR_OK(status)) {
738 return status;
741 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
742 os_sp);
743 if (!ADS_ERR_OK(status)) {
744 return status;
747 return ads_gen_mod(r->in.ads, r->out.dn, mods);
750 /****************************************************************
751 ****************************************************************/
753 static ADS_STATUS libnet_join_set_etypes(TALLOC_CTX *mem_ctx,
754 struct libnet_JoinCtx *r)
756 ADS_STATUS status;
757 ADS_MODLIST mods;
758 const char *etype_list_str;
760 etype_list_str = talloc_asprintf(mem_ctx, "%d",
761 r->in.desired_encryption_types);
762 if (!etype_list_str) {
763 return ADS_ERROR(LDAP_NO_MEMORY);
766 /* Find our DN */
768 status = libnet_join_find_machine_acct(mem_ctx, r);
769 if (!ADS_ERR_OK(status)) {
770 return status;
773 if (r->in.desired_encryption_types == r->out.set_encryption_types) {
774 return ADS_SUCCESS;
777 /* now do the mods */
779 mods = ads_init_mods(mem_ctx);
780 if (!mods) {
781 return ADS_ERROR(LDAP_NO_MEMORY);
784 status = ads_mod_str(mem_ctx, &mods, "msDS-SupportedEncryptionTypes",
785 etype_list_str);
786 if (!ADS_ERR_OK(status)) {
787 return status;
790 status = ads_gen_mod(r->in.ads, r->out.dn, mods);
791 if (!ADS_ERR_OK(status)) {
792 return status;
795 r->out.set_encryption_types = r->in.desired_encryption_types;
797 return ADS_SUCCESS;
800 /****************************************************************
801 ****************************************************************/
803 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
804 struct libnet_JoinCtx *r)
806 if (!USE_SYSTEM_KEYTAB) {
807 return true;
810 if (ads_keytab_create_default(r->in.ads) != 0) {
811 return false;
814 return true;
817 /****************************************************************
818 ****************************************************************/
820 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
821 struct libnet_JoinCtx *r)
823 uint32_t domain_func;
824 ADS_STATUS status;
825 const char *salt = NULL;
826 char *std_salt = NULL;
828 status = ads_domain_func_level(r->in.ads, &domain_func);
829 if (!ADS_ERR_OK(status)) {
830 libnet_join_set_error_string(mem_ctx, r,
831 "failed to determine domain functional level: %s",
832 ads_errstr(status));
833 return false;
836 /* go ahead and setup the default salt */
838 std_salt = kerberos_standard_des_salt();
839 if (!std_salt) {
840 libnet_join_set_error_string(mem_ctx, r,
841 "failed to obtain standard DES salt");
842 return false;
845 salt = talloc_strdup(mem_ctx, std_salt);
846 if (!salt) {
847 return false;
850 SAFE_FREE(std_salt);
852 /* if it's a Windows functional domain, we have to look for the UPN */
854 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
855 char *upn;
857 upn = ads_get_upn(r->in.ads, mem_ctx,
858 r->in.machine_name);
859 if (upn) {
860 salt = talloc_strdup(mem_ctx, upn);
861 if (!salt) {
862 return false;
867 r->out.krb5_salt = salt;
868 return true;
871 /****************************************************************
872 ****************************************************************/
874 static ADS_STATUS libnet_join_post_processing_ads_modify(TALLOC_CTX *mem_ctx,
875 struct libnet_JoinCtx *r)
877 ADS_STATUS status;
878 bool need_etype_update = false;
880 if (!r->in.ads) {
881 status = libnet_join_connect_ads_user(mem_ctx, r);
882 if (!ADS_ERR_OK(status)) {
883 return status;
887 status = libnet_join_set_machine_spn(mem_ctx, r);
888 if (!ADS_ERR_OK(status)) {
889 libnet_join_set_error_string(mem_ctx, r,
890 "Failed to set machine spn: %s\n"
891 "Do you have sufficient permissions to create machine "
892 "accounts?",
893 ads_errstr(status));
894 return status;
897 status = libnet_join_set_os_attributes(mem_ctx, r);
898 if (!ADS_ERR_OK(status)) {
899 libnet_join_set_error_string(mem_ctx, r,
900 "failed to set machine os attributes: %s",
901 ads_errstr(status));
902 return status;
905 status = libnet_join_set_machine_upn(mem_ctx, r);
906 if (!ADS_ERR_OK(status)) {
907 libnet_join_set_error_string(mem_ctx, r,
908 "failed to set machine upn: %s",
909 ads_errstr(status));
910 return status;
913 status = libnet_join_find_machine_acct(mem_ctx, r);
914 if (!ADS_ERR_OK(status)) {
915 return status;
918 if (r->in.desired_encryption_types != r->out.set_encryption_types) {
919 uint32_t func_level = 0;
921 status = ads_domain_func_level(r->in.ads, &func_level);
922 if (!ADS_ERR_OK(status)) {
923 libnet_join_set_error_string(mem_ctx, r,
924 "failed to query domain controller functional level: %s",
925 ads_errstr(status));
926 return status;
929 if (func_level >= DS_DOMAIN_FUNCTION_2008) {
930 need_etype_update = true;
934 if (need_etype_update) {
936 * We need to reconnect as machine account in order
937 * to update msDS-SupportedEncryptionTypes reliable
940 if (r->in.ads->auth.ccache_name != NULL) {
941 ads_kdestroy(r->in.ads->auth.ccache_name);
944 ads_destroy(&r->in.ads);
946 status = libnet_join_connect_ads_machine(mem_ctx, r);
947 if (!ADS_ERR_OK(status)) {
948 libnet_join_set_error_string(mem_ctx, r,
949 "Failed to connect as machine account: %s",
950 ads_errstr(status));
951 return status;
954 status = libnet_join_set_etypes(mem_ctx, r);
955 if (!ADS_ERR_OK(status)) {
956 libnet_join_set_error_string(mem_ctx, r,
957 "failed to set machine kerberos encryption types: %s",
958 ads_errstr(status));
959 return status;
963 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
964 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
967 return ADS_SUCCESS;
970 static ADS_STATUS libnet_join_post_processing_ads_sync(TALLOC_CTX *mem_ctx,
971 struct libnet_JoinCtx *r)
973 if (!libnet_join_create_keytab(mem_ctx, r)) {
974 libnet_join_set_error_string(mem_ctx, r,
975 "failed to create kerberos keytab");
976 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
979 return ADS_SUCCESS;
981 #endif /* HAVE_ADS */
983 /****************************************************************
984 Store the machine password and domain SID
985 ****************************************************************/
987 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
988 struct libnet_JoinCtx *r)
990 NTSTATUS status;
992 status = secrets_store_JoinCtx(r);
993 if (!NT_STATUS_IS_OK(status)) {
994 DBG_ERR("secrets_store_JoinCtx() failed %s\n",
995 nt_errstr(status));
996 return false;
999 return true;
1002 /****************************************************************
1003 Connect dc's IPC$ share
1004 ****************************************************************/
1006 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
1007 const char *user,
1008 const char *domain,
1009 const char *pass,
1010 bool use_kerberos,
1011 struct cli_state **cli)
1013 int flags = 0;
1015 if (use_kerberos) {
1016 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1019 if (use_kerberos && pass) {
1020 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1023 return cli_full_connection(cli, NULL,
1025 NULL, 0,
1026 "IPC$", "IPC",
1027 user,
1028 domain,
1029 pass,
1030 flags,
1031 SMB_SIGNING_IPC_DEFAULT);
1034 /****************************************************************
1035 Lookup domain dc's info
1036 ****************************************************************/
1038 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
1039 struct libnet_JoinCtx *r,
1040 struct cli_state **cli)
1042 struct rpc_pipe_client *pipe_hnd = NULL;
1043 struct policy_handle lsa_pol;
1044 NTSTATUS status, result;
1045 union lsa_PolicyInformation *info = NULL;
1046 struct dcerpc_binding_handle *b;
1048 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1049 r->in.admin_account,
1050 r->in.admin_domain,
1051 r->in.admin_password,
1052 r->in.use_kerberos,
1053 cli);
1054 if (!NT_STATUS_IS_OK(status)) {
1055 goto done;
1058 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
1059 &pipe_hnd);
1060 if (!NT_STATUS_IS_OK(status)) {
1061 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
1062 nt_errstr(status)));
1063 goto done;
1066 b = pipe_hnd->binding_handle;
1068 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
1069 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1070 if (!NT_STATUS_IS_OK(status)) {
1071 goto done;
1074 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
1075 &lsa_pol,
1076 LSA_POLICY_INFO_DNS,
1077 &info,
1078 &result);
1079 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
1080 r->out.domain_is_ad = true;
1081 r->out.netbios_domain_name = info->dns.name.string;
1082 r->out.dns_domain_name = info->dns.dns_domain.string;
1083 r->out.forest_name = info->dns.dns_forest.string;
1084 r->out.domain_guid = info->dns.domain_guid;
1085 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
1086 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1089 if (!NT_STATUS_IS_OK(status)) {
1090 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
1091 &lsa_pol,
1092 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
1093 &info,
1094 &result);
1095 if (!NT_STATUS_IS_OK(status)) {
1096 goto done;
1098 if (!NT_STATUS_IS_OK(result)) {
1099 status = result;
1100 goto done;
1103 r->out.netbios_domain_name = info->account_domain.name.string;
1104 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
1105 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1108 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1109 TALLOC_FREE(pipe_hnd);
1111 done:
1112 return status;
1115 /****************************************************************
1116 Do the domain join unsecure
1117 ****************************************************************/
1119 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
1120 struct libnet_JoinCtx *r,
1121 struct cli_state *cli)
1123 TALLOC_CTX *frame = talloc_stackframe();
1124 struct rpc_pipe_client *netlogon_pipe = NULL;
1125 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1126 struct samr_Password current_nt_hash;
1127 size_t len = 0;
1128 bool ok;
1129 DATA_BLOB new_trust_blob = data_blob_null;
1130 NTSTATUS status;
1132 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
1133 &netlogon_pipe);
1134 if (!NT_STATUS_IS_OK(status)) {
1135 TALLOC_FREE(frame);
1136 return status;
1139 if (!r->in.machine_password) {
1140 int security = r->in.ads ? SEC_ADS : SEC_DOMAIN;
1142 r->in.machine_password = trust_pw_new_value(mem_ctx,
1143 r->in.secure_channel_type,
1144 security);
1145 if (r->in.machine_password == NULL) {
1146 TALLOC_FREE(frame);
1147 return NT_STATUS_NO_MEMORY;
1151 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
1152 E_md4hash(r->in.admin_password, current_nt_hash.hash);
1154 status = rpccli_create_netlogon_creds(netlogon_pipe->desthost,
1155 r->in.domain_name,
1156 r->out.account_name,
1157 r->in.secure_channel_type,
1158 r->in.msg_ctx,
1159 frame,
1160 &netlogon_creds);
1161 if (!NT_STATUS_IS_OK(status)) {
1162 TALLOC_FREE(frame);
1163 return status;
1166 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
1167 netlogon_creds,
1168 true, /* force_reauth */
1169 current_nt_hash,
1170 NULL); /* previous_nt_hash */
1171 if (!NT_STATUS_IS_OK(status)) {
1172 TALLOC_FREE(frame);
1173 return status;
1176 len = strlen(r->in.machine_password);
1177 ok = convert_string_talloc(frame, CH_UNIX, CH_UTF16,
1178 r->in.machine_password, len,
1179 (void **)&new_trust_blob.data,
1180 &new_trust_blob.length);
1181 if (!ok) {
1182 status = NT_STATUS_UNMAPPABLE_CHARACTER;
1183 if (errno == ENOMEM) {
1184 status = NT_STATUS_NO_MEMORY;
1186 TALLOC_FREE(frame);
1187 return status;
1190 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
1191 netlogon_pipe->binding_handle,
1192 &new_trust_blob,
1193 NULL); /* new_version */
1194 if (!NT_STATUS_IS_OK(status)) {
1195 TALLOC_FREE(frame);
1196 return status;
1199 TALLOC_FREE(frame);
1200 return NT_STATUS_OK;
1203 /****************************************************************
1204 Do the domain join
1205 ****************************************************************/
1207 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
1208 struct libnet_JoinCtx *r,
1209 struct cli_state *cli)
1211 struct rpc_pipe_client *pipe_hnd = NULL;
1212 struct policy_handle sam_pol, domain_pol, user_pol;
1213 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1214 char *acct_name;
1215 struct lsa_String lsa_acct_name;
1216 uint32_t user_rid;
1217 uint32_t acct_flags = ACB_WSTRUST;
1218 struct samr_Ids user_rids;
1219 struct samr_Ids name_types;
1220 union samr_UserInfo user_info;
1221 struct dcerpc_binding_handle *b = NULL;
1222 unsigned int old_timeout = 0;
1224 DATA_BLOB session_key = data_blob_null;
1225 struct samr_CryptPassword crypt_pwd;
1226 struct samr_CryptPasswordEx crypt_pwd_ex;
1228 ZERO_STRUCT(sam_pol);
1229 ZERO_STRUCT(domain_pol);
1230 ZERO_STRUCT(user_pol);
1232 switch (r->in.secure_channel_type) {
1233 case SEC_CHAN_WKSTA:
1234 acct_flags = ACB_WSTRUST;
1235 break;
1236 case SEC_CHAN_BDC:
1237 acct_flags = ACB_SVRTRUST;
1238 break;
1239 default:
1240 return NT_STATUS_INVALID_PARAMETER;
1243 if (!r->in.machine_password) {
1244 int security = r->in.ads ? SEC_ADS : SEC_DOMAIN;
1246 r->in.machine_password = trust_pw_new_value(mem_ctx,
1247 r->in.secure_channel_type,
1248 security);
1249 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
1252 /* Open the domain */
1254 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1255 &pipe_hnd);
1256 if (!NT_STATUS_IS_OK(status)) {
1257 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1258 nt_errstr(status)));
1259 goto done;
1262 b = pipe_hnd->binding_handle;
1264 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1265 if (!NT_STATUS_IS_OK(status)) {
1266 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1267 nt_errstr(status)));
1268 goto done;
1271 status = dcerpc_samr_Connect2(b, mem_ctx,
1272 pipe_hnd->desthost,
1273 SAMR_ACCESS_ENUM_DOMAINS
1274 | SAMR_ACCESS_LOOKUP_DOMAIN,
1275 &sam_pol,
1276 &result);
1277 if (!NT_STATUS_IS_OK(status)) {
1278 goto done;
1280 if (!NT_STATUS_IS_OK(result)) {
1281 status = result;
1282 goto done;
1285 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1286 &sam_pol,
1287 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1288 | SAMR_DOMAIN_ACCESS_CREATE_USER
1289 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1290 r->out.domain_sid,
1291 &domain_pol,
1292 &result);
1293 if (!NT_STATUS_IS_OK(status)) {
1294 goto done;
1296 if (!NT_STATUS_IS_OK(result)) {
1297 status = result;
1298 goto done;
1301 /* Create domain user */
1303 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1304 if (!strlower_m(acct_name)) {
1305 status = NT_STATUS_INVALID_PARAMETER;
1306 goto done;
1309 init_lsa_String(&lsa_acct_name, acct_name);
1311 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1312 uint32_t access_desired =
1313 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1314 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1315 SAMR_USER_ACCESS_SET_PASSWORD |
1316 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1317 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1318 uint32_t access_granted = 0;
1320 DEBUG(10,("Creating account with desired access mask: %d\n",
1321 access_desired));
1323 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1324 &domain_pol,
1325 &lsa_acct_name,
1326 acct_flags,
1327 access_desired,
1328 &user_pol,
1329 &access_granted,
1330 &user_rid,
1331 &result);
1332 if (!NT_STATUS_IS_OK(status)) {
1333 goto done;
1336 status = result;
1337 if (!NT_STATUS_IS_OK(status) &&
1338 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1340 DEBUG(10,("Creation of workstation account failed: %s\n",
1341 nt_errstr(status)));
1343 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1344 username/password combo but the user does not have
1345 administrator access. */
1347 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1348 libnet_join_set_error_string(mem_ctx, r,
1349 "User specified does not have "
1350 "administrator privileges");
1353 goto done;
1356 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1357 if (!(r->in.join_flags &
1358 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1359 goto done;
1363 /* We *must* do this.... don't ask... */
1365 if (NT_STATUS_IS_OK(status)) {
1366 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1370 status = dcerpc_samr_LookupNames(b, mem_ctx,
1371 &domain_pol,
1373 &lsa_acct_name,
1374 &user_rids,
1375 &name_types,
1376 &result);
1377 if (!NT_STATUS_IS_OK(status)) {
1378 goto done;
1380 if (!NT_STATUS_IS_OK(result)) {
1381 status = result;
1382 goto done;
1384 if (user_rids.count != 1) {
1385 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1386 goto done;
1388 if (name_types.count != 1) {
1389 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1390 goto done;
1393 if (name_types.ids[0] != SID_NAME_USER) {
1394 DEBUG(0,("%s is not a user account (type=%d)\n",
1395 acct_name, name_types.ids[0]));
1396 status = NT_STATUS_INVALID_WORKSTATION;
1397 goto done;
1400 user_rid = user_rids.ids[0];
1402 /* Open handle on user */
1404 status = dcerpc_samr_OpenUser(b, mem_ctx,
1405 &domain_pol,
1406 SEC_FLAG_MAXIMUM_ALLOWED,
1407 user_rid,
1408 &user_pol,
1409 &result);
1410 if (!NT_STATUS_IS_OK(status)) {
1411 goto done;
1413 if (!NT_STATUS_IS_OK(result)) {
1414 status = result;
1415 goto done;
1418 /* Fill in the additional account flags now */
1420 acct_flags |= ACB_PWNOEXP;
1422 /* Set account flags on machine account */
1423 ZERO_STRUCT(user_info.info16);
1424 user_info.info16.acct_flags = acct_flags;
1426 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1427 &user_pol,
1428 UserControlInformation,
1429 &user_info,
1430 &result);
1431 if (!NT_STATUS_IS_OK(status)) {
1432 dcerpc_samr_DeleteUser(b, mem_ctx,
1433 &user_pol,
1434 &result);
1436 libnet_join_set_error_string(mem_ctx, r,
1437 "Failed to set account flags for machine account (%s)\n",
1438 nt_errstr(status));
1439 goto done;
1442 if (!NT_STATUS_IS_OK(result)) {
1443 status = result;
1445 dcerpc_samr_DeleteUser(b, mem_ctx,
1446 &user_pol,
1447 &result);
1449 libnet_join_set_error_string(mem_ctx, r,
1450 "Failed to set account flags for machine account (%s)\n",
1451 nt_errstr(status));
1452 goto done;
1455 /* Set password on machine account - first try level 26 */
1458 * increase the timeout as password filter modules on the DC
1459 * might delay the operation for a significant amount of time
1461 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1463 init_samr_CryptPasswordEx(r->in.machine_password,
1464 &session_key,
1465 &crypt_pwd_ex);
1467 user_info.info26.password = crypt_pwd_ex;
1468 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1470 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1471 &user_pol,
1472 UserInternal5InformationNew,
1473 &user_info,
1474 &result);
1476 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1478 /* retry with level 24 */
1480 init_samr_CryptPassword(r->in.machine_password,
1481 &session_key,
1482 &crypt_pwd);
1484 user_info.info24.password = crypt_pwd;
1485 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1487 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1488 &user_pol,
1489 UserInternal5Information,
1490 &user_info,
1491 &result);
1494 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1496 if (!NT_STATUS_IS_OK(status)) {
1498 dcerpc_samr_DeleteUser(b, mem_ctx,
1499 &user_pol,
1500 &result);
1502 libnet_join_set_error_string(mem_ctx, r,
1503 "Failed to set password for machine account (%s)\n",
1504 nt_errstr(status));
1505 goto done;
1507 if (!NT_STATUS_IS_OK(result)) {
1508 status = result;
1510 dcerpc_samr_DeleteUser(b, mem_ctx,
1511 &user_pol,
1512 &result);
1514 libnet_join_set_error_string(mem_ctx, r,
1515 "Failed to set password for machine account (%s)\n",
1516 nt_errstr(status));
1517 goto done;
1520 status = NT_STATUS_OK;
1522 done:
1523 if (!pipe_hnd) {
1524 return status;
1527 data_blob_clear_free(&session_key);
1529 if (is_valid_policy_hnd(&sam_pol)) {
1530 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1532 if (is_valid_policy_hnd(&domain_pol)) {
1533 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1535 if (is_valid_policy_hnd(&user_pol)) {
1536 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1538 TALLOC_FREE(pipe_hnd);
1540 return status;
1543 /****************************************************************
1544 ****************************************************************/
1546 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1547 const char *netbios_domain_name,
1548 const char *dc_name,
1549 const bool use_kerberos)
1551 TALLOC_CTX *frame = talloc_stackframe();
1552 struct cli_state *cli = NULL;
1553 struct rpc_pipe_client *netlogon_pipe = NULL;
1554 struct cli_credentials *cli_creds = NULL;
1555 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1556 struct netlogon_creds_CredentialState *creds = NULL;
1557 uint32_t netlogon_flags = 0;
1558 NTSTATUS status;
1559 int flags = 0;
1561 if (!dc_name) {
1562 TALLOC_FREE(frame);
1563 return NT_STATUS_INVALID_PARAMETER;
1566 if (!secrets_init()) {
1567 TALLOC_FREE(frame);
1568 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1571 status = pdb_get_trust_credentials(netbios_domain_name, NULL,
1572 frame, &cli_creds);
1573 if (!NT_STATUS_IS_OK(status)) {
1574 TALLOC_FREE(frame);
1575 return status;
1578 /* we don't want any old password */
1579 cli_credentials_set_old_password(cli_creds, NULL, CRED_SPECIFIED);
1581 if (use_kerberos) {
1582 cli_credentials_set_kerberos_state(cli_creds,
1583 CRED_MUST_USE_KERBEROS);
1586 status = cli_full_connection_creds(&cli, NULL,
1587 dc_name,
1588 NULL, 0,
1589 "IPC$", "IPC",
1590 cli_creds,
1591 flags,
1592 SMB_SIGNING_IPC_DEFAULT);
1594 if (!NT_STATUS_IS_OK(status)) {
1595 status = cli_full_connection(&cli, NULL,
1596 dc_name,
1597 NULL, 0,
1598 "IPC$", "IPC",
1600 NULL,
1603 SMB_SIGNING_IPC_DEFAULT);
1606 if (!NT_STATUS_IS_OK(status)) {
1607 TALLOC_FREE(frame);
1608 return status;
1611 status = rpccli_create_netlogon_creds_with_creds(cli_creds,
1612 dc_name,
1613 msg_ctx,
1614 frame,
1615 &netlogon_creds);
1616 if (!NT_STATUS_IS_OK(status)) {
1617 cli_shutdown(cli);
1618 TALLOC_FREE(frame);
1619 return status;
1622 status = rpccli_setup_netlogon_creds_with_creds(cli, NCACN_NP,
1623 netlogon_creds,
1624 true, /* force_reauth */
1625 cli_creds);
1626 if (!NT_STATUS_IS_OK(status)) {
1627 DEBUG(0,("connect_to_domain_password_server: "
1628 "unable to open the domain client session to "
1629 "machine %s. Flags[0x%08X] Error was : %s.\n",
1630 dc_name, (unsigned)netlogon_flags,
1631 nt_errstr(status)));
1632 cli_shutdown(cli);
1633 TALLOC_FREE(frame);
1634 return status;
1637 status = netlogon_creds_cli_get(netlogon_creds,
1638 talloc_tos(),
1639 &creds);
1640 if (!NT_STATUS_IS_OK(status)) {
1641 cli_shutdown(cli);
1642 TALLOC_FREE(frame);
1643 return status;
1645 netlogon_flags = creds->negotiate_flags;
1646 TALLOC_FREE(creds);
1648 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1649 cli_shutdown(cli);
1650 TALLOC_FREE(frame);
1651 return NT_STATUS_OK;
1654 status = cli_rpc_pipe_open_schannel_with_creds(
1655 cli, &ndr_table_netlogon, NCACN_NP,
1656 cli_creds,
1657 netlogon_creds, &netlogon_pipe);
1659 TALLOC_FREE(netlogon_pipe);
1661 if (!NT_STATUS_IS_OK(status)) {
1662 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1663 "on netlogon pipe to server %s for domain %s. "
1664 "Error was %s\n",
1665 smbXcli_conn_remote_name(cli->conn),
1666 netbios_domain_name, nt_errstr(status)));
1667 cli_shutdown(cli);
1668 TALLOC_FREE(frame);
1669 return status;
1672 cli_shutdown(cli);
1673 TALLOC_FREE(frame);
1674 return NT_STATUS_OK;
1677 /****************************************************************
1678 ****************************************************************/
1680 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1681 struct libnet_JoinCtx *r)
1683 NTSTATUS status;
1685 status = libnet_join_ok(r->in.msg_ctx,
1686 r->out.netbios_domain_name,
1687 r->in.dc_name,
1688 r->in.use_kerberos);
1689 if (!NT_STATUS_IS_OK(status)) {
1690 libnet_join_set_error_string(mem_ctx, r,
1691 "failed to verify domain membership after joining: %s",
1692 get_friendly_nt_error_msg(status));
1693 return WERR_NERR_SETUPNOTJOINED;
1696 return WERR_OK;
1699 /****************************************************************
1700 ****************************************************************/
1702 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1703 struct libnet_UnjoinCtx *r)
1706 * TODO: use values from 'struct libnet_UnjoinCtx' ?
1708 return secrets_delete_machine_password_ex(lp_workgroup(), lp_realm());
1711 /****************************************************************
1712 ****************************************************************/
1714 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1715 struct libnet_UnjoinCtx *r)
1717 struct cli_state *cli = NULL;
1718 struct rpc_pipe_client *pipe_hnd = NULL;
1719 struct policy_handle sam_pol, domain_pol, user_pol;
1720 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1721 char *acct_name;
1722 uint32_t user_rid;
1723 struct lsa_String lsa_acct_name;
1724 struct samr_Ids user_rids;
1725 struct samr_Ids name_types;
1726 union samr_UserInfo *info = NULL;
1727 struct dcerpc_binding_handle *b = NULL;
1729 ZERO_STRUCT(sam_pol);
1730 ZERO_STRUCT(domain_pol);
1731 ZERO_STRUCT(user_pol);
1733 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1734 r->in.admin_account,
1735 r->in.admin_domain,
1736 r->in.admin_password,
1737 r->in.use_kerberos,
1738 &cli);
1739 if (!NT_STATUS_IS_OK(status)) {
1740 goto done;
1743 /* Open the domain */
1745 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1746 &pipe_hnd);
1747 if (!NT_STATUS_IS_OK(status)) {
1748 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1749 nt_errstr(status)));
1750 goto done;
1753 b = pipe_hnd->binding_handle;
1755 status = dcerpc_samr_Connect2(b, mem_ctx,
1756 pipe_hnd->desthost,
1757 SEC_FLAG_MAXIMUM_ALLOWED,
1758 &sam_pol,
1759 &result);
1760 if (!NT_STATUS_IS_OK(status)) {
1761 goto done;
1763 if (!NT_STATUS_IS_OK(result)) {
1764 status = result;
1765 goto done;
1768 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1769 &sam_pol,
1770 SEC_FLAG_MAXIMUM_ALLOWED,
1771 r->in.domain_sid,
1772 &domain_pol,
1773 &result);
1774 if (!NT_STATUS_IS_OK(status)) {
1775 goto done;
1777 if (!NT_STATUS_IS_OK(result)) {
1778 status = result;
1779 goto done;
1782 /* Create domain user */
1784 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1785 if (!strlower_m(acct_name)) {
1786 status = NT_STATUS_INVALID_PARAMETER;
1787 goto done;
1790 init_lsa_String(&lsa_acct_name, acct_name);
1792 status = dcerpc_samr_LookupNames(b, mem_ctx,
1793 &domain_pol,
1795 &lsa_acct_name,
1796 &user_rids,
1797 &name_types,
1798 &result);
1800 if (!NT_STATUS_IS_OK(status)) {
1801 goto done;
1803 if (!NT_STATUS_IS_OK(result)) {
1804 status = result;
1805 goto done;
1807 if (user_rids.count != 1) {
1808 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1809 goto done;
1811 if (name_types.count != 1) {
1812 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1813 goto done;
1816 if (name_types.ids[0] != SID_NAME_USER) {
1817 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1818 name_types.ids[0]));
1819 status = NT_STATUS_INVALID_WORKSTATION;
1820 goto done;
1823 user_rid = user_rids.ids[0];
1825 /* Open handle on user */
1827 status = dcerpc_samr_OpenUser(b, mem_ctx,
1828 &domain_pol,
1829 SEC_FLAG_MAXIMUM_ALLOWED,
1830 user_rid,
1831 &user_pol,
1832 &result);
1833 if (!NT_STATUS_IS_OK(status)) {
1834 goto done;
1836 if (!NT_STATUS_IS_OK(result)) {
1837 status = result;
1838 goto done;
1841 /* Get user info */
1843 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1844 &user_pol,
1846 &info,
1847 &result);
1848 if (!NT_STATUS_IS_OK(status)) {
1849 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1850 goto done;
1852 if (!NT_STATUS_IS_OK(result)) {
1853 status = result;
1854 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1855 goto done;
1858 /* now disable and setuser info */
1860 info->info16.acct_flags |= ACB_DISABLED;
1862 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1863 &user_pol,
1865 info,
1866 &result);
1867 if (!NT_STATUS_IS_OK(status)) {
1868 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1869 goto done;
1871 if (!NT_STATUS_IS_OK(result)) {
1872 status = result;
1873 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1874 goto done;
1876 status = result;
1877 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1879 done:
1880 if (pipe_hnd && b) {
1881 if (is_valid_policy_hnd(&domain_pol)) {
1882 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1884 if (is_valid_policy_hnd(&sam_pol)) {
1885 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1887 TALLOC_FREE(pipe_hnd);
1890 if (cli) {
1891 cli_shutdown(cli);
1894 return status;
1897 /****************************************************************
1898 ****************************************************************/
1900 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1902 WERROR werr = WERR_OK;
1903 sbcErr err;
1904 struct smbconf_ctx *ctx;
1906 err = smbconf_init_reg(r, &ctx, NULL);
1907 if (!SBC_ERROR_IS_OK(err)) {
1908 werr = WERR_SERVICE_DOES_NOT_EXIST;
1909 goto done;
1912 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1914 err = smbconf_set_global_parameter(ctx, "security", "user");
1915 if (!SBC_ERROR_IS_OK(err)) {
1916 werr = WERR_SERVICE_DOES_NOT_EXIST;
1917 goto done;
1920 err = smbconf_set_global_parameter(ctx, "workgroup",
1921 r->in.domain_name);
1922 if (!SBC_ERROR_IS_OK(err)) {
1923 werr = WERR_SERVICE_DOES_NOT_EXIST;
1924 goto done;
1927 smbconf_delete_global_parameter(ctx, "realm");
1928 goto done;
1931 err = smbconf_set_global_parameter(ctx, "security", "domain");
1932 if (!SBC_ERROR_IS_OK(err)) {
1933 werr = WERR_SERVICE_DOES_NOT_EXIST;
1934 goto done;
1937 err = smbconf_set_global_parameter(ctx, "workgroup",
1938 r->out.netbios_domain_name);
1939 if (!SBC_ERROR_IS_OK(err)) {
1940 werr = WERR_SERVICE_DOES_NOT_EXIST;
1941 goto done;
1944 if (r->out.domain_is_ad) {
1945 err = smbconf_set_global_parameter(ctx, "security", "ads");
1946 if (!SBC_ERROR_IS_OK(err)) {
1947 werr = WERR_SERVICE_DOES_NOT_EXIST;
1948 goto done;
1951 err = smbconf_set_global_parameter(ctx, "realm",
1952 r->out.dns_domain_name);
1953 if (!SBC_ERROR_IS_OK(err)) {
1954 werr = WERR_SERVICE_DOES_NOT_EXIST;
1955 goto done;
1959 done:
1960 smbconf_shutdown(ctx);
1961 return werr;
1964 /****************************************************************
1965 ****************************************************************/
1967 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1969 WERROR werr = WERR_OK;
1970 sbcErr err;
1971 struct smbconf_ctx *ctx;
1973 err = smbconf_init_reg(r, &ctx, NULL);
1974 if (!SBC_ERROR_IS_OK(err)) {
1975 werr = WERR_SERVICE_DOES_NOT_EXIST;
1976 goto done;
1979 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1981 err = smbconf_set_global_parameter(ctx, "security", "user");
1982 if (!SBC_ERROR_IS_OK(err)) {
1983 werr = WERR_SERVICE_DOES_NOT_EXIST;
1984 goto done;
1987 err = smbconf_delete_global_parameter(ctx, "workgroup");
1988 if (!SBC_ERROR_IS_OK(err)) {
1989 werr = WERR_SERVICE_DOES_NOT_EXIST;
1990 goto done;
1993 smbconf_delete_global_parameter(ctx, "realm");
1996 done:
1997 smbconf_shutdown(ctx);
1998 return werr;
2001 /****************************************************************
2002 ****************************************************************/
2004 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
2006 WERROR werr;
2008 if (!W_ERROR_IS_OK(r->out.result)) {
2009 return r->out.result;
2012 if (!r->in.modify_config) {
2013 return WERR_OK;
2016 werr = do_join_modify_vals_config(r);
2017 if (!W_ERROR_IS_OK(werr)) {
2018 return werr;
2021 lp_load_global(get_dyn_CONFIGFILE());
2023 r->out.modified_config = true;
2024 r->out.result = werr;
2026 return werr;
2029 /****************************************************************
2030 ****************************************************************/
2032 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
2034 WERROR werr;
2036 if (!W_ERROR_IS_OK(r->out.result)) {
2037 return r->out.result;
2040 if (!r->in.modify_config) {
2041 return WERR_OK;
2044 werr = do_unjoin_modify_vals_config(r);
2045 if (!W_ERROR_IS_OK(werr)) {
2046 return werr;
2049 lp_load_global(get_dyn_CONFIGFILE());
2051 r->out.modified_config = true;
2052 r->out.result = werr;
2054 return werr;
2057 /****************************************************************
2058 ****************************************************************/
2060 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
2061 const char *domain_str,
2062 const char **domain_p,
2063 const char **dc_p)
2065 char *domain = NULL;
2066 char *dc = NULL;
2067 const char *p = NULL;
2069 if (!domain_str || !domain_p || !dc_p) {
2070 return false;
2073 p = strchr_m(domain_str, '\\');
2075 if (p != NULL) {
2076 domain = talloc_strndup(mem_ctx, domain_str,
2077 PTR_DIFF(p, domain_str));
2078 dc = talloc_strdup(mem_ctx, p+1);
2079 if (!dc) {
2080 return false;
2082 } else {
2083 domain = talloc_strdup(mem_ctx, domain_str);
2084 dc = NULL;
2086 if (!domain) {
2087 return false;
2090 *domain_p = domain;
2092 if (!*dc_p && dc) {
2093 *dc_p = dc;
2096 return true;
2099 /****************************************************************
2100 ****************************************************************/
2102 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
2103 struct libnet_JoinCtx *r)
2105 if (!r->in.domain_name) {
2106 libnet_join_set_error_string(mem_ctx, r,
2107 "No domain name defined");
2108 return WERR_INVALID_PARAMETER;
2111 if (strlen(r->in.machine_name) > 15) {
2112 libnet_join_set_error_string(mem_ctx, r,
2113 "Our netbios name can be at most 15 chars long, "
2114 "\"%s\" is %u chars long\n",
2115 r->in.machine_name,
2116 (unsigned int)strlen(r->in.machine_name));
2117 return WERR_INVALID_PARAMETER;
2120 r->out.account_name = talloc_asprintf(mem_ctx, "%s$",
2121 r->in.machine_name);
2122 if (r->out.account_name == NULL) {
2123 libnet_join_set_error_string(mem_ctx, r,
2124 "Unable to construct r->out.account_name");
2125 return WERR_NOT_ENOUGH_MEMORY;
2128 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2129 &r->in.domain_name,
2130 &r->in.dc_name)) {
2131 libnet_join_set_error_string(mem_ctx, r,
2132 "Failed to parse domain name");
2133 return WERR_INVALID_PARAMETER;
2136 if (!r->in.admin_domain) {
2137 char *admin_domain = NULL;
2138 char *admin_account = NULL;
2139 bool ok;
2141 ok = split_domain_user(mem_ctx,
2142 r->in.admin_account,
2143 &admin_domain,
2144 &admin_account);
2145 if (!ok) {
2146 return WERR_NOT_ENOUGH_MEMORY;
2149 if (admin_domain != NULL) {
2150 r->in.admin_domain = admin_domain;
2151 } else {
2152 r->in.admin_domain = r->in.domain_name;
2154 r->in.admin_account = admin_account;
2157 if (!secrets_init()) {
2158 libnet_join_set_error_string(mem_ctx, r,
2159 "Unable to open secrets database");
2160 return WERR_CAN_NOT_COMPLETE;
2163 return WERR_OK;
2166 /****************************************************************
2167 ****************************************************************/
2169 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
2171 NTSTATUS status;
2173 /* Try adding dom admins to builtin\admins. Only log failures. */
2174 status = create_builtin_administrators(domain_sid);
2175 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2176 DEBUG(10,("Unable to auto-add domain administrators to "
2177 "BUILTIN\\Administrators during join because "
2178 "winbindd must be running.\n"));
2179 } else if (!NT_STATUS_IS_OK(status)) {
2180 DEBUG(5, ("Failed to auto-add domain administrators to "
2181 "BUILTIN\\Administrators during join: %s\n",
2182 nt_errstr(status)));
2185 /* Try adding dom users to builtin\users. Only log failures. */
2186 status = create_builtin_users(domain_sid);
2187 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2188 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2189 "during join because winbindd must be running.\n"));
2190 } else if (!NT_STATUS_IS_OK(status)) {
2191 DEBUG(5, ("Failed to auto-add domain administrators to "
2192 "BUILTIN\\Administrators during join: %s\n",
2193 nt_errstr(status)));
2197 /****************************************************************
2198 ****************************************************************/
2200 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
2201 struct libnet_JoinCtx *r)
2203 WERROR werr;
2205 if (!W_ERROR_IS_OK(r->out.result)) {
2206 return r->out.result;
2209 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2210 werr = do_JoinConfig(r);
2211 if (!W_ERROR_IS_OK(werr)) {
2212 return werr;
2215 return WERR_OK;
2218 #ifdef HAVE_ADS
2219 if (r->out.domain_is_ad &&
2220 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2221 ADS_STATUS ads_status;
2223 ads_status = libnet_join_post_processing_ads_modify(mem_ctx, r);
2224 if (!ADS_ERR_OK(ads_status)) {
2225 return WERR_GEN_FAILURE;
2228 #endif /* HAVE_ADS */
2230 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
2231 if (r->out.dns_domain_name) {
2232 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
2235 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2236 return WERR_NERR_SETUPNOTJOINED;
2239 werr = do_JoinConfig(r);
2240 if (!W_ERROR_IS_OK(werr)) {
2241 return werr;
2244 #ifdef HAVE_ADS
2245 if (r->out.domain_is_ad &&
2246 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2247 ADS_STATUS ads_status;
2249 ads_status = libnet_join_post_processing_ads_sync(mem_ctx, r);
2250 if (!ADS_ERR_OK(ads_status)) {
2251 return WERR_GEN_FAILURE;
2254 #endif /* HAVE_ADS */
2256 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
2258 return WERR_OK;
2261 /****************************************************************
2262 ****************************************************************/
2264 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
2266 if (r->in.ads) {
2267 ads_destroy(&r->in.ads);
2270 return 0;
2273 /****************************************************************
2274 ****************************************************************/
2276 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2278 if (r->in.ads) {
2279 ads_destroy(&r->in.ads);
2282 return 0;
2285 /****************************************************************
2286 ****************************************************************/
2288 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2289 struct libnet_JoinCtx **r)
2291 struct libnet_JoinCtx *ctx;
2293 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2294 if (!ctx) {
2295 return WERR_NOT_ENOUGH_MEMORY;
2298 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2300 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2301 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2303 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2305 ctx->in.desired_encryption_types = ENC_CRC32 |
2306 ENC_RSA_MD5 |
2307 ENC_RC4_HMAC_MD5;
2308 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2309 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES128;
2310 #endif
2311 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2312 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES256;
2313 #endif
2315 *r = ctx;
2317 return WERR_OK;
2320 /****************************************************************
2321 ****************************************************************/
2323 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2324 struct libnet_UnjoinCtx **r)
2326 struct libnet_UnjoinCtx *ctx;
2328 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2329 if (!ctx) {
2330 return WERR_NOT_ENOUGH_MEMORY;
2333 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2335 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2336 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2338 *r = ctx;
2340 return WERR_OK;
2343 /****************************************************************
2344 ****************************************************************/
2346 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2347 struct libnet_JoinCtx *r)
2349 bool valid_security = false;
2350 bool valid_workgroup = false;
2351 bool valid_realm = false;
2352 bool ignored_realm = false;
2354 /* check if configuration is already set correctly */
2356 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2358 switch (r->out.domain_is_ad) {
2359 case false:
2360 valid_security = (lp_security() == SEC_DOMAIN)
2361 || (lp_server_role() == ROLE_DOMAIN_PDC)
2362 || (lp_server_role() == ROLE_DOMAIN_BDC);
2363 if (valid_workgroup && valid_security) {
2364 /* nothing to be done */
2365 return WERR_OK;
2367 break;
2368 case true:
2369 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2370 switch (lp_security()) {
2371 case SEC_DOMAIN:
2372 if (!valid_realm && lp_winbind_rpc_only()) {
2373 valid_realm = true;
2374 ignored_realm = true;
2376 /* FALL THROUGH */
2377 case SEC_ADS:
2378 valid_security = true;
2381 if (valid_workgroup && valid_realm && valid_security) {
2382 if (ignored_realm && !r->in.modify_config)
2384 libnet_join_set_error_string(mem_ctx, r,
2385 "Warning: ignoring realm when "
2386 "joining AD domain with "
2387 "'security=domain' and "
2388 "'winbind rpc only = yes'. "
2389 "(realm set to '%s', "
2390 "should be '%s').", lp_realm(),
2391 r->out.dns_domain_name);
2393 /* nothing to be done */
2394 return WERR_OK;
2396 break;
2399 /* check if we are supposed to manipulate configuration */
2401 if (!r->in.modify_config) {
2403 char *wrong_conf = talloc_strdup(mem_ctx, "");
2405 if (!valid_workgroup) {
2406 wrong_conf = talloc_asprintf_append(wrong_conf,
2407 "\"workgroup\" set to '%s', should be '%s'",
2408 lp_workgroup(), r->out.netbios_domain_name);
2409 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2412 if (!valid_realm) {
2413 wrong_conf = talloc_asprintf_append(wrong_conf,
2414 "\"realm\" set to '%s', should be '%s'",
2415 lp_realm(), r->out.dns_domain_name);
2416 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2419 if (!valid_security) {
2420 const char *sec = NULL;
2421 switch (lp_security()) {
2422 case SEC_USER: sec = "user"; break;
2423 case SEC_DOMAIN: sec = "domain"; break;
2424 case SEC_ADS: sec = "ads"; break;
2426 wrong_conf = talloc_asprintf_append(wrong_conf,
2427 "\"security\" set to '%s', should be %s",
2428 sec, r->out.domain_is_ad ?
2429 "either 'domain' or 'ads'" : "'domain'");
2430 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2433 libnet_join_set_error_string(mem_ctx, r,
2434 "Invalid configuration (%s) and configuration modification "
2435 "was not requested", wrong_conf);
2436 return WERR_CAN_NOT_COMPLETE;
2439 /* check if we are able to manipulate configuration */
2441 if (!lp_config_backend_is_registry()) {
2442 libnet_join_set_error_string(mem_ctx, r,
2443 "Configuration manipulation requested but not "
2444 "supported by backend");
2445 return WERR_NOT_SUPPORTED;
2448 return WERR_OK;
2451 /****************************************************************
2452 ****************************************************************/
2454 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2455 struct libnet_JoinCtx *r)
2457 NTSTATUS status;
2458 WERROR werr;
2459 struct cli_state *cli = NULL;
2460 #ifdef HAVE_ADS
2461 ADS_STATUS ads_status;
2462 #endif /* HAVE_ADS */
2463 const char *pre_connect_realm = NULL;
2464 const char *numeric_dcip = NULL;
2465 const char *sitename = NULL;
2467 /* Before contacting a DC, we can securely know
2468 * the realm only if the user specifies it.
2470 if (r->in.use_kerberos &&
2471 r->in.domain_name_type == JoinDomNameTypeDNS) {
2472 pre_connect_realm = r->in.domain_name;
2475 if (!r->in.dc_name) {
2476 struct netr_DsRGetDCNameInfo *info;
2477 const char *dc;
2478 uint32_t name_type_flags = 0;
2479 if (r->in.domain_name_type == JoinDomNameTypeDNS) {
2480 name_type_flags = DS_IS_DNS_NAME;
2481 } else if (r->in.domain_name_type == JoinDomNameTypeNBT) {
2482 name_type_flags = DS_IS_FLAT_NAME;
2484 status = dsgetdcname(mem_ctx,
2485 r->in.msg_ctx,
2486 r->in.domain_name,
2487 NULL,
2488 NULL,
2489 DS_FORCE_REDISCOVERY |
2490 DS_DIRECTORY_SERVICE_REQUIRED |
2491 DS_WRITABLE_REQUIRED |
2492 DS_RETURN_DNS_NAME |
2493 name_type_flags,
2494 &info);
2495 if (!NT_STATUS_IS_OK(status)) {
2496 libnet_join_set_error_string(mem_ctx, r,
2497 "failed to find DC for domain %s - %s",
2498 r->in.domain_name,
2499 get_friendly_nt_error_msg(status));
2500 return WERR_NERR_DCNOTFOUND;
2503 dc = strip_hostname(info->dc_unc);
2504 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2505 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2507 if (info->dc_address == NULL || info->dc_address[0] != '\\' ||
2508 info->dc_address[1] != '\\') {
2509 DBG_ERR("ill-formed DC address '%s'\n",
2510 info->dc_address);
2511 return WERR_NERR_DCNOTFOUND;
2514 numeric_dcip = info->dc_address + 2;
2515 sitename = info->dc_site_name;
2516 /* info goes out of scope but the memory stays
2517 allocated on the talloc context */
2520 if (pre_connect_realm != NULL) {
2521 struct sockaddr_storage ss = {0};
2523 if (numeric_dcip != NULL) {
2524 if (!interpret_string_addr(&ss, numeric_dcip,
2525 AI_NUMERICHOST)) {
2526 DBG_ERR(
2527 "cannot parse IP address '%s' of DC '%s'\n",
2528 numeric_dcip, r->in.dc_name);
2529 return WERR_NERR_DCNOTFOUND;
2531 } else {
2532 if (!interpret_string_addr(&ss, r->in.dc_name, 0)) {
2533 DBG_WARNING(
2534 "cannot resolve IP address of DC '%s'\n",
2535 r->in.dc_name);
2536 return WERR_NERR_DCNOTFOUND;
2540 /* The domain parameter is only used as modifier
2541 * to krb5.conf file name. .JOIN is is not a valid
2542 * NetBIOS name so it cannot clash with another domain
2543 * -- Uri.
2545 create_local_private_krb5_conf_for_domain(
2546 pre_connect_realm, ".JOIN", sitename, &ss);
2549 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2550 if (!NT_STATUS_IS_OK(status)) {
2551 libnet_join_set_error_string(mem_ctx, r,
2552 "failed to lookup DC info for domain '%s' over rpc: %s",
2553 r->in.domain_name, get_friendly_nt_error_msg(status));
2554 return ntstatus_to_werror(status);
2557 werr = libnet_join_check_config(mem_ctx, r);
2558 if (!W_ERROR_IS_OK(werr)) {
2559 goto done;
2562 #ifdef HAVE_ADS
2564 if (r->out.domain_is_ad) {
2565 create_local_private_krb5_conf_for_domain(
2566 r->out.dns_domain_name, r->out.netbios_domain_name,
2567 sitename, smbXcli_conn_remote_sockaddr(cli->conn));
2570 if (r->out.domain_is_ad &&
2571 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2573 const char *initial_account_ou = r->in.account_ou;
2576 * we want to create the msDS-SupportedEncryptionTypes attribute
2577 * as early as possible so always try an LDAP create as the user
2578 * first. We copy r->in.account_ou because it may be changed
2579 * during the machine pre-creation.
2582 ads_status = libnet_join_connect_ads_user(mem_ctx, r);
2583 if (!ADS_ERR_OK(ads_status)) {
2584 return WERR_NERR_DEFAULTJOINREQUIRED;
2587 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2588 if (ADS_ERR_OK(ads_status)) {
2591 * LDAP object create succeeded, now go to the rpc
2592 * password set routines
2595 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2596 goto rpc_join;
2599 if (initial_account_ou != NULL) {
2600 libnet_join_set_error_string(mem_ctx, r,
2601 "failed to precreate account in ou %s: %s",
2602 r->in.account_ou,
2603 ads_errstr(ads_status));
2604 return WERR_NERR_DEFAULTJOINREQUIRED;
2607 DEBUG(5, ("failed to precreate account in ou %s: %s",
2608 r->in.account_ou, ads_errstr(ads_status)));
2610 #endif /* HAVE_ADS */
2612 rpc_join:
2613 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2614 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2615 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2616 } else {
2617 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2619 if (!NT_STATUS_IS_OK(status)) {
2620 libnet_join_set_error_string(mem_ctx, r,
2621 "failed to join domain '%s' over rpc: %s",
2622 r->in.domain_name, get_friendly_nt_error_msg(status));
2623 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2624 return WERR_NERR_SETUPALREADYJOINED;
2626 werr = ntstatus_to_werror(status);
2627 goto done;
2630 werr = WERR_OK;
2632 done:
2633 if (cli) {
2634 cli_shutdown(cli);
2637 return werr;
2640 /****************************************************************
2641 ****************************************************************/
2643 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2644 struct libnet_JoinCtx *r)
2646 WERROR werr;
2647 struct libnet_UnjoinCtx *u = NULL;
2649 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2650 if (!W_ERROR_IS_OK(werr)) {
2651 return werr;
2654 u->in.debug = r->in.debug;
2655 u->in.dc_name = r->in.dc_name;
2656 u->in.domain_name = r->in.domain_name;
2657 u->in.admin_account = r->in.admin_account;
2658 u->in.admin_password = r->in.admin_password;
2659 u->in.modify_config = r->in.modify_config;
2660 u->in.use_kerberos = r->in.use_kerberos;
2661 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2662 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2664 werr = libnet_Unjoin(mem_ctx, u);
2665 TALLOC_FREE(u);
2667 return werr;
2670 /****************************************************************
2671 ****************************************************************/
2673 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2674 struct libnet_JoinCtx *r)
2676 WERROR werr;
2678 if (r->in.debug) {
2679 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2682 ZERO_STRUCT(r->out);
2684 werr = libnet_join_pre_processing(mem_ctx, r);
2685 if (!W_ERROR_IS_OK(werr)) {
2686 goto done;
2689 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2690 werr = libnet_DomainJoin(mem_ctx, r);
2691 if (!W_ERROR_IS_OK(werr)) {
2692 goto done;
2696 werr = libnet_join_post_processing(mem_ctx, r);
2697 if (!W_ERROR_IS_OK(werr)) {
2698 goto done;
2701 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2702 werr = libnet_join_post_verify(mem_ctx, r);
2703 if (!W_ERROR_IS_OK(werr)) {
2704 libnet_join_rollback(mem_ctx, r);
2708 done:
2709 r->out.result = werr;
2711 if (r->in.debug) {
2712 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2714 return werr;
2717 /****************************************************************
2718 ****************************************************************/
2720 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2721 struct libnet_UnjoinCtx *r)
2723 NTSTATUS status;
2725 if (!r->in.domain_sid) {
2726 struct dom_sid sid;
2727 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2728 libnet_unjoin_set_error_string(mem_ctx, r,
2729 "Unable to fetch domain sid: are we joined?");
2730 return WERR_NERR_SETUPNOTJOINED;
2732 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2733 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2736 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2737 !r->in.delete_machine_account) {
2738 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2739 return WERR_OK;
2742 if (!r->in.dc_name) {
2743 struct netr_DsRGetDCNameInfo *info;
2744 const char *dc;
2745 status = dsgetdcname(mem_ctx,
2746 r->in.msg_ctx,
2747 r->in.domain_name,
2748 NULL,
2749 NULL,
2750 DS_DIRECTORY_SERVICE_REQUIRED |
2751 DS_WRITABLE_REQUIRED |
2752 DS_RETURN_DNS_NAME,
2753 &info);
2754 if (!NT_STATUS_IS_OK(status)) {
2755 libnet_unjoin_set_error_string(mem_ctx, r,
2756 "failed to find DC for domain %s - %s",
2757 r->in.domain_name,
2758 get_friendly_nt_error_msg(status));
2759 return WERR_NERR_DCNOTFOUND;
2762 dc = strip_hostname(info->dc_unc);
2763 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2764 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2767 #ifdef HAVE_ADS
2768 /* for net ads leave, try to delete the account. If it works,
2769 no sense in disabling. If it fails, we can still try to
2770 disable it. jmcd */
2772 if (r->in.delete_machine_account) {
2773 ADS_STATUS ads_status;
2774 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2775 if (ADS_ERR_OK(ads_status)) {
2776 /* dirty hack */
2777 r->out.dns_domain_name =
2778 talloc_strdup(mem_ctx,
2779 r->in.ads->server.realm);
2780 ads_status =
2781 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2783 if (!ADS_ERR_OK(ads_status)) {
2784 libnet_unjoin_set_error_string(mem_ctx, r,
2785 "failed to remove machine account from AD: %s",
2786 ads_errstr(ads_status));
2787 } else {
2788 r->out.deleted_machine_account = true;
2789 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2790 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2791 return WERR_OK;
2794 #endif /* HAVE_ADS */
2796 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2797 "disable". */
2798 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2799 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2800 if (!NT_STATUS_IS_OK(status)) {
2801 libnet_unjoin_set_error_string(mem_ctx, r,
2802 "failed to disable machine account via rpc: %s",
2803 get_friendly_nt_error_msg(status));
2804 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2805 return WERR_NERR_SETUPNOTJOINED;
2807 return ntstatus_to_werror(status);
2810 r->out.disabled_machine_account = true;
2813 /* If disable succeeded or was not requested at all, we
2814 should be getting rid of our end of things */
2816 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2818 return WERR_OK;
2821 /****************************************************************
2822 ****************************************************************/
2824 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2825 struct libnet_UnjoinCtx *r)
2827 if (!r->in.domain_name) {
2828 libnet_unjoin_set_error_string(mem_ctx, r,
2829 "No domain name defined");
2830 return WERR_INVALID_PARAMETER;
2833 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2834 &r->in.domain_name,
2835 &r->in.dc_name)) {
2836 libnet_unjoin_set_error_string(mem_ctx, r,
2837 "Failed to parse domain name");
2838 return WERR_INVALID_PARAMETER;
2841 if (IS_DC) {
2842 return WERR_NERR_SETUPDOMAINCONTROLLER;
2845 if (!r->in.admin_domain) {
2846 char *admin_domain = NULL;
2847 char *admin_account = NULL;
2848 bool ok;
2850 ok = split_domain_user(mem_ctx,
2851 r->in.admin_account,
2852 &admin_domain,
2853 &admin_account);
2854 if (!ok) {
2855 return WERR_NOT_ENOUGH_MEMORY;
2858 if (admin_domain != NULL) {
2859 r->in.admin_domain = admin_domain;
2860 } else {
2861 r->in.admin_domain = r->in.domain_name;
2863 r->in.admin_account = admin_account;
2866 if (!secrets_init()) {
2867 libnet_unjoin_set_error_string(mem_ctx, r,
2868 "Unable to open secrets database");
2869 return WERR_CAN_NOT_COMPLETE;
2872 return WERR_OK;
2875 /****************************************************************
2876 ****************************************************************/
2878 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2879 struct libnet_UnjoinCtx *r)
2881 saf_delete(r->out.netbios_domain_name);
2882 saf_delete(r->out.dns_domain_name);
2884 return libnet_unjoin_config(r);
2887 /****************************************************************
2888 ****************************************************************/
2890 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2891 struct libnet_UnjoinCtx *r)
2893 WERROR werr;
2895 if (r->in.debug) {
2896 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2899 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2900 if (!W_ERROR_IS_OK(werr)) {
2901 goto done;
2904 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2905 werr = libnet_DomainUnjoin(mem_ctx, r);
2906 if (!W_ERROR_IS_OK(werr)) {
2907 libnet_unjoin_config(r);
2908 goto done;
2912 werr = libnet_unjoin_post_processing(mem_ctx, r);
2913 if (!W_ERROR_IS_OK(werr)) {
2914 goto done;
2917 done:
2918 r->out.result = werr;
2920 if (r->in.debug) {
2921 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2924 return werr;