s3:libads: Fix creating machine account using LDAP
[Samba.git] / source3 / libnet / libnet_join.c
blobd5c8599beeea9dded44c2e91fa16f1affcc52d59
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 DEBUG(1,("machine account creation 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 DEBUG(1,("machine account creation failed\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 /****************************************************************
487 Set a machines dNSHostName and servicePrincipalName attributes
488 ****************************************************************/
490 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
491 struct libnet_JoinCtx *r)
493 ADS_STATUS status;
494 ADS_MODLIST mods;
495 fstring my_fqdn;
496 const char **spn_array = NULL;
497 size_t num_spns = 0;
498 char *spn = NULL;
499 bool ok;
500 const char **netbios_aliases = NULL;
502 /* Find our DN */
504 status = libnet_join_find_machine_acct(mem_ctx, r);
505 if (!ADS_ERR_OK(status)) {
506 return status;
509 status = libnet_join_get_machine_spns(mem_ctx,
511 discard_const_p(char **, &spn_array),
512 &num_spns);
513 if (!ADS_ERR_OK(status)) {
514 DEBUG(5, ("Retrieving the servicePrincipalNames failed.\n"));
517 /* Windows only creates HOST/shortname & HOST/fqdn. */
519 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
520 if (!spn) {
521 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
523 if (!strupper_m(spn)) {
524 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
527 ok = ads_element_in_array(spn_array, num_spns, spn);
528 if (!ok) {
529 ok = add_string_to_array(spn_array, spn,
530 &spn_array, &num_spns);
531 if (!ok) {
532 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
536 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
537 || (strchr(my_fqdn, '.') == NULL)) {
538 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
539 r->out.dns_domain_name);
542 if (!strlower_m(my_fqdn)) {
543 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
546 if (!strequal(my_fqdn, r->in.machine_name)) {
547 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
548 if (!spn) {
549 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
552 ok = ads_element_in_array(spn_array, num_spns, spn);
553 if (!ok) {
554 ok = add_string_to_array(spn_array, spn,
555 &spn_array, &num_spns);
556 if (!ok) {
557 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
562 netbios_aliases = lp_netbios_aliases();
563 if (netbios_aliases != NULL) {
564 for (; *netbios_aliases != NULL; netbios_aliases++) {
566 * Add HOST/NETBIOSNAME
568 spn = talloc_asprintf(mem_ctx, "HOST/%s", *netbios_aliases);
569 if (spn == NULL) {
570 TALLOC_FREE(spn);
571 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
573 if (!strupper_m(spn)) {
574 TALLOC_FREE(spn);
575 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
578 ok = ads_element_in_array(spn_array, num_spns, spn);
579 if (ok) {
580 TALLOC_FREE(spn);
581 continue;
583 ok = add_string_to_array(spn_array, spn,
584 &spn_array, &num_spns);
585 if (!ok) {
586 TALLOC_FREE(spn);
587 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
589 TALLOC_FREE(spn);
592 * Add HOST/netbiosname.domainname
594 if (r->out.dns_domain_name == NULL) {
595 continue;
597 fstr_sprintf(my_fqdn, "%s.%s",
598 *netbios_aliases,
599 r->out.dns_domain_name);
601 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
602 if (spn == NULL) {
603 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
606 ok = ads_element_in_array(spn_array, num_spns, spn);
607 if (ok) {
608 TALLOC_FREE(spn);
609 continue;
611 ok = add_string_to_array(spn_array, spn,
612 &spn_array, &num_spns);
613 if (!ok) {
614 TALLOC_FREE(spn);
615 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
617 TALLOC_FREE(spn);
621 /* make sure to NULL terminate the array */
622 spn_array = talloc_realloc(mem_ctx, spn_array, const char *, num_spns + 1);
623 if (spn_array == NULL) {
624 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
626 spn_array[num_spns] = NULL;
628 mods = ads_init_mods(mem_ctx);
629 if (!mods) {
630 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
633 /* fields of primary importance */
635 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
636 if (!ADS_ERR_OK(status)) {
637 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
640 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
641 spn_array);
642 if (!ADS_ERR_OK(status)) {
643 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
646 return ads_gen_mod(r->in.ads, r->out.dn, mods);
649 /****************************************************************
650 ****************************************************************/
652 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
653 struct libnet_JoinCtx *r)
655 ADS_STATUS status;
656 ADS_MODLIST mods;
658 if (!r->in.create_upn) {
659 return ADS_SUCCESS;
662 /* Find our DN */
664 status = libnet_join_find_machine_acct(mem_ctx, r);
665 if (!ADS_ERR_OK(status)) {
666 return status;
669 if (!r->in.upn) {
670 const char *realm = r->out.dns_domain_name;
672 /* in case we are about to generate a keytab during the join
673 * make sure the default upn we create is usable with kinit -k.
674 * gd */
676 if (USE_KERBEROS_KEYTAB) {
677 realm = talloc_strdup_upper(mem_ctx,
678 r->out.dns_domain_name);
681 if (!realm) {
682 return ADS_ERROR(LDAP_NO_MEMORY);
685 r->in.upn = talloc_asprintf(mem_ctx,
686 "host/%s@%s",
687 r->in.machine_name,
688 realm);
689 if (!r->in.upn) {
690 return ADS_ERROR(LDAP_NO_MEMORY);
694 /* now do the mods */
696 mods = ads_init_mods(mem_ctx);
697 if (!mods) {
698 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
701 /* fields of primary importance */
703 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
704 if (!ADS_ERR_OK(status)) {
705 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
708 return ads_gen_mod(r->in.ads, r->out.dn, mods);
712 /****************************************************************
713 ****************************************************************/
715 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
716 struct libnet_JoinCtx *r)
718 ADS_STATUS status;
719 ADS_MODLIST mods;
720 char *os_sp = NULL;
722 if (!r->in.os_name || !r->in.os_version ) {
723 return ADS_SUCCESS;
726 /* Find our DN */
728 status = libnet_join_find_machine_acct(mem_ctx, r);
729 if (!ADS_ERR_OK(status)) {
730 return status;
733 /* now do the mods */
735 mods = ads_init_mods(mem_ctx);
736 if (!mods) {
737 return ADS_ERROR(LDAP_NO_MEMORY);
740 if (r->in.os_servicepack) {
742 * if blank string then leave os_sp equal to NULL to force
743 * attribute delete (LDAP_MOD_DELETE)
745 if (!strequal(r->in.os_servicepack,"")) {
746 os_sp = talloc_strdup(mem_ctx, r->in.os_servicepack);
748 } else {
749 os_sp = talloc_asprintf(mem_ctx, "Samba %s",
750 samba_version_string());
752 if (!os_sp && !strequal(r->in.os_servicepack,"")) {
753 return ADS_ERROR(LDAP_NO_MEMORY);
756 /* fields of primary importance */
758 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
759 r->in.os_name);
760 if (!ADS_ERR_OK(status)) {
761 return status;
764 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
765 r->in.os_version);
766 if (!ADS_ERR_OK(status)) {
767 return status;
770 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
771 os_sp);
772 if (!ADS_ERR_OK(status)) {
773 return status;
776 return ads_gen_mod(r->in.ads, r->out.dn, mods);
779 /****************************************************************
780 ****************************************************************/
782 static ADS_STATUS libnet_join_set_etypes(TALLOC_CTX *mem_ctx,
783 struct libnet_JoinCtx *r)
785 ADS_STATUS status;
786 ADS_MODLIST mods;
787 const char *etype_list_str;
789 etype_list_str = talloc_asprintf(mem_ctx, "%d",
790 r->in.desired_encryption_types);
791 if (!etype_list_str) {
792 return ADS_ERROR(LDAP_NO_MEMORY);
795 /* Find our DN */
797 status = libnet_join_find_machine_acct(mem_ctx, r);
798 if (!ADS_ERR_OK(status)) {
799 return status;
802 if (r->in.desired_encryption_types == r->out.set_encryption_types) {
803 return ADS_SUCCESS;
806 /* now do the mods */
808 mods = ads_init_mods(mem_ctx);
809 if (!mods) {
810 return ADS_ERROR(LDAP_NO_MEMORY);
813 status = ads_mod_str(mem_ctx, &mods, "msDS-SupportedEncryptionTypes",
814 etype_list_str);
815 if (!ADS_ERR_OK(status)) {
816 return status;
819 status = ads_gen_mod(r->in.ads, r->out.dn, mods);
820 if (!ADS_ERR_OK(status)) {
821 return status;
824 r->out.set_encryption_types = r->in.desired_encryption_types;
826 return ADS_SUCCESS;
829 /****************************************************************
830 ****************************************************************/
832 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
833 struct libnet_JoinCtx *r)
835 if (!USE_SYSTEM_KEYTAB) {
836 return true;
839 if (ads_keytab_create_default(r->in.ads) != 0) {
840 return false;
843 return true;
846 /****************************************************************
847 ****************************************************************/
849 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
850 struct libnet_JoinCtx *r)
852 uint32_t domain_func;
853 ADS_STATUS status;
854 const char *salt = NULL;
855 char *std_salt = NULL;
857 status = ads_domain_func_level(r->in.ads, &domain_func);
858 if (!ADS_ERR_OK(status)) {
859 libnet_join_set_error_string(mem_ctx, r,
860 "failed to determine domain functional level: %s",
861 ads_errstr(status));
862 return false;
865 /* go ahead and setup the default salt */
867 std_salt = kerberos_standard_des_salt();
868 if (!std_salt) {
869 libnet_join_set_error_string(mem_ctx, r,
870 "failed to obtain standard DES salt");
871 return false;
874 salt = talloc_strdup(mem_ctx, std_salt);
875 if (!salt) {
876 return false;
879 SAFE_FREE(std_salt);
881 /* if it's a Windows functional domain, we have to look for the UPN */
883 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
884 char *upn;
886 upn = ads_get_upn(r->in.ads, mem_ctx,
887 r->in.machine_name);
888 if (upn) {
889 salt = talloc_strdup(mem_ctx, upn);
890 if (!salt) {
891 return false;
896 r->out.krb5_salt = salt;
897 return true;
900 /****************************************************************
901 ****************************************************************/
903 static ADS_STATUS libnet_join_post_processing_ads_modify(TALLOC_CTX *mem_ctx,
904 struct libnet_JoinCtx *r)
906 ADS_STATUS status;
907 bool need_etype_update = false;
909 if (!r->in.ads) {
910 status = libnet_join_connect_ads_user(mem_ctx, r);
911 if (!ADS_ERR_OK(status)) {
912 return status;
916 status = libnet_join_set_machine_spn(mem_ctx, r);
917 if (!ADS_ERR_OK(status)) {
918 libnet_join_set_error_string(mem_ctx, r,
919 "Failed to set machine spn: %s\n"
920 "Do you have sufficient permissions to create machine "
921 "accounts?",
922 ads_errstr(status));
923 return status;
926 status = libnet_join_set_os_attributes(mem_ctx, r);
927 if (!ADS_ERR_OK(status)) {
928 libnet_join_set_error_string(mem_ctx, r,
929 "failed to set machine os attributes: %s",
930 ads_errstr(status));
931 return status;
934 status = libnet_join_set_machine_upn(mem_ctx, r);
935 if (!ADS_ERR_OK(status)) {
936 libnet_join_set_error_string(mem_ctx, r,
937 "failed to set machine upn: %s",
938 ads_errstr(status));
939 return status;
942 status = libnet_join_find_machine_acct(mem_ctx, r);
943 if (!ADS_ERR_OK(status)) {
944 return status;
947 if (r->in.desired_encryption_types != r->out.set_encryption_types) {
948 uint32_t func_level = 0;
950 status = ads_domain_func_level(r->in.ads, &func_level);
951 if (!ADS_ERR_OK(status)) {
952 libnet_join_set_error_string(mem_ctx, r,
953 "failed to query domain controller functional level: %s",
954 ads_errstr(status));
955 return status;
958 if (func_level >= DS_DOMAIN_FUNCTION_2008) {
959 need_etype_update = true;
963 if (need_etype_update) {
965 * We need to reconnect as machine account in order
966 * to update msDS-SupportedEncryptionTypes reliable
969 if (r->in.ads->auth.ccache_name != NULL) {
970 ads_kdestroy(r->in.ads->auth.ccache_name);
973 ads_destroy(&r->in.ads);
975 status = libnet_join_connect_ads_machine(mem_ctx, r);
976 if (!ADS_ERR_OK(status)) {
977 libnet_join_set_error_string(mem_ctx, r,
978 "Failed to connect as machine account: %s",
979 ads_errstr(status));
980 return status;
983 status = libnet_join_set_etypes(mem_ctx, r);
984 if (!ADS_ERR_OK(status)) {
985 libnet_join_set_error_string(mem_ctx, r,
986 "failed to set machine kerberos encryption types: %s",
987 ads_errstr(status));
988 return status;
992 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
993 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
996 return ADS_SUCCESS;
999 static ADS_STATUS libnet_join_post_processing_ads_sync(TALLOC_CTX *mem_ctx,
1000 struct libnet_JoinCtx *r)
1002 if (!libnet_join_create_keytab(mem_ctx, r)) {
1003 libnet_join_set_error_string(mem_ctx, r,
1004 "failed to create kerberos keytab");
1005 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1008 return ADS_SUCCESS;
1010 #endif /* HAVE_ADS */
1012 /****************************************************************
1013 Store the machine password and domain SID
1014 ****************************************************************/
1016 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
1017 struct libnet_JoinCtx *r)
1019 NTSTATUS status;
1021 status = secrets_store_JoinCtx(r);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 DBG_ERR("secrets_store_JoinCtx() failed %s\n",
1024 nt_errstr(status));
1025 return false;
1028 return true;
1031 /****************************************************************
1032 Connect dc's IPC$ share
1033 ****************************************************************/
1035 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
1036 const char *user,
1037 const char *domain,
1038 const char *pass,
1039 bool use_kerberos,
1040 struct cli_state **cli)
1042 int flags = 0;
1044 if (use_kerberos) {
1045 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1048 if (use_kerberos && pass) {
1049 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1052 return cli_full_connection(cli, NULL,
1054 NULL, 0,
1055 "IPC$", "IPC",
1056 user,
1057 domain,
1058 pass,
1059 flags,
1060 SMB_SIGNING_IPC_DEFAULT);
1063 /****************************************************************
1064 Lookup domain dc's info
1065 ****************************************************************/
1067 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
1068 struct libnet_JoinCtx *r,
1069 struct cli_state **cli)
1071 struct rpc_pipe_client *pipe_hnd = NULL;
1072 struct policy_handle lsa_pol;
1073 NTSTATUS status, result;
1074 union lsa_PolicyInformation *info = NULL;
1075 struct dcerpc_binding_handle *b;
1076 const char *account = r->in.admin_account;
1077 const char *domain = r->in.admin_domain;
1078 const char *password = r->in.admin_password;
1079 bool use_kerberos = r->in.use_kerberos;
1081 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) {
1082 account = "";
1083 domain = "";
1084 password = NULL;
1085 use_kerberos = false;
1088 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1089 account,
1090 domain,
1091 password,
1092 use_kerberos,
1093 cli);
1094 if (!NT_STATUS_IS_OK(status)) {
1095 goto done;
1098 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc,
1099 &pipe_hnd);
1100 if (!NT_STATUS_IS_OK(status)) {
1101 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
1102 nt_errstr(status)));
1103 goto done;
1106 b = pipe_hnd->binding_handle;
1108 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
1109 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1110 if (!NT_STATUS_IS_OK(status)) {
1111 goto done;
1114 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
1115 &lsa_pol,
1116 LSA_POLICY_INFO_DNS,
1117 &info,
1118 &result);
1119 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
1120 r->out.domain_is_ad = true;
1121 r->out.netbios_domain_name = info->dns.name.string;
1122 r->out.dns_domain_name = info->dns.dns_domain.string;
1123 r->out.forest_name = info->dns.dns_forest.string;
1124 r->out.domain_guid = info->dns.domain_guid;
1125 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
1126 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1129 if (!NT_STATUS_IS_OK(status)) {
1130 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
1131 &lsa_pol,
1132 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
1133 &info,
1134 &result);
1135 if (!NT_STATUS_IS_OK(status)) {
1136 goto done;
1138 if (!NT_STATUS_IS_OK(result)) {
1139 status = result;
1140 goto done;
1143 r->out.netbios_domain_name = info->account_domain.name.string;
1144 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
1145 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
1148 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1149 TALLOC_FREE(pipe_hnd);
1151 done:
1152 return status;
1155 /****************************************************************
1156 Do the domain join unsecure
1157 ****************************************************************/
1159 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
1160 struct libnet_JoinCtx *r,
1161 struct cli_state *cli)
1163 TALLOC_CTX *frame = talloc_stackframe();
1164 struct rpc_pipe_client *authenticate_pipe = NULL;
1165 struct rpc_pipe_client *passwordset_pipe = NULL;
1166 struct cli_credentials *cli_creds;
1167 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1168 struct netlogon_creds_CredentialState *creds = NULL;
1169 uint32_t netlogon_flags = 0;
1170 size_t len = 0;
1171 bool ok;
1172 DATA_BLOB new_trust_blob = data_blob_null;
1173 NTSTATUS status;
1175 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon,
1176 &authenticate_pipe);
1177 if (!NT_STATUS_IS_OK(status)) {
1178 TALLOC_FREE(frame);
1179 return status;
1182 if (!r->in.machine_password) {
1183 int security = r->in.ads ? SEC_ADS : SEC_DOMAIN;
1185 r->in.machine_password = trust_pw_new_value(mem_ctx,
1186 r->in.secure_channel_type,
1187 security);
1188 if (r->in.machine_password == NULL) {
1189 TALLOC_FREE(frame);
1190 return NT_STATUS_NO_MEMORY;
1194 cli_creds = cli_credentials_init(talloc_tos());
1195 if (cli_creds == NULL) {
1196 TALLOC_FREE(frame);
1197 return NT_STATUS_NO_MEMORY;
1200 cli_credentials_set_username(cli_creds, r->out.account_name,
1201 CRED_SPECIFIED);
1202 cli_credentials_set_domain(cli_creds, r->in.domain_name,
1203 CRED_SPECIFIED);
1204 cli_credentials_set_realm(cli_creds, "", CRED_SPECIFIED);
1205 cli_credentials_set_secure_channel_type(cli_creds,
1206 r->in.secure_channel_type);
1208 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
1209 cli_credentials_set_password(cli_creds, r->in.admin_password,
1210 CRED_SPECIFIED);
1212 status = rpccli_create_netlogon_creds_ctx(
1213 cli_creds, authenticate_pipe->desthost, r->in.msg_ctx,
1214 frame, &netlogon_creds);
1215 if (!NT_STATUS_IS_OK(status)) {
1216 TALLOC_FREE(frame);
1217 return status;
1220 status = rpccli_setup_netlogon_creds(
1221 cli, NCACN_NP, netlogon_creds, true /* force_reauth */,
1222 cli_creds);
1223 if (!NT_STATUS_IS_OK(status)) {
1224 TALLOC_FREE(frame);
1225 return status;
1228 status = netlogon_creds_cli_get(netlogon_creds, frame, &creds);
1229 if (!NT_STATUS_IS_OK(status)) {
1230 TALLOC_FREE(frame);
1231 return status;
1234 netlogon_flags = creds->negotiate_flags;
1235 TALLOC_FREE(creds);
1237 if (netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC) {
1238 status = cli_rpc_pipe_open_schannel_with_creds(cli,
1239 &ndr_table_netlogon,
1240 NCACN_NP,
1241 netlogon_creds,
1242 &passwordset_pipe);
1243 if (!NT_STATUS_IS_OK(status)) {
1244 TALLOC_FREE(frame);
1245 return status;
1247 } else {
1248 passwordset_pipe = authenticate_pipe;
1251 len = strlen(r->in.machine_password);
1252 ok = convert_string_talloc(frame, CH_UNIX, CH_UTF16,
1253 r->in.machine_password, len,
1254 (void **)&new_trust_blob.data,
1255 &new_trust_blob.length);
1256 if (!ok) {
1257 status = NT_STATUS_UNMAPPABLE_CHARACTER;
1258 if (errno == ENOMEM) {
1259 status = NT_STATUS_NO_MEMORY;
1261 TALLOC_FREE(frame);
1262 return status;
1265 status = netlogon_creds_cli_ServerPasswordSet(netlogon_creds,
1266 passwordset_pipe->binding_handle,
1267 &new_trust_blob,
1268 NULL); /* new_version */
1269 if (!NT_STATUS_IS_OK(status)) {
1270 TALLOC_FREE(frame);
1271 return status;
1274 TALLOC_FREE(frame);
1275 return NT_STATUS_OK;
1278 /****************************************************************
1279 Do the domain join
1280 ****************************************************************/
1282 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
1283 struct libnet_JoinCtx *r,
1284 struct cli_state *cli)
1286 struct rpc_pipe_client *pipe_hnd = NULL;
1287 struct policy_handle sam_pol, domain_pol, user_pol;
1288 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1289 char *acct_name;
1290 struct lsa_String lsa_acct_name;
1291 uint32_t user_rid;
1292 uint32_t acct_flags = ACB_WSTRUST;
1293 struct samr_Ids user_rids;
1294 struct samr_Ids name_types;
1295 union samr_UserInfo user_info;
1296 struct dcerpc_binding_handle *b = NULL;
1297 unsigned int old_timeout = 0;
1299 DATA_BLOB session_key = data_blob_null;
1300 struct samr_CryptPassword crypt_pwd;
1301 struct samr_CryptPasswordEx crypt_pwd_ex;
1303 ZERO_STRUCT(sam_pol);
1304 ZERO_STRUCT(domain_pol);
1305 ZERO_STRUCT(user_pol);
1307 switch (r->in.secure_channel_type) {
1308 case SEC_CHAN_WKSTA:
1309 acct_flags = ACB_WSTRUST;
1310 break;
1311 case SEC_CHAN_BDC:
1312 acct_flags = ACB_SVRTRUST;
1313 break;
1314 default:
1315 return NT_STATUS_INVALID_PARAMETER;
1318 if (!r->in.machine_password) {
1319 int security = r->in.ads ? SEC_ADS : SEC_DOMAIN;
1321 r->in.machine_password = trust_pw_new_value(mem_ctx,
1322 r->in.secure_channel_type,
1323 security);
1324 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
1327 /* Open the domain */
1329 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1330 &pipe_hnd);
1331 if (!NT_STATUS_IS_OK(status)) {
1332 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1333 nt_errstr(status)));
1334 goto done;
1337 b = pipe_hnd->binding_handle;
1339 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
1340 if (!NT_STATUS_IS_OK(status)) {
1341 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
1342 nt_errstr(status)));
1343 goto done;
1346 status = dcerpc_samr_Connect2(b, mem_ctx,
1347 pipe_hnd->desthost,
1348 SAMR_ACCESS_ENUM_DOMAINS
1349 | SAMR_ACCESS_LOOKUP_DOMAIN,
1350 &sam_pol,
1351 &result);
1352 if (!NT_STATUS_IS_OK(status)) {
1353 goto done;
1355 if (!NT_STATUS_IS_OK(result)) {
1356 status = result;
1357 goto done;
1360 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1361 &sam_pol,
1362 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
1363 | SAMR_DOMAIN_ACCESS_CREATE_USER
1364 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
1365 r->out.domain_sid,
1366 &domain_pol,
1367 &result);
1368 if (!NT_STATUS_IS_OK(status)) {
1369 goto done;
1371 if (!NT_STATUS_IS_OK(result)) {
1372 status = result;
1373 goto done;
1376 /* Create domain user */
1378 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1379 if (!strlower_m(acct_name)) {
1380 status = NT_STATUS_INVALID_PARAMETER;
1381 goto done;
1384 init_lsa_String(&lsa_acct_name, acct_name);
1386 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
1387 uint32_t access_desired =
1388 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1389 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1390 SAMR_USER_ACCESS_SET_PASSWORD |
1391 SAMR_USER_ACCESS_GET_ATTRIBUTES |
1392 SAMR_USER_ACCESS_SET_ATTRIBUTES;
1393 uint32_t access_granted = 0;
1395 DEBUG(10,("Creating account with desired access mask: %d\n",
1396 access_desired));
1398 status = dcerpc_samr_CreateUser2(b, mem_ctx,
1399 &domain_pol,
1400 &lsa_acct_name,
1401 acct_flags,
1402 access_desired,
1403 &user_pol,
1404 &access_granted,
1405 &user_rid,
1406 &result);
1407 if (!NT_STATUS_IS_OK(status)) {
1408 goto done;
1411 status = result;
1412 if (!NT_STATUS_IS_OK(status) &&
1413 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1415 DEBUG(10,("Creation of workstation account failed: %s\n",
1416 nt_errstr(status)));
1418 /* If NT_STATUS_ACCESS_DENIED then we have a valid
1419 username/password combo but the user does not have
1420 administrator access. */
1422 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1423 libnet_join_set_error_string(mem_ctx, r,
1424 "User specified does not have "
1425 "administrator privileges");
1428 goto done;
1431 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1432 if (!(r->in.join_flags &
1433 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
1434 goto done;
1438 /* We *must* do this.... don't ask... */
1440 if (NT_STATUS_IS_OK(status)) {
1441 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1445 status = dcerpc_samr_LookupNames(b, mem_ctx,
1446 &domain_pol,
1448 &lsa_acct_name,
1449 &user_rids,
1450 &name_types,
1451 &result);
1452 if (!NT_STATUS_IS_OK(status)) {
1453 goto done;
1455 if (!NT_STATUS_IS_OK(result)) {
1456 status = result;
1457 goto done;
1459 if (user_rids.count != 1) {
1460 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1461 goto done;
1463 if (name_types.count != 1) {
1464 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1465 goto done;
1468 if (name_types.ids[0] != SID_NAME_USER) {
1469 DEBUG(0,("%s is not a user account (type=%d)\n",
1470 acct_name, name_types.ids[0]));
1471 status = NT_STATUS_INVALID_WORKSTATION;
1472 goto done;
1475 user_rid = user_rids.ids[0];
1477 /* Open handle on user */
1479 status = dcerpc_samr_OpenUser(b, mem_ctx,
1480 &domain_pol,
1481 SEC_FLAG_MAXIMUM_ALLOWED,
1482 user_rid,
1483 &user_pol,
1484 &result);
1485 if (!NT_STATUS_IS_OK(status)) {
1486 goto done;
1488 if (!NT_STATUS_IS_OK(result)) {
1489 status = result;
1490 goto done;
1493 /* Fill in the additional account flags now */
1495 acct_flags |= ACB_PWNOEXP;
1497 /* Set account flags on machine account */
1498 ZERO_STRUCT(user_info.info16);
1499 user_info.info16.acct_flags = acct_flags;
1501 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1502 &user_pol,
1503 UserControlInformation,
1504 &user_info,
1505 &result);
1506 if (!NT_STATUS_IS_OK(status)) {
1507 dcerpc_samr_DeleteUser(b, mem_ctx,
1508 &user_pol,
1509 &result);
1511 libnet_join_set_error_string(mem_ctx, r,
1512 "Failed to set account flags for machine account (%s)\n",
1513 nt_errstr(status));
1514 goto done;
1517 if (!NT_STATUS_IS_OK(result)) {
1518 status = result;
1520 dcerpc_samr_DeleteUser(b, mem_ctx,
1521 &user_pol,
1522 &result);
1524 libnet_join_set_error_string(mem_ctx, r,
1525 "Failed to set account flags for machine account (%s)\n",
1526 nt_errstr(status));
1527 goto done;
1530 /* Set password on machine account - first try level 26 */
1533 * increase the timeout as password filter modules on the DC
1534 * might delay the operation for a significant amount of time
1536 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1538 init_samr_CryptPasswordEx(r->in.machine_password,
1539 &session_key,
1540 &crypt_pwd_ex);
1542 user_info.info26.password = crypt_pwd_ex;
1543 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1545 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1546 &user_pol,
1547 UserInternal5InformationNew,
1548 &user_info,
1549 &result);
1551 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1553 /* retry with level 24 */
1555 init_samr_CryptPassword(r->in.machine_password,
1556 &session_key,
1557 &crypt_pwd);
1559 user_info.info24.password = crypt_pwd;
1560 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1562 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1563 &user_pol,
1564 UserInternal5Information,
1565 &user_info,
1566 &result);
1569 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1571 if (!NT_STATUS_IS_OK(status)) {
1573 dcerpc_samr_DeleteUser(b, mem_ctx,
1574 &user_pol,
1575 &result);
1577 libnet_join_set_error_string(mem_ctx, r,
1578 "Failed to set password for machine account (%s)\n",
1579 nt_errstr(status));
1580 goto done;
1582 if (!NT_STATUS_IS_OK(result)) {
1583 status = result;
1585 dcerpc_samr_DeleteUser(b, mem_ctx,
1586 &user_pol,
1587 &result);
1589 libnet_join_set_error_string(mem_ctx, r,
1590 "Failed to set password for machine account (%s)\n",
1591 nt_errstr(status));
1592 goto done;
1595 status = NT_STATUS_OK;
1597 done:
1598 if (!pipe_hnd) {
1599 return status;
1602 data_blob_clear_free(&session_key);
1604 if (is_valid_policy_hnd(&sam_pol)) {
1605 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1607 if (is_valid_policy_hnd(&domain_pol)) {
1608 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1610 if (is_valid_policy_hnd(&user_pol)) {
1611 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1613 TALLOC_FREE(pipe_hnd);
1615 return status;
1618 /****************************************************************
1619 ****************************************************************/
1621 NTSTATUS libnet_join_ok(struct messaging_context *msg_ctx,
1622 const char *netbios_domain_name,
1623 const char *dc_name,
1624 const bool use_kerberos)
1626 TALLOC_CTX *frame = talloc_stackframe();
1627 struct cli_state *cli = NULL;
1628 struct rpc_pipe_client *netlogon_pipe = NULL;
1629 struct cli_credentials *cli_creds = NULL;
1630 struct netlogon_creds_cli_context *netlogon_creds = NULL;
1631 struct netlogon_creds_CredentialState *creds = NULL;
1632 uint32_t netlogon_flags = 0;
1633 NTSTATUS status;
1634 int flags = 0;
1636 if (!dc_name) {
1637 TALLOC_FREE(frame);
1638 return NT_STATUS_INVALID_PARAMETER;
1641 if (!secrets_init()) {
1642 TALLOC_FREE(frame);
1643 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1646 status = pdb_get_trust_credentials(netbios_domain_name, NULL,
1647 frame, &cli_creds);
1648 if (!NT_STATUS_IS_OK(status)) {
1649 TALLOC_FREE(frame);
1650 return status;
1653 /* we don't want any old password */
1654 cli_credentials_set_old_password(cli_creds, NULL, CRED_SPECIFIED);
1656 if (use_kerberos) {
1657 cli_credentials_set_kerberos_state(cli_creds,
1658 CRED_MUST_USE_KERBEROS);
1661 status = cli_full_connection_creds(&cli, NULL,
1662 dc_name,
1663 NULL, 0,
1664 "IPC$", "IPC",
1665 cli_creds,
1666 flags,
1667 SMB_SIGNING_IPC_DEFAULT);
1669 if (!NT_STATUS_IS_OK(status)) {
1670 status = cli_full_connection(&cli, NULL,
1671 dc_name,
1672 NULL, 0,
1673 "IPC$", "IPC",
1675 NULL,
1678 SMB_SIGNING_IPC_DEFAULT);
1681 if (!NT_STATUS_IS_OK(status)) {
1682 TALLOC_FREE(frame);
1683 return status;
1686 status = rpccli_create_netlogon_creds_ctx(cli_creds,
1687 dc_name,
1688 msg_ctx,
1689 frame,
1690 &netlogon_creds);
1691 if (!NT_STATUS_IS_OK(status)) {
1692 cli_shutdown(cli);
1693 TALLOC_FREE(frame);
1694 return status;
1697 status = rpccli_setup_netlogon_creds(cli, NCACN_NP,
1698 netlogon_creds,
1699 true, /* force_reauth */
1700 cli_creds);
1701 if (!NT_STATUS_IS_OK(status)) {
1702 DEBUG(0,("connect_to_domain_password_server: "
1703 "unable to open the domain client session to "
1704 "machine %s. Flags[0x%08X] Error was : %s.\n",
1705 dc_name, (unsigned)netlogon_flags,
1706 nt_errstr(status)));
1707 cli_shutdown(cli);
1708 TALLOC_FREE(frame);
1709 return status;
1712 status = netlogon_creds_cli_get(netlogon_creds,
1713 talloc_tos(),
1714 &creds);
1715 if (!NT_STATUS_IS_OK(status)) {
1716 cli_shutdown(cli);
1717 TALLOC_FREE(frame);
1718 return status;
1720 netlogon_flags = creds->negotiate_flags;
1721 TALLOC_FREE(creds);
1723 if (!(netlogon_flags & NETLOGON_NEG_AUTHENTICATED_RPC)) {
1724 cli_shutdown(cli);
1725 TALLOC_FREE(frame);
1726 return NT_STATUS_OK;
1729 status = cli_rpc_pipe_open_schannel_with_creds(
1730 cli, &ndr_table_netlogon, NCACN_NP,
1731 netlogon_creds, &netlogon_pipe);
1733 TALLOC_FREE(netlogon_pipe);
1735 if (!NT_STATUS_IS_OK(status)) {
1736 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1737 "on netlogon pipe to server %s for domain %s. "
1738 "Error was %s\n",
1739 smbXcli_conn_remote_name(cli->conn),
1740 netbios_domain_name, nt_errstr(status)));
1741 cli_shutdown(cli);
1742 TALLOC_FREE(frame);
1743 return status;
1746 cli_shutdown(cli);
1747 TALLOC_FREE(frame);
1748 return NT_STATUS_OK;
1751 /****************************************************************
1752 ****************************************************************/
1754 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1755 struct libnet_JoinCtx *r)
1757 NTSTATUS status;
1759 status = libnet_join_ok(r->in.msg_ctx,
1760 r->out.netbios_domain_name,
1761 r->in.dc_name,
1762 r->in.use_kerberos);
1763 if (!NT_STATUS_IS_OK(status)) {
1764 libnet_join_set_error_string(mem_ctx, r,
1765 "failed to verify domain membership after joining: %s",
1766 get_friendly_nt_error_msg(status));
1767 return WERR_NERR_SETUPNOTJOINED;
1770 return WERR_OK;
1773 /****************************************************************
1774 ****************************************************************/
1776 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1777 struct libnet_UnjoinCtx *r)
1780 * TODO: use values from 'struct libnet_UnjoinCtx' ?
1782 return secrets_delete_machine_password_ex(lp_workgroup(), lp_realm());
1785 /****************************************************************
1786 ****************************************************************/
1788 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1789 struct libnet_UnjoinCtx *r)
1791 struct cli_state *cli = NULL;
1792 struct rpc_pipe_client *pipe_hnd = NULL;
1793 struct policy_handle sam_pol, domain_pol, user_pol;
1794 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1795 char *acct_name;
1796 uint32_t user_rid;
1797 struct lsa_String lsa_acct_name;
1798 struct samr_Ids user_rids;
1799 struct samr_Ids name_types;
1800 union samr_UserInfo *info = NULL;
1801 struct dcerpc_binding_handle *b = NULL;
1803 ZERO_STRUCT(sam_pol);
1804 ZERO_STRUCT(domain_pol);
1805 ZERO_STRUCT(user_pol);
1807 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1808 r->in.admin_account,
1809 r->in.admin_domain,
1810 r->in.admin_password,
1811 r->in.use_kerberos,
1812 &cli);
1813 if (!NT_STATUS_IS_OK(status)) {
1814 goto done;
1817 /* Open the domain */
1819 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
1820 &pipe_hnd);
1821 if (!NT_STATUS_IS_OK(status)) {
1822 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1823 nt_errstr(status)));
1824 goto done;
1827 b = pipe_hnd->binding_handle;
1829 status = dcerpc_samr_Connect2(b, mem_ctx,
1830 pipe_hnd->desthost,
1831 SEC_FLAG_MAXIMUM_ALLOWED,
1832 &sam_pol,
1833 &result);
1834 if (!NT_STATUS_IS_OK(status)) {
1835 goto done;
1837 if (!NT_STATUS_IS_OK(result)) {
1838 status = result;
1839 goto done;
1842 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1843 &sam_pol,
1844 SEC_FLAG_MAXIMUM_ALLOWED,
1845 r->in.domain_sid,
1846 &domain_pol,
1847 &result);
1848 if (!NT_STATUS_IS_OK(status)) {
1849 goto done;
1851 if (!NT_STATUS_IS_OK(result)) {
1852 status = result;
1853 goto done;
1856 /* Create domain user */
1858 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1859 if (!strlower_m(acct_name)) {
1860 status = NT_STATUS_INVALID_PARAMETER;
1861 goto done;
1864 init_lsa_String(&lsa_acct_name, acct_name);
1866 status = dcerpc_samr_LookupNames(b, mem_ctx,
1867 &domain_pol,
1869 &lsa_acct_name,
1870 &user_rids,
1871 &name_types,
1872 &result);
1874 if (!NT_STATUS_IS_OK(status)) {
1875 goto done;
1877 if (!NT_STATUS_IS_OK(result)) {
1878 status = result;
1879 goto done;
1881 if (user_rids.count != 1) {
1882 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1883 goto done;
1885 if (name_types.count != 1) {
1886 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1887 goto done;
1890 if (name_types.ids[0] != SID_NAME_USER) {
1891 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1892 name_types.ids[0]));
1893 status = NT_STATUS_INVALID_WORKSTATION;
1894 goto done;
1897 user_rid = user_rids.ids[0];
1899 /* Open handle on user */
1901 status = dcerpc_samr_OpenUser(b, mem_ctx,
1902 &domain_pol,
1903 SEC_FLAG_MAXIMUM_ALLOWED,
1904 user_rid,
1905 &user_pol,
1906 &result);
1907 if (!NT_STATUS_IS_OK(status)) {
1908 goto done;
1910 if (!NT_STATUS_IS_OK(result)) {
1911 status = result;
1912 goto done;
1915 /* Get user info */
1917 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1918 &user_pol,
1920 &info,
1921 &result);
1922 if (!NT_STATUS_IS_OK(status)) {
1923 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1924 goto done;
1926 if (!NT_STATUS_IS_OK(result)) {
1927 status = result;
1928 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1929 goto done;
1932 /* now disable and setuser info */
1934 info->info16.acct_flags |= ACB_DISABLED;
1936 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1937 &user_pol,
1939 info,
1940 &result);
1941 if (!NT_STATUS_IS_OK(status)) {
1942 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1943 goto done;
1945 if (!NT_STATUS_IS_OK(result)) {
1946 status = result;
1947 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1948 goto done;
1950 status = result;
1951 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1953 done:
1954 if (pipe_hnd && b) {
1955 if (is_valid_policy_hnd(&domain_pol)) {
1956 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1958 if (is_valid_policy_hnd(&sam_pol)) {
1959 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1961 TALLOC_FREE(pipe_hnd);
1964 if (cli) {
1965 cli_shutdown(cli);
1968 return status;
1971 /****************************************************************
1972 ****************************************************************/
1974 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1976 WERROR werr = WERR_OK;
1977 sbcErr err;
1978 struct smbconf_ctx *ctx;
1980 err = smbconf_init_reg(r, &ctx, NULL);
1981 if (!SBC_ERROR_IS_OK(err)) {
1982 werr = WERR_SERVICE_DOES_NOT_EXIST;
1983 goto done;
1986 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1988 err = smbconf_set_global_parameter(ctx, "security", "user");
1989 if (!SBC_ERROR_IS_OK(err)) {
1990 werr = WERR_SERVICE_DOES_NOT_EXIST;
1991 goto done;
1994 err = smbconf_set_global_parameter(ctx, "workgroup",
1995 r->in.domain_name);
1996 if (!SBC_ERROR_IS_OK(err)) {
1997 werr = WERR_SERVICE_DOES_NOT_EXIST;
1998 goto done;
2001 smbconf_delete_global_parameter(ctx, "realm");
2002 goto done;
2005 err = smbconf_set_global_parameter(ctx, "security", "domain");
2006 if (!SBC_ERROR_IS_OK(err)) {
2007 werr = WERR_SERVICE_DOES_NOT_EXIST;
2008 goto done;
2011 err = smbconf_set_global_parameter(ctx, "workgroup",
2012 r->out.netbios_domain_name);
2013 if (!SBC_ERROR_IS_OK(err)) {
2014 werr = WERR_SERVICE_DOES_NOT_EXIST;
2015 goto done;
2018 if (r->out.domain_is_ad) {
2019 err = smbconf_set_global_parameter(ctx, "security", "ads");
2020 if (!SBC_ERROR_IS_OK(err)) {
2021 werr = WERR_SERVICE_DOES_NOT_EXIST;
2022 goto done;
2025 err = smbconf_set_global_parameter(ctx, "realm",
2026 r->out.dns_domain_name);
2027 if (!SBC_ERROR_IS_OK(err)) {
2028 werr = WERR_SERVICE_DOES_NOT_EXIST;
2029 goto done;
2033 done:
2034 smbconf_shutdown(ctx);
2035 return werr;
2038 /****************************************************************
2039 ****************************************************************/
2041 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
2043 WERROR werr = WERR_OK;
2044 sbcErr err;
2045 struct smbconf_ctx *ctx;
2047 err = smbconf_init_reg(r, &ctx, NULL);
2048 if (!SBC_ERROR_IS_OK(err)) {
2049 werr = WERR_SERVICE_DOES_NOT_EXIST;
2050 goto done;
2053 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2055 err = smbconf_set_global_parameter(ctx, "security", "user");
2056 if (!SBC_ERROR_IS_OK(err)) {
2057 werr = WERR_SERVICE_DOES_NOT_EXIST;
2058 goto done;
2061 err = smbconf_delete_global_parameter(ctx, "workgroup");
2062 if (!SBC_ERROR_IS_OK(err)) {
2063 werr = WERR_SERVICE_DOES_NOT_EXIST;
2064 goto done;
2067 smbconf_delete_global_parameter(ctx, "realm");
2070 done:
2071 smbconf_shutdown(ctx);
2072 return werr;
2075 /****************************************************************
2076 ****************************************************************/
2078 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
2080 WERROR werr;
2082 if (!W_ERROR_IS_OK(r->out.result)) {
2083 return r->out.result;
2086 if (!r->in.modify_config) {
2087 return WERR_OK;
2090 werr = do_join_modify_vals_config(r);
2091 if (!W_ERROR_IS_OK(werr)) {
2092 return werr;
2095 lp_load_global(get_dyn_CONFIGFILE());
2097 r->out.modified_config = true;
2098 r->out.result = werr;
2100 return werr;
2103 /****************************************************************
2104 ****************************************************************/
2106 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
2108 WERROR werr;
2110 if (!W_ERROR_IS_OK(r->out.result)) {
2111 return r->out.result;
2114 if (!r->in.modify_config) {
2115 return WERR_OK;
2118 werr = do_unjoin_modify_vals_config(r);
2119 if (!W_ERROR_IS_OK(werr)) {
2120 return werr;
2123 lp_load_global(get_dyn_CONFIGFILE());
2125 r->out.modified_config = true;
2126 r->out.result = werr;
2128 return werr;
2131 /****************************************************************
2132 ****************************************************************/
2134 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
2135 const char *domain_str,
2136 const char **domain_p,
2137 const char **dc_p)
2139 char *domain = NULL;
2140 char *dc = NULL;
2141 const char *p = NULL;
2143 if (!domain_str || !domain_p || !dc_p) {
2144 return false;
2147 p = strchr_m(domain_str, '\\');
2149 if (p != NULL) {
2150 domain = talloc_strndup(mem_ctx, domain_str,
2151 PTR_DIFF(p, domain_str));
2152 dc = talloc_strdup(mem_ctx, p+1);
2153 if (!dc) {
2154 return false;
2156 } else {
2157 domain = talloc_strdup(mem_ctx, domain_str);
2158 dc = NULL;
2160 if (!domain) {
2161 return false;
2164 *domain_p = domain;
2166 if (!*dc_p && dc) {
2167 *dc_p = dc;
2170 return true;
2173 /****************************************************************
2174 ****************************************************************/
2176 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
2177 struct libnet_JoinCtx *r)
2179 if (!r->in.domain_name) {
2180 libnet_join_set_error_string(mem_ctx, r,
2181 "No domain name defined");
2182 return WERR_INVALID_PARAMETER;
2185 if (strlen(r->in.machine_name) > 15) {
2186 libnet_join_set_error_string(mem_ctx, r,
2187 "Our netbios name can be at most 15 chars long, "
2188 "\"%s\" is %u chars long\n",
2189 r->in.machine_name,
2190 (unsigned int)strlen(r->in.machine_name));
2191 return WERR_INVALID_PARAMETER;
2194 r->out.account_name = talloc_asprintf(mem_ctx, "%s$",
2195 r->in.machine_name);
2196 if (r->out.account_name == NULL) {
2197 libnet_join_set_error_string(mem_ctx, r,
2198 "Unable to construct r->out.account_name");
2199 return WERR_NOT_ENOUGH_MEMORY;
2202 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2203 &r->in.domain_name,
2204 &r->in.dc_name)) {
2205 libnet_join_set_error_string(mem_ctx, r,
2206 "Failed to parse domain name");
2207 return WERR_INVALID_PARAMETER;
2210 if (!r->in.admin_domain) {
2211 char *admin_domain = NULL;
2212 char *admin_account = NULL;
2213 bool ok;
2215 ok = split_domain_user(mem_ctx,
2216 r->in.admin_account,
2217 &admin_domain,
2218 &admin_account);
2219 if (!ok) {
2220 return WERR_NOT_ENOUGH_MEMORY;
2223 if (admin_domain != NULL) {
2224 r->in.admin_domain = admin_domain;
2225 } else {
2226 r->in.admin_domain = r->in.domain_name;
2228 r->in.admin_account = admin_account;
2231 if (!secrets_init()) {
2232 libnet_join_set_error_string(mem_ctx, r,
2233 "Unable to open secrets database");
2234 return WERR_CAN_NOT_COMPLETE;
2237 return WERR_OK;
2240 /****************************************************************
2241 ****************************************************************/
2243 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
2245 NTSTATUS status;
2247 /* Try adding dom admins to builtin\admins. Only log failures. */
2248 status = create_builtin_administrators(domain_sid);
2249 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2250 DEBUG(10,("Unable to auto-add domain administrators to "
2251 "BUILTIN\\Administrators during join because "
2252 "winbindd must be running.\n"));
2253 } else if (!NT_STATUS_IS_OK(status)) {
2254 DEBUG(5, ("Failed to auto-add domain administrators to "
2255 "BUILTIN\\Administrators during join: %s\n",
2256 nt_errstr(status)));
2259 /* Try adding dom users to builtin\users. Only log failures. */
2260 status = create_builtin_users(domain_sid);
2261 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2262 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
2263 "during join because winbindd must be running.\n"));
2264 } else if (!NT_STATUS_IS_OK(status)) {
2265 DEBUG(5, ("Failed to auto-add domain administrators to "
2266 "BUILTIN\\Administrators during join: %s\n",
2267 nt_errstr(status)));
2270 /* Try adding dom guests to builtin\guests. Only log failures. */
2271 status = create_builtin_guests(domain_sid);
2272 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
2273 DEBUG(10,("Unable to auto-add domain guests to "
2274 "BUILTIN\\Guests during join because "
2275 "winbindd must be running.\n"));
2276 } else if (!NT_STATUS_IS_OK(status)) {
2277 DEBUG(5, ("Failed to auto-add domain guests to "
2278 "BUILTIN\\Guests during join: %s\n",
2279 nt_errstr(status)));
2283 /****************************************************************
2284 ****************************************************************/
2286 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
2287 struct libnet_JoinCtx *r)
2289 WERROR werr;
2291 if (!W_ERROR_IS_OK(r->out.result)) {
2292 return r->out.result;
2295 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
2296 werr = do_JoinConfig(r);
2297 if (!W_ERROR_IS_OK(werr)) {
2298 return werr;
2301 return WERR_OK;
2304 #ifdef HAVE_ADS
2305 if (r->out.domain_is_ad &&
2306 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2307 ADS_STATUS ads_status;
2309 ads_status = libnet_join_post_processing_ads_modify(mem_ctx, r);
2310 if (!ADS_ERR_OK(ads_status)) {
2311 return WERR_GEN_FAILURE;
2314 #endif /* HAVE_ADS */
2316 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
2317 if (r->out.dns_domain_name) {
2318 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
2321 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2322 return WERR_NERR_SETUPNOTJOINED;
2325 werr = do_JoinConfig(r);
2326 if (!W_ERROR_IS_OK(werr)) {
2327 return werr;
2330 #ifdef HAVE_ADS
2331 if (r->out.domain_is_ad &&
2332 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2333 ADS_STATUS ads_status;
2335 ads_status = libnet_join_post_processing_ads_sync(mem_ctx, r);
2336 if (!ADS_ERR_OK(ads_status)) {
2337 return WERR_GEN_FAILURE;
2340 #endif /* HAVE_ADS */
2342 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
2344 return WERR_OK;
2347 /****************************************************************
2348 ****************************************************************/
2350 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
2352 if (r->in.ads) {
2353 ads_destroy(&r->in.ads);
2356 return 0;
2359 /****************************************************************
2360 ****************************************************************/
2362 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
2364 if (r->in.ads) {
2365 ads_destroy(&r->in.ads);
2368 return 0;
2371 /****************************************************************
2372 ****************************************************************/
2374 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
2375 struct libnet_JoinCtx **r)
2377 struct libnet_JoinCtx *ctx;
2379 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
2380 if (!ctx) {
2381 return WERR_NOT_ENOUGH_MEMORY;
2384 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
2386 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2387 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2389 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
2391 ctx->in.desired_encryption_types = ENC_CRC32 |
2392 ENC_RSA_MD5 |
2393 ENC_RC4_HMAC_MD5;
2394 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
2395 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES128;
2396 #endif
2397 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
2398 ctx->in.desired_encryption_types |= ENC_HMAC_SHA1_96_AES256;
2399 #endif
2401 *r = ctx;
2403 return WERR_OK;
2406 /****************************************************************
2407 ****************************************************************/
2409 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
2410 struct libnet_UnjoinCtx **r)
2412 struct libnet_UnjoinCtx *ctx;
2414 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
2415 if (!ctx) {
2416 return WERR_NOT_ENOUGH_MEMORY;
2419 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
2421 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
2422 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
2424 *r = ctx;
2426 return WERR_OK;
2429 /****************************************************************
2430 ****************************************************************/
2432 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
2433 struct libnet_JoinCtx *r)
2435 bool valid_security = false;
2436 bool valid_workgroup = false;
2437 bool valid_realm = false;
2438 bool ignored_realm = false;
2440 /* check if configuration is already set correctly */
2442 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
2444 switch (r->out.domain_is_ad) {
2445 case false:
2446 valid_security = (lp_security() == SEC_DOMAIN)
2447 || (lp_server_role() == ROLE_DOMAIN_PDC)
2448 || (lp_server_role() == ROLE_DOMAIN_BDC);
2449 if (valid_workgroup && valid_security) {
2450 /* nothing to be done */
2451 return WERR_OK;
2453 break;
2454 case true:
2455 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
2456 switch (lp_security()) {
2457 case SEC_DOMAIN:
2458 if (!valid_realm && lp_winbind_rpc_only()) {
2459 valid_realm = true;
2460 ignored_realm = true;
2463 FALL_THROUGH;
2464 case SEC_ADS:
2465 valid_security = true;
2468 if (valid_workgroup && valid_realm && valid_security) {
2469 if (ignored_realm && !r->in.modify_config)
2471 libnet_join_set_error_string(mem_ctx, r,
2472 "Warning: ignoring realm when "
2473 "joining AD domain with "
2474 "'security=domain' and "
2475 "'winbind rpc only = yes'. "
2476 "(realm set to '%s', "
2477 "should be '%s').", lp_realm(),
2478 r->out.dns_domain_name);
2480 /* nothing to be done */
2481 return WERR_OK;
2483 break;
2486 /* check if we are supposed to manipulate configuration */
2488 if (!r->in.modify_config) {
2490 char *wrong_conf = talloc_strdup(mem_ctx, "");
2492 if (!valid_workgroup) {
2493 wrong_conf = talloc_asprintf_append(wrong_conf,
2494 "\"workgroup\" set to '%s', should be '%s'",
2495 lp_workgroup(), r->out.netbios_domain_name);
2496 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2499 if (!valid_realm) {
2500 wrong_conf = talloc_asprintf_append(wrong_conf,
2501 "\"realm\" set to '%s', should be '%s'",
2502 lp_realm(), r->out.dns_domain_name);
2503 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2506 if (!valid_security) {
2507 const char *sec = NULL;
2508 switch (lp_security()) {
2509 case SEC_USER: sec = "user"; break;
2510 case SEC_DOMAIN: sec = "domain"; break;
2511 case SEC_ADS: sec = "ads"; break;
2513 wrong_conf = talloc_asprintf_append(wrong_conf,
2514 "\"security\" set to '%s', should be %s",
2515 sec, r->out.domain_is_ad ?
2516 "either 'domain' or 'ads'" : "'domain'");
2517 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
2520 libnet_join_set_error_string(mem_ctx, r,
2521 "Invalid configuration (%s) and configuration modification "
2522 "was not requested", wrong_conf);
2523 return WERR_CAN_NOT_COMPLETE;
2526 /* check if we are able to manipulate configuration */
2528 if (!lp_config_backend_is_registry()) {
2529 libnet_join_set_error_string(mem_ctx, r,
2530 "Configuration manipulation requested but not "
2531 "supported by backend");
2532 return WERR_NOT_SUPPORTED;
2535 return WERR_OK;
2538 /****************************************************************
2539 ****************************************************************/
2541 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
2542 struct libnet_JoinCtx *r)
2544 NTSTATUS status;
2545 WERROR werr;
2546 struct cli_state *cli = NULL;
2547 #ifdef HAVE_ADS
2548 ADS_STATUS ads_status;
2549 #endif /* HAVE_ADS */
2550 const char *pre_connect_realm = NULL;
2551 const char *numeric_dcip = NULL;
2552 const char *sitename = NULL;
2554 /* Before contacting a DC, we can securely know
2555 * the realm only if the user specifies it.
2557 if (r->in.use_kerberos &&
2558 r->in.domain_name_type == JoinDomNameTypeDNS) {
2559 pre_connect_realm = r->in.domain_name;
2562 if (!r->in.dc_name) {
2563 struct netr_DsRGetDCNameInfo *info;
2564 const char *dc;
2565 uint32_t name_type_flags = 0;
2566 if (r->in.domain_name_type == JoinDomNameTypeDNS) {
2567 name_type_flags = DS_IS_DNS_NAME;
2568 } else if (r->in.domain_name_type == JoinDomNameTypeNBT) {
2569 name_type_flags = DS_IS_FLAT_NAME;
2571 status = dsgetdcname(mem_ctx,
2572 r->in.msg_ctx,
2573 r->in.domain_name,
2574 NULL,
2575 NULL,
2576 DS_FORCE_REDISCOVERY |
2577 DS_DIRECTORY_SERVICE_REQUIRED |
2578 DS_WRITABLE_REQUIRED |
2579 DS_RETURN_DNS_NAME |
2580 name_type_flags,
2581 &info);
2582 if (!NT_STATUS_IS_OK(status)) {
2583 libnet_join_set_error_string(mem_ctx, r,
2584 "failed to find DC for domain %s - %s",
2585 r->in.domain_name,
2586 get_friendly_nt_error_msg(status));
2587 return WERR_NERR_DCNOTFOUND;
2590 dc = strip_hostname(info->dc_unc);
2591 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2592 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2594 if (info->dc_address == NULL || info->dc_address[0] != '\\' ||
2595 info->dc_address[1] != '\\') {
2596 DBG_ERR("ill-formed DC address '%s'\n",
2597 info->dc_address);
2598 return WERR_NERR_DCNOTFOUND;
2601 numeric_dcip = info->dc_address + 2;
2602 sitename = info->dc_site_name;
2603 /* info goes out of scope but the memory stays
2604 allocated on the talloc context */
2607 if (pre_connect_realm != NULL) {
2608 struct sockaddr_storage ss = {0};
2610 if (numeric_dcip != NULL) {
2611 if (!interpret_string_addr(&ss, numeric_dcip,
2612 AI_NUMERICHOST)) {
2613 DBG_ERR(
2614 "cannot parse IP address '%s' of DC '%s'\n",
2615 numeric_dcip, r->in.dc_name);
2616 return WERR_NERR_DCNOTFOUND;
2618 } else {
2619 if (!interpret_string_addr(&ss, r->in.dc_name, 0)) {
2620 DBG_WARNING(
2621 "cannot resolve IP address of DC '%s'\n",
2622 r->in.dc_name);
2623 return WERR_NERR_DCNOTFOUND;
2627 /* The domain parameter is only used as modifier
2628 * to krb5.conf file name. _JOIN_ is is not a valid
2629 * NetBIOS name so it cannot clash with another domain
2630 * -- Uri.
2632 create_local_private_krb5_conf_for_domain(pre_connect_realm,
2633 "_JOIN_",
2634 sitename,
2635 &ss);
2638 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
2639 if (!NT_STATUS_IS_OK(status)) {
2640 libnet_join_set_error_string(mem_ctx, r,
2641 "failed to lookup DC info for domain '%s' over rpc: %s",
2642 r->in.domain_name, get_friendly_nt_error_msg(status));
2643 return ntstatus_to_werror(status);
2646 werr = libnet_join_check_config(mem_ctx, r);
2647 if (!W_ERROR_IS_OK(werr)) {
2648 goto done;
2651 #ifdef HAVE_ADS
2653 if (r->out.domain_is_ad) {
2654 create_local_private_krb5_conf_for_domain(
2655 r->out.dns_domain_name, r->out.netbios_domain_name,
2656 sitename, smbXcli_conn_remote_sockaddr(cli->conn));
2659 if (r->out.domain_is_ad &&
2660 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
2662 const char *initial_account_ou = r->in.account_ou;
2665 * we want to create the msDS-SupportedEncryptionTypes attribute
2666 * as early as possible so always try an LDAP create as the user
2667 * first. We copy r->in.account_ou because it may be changed
2668 * during the machine pre-creation.
2671 ads_status = libnet_join_connect_ads_user(mem_ctx, r);
2672 if (!ADS_ERR_OK(ads_status)) {
2673 libnet_join_set_error_string(mem_ctx, r,
2674 "failed to connect to AD: %s",
2675 ads_errstr(ads_status));
2676 return WERR_NERR_DEFAULTJOINREQUIRED;
2679 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
2680 if (ADS_ERR_OK(ads_status)) {
2683 * LDAP object creation succeeded.
2685 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2687 return WERR_OK;
2690 if (initial_account_ou != NULL) {
2691 libnet_join_set_error_string(mem_ctx, r,
2692 "failed to precreate account in ou %s: %s",
2693 r->in.account_ou,
2694 ads_errstr(ads_status));
2695 return WERR_NERR_DEFAULTJOINREQUIRED;
2698 DBG_INFO("Failed to pre-create account in OU %s: %s\n",
2699 r->in.account_ou, ads_errstr(ads_status));
2701 #endif /* HAVE_ADS */
2703 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2704 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2705 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2706 } else {
2707 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2709 if (!NT_STATUS_IS_OK(status)) {
2710 libnet_join_set_error_string(mem_ctx, r,
2711 "failed to join domain '%s' over rpc: %s",
2712 r->in.domain_name, get_friendly_nt_error_msg(status));
2713 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2714 return WERR_NERR_SETUPALREADYJOINED;
2716 werr = ntstatus_to_werror(status);
2717 goto done;
2720 werr = WERR_OK;
2722 done:
2723 if (cli) {
2724 cli_shutdown(cli);
2727 return werr;
2730 /****************************************************************
2731 ****************************************************************/
2733 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2734 struct libnet_JoinCtx *r)
2736 WERROR werr;
2737 struct libnet_UnjoinCtx *u = NULL;
2739 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2740 if (!W_ERROR_IS_OK(werr)) {
2741 return werr;
2744 u->in.debug = r->in.debug;
2745 u->in.dc_name = r->in.dc_name;
2746 u->in.domain_name = r->in.domain_name;
2747 u->in.admin_account = r->in.admin_account;
2748 u->in.admin_password = r->in.admin_password;
2749 u->in.modify_config = r->in.modify_config;
2750 u->in.use_kerberos = r->in.use_kerberos;
2751 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2752 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2754 werr = libnet_Unjoin(mem_ctx, u);
2755 TALLOC_FREE(u);
2757 return werr;
2760 /****************************************************************
2761 ****************************************************************/
2763 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2764 struct libnet_JoinCtx *r)
2766 WERROR werr;
2768 if (r->in.debug) {
2769 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2772 ZERO_STRUCT(r->out);
2774 werr = libnet_join_pre_processing(mem_ctx, r);
2775 if (!W_ERROR_IS_OK(werr)) {
2776 goto done;
2779 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2780 werr = libnet_DomainJoin(mem_ctx, r);
2781 if (!W_ERROR_IS_OK(werr)) {
2782 goto done;
2786 werr = libnet_join_post_processing(mem_ctx, r);
2787 if (!W_ERROR_IS_OK(werr)) {
2788 goto done;
2791 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2792 werr = libnet_join_post_verify(mem_ctx, r);
2793 if (!W_ERROR_IS_OK(werr)) {
2794 libnet_join_rollback(mem_ctx, r);
2798 done:
2799 r->out.result = werr;
2801 if (r->in.debug) {
2802 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2804 return werr;
2807 /****************************************************************
2808 ****************************************************************/
2810 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2811 struct libnet_UnjoinCtx *r)
2813 NTSTATUS status;
2815 if (!r->in.domain_sid) {
2816 struct dom_sid sid;
2817 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2818 libnet_unjoin_set_error_string(mem_ctx, r,
2819 "Unable to fetch domain sid: are we joined?");
2820 return WERR_NERR_SETUPNOTJOINED;
2822 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2823 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2826 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2827 !r->in.delete_machine_account) {
2828 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2829 return WERR_OK;
2832 if (!r->in.dc_name) {
2833 struct netr_DsRGetDCNameInfo *info;
2834 const char *dc;
2835 status = dsgetdcname(mem_ctx,
2836 r->in.msg_ctx,
2837 r->in.domain_name,
2838 NULL,
2839 NULL,
2840 DS_DIRECTORY_SERVICE_REQUIRED |
2841 DS_WRITABLE_REQUIRED |
2842 DS_RETURN_DNS_NAME,
2843 &info);
2844 if (!NT_STATUS_IS_OK(status)) {
2845 libnet_unjoin_set_error_string(mem_ctx, r,
2846 "failed to find DC for domain %s - %s",
2847 r->in.domain_name,
2848 get_friendly_nt_error_msg(status));
2849 return WERR_NERR_DCNOTFOUND;
2852 dc = strip_hostname(info->dc_unc);
2853 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2854 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2857 #ifdef HAVE_ADS
2858 /* for net ads leave, try to delete the account. If it works,
2859 no sense in disabling. If it fails, we can still try to
2860 disable it. jmcd */
2862 if (r->in.delete_machine_account) {
2863 ADS_STATUS ads_status;
2864 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2865 if (ADS_ERR_OK(ads_status)) {
2866 /* dirty hack */
2867 r->out.dns_domain_name =
2868 talloc_strdup(mem_ctx,
2869 r->in.ads->server.realm);
2870 ads_status =
2871 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2873 if (!ADS_ERR_OK(ads_status)) {
2874 libnet_unjoin_set_error_string(mem_ctx, r,
2875 "failed to remove machine account from AD: %s",
2876 ads_errstr(ads_status));
2877 } else {
2878 r->out.deleted_machine_account = true;
2879 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2880 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2881 return WERR_OK;
2884 #endif /* HAVE_ADS */
2886 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2887 "disable". */
2888 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2889 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2890 if (!NT_STATUS_IS_OK(status)) {
2891 libnet_unjoin_set_error_string(mem_ctx, r,
2892 "failed to disable machine account via rpc: %s",
2893 get_friendly_nt_error_msg(status));
2894 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2895 return WERR_NERR_SETUPNOTJOINED;
2897 return ntstatus_to_werror(status);
2900 r->out.dns_domain_name = talloc_strdup(mem_ctx,
2901 r->in.domain_name);
2902 r->out.disabled_machine_account = true;
2905 /* If disable succeeded or was not requested at all, we
2906 should be getting rid of our end of things */
2908 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2910 return WERR_OK;
2913 /****************************************************************
2914 ****************************************************************/
2916 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2917 struct libnet_UnjoinCtx *r)
2919 if (!r->in.domain_name) {
2920 libnet_unjoin_set_error_string(mem_ctx, r,
2921 "No domain name defined");
2922 return WERR_INVALID_PARAMETER;
2925 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2926 &r->in.domain_name,
2927 &r->in.dc_name)) {
2928 libnet_unjoin_set_error_string(mem_ctx, r,
2929 "Failed to parse domain name");
2930 return WERR_INVALID_PARAMETER;
2933 if (IS_DC) {
2934 return WERR_NERR_SETUPDOMAINCONTROLLER;
2937 if (!r->in.admin_domain) {
2938 char *admin_domain = NULL;
2939 char *admin_account = NULL;
2940 bool ok;
2942 ok = split_domain_user(mem_ctx,
2943 r->in.admin_account,
2944 &admin_domain,
2945 &admin_account);
2946 if (!ok) {
2947 return WERR_NOT_ENOUGH_MEMORY;
2950 if (admin_domain != NULL) {
2951 r->in.admin_domain = admin_domain;
2952 } else {
2953 r->in.admin_domain = r->in.domain_name;
2955 r->in.admin_account = admin_account;
2958 if (!secrets_init()) {
2959 libnet_unjoin_set_error_string(mem_ctx, r,
2960 "Unable to open secrets database");
2961 return WERR_CAN_NOT_COMPLETE;
2964 return WERR_OK;
2967 /****************************************************************
2968 ****************************************************************/
2970 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2971 struct libnet_UnjoinCtx *r)
2973 saf_delete(r->out.netbios_domain_name);
2974 saf_delete(r->out.dns_domain_name);
2976 return libnet_unjoin_config(r);
2979 /****************************************************************
2980 ****************************************************************/
2982 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2983 struct libnet_UnjoinCtx *r)
2985 WERROR werr;
2987 if (r->in.debug) {
2988 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2991 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2992 if (!W_ERROR_IS_OK(werr)) {
2993 goto done;
2996 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2997 werr = libnet_DomainUnjoin(mem_ctx, r);
2998 if (!W_ERROR_IS_OK(werr)) {
2999 libnet_unjoin_config(r);
3000 goto done;
3004 werr = libnet_unjoin_post_processing(mem_ctx, r);
3005 if (!W_ERROR_IS_OK(werr)) {
3006 goto done;
3009 done:
3010 r->out.result = werr;
3012 if (r->in.debug) {
3013 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
3016 return werr;