s3:libnet_join: use DS_FORCE_REDISCOVERY
[Samba/ekacnet.git] / source3 / libnet / libnet_join.c
blob691f6ff8ebb7038d57b8373e6d49385c8c687a51
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 "libnet/libnet.h"
24 /****************************************************************
25 ****************************************************************/
27 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
28 do { \
29 char *str = NULL; \
30 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
31 DEBUG(1,("libnet_Join:\n%s", str)); \
32 TALLOC_FREE(str); \
33 } while (0)
35 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
36 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
37 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
38 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
40 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
41 do { \
42 char *str = NULL; \
43 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
44 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
45 TALLOC_FREE(str); \
46 } while (0)
48 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
49 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
50 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
51 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
53 #define W_ERROR_NOT_OK_GOTO_DONE(x) do { \
54 if (!W_ERROR_IS_OK(x)) {\
55 goto done;\
57 } while (0)
59 /****************************************************************
60 ****************************************************************/
62 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
63 struct libnet_JoinCtx *r,
64 const char *format, ...)
66 va_list args;
68 if (r->out.error_string) {
69 return;
72 va_start(args, format);
73 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
74 va_end(args);
77 /****************************************************************
78 ****************************************************************/
80 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
81 struct libnet_UnjoinCtx *r,
82 const char *format, ...)
84 va_list args;
86 if (r->out.error_string) {
87 return;
90 va_start(args, format);
91 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
92 va_end(args);
95 #ifdef WITH_ADS
97 /****************************************************************
98 ****************************************************************/
100 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
101 const char *netbios_domain_name,
102 const char *dc_name,
103 const char *user_name,
104 const char *password,
105 ADS_STRUCT **ads)
107 ADS_STATUS status;
108 ADS_STRUCT *my_ads = NULL;
110 my_ads = ads_init(dns_domain_name,
111 netbios_domain_name,
112 dc_name);
113 if (!my_ads) {
114 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
117 if (user_name) {
118 SAFE_FREE(my_ads->auth.user_name);
119 my_ads->auth.user_name = SMB_STRDUP(user_name);
122 if (password) {
123 SAFE_FREE(my_ads->auth.password);
124 my_ads->auth.password = SMB_STRDUP(password);
127 status = ads_connect_user_creds(my_ads);
128 if (!ADS_ERR_OK(status)) {
129 ads_destroy(&my_ads);
130 return status;
133 *ads = my_ads;
134 return ADS_SUCCESS;
137 /****************************************************************
138 ****************************************************************/
140 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
141 struct libnet_JoinCtx *r)
143 ADS_STATUS status;
145 status = libnet_connect_ads(r->out.dns_domain_name,
146 r->out.netbios_domain_name,
147 r->in.dc_name,
148 r->in.admin_account,
149 r->in.admin_password,
150 &r->in.ads);
151 if (!ADS_ERR_OK(status)) {
152 libnet_join_set_error_string(mem_ctx, r,
153 "failed to connect to AD: %s",
154 ads_errstr(status));
155 return status;
158 if (!r->out.netbios_domain_name) {
159 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
160 r->in.ads->server.workgroup);
161 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
164 if (!r->out.dns_domain_name) {
165 r->out.dns_domain_name = talloc_strdup(mem_ctx,
166 r->in.ads->config.realm);
167 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
170 r->out.domain_is_ad = true;
172 return ADS_SUCCESS;
175 /****************************************************************
176 ****************************************************************/
178 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
179 struct libnet_UnjoinCtx *r)
181 ADS_STATUS status;
183 status = libnet_connect_ads(r->in.domain_name,
184 r->in.domain_name,
185 r->in.dc_name,
186 r->in.admin_account,
187 r->in.admin_password,
188 &r->in.ads);
189 if (!ADS_ERR_OK(status)) {
190 libnet_unjoin_set_error_string(mem_ctx, r,
191 "failed to connect to AD: %s",
192 ads_errstr(status));
195 return status;
198 /****************************************************************
199 join a domain using ADS (LDAP mods)
200 ****************************************************************/
202 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
203 struct libnet_JoinCtx *r)
205 ADS_STATUS status;
206 LDAPMessage *res = NULL;
207 const char *attrs[] = { "dn", NULL };
208 bool moved = false;
210 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
211 if (!ADS_ERR_OK(status)) {
212 return status;
215 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
216 if (!ADS_ERR_OK(status)) {
217 return status;
220 if (ads_count_replies(r->in.ads, res) != 1) {
221 ads_msgfree(r->in.ads, res);
222 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
225 ads_msgfree(r->in.ads, res);
227 /* Attempt to create the machine account and bail if this fails.
228 Assume that the admin wants exactly what they requested */
230 status = ads_create_machine_acct(r->in.ads,
231 r->in.machine_name,
232 r->in.account_ou);
234 if (ADS_ERR_OK(status)) {
235 DEBUG(1,("machine account creation created\n"));
236 return status;
237 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
238 (status.err.rc == LDAP_ALREADY_EXISTS)) {
239 status = ADS_SUCCESS;
242 if (!ADS_ERR_OK(status)) {
243 DEBUG(1,("machine account creation failed\n"));
244 return status;
247 status = ads_move_machine_acct(r->in.ads,
248 r->in.machine_name,
249 r->in.account_ou,
250 &moved);
251 if (!ADS_ERR_OK(status)) {
252 DEBUG(1,("failure to locate/move pre-existing "
253 "machine account\n"));
254 return status;
257 DEBUG(1,("The machine account %s the specified OU.\n",
258 moved ? "was moved into" : "already exists in"));
260 return status;
263 /****************************************************************
264 ****************************************************************/
266 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
267 struct libnet_UnjoinCtx *r)
269 ADS_STATUS status;
271 if (!r->in.ads) {
272 return libnet_unjoin_connect_ads(mem_ctx, r);
275 status = ads_leave_realm(r->in.ads, r->in.machine_name);
276 if (!ADS_ERR_OK(status)) {
277 libnet_unjoin_set_error_string(mem_ctx, r,
278 "failed to leave realm: %s",
279 ads_errstr(status));
280 return status;
283 return ADS_SUCCESS;
286 /****************************************************************
287 ****************************************************************/
289 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
290 struct libnet_JoinCtx *r)
292 ADS_STATUS status;
293 LDAPMessage *res = NULL;
294 char *dn = NULL;
296 if (!r->in.machine_name) {
297 return ADS_ERROR(LDAP_NO_MEMORY);
300 status = ads_find_machine_acct(r->in.ads,
301 &res,
302 r->in.machine_name);
303 if (!ADS_ERR_OK(status)) {
304 return status;
307 if (ads_count_replies(r->in.ads, res) != 1) {
308 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
309 goto done;
312 dn = ads_get_dn(r->in.ads, res);
313 if (!dn) {
314 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
315 goto done;
318 r->out.dn = talloc_strdup(mem_ctx, dn);
319 if (!r->out.dn) {
320 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
321 goto done;
324 done:
325 ads_msgfree(r->in.ads, res);
326 ads_memfree(r->in.ads, dn);
328 return status;
331 /****************************************************************
332 Set a machines dNSHostName and servicePrincipalName attributes
333 ****************************************************************/
335 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
336 struct libnet_JoinCtx *r)
338 ADS_STATUS status;
339 ADS_MODLIST mods;
340 fstring my_fqdn;
341 const char *spn_array[3] = {NULL, NULL, NULL};
342 char *spn = NULL;
344 /* Find our DN */
346 status = libnet_join_find_machine_acct(mem_ctx, r);
347 if (!ADS_ERR_OK(status)) {
348 return status;
351 /* Windows only creates HOST/shortname & HOST/fqdn. */
353 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
354 if (!spn) {
355 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
357 strupper_m(spn);
358 spn_array[0] = spn;
360 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
361 || (strchr(my_fqdn, '.') == NULL)) {
362 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
363 r->out.dns_domain_name);
366 strlower_m(my_fqdn);
368 if (!strequal(my_fqdn, r->in.machine_name)) {
369 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
370 if (!spn) {
371 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
373 spn_array[1] = spn;
376 mods = ads_init_mods(mem_ctx);
377 if (!mods) {
378 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
381 /* fields of primary importance */
383 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
384 if (!ADS_ERR_OK(status)) {
385 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
388 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
389 spn_array);
390 if (!ADS_ERR_OK(status)) {
391 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
394 return ads_gen_mod(r->in.ads, r->out.dn, mods);
397 /****************************************************************
398 ****************************************************************/
400 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
401 struct libnet_JoinCtx *r)
403 ADS_STATUS status;
404 ADS_MODLIST mods;
406 if (!r->in.create_upn) {
407 return ADS_SUCCESS;
410 /* Find our DN */
412 status = libnet_join_find_machine_acct(mem_ctx, r);
413 if (!ADS_ERR_OK(status)) {
414 return status;
417 if (!r->in.upn) {
418 r->in.upn = talloc_asprintf(mem_ctx,
419 "host/%s@%s",
420 r->in.machine_name,
421 r->out.dns_domain_name);
422 if (!r->in.upn) {
423 return ADS_ERROR(LDAP_NO_MEMORY);
427 /* now do the mods */
429 mods = ads_init_mods(mem_ctx);
430 if (!mods) {
431 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
434 /* fields of primary importance */
436 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
437 if (!ADS_ERR_OK(status)) {
438 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
441 return ads_gen_mod(r->in.ads, r->out.dn, mods);
445 /****************************************************************
446 ****************************************************************/
448 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
449 struct libnet_JoinCtx *r)
451 ADS_STATUS status;
452 ADS_MODLIST mods;
453 char *os_sp = NULL;
455 if (!r->in.os_name || !r->in.os_version ) {
456 return ADS_SUCCESS;
459 /* Find our DN */
461 status = libnet_join_find_machine_acct(mem_ctx, r);
462 if (!ADS_ERR_OK(status)) {
463 return status;
466 /* now do the mods */
468 mods = ads_init_mods(mem_ctx);
469 if (!mods) {
470 return ADS_ERROR(LDAP_NO_MEMORY);
473 os_sp = talloc_asprintf(mem_ctx, "Samba %s", SAMBA_VERSION_STRING);
474 if (!os_sp) {
475 return ADS_ERROR(LDAP_NO_MEMORY);
478 /* fields of primary importance */
480 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
481 r->in.os_name);
482 if (!ADS_ERR_OK(status)) {
483 return status;
486 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
487 r->in.os_version);
488 if (!ADS_ERR_OK(status)) {
489 return status;
492 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
493 os_sp);
494 if (!ADS_ERR_OK(status)) {
495 return status;
498 return ads_gen_mod(r->in.ads, r->out.dn, mods);
501 /****************************************************************
502 ****************************************************************/
504 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
505 struct libnet_JoinCtx *r)
507 if (!lp_use_kerberos_keytab()) {
508 return true;
511 if (ads_keytab_create_default(r->in.ads) != 0) {
512 return false;
515 return true;
518 /****************************************************************
519 ****************************************************************/
521 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
522 struct libnet_JoinCtx *r)
524 uint32_t domain_func;
525 ADS_STATUS status;
526 const char *salt = NULL;
527 char *std_salt = NULL;
529 status = ads_domain_func_level(r->in.ads, &domain_func);
530 if (!ADS_ERR_OK(status)) {
531 libnet_join_set_error_string(mem_ctx, r,
532 "failed to determine domain functional level: %s",
533 ads_errstr(status));
534 return false;
537 /* go ahead and setup the default salt */
539 std_salt = kerberos_standard_des_salt();
540 if (!std_salt) {
541 libnet_join_set_error_string(mem_ctx, r,
542 "failed to obtain standard DES salt");
543 return false;
546 salt = talloc_strdup(mem_ctx, std_salt);
547 if (!salt) {
548 return false;
551 SAFE_FREE(std_salt);
553 /* if it's a Windows functional domain, we have to look for the UPN */
555 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
556 char *upn;
558 upn = ads_get_upn(r->in.ads, mem_ctx,
559 r->in.machine_name);
560 if (upn) {
561 salt = talloc_strdup(mem_ctx, upn);
562 if (!salt) {
563 return false;
568 return kerberos_secrets_store_des_salt(salt);
571 /****************************************************************
572 ****************************************************************/
574 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
575 struct libnet_JoinCtx *r)
577 ADS_STATUS status;
579 if (!r->in.ads) {
580 status = libnet_join_connect_ads(mem_ctx, r);
581 if (!ADS_ERR_OK(status)) {
582 return status;
586 status = libnet_join_set_machine_spn(mem_ctx, r);
587 if (!ADS_ERR_OK(status)) {
588 libnet_join_set_error_string(mem_ctx, r,
589 "failed to set machine spn: %s",
590 ads_errstr(status));
591 return status;
594 status = libnet_join_set_os_attributes(mem_ctx, r);
595 if (!ADS_ERR_OK(status)) {
596 libnet_join_set_error_string(mem_ctx, r,
597 "failed to set machine os attributes: %s",
598 ads_errstr(status));
599 return status;
602 status = libnet_join_set_machine_upn(mem_ctx, r);
603 if (!ADS_ERR_OK(status)) {
604 libnet_join_set_error_string(mem_ctx, r,
605 "failed to set machine upn: %s",
606 ads_errstr(status));
607 return status;
610 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
611 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
614 if (!libnet_join_create_keytab(mem_ctx, r)) {
615 libnet_join_set_error_string(mem_ctx, r,
616 "failed to create kerberos keytab");
617 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
620 return ADS_SUCCESS;
622 #endif /* WITH_ADS */
624 /****************************************************************
625 Store the machine password and domain SID
626 ****************************************************************/
628 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
629 struct libnet_JoinCtx *r)
631 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
632 r->out.domain_sid))
634 DEBUG(1,("Failed to save domain sid\n"));
635 return false;
638 if (!secrets_store_machine_password(r->in.machine_password,
639 r->out.netbios_domain_name,
640 r->in.secure_channel_type))
642 DEBUG(1,("Failed to save machine password\n"));
643 return false;
646 return true;
649 /****************************************************************
650 Connect dc's IPC$ share
651 ****************************************************************/
653 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
654 const char *user,
655 const char *pass,
656 bool use_kerberos,
657 struct cli_state **cli)
659 int flags = 0;
661 if (use_kerberos) {
662 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
665 if (use_kerberos && pass) {
666 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
669 return cli_full_connection(cli, NULL,
671 NULL, 0,
672 "IPC$", "IPC",
673 user,
674 NULL,
675 pass,
676 flags,
677 Undefined, NULL);
680 /****************************************************************
681 Lookup domain dc's info
682 ****************************************************************/
684 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
685 struct libnet_JoinCtx *r,
686 struct cli_state **cli)
688 struct rpc_pipe_client *pipe_hnd = NULL;
689 POLICY_HND lsa_pol;
690 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
691 union lsa_PolicyInformation *info = NULL;
693 status = libnet_join_connect_dc_ipc(r->in.dc_name,
694 r->in.admin_account,
695 r->in.admin_password,
696 r->in.use_kerberos,
697 cli);
698 if (!NT_STATUS_IS_OK(status)) {
699 goto done;
702 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
703 &pipe_hnd);
704 if (!NT_STATUS_IS_OK(status)) {
705 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
706 nt_errstr(status)));
707 goto done;
710 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
711 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
712 if (!NT_STATUS_IS_OK(status)) {
713 goto done;
716 status = rpccli_lsa_QueryInfoPolicy2(pipe_hnd, mem_ctx,
717 &lsa_pol,
718 LSA_POLICY_INFO_DNS,
719 &info);
720 if (NT_STATUS_IS_OK(status)) {
721 r->out.domain_is_ad = true;
722 r->out.netbios_domain_name = info->dns.name.string;
723 r->out.dns_domain_name = info->dns.dns_domain.string;
724 r->out.forest_name = info->dns.dns_forest.string;
725 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->dns.sid);
726 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
729 if (!NT_STATUS_IS_OK(status)) {
730 status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
731 &lsa_pol,
732 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
733 &info);
734 if (!NT_STATUS_IS_OK(status)) {
735 goto done;
738 r->out.netbios_domain_name = info->account_domain.name.string;
739 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->account_domain.sid);
740 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
743 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
744 TALLOC_FREE(pipe_hnd);
746 done:
747 return status;
750 /****************************************************************
751 Do the domain join
752 ****************************************************************/
754 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
755 struct libnet_JoinCtx *r,
756 struct cli_state *cli)
758 struct rpc_pipe_client *pipe_hnd = NULL;
759 POLICY_HND sam_pol, domain_pol, user_pol;
760 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
761 char *acct_name;
762 struct lsa_String lsa_acct_name;
763 uint32_t user_rid;
764 uint32_t acct_flags = ACB_WSTRUST;
765 struct samr_Ids user_rids;
766 struct samr_Ids name_types;
767 union samr_UserInfo user_info;
769 struct samr_CryptPassword crypt_pwd;
770 struct samr_CryptPasswordEx crypt_pwd_ex;
772 ZERO_STRUCT(sam_pol);
773 ZERO_STRUCT(domain_pol);
774 ZERO_STRUCT(user_pol);
776 if (!r->in.machine_password) {
777 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
778 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
781 /* Open the domain */
783 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
784 &pipe_hnd);
785 if (!NT_STATUS_IS_OK(status)) {
786 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
787 nt_errstr(status)));
788 goto done;
791 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
792 pipe_hnd->desthost,
793 SEC_RIGHTS_MAXIMUM_ALLOWED,
794 &sam_pol);
795 if (!NT_STATUS_IS_OK(status)) {
796 goto done;
799 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
800 &sam_pol,
801 SEC_RIGHTS_MAXIMUM_ALLOWED,
802 r->out.domain_sid,
803 &domain_pol);
804 if (!NT_STATUS_IS_OK(status)) {
805 goto done;
808 /* Create domain user */
810 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
811 strlower_m(acct_name);
813 init_lsa_String(&lsa_acct_name, acct_name);
815 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
816 uint32_t access_desired =
817 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
818 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
819 SAMR_USER_ACCESS_SET_PASSWORD |
820 SAMR_USER_ACCESS_GET_ATTRIBUTES |
821 SAMR_USER_ACCESS_SET_ATTRIBUTES;
822 uint32_t access_granted = 0;
824 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
826 DEBUG(10,("Creating account with desired access mask: %d\n",
827 access_desired));
829 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
830 &domain_pol,
831 &lsa_acct_name,
832 ACB_WSTRUST,
833 access_desired,
834 &user_pol,
835 &access_granted,
836 &user_rid);
837 if (!NT_STATUS_IS_OK(status) &&
838 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
840 DEBUG(10,("Creation of workstation account failed: %s\n",
841 nt_errstr(status)));
843 /* If NT_STATUS_ACCESS_DENIED then we have a valid
844 username/password combo but the user does not have
845 administrator access. */
847 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
848 libnet_join_set_error_string(mem_ctx, r,
849 "User specified does not have "
850 "administrator privileges");
853 goto done;
856 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
857 if (!(r->in.join_flags &
858 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
859 goto done;
863 /* We *must* do this.... don't ask... */
865 if (NT_STATUS_IS_OK(status)) {
866 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
870 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
871 &domain_pol,
873 &lsa_acct_name,
874 &user_rids,
875 &name_types);
876 if (!NT_STATUS_IS_OK(status)) {
877 goto done;
880 if (name_types.ids[0] != SID_NAME_USER) {
881 DEBUG(0,("%s is not a user account (type=%d)\n",
882 acct_name, name_types.ids[0]));
883 status = NT_STATUS_INVALID_WORKSTATION;
884 goto done;
887 user_rid = user_rids.ids[0];
889 /* Open handle on user */
891 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
892 &domain_pol,
893 SEC_RIGHTS_MAXIMUM_ALLOWED,
894 user_rid,
895 &user_pol);
896 if (!NT_STATUS_IS_OK(status)) {
897 goto done;
900 /* Fill in the additional account flags now */
902 acct_flags |= ACB_PWNOEXP;
903 if (r->out.domain_is_ad) {
904 #if !defined(ENCTYPE_ARCFOUR_HMAC)
905 acct_flags |= ACB_USE_DES_KEY_ONLY;
906 #endif
910 /* Set account flags on machine account */
911 ZERO_STRUCT(user_info.info16);
912 user_info.info16.acct_flags = acct_flags;
914 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
915 &user_pol,
917 &user_info);
919 if (!NT_STATUS_IS_OK(status)) {
921 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
922 &user_pol);
924 libnet_join_set_error_string(mem_ctx, r,
925 "Failed to set account flags for machine account (%s)\n",
926 nt_errstr(status));
927 goto done;
930 /* Set password on machine account - first try level 26 */
932 init_samr_CryptPasswordEx(r->in.machine_password,
933 &cli->user_session_key,
934 &crypt_pwd_ex);
936 init_samr_user_info26(&user_info.info26, &crypt_pwd_ex,
937 PASS_DONT_CHANGE_AT_NEXT_LOGON);
939 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
940 &user_pol,
942 &user_info);
944 if (NT_STATUS_EQUAL(status, NT_STATUS(DCERPC_FAULT_INVALID_TAG))) {
946 /* retry with level 24 */
948 init_samr_CryptPassword(r->in.machine_password,
949 &cli->user_session_key,
950 &crypt_pwd);
952 init_samr_user_info24(&user_info.info24, &crypt_pwd,
953 PASS_DONT_CHANGE_AT_NEXT_LOGON);
955 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
956 &user_pol,
958 &user_info);
961 if (!NT_STATUS_IS_OK(status)) {
963 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
964 &user_pol);
966 libnet_join_set_error_string(mem_ctx, r,
967 "Failed to set password for machine account (%s)\n",
968 nt_errstr(status));
969 goto done;
972 status = NT_STATUS_OK;
974 done:
975 if (!pipe_hnd) {
976 return status;
979 if (is_valid_policy_hnd(&sam_pol)) {
980 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
982 if (is_valid_policy_hnd(&domain_pol)) {
983 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
985 if (is_valid_policy_hnd(&user_pol)) {
986 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
988 TALLOC_FREE(pipe_hnd);
990 return status;
993 /****************************************************************
994 ****************************************************************/
996 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
997 const char *machine_name,
998 const char *dc_name)
1000 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1001 struct cli_state *cli = NULL;
1002 struct rpc_pipe_client *pipe_hnd = NULL;
1003 struct rpc_pipe_client *netlogon_pipe = NULL;
1004 NTSTATUS status;
1005 char *machine_password = NULL;
1006 char *machine_account = NULL;
1008 if (!dc_name) {
1009 return NT_STATUS_INVALID_PARAMETER;
1012 if (!secrets_init()) {
1013 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1016 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1017 NULL, NULL);
1018 if (!machine_password) {
1019 return NT_STATUS_NO_TRUST_LSA_SECRET;
1022 asprintf(&machine_account, "%s$", machine_name);
1023 if (!machine_account) {
1024 SAFE_FREE(machine_password);
1025 return NT_STATUS_NO_MEMORY;
1028 status = cli_full_connection(&cli, NULL,
1029 dc_name,
1030 NULL, 0,
1031 "IPC$", "IPC",
1032 machine_account,
1033 NULL,
1034 machine_password,
1036 Undefined, NULL);
1037 free(machine_account);
1038 free(machine_password);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 status = cli_full_connection(&cli, NULL,
1042 dc_name,
1043 NULL, 0,
1044 "IPC$", "IPC",
1046 NULL,
1049 Undefined, NULL);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 return status;
1056 status = get_schannel_session_key(cli, netbios_domain_name,
1057 &neg_flags, &netlogon_pipe);
1058 if (!NT_STATUS_IS_OK(status)) {
1059 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1060 cli_shutdown(cli);
1061 return NT_STATUS_OK;
1064 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1065 "key from server %s for domain %s. Error was %s\n",
1066 cli->desthost, netbios_domain_name, nt_errstr(status)));
1067 cli_shutdown(cli);
1068 return status;
1071 if (!lp_client_schannel()) {
1072 cli_shutdown(cli);
1073 return NT_STATUS_OK;
1076 status = cli_rpc_pipe_open_schannel_with_key(
1077 cli, &ndr_table_netlogon.syntax_id, PIPE_AUTH_LEVEL_PRIVACY,
1078 netbios_domain_name, netlogon_pipe->dc, &pipe_hnd);
1080 cli_shutdown(cli);
1082 if (!NT_STATUS_IS_OK(status)) {
1083 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1084 "on netlogon pipe to server %s for domain %s. "
1085 "Error was %s\n",
1086 cli->desthost, netbios_domain_name, nt_errstr(status)));
1087 return status;
1090 return NT_STATUS_OK;
1093 /****************************************************************
1094 ****************************************************************/
1096 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1097 struct libnet_JoinCtx *r)
1099 NTSTATUS status;
1101 status = libnet_join_ok(r->out.netbios_domain_name,
1102 r->in.machine_name,
1103 r->in.dc_name);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 libnet_join_set_error_string(mem_ctx, r,
1106 "failed to verify domain membership after joining: %s",
1107 get_friendly_nt_error_msg(status));
1108 return WERR_SETUP_NOT_JOINED;
1111 return WERR_OK;
1114 /****************************************************************
1115 ****************************************************************/
1117 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1118 struct libnet_UnjoinCtx *r)
1120 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1121 return false;
1124 if (!secrets_delete_domain_sid(lp_workgroup())) {
1125 return false;
1128 return true;
1131 /****************************************************************
1132 ****************************************************************/
1134 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1135 struct libnet_UnjoinCtx *r)
1137 struct cli_state *cli = NULL;
1138 struct rpc_pipe_client *pipe_hnd = NULL;
1139 POLICY_HND sam_pol, domain_pol, user_pol;
1140 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1141 char *acct_name;
1142 uint32_t user_rid;
1143 struct lsa_String lsa_acct_name;
1144 struct samr_Ids user_rids;
1145 struct samr_Ids name_types;
1146 union samr_UserInfo *info = NULL;
1148 ZERO_STRUCT(sam_pol);
1149 ZERO_STRUCT(domain_pol);
1150 ZERO_STRUCT(user_pol);
1152 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1153 r->in.admin_account,
1154 r->in.admin_password,
1155 r->in.use_kerberos,
1156 &cli);
1157 if (!NT_STATUS_IS_OK(status)) {
1158 goto done;
1161 /* Open the domain */
1163 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1164 &pipe_hnd);
1165 if (!NT_STATUS_IS_OK(status)) {
1166 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1167 nt_errstr(status)));
1168 goto done;
1171 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1172 pipe_hnd->desthost,
1173 SEC_RIGHTS_MAXIMUM_ALLOWED,
1174 &sam_pol);
1175 if (!NT_STATUS_IS_OK(status)) {
1176 goto done;
1179 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1180 &sam_pol,
1181 SEC_RIGHTS_MAXIMUM_ALLOWED,
1182 r->in.domain_sid,
1183 &domain_pol);
1184 if (!NT_STATUS_IS_OK(status)) {
1185 goto done;
1188 /* Create domain user */
1190 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1191 strlower_m(acct_name);
1193 init_lsa_String(&lsa_acct_name, acct_name);
1195 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1196 &domain_pol,
1198 &lsa_acct_name,
1199 &user_rids,
1200 &name_types);
1202 if (!NT_STATUS_IS_OK(status)) {
1203 goto done;
1206 if (name_types.ids[0] != SID_NAME_USER) {
1207 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1208 name_types.ids[0]));
1209 status = NT_STATUS_INVALID_WORKSTATION;
1210 goto done;
1213 user_rid = user_rids.ids[0];
1215 /* Open handle on user */
1217 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1218 &domain_pol,
1219 SEC_RIGHTS_MAXIMUM_ALLOWED,
1220 user_rid,
1221 &user_pol);
1222 if (!NT_STATUS_IS_OK(status)) {
1223 goto done;
1226 /* Get user info */
1228 status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1229 &user_pol,
1231 &info);
1232 if (!NT_STATUS_IS_OK(status)) {
1233 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1234 goto done;
1237 /* now disable and setuser info */
1239 info->info16.acct_flags |= ACB_DISABLED;
1241 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1242 &user_pol,
1244 info);
1246 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1248 done:
1249 if (pipe_hnd) {
1250 if (is_valid_policy_hnd(&domain_pol)) {
1251 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1253 if (is_valid_policy_hnd(&sam_pol)) {
1254 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1256 TALLOC_FREE(pipe_hnd);
1259 if (cli) {
1260 cli_shutdown(cli);
1263 return status;
1266 /****************************************************************
1267 ****************************************************************/
1269 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1271 WERROR werr;
1272 struct smbconf_ctx *ctx;
1274 werr = smbconf_init_reg(r, &ctx, NULL);
1275 if (!W_ERROR_IS_OK(werr)) {
1276 goto done;
1279 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1281 werr = smbconf_set_global_parameter(ctx, "security", "user");
1282 W_ERROR_NOT_OK_GOTO_DONE(werr);
1284 werr = smbconf_set_global_parameter(ctx, "workgroup",
1285 r->in.domain_name);
1287 smbconf_delete_global_parameter(ctx, "realm");
1288 goto done;
1291 werr = smbconf_set_global_parameter(ctx, "security", "domain");
1292 W_ERROR_NOT_OK_GOTO_DONE(werr);
1294 werr = smbconf_set_global_parameter(ctx, "workgroup",
1295 r->out.netbios_domain_name);
1296 W_ERROR_NOT_OK_GOTO_DONE(werr);
1298 if (r->out.domain_is_ad) {
1299 werr = smbconf_set_global_parameter(ctx, "security", "ads");
1300 W_ERROR_NOT_OK_GOTO_DONE(werr);
1302 werr = smbconf_set_global_parameter(ctx, "realm",
1303 r->out.dns_domain_name);
1304 W_ERROR_NOT_OK_GOTO_DONE(werr);
1307 done:
1308 smbconf_shutdown(ctx);
1309 return werr;
1312 /****************************************************************
1313 ****************************************************************/
1315 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1317 WERROR werr = WERR_OK;
1318 struct smbconf_ctx *ctx;
1320 werr = smbconf_init_reg(r, &ctx, NULL);
1321 if (!W_ERROR_IS_OK(werr)) {
1322 goto done;
1325 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1327 werr = smbconf_set_global_parameter(ctx, "security", "user");
1328 W_ERROR_NOT_OK_GOTO_DONE(werr);
1330 werr = smbconf_delete_global_parameter(ctx, "workgroup");
1331 W_ERROR_NOT_OK_GOTO_DONE(werr);
1333 smbconf_delete_global_parameter(ctx, "realm");
1336 done:
1337 smbconf_shutdown(ctx);
1338 return werr;
1341 /****************************************************************
1342 ****************************************************************/
1344 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1346 WERROR werr;
1348 if (!W_ERROR_IS_OK(r->out.result)) {
1349 return r->out.result;
1352 if (!r->in.modify_config) {
1353 return WERR_OK;
1356 werr = do_join_modify_vals_config(r);
1357 if (!W_ERROR_IS_OK(werr)) {
1358 return werr;
1361 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1363 r->out.modified_config = true;
1364 r->out.result = werr;
1366 return werr;
1369 /****************************************************************
1370 ****************************************************************/
1372 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1374 WERROR werr;
1376 if (!W_ERROR_IS_OK(r->out.result)) {
1377 return r->out.result;
1380 if (!r->in.modify_config) {
1381 return WERR_OK;
1384 werr = do_unjoin_modify_vals_config(r);
1385 if (!W_ERROR_IS_OK(werr)) {
1386 return werr;
1389 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1391 r->out.modified_config = true;
1392 r->out.result = werr;
1394 return werr;
1397 /****************************************************************
1398 ****************************************************************/
1400 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1401 const char *domain_str,
1402 const char **domain_p,
1403 const char **dc_p)
1405 char *domain = NULL;
1406 char *dc = NULL;
1407 const char *p = NULL;
1409 if (!domain_str || !domain_p || !dc_p) {
1410 return false;
1413 p = strchr_m(domain_str, '\\');
1415 if (p != NULL) {
1416 domain = talloc_strndup(mem_ctx, domain_str,
1417 PTR_DIFF(p, domain_str));
1418 dc = talloc_strdup(mem_ctx, p+1);
1419 if (!dc) {
1420 return false;
1422 } else {
1423 domain = talloc_strdup(mem_ctx, domain_str);
1424 dc = NULL;
1426 if (!domain) {
1427 return false;
1430 *domain_p = domain;
1432 if (!*dc_p && dc) {
1433 *dc_p = dc;
1436 return true;
1439 /****************************************************************
1440 ****************************************************************/
1442 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1443 struct libnet_JoinCtx *r)
1445 if (!r->in.domain_name) {
1446 libnet_join_set_error_string(mem_ctx, r,
1447 "No domain name defined");
1448 return WERR_INVALID_PARAM;
1451 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1452 &r->in.domain_name,
1453 &r->in.dc_name)) {
1454 libnet_join_set_error_string(mem_ctx, r,
1455 "Failed to parse domain name");
1456 return WERR_INVALID_PARAM;
1459 if (IS_DC) {
1460 return WERR_SETUP_DOMAIN_CONTROLLER;
1463 if (!secrets_init()) {
1464 libnet_join_set_error_string(mem_ctx, r,
1465 "Unable to open secrets database");
1466 return WERR_CAN_NOT_COMPLETE;
1469 return WERR_OK;
1472 /****************************************************************
1473 ****************************************************************/
1475 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1477 NTSTATUS status;
1479 /* Try adding dom admins to builtin\admins. Only log failures. */
1480 status = create_builtin_administrators(domain_sid);
1481 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1482 DEBUG(10,("Unable to auto-add domain administrators to "
1483 "BUILTIN\\Administrators during join because "
1484 "winbindd must be running."));
1485 } else if (!NT_STATUS_IS_OK(status)) {
1486 DEBUG(5, ("Failed to auto-add domain administrators to "
1487 "BUILTIN\\Administrators during join: %s\n",
1488 nt_errstr(status)));
1491 /* Try adding dom users to builtin\users. Only log failures. */
1492 status = create_builtin_users(domain_sid);
1493 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1494 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1495 "during join because winbindd must be running."));
1496 } else if (!NT_STATUS_IS_OK(status)) {
1497 DEBUG(5, ("Failed to auto-add domain administrators to "
1498 "BUILTIN\\Administrators during join: %s\n",
1499 nt_errstr(status)));
1503 /****************************************************************
1504 ****************************************************************/
1506 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1507 struct libnet_JoinCtx *r)
1509 WERROR werr;
1511 if (!W_ERROR_IS_OK(r->out.result)) {
1512 return r->out.result;
1515 werr = do_JoinConfig(r);
1516 if (!W_ERROR_IS_OK(werr)) {
1517 return werr;
1520 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1521 return WERR_OK;
1524 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1525 if (r->out.dns_domain_name) {
1526 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1529 #ifdef WITH_ADS
1530 if (r->out.domain_is_ad) {
1531 ADS_STATUS ads_status;
1533 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1534 if (!ADS_ERR_OK(ads_status)) {
1535 return WERR_GENERAL_FAILURE;
1538 #endif /* WITH_ADS */
1540 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1542 return WERR_OK;
1545 /****************************************************************
1546 ****************************************************************/
1548 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1550 const char *krb5_cc_env = NULL;
1552 if (r->in.ads) {
1553 ads_destroy(&r->in.ads);
1556 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1557 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1558 unsetenv(KRB5_ENV_CCNAME);
1561 return 0;
1564 /****************************************************************
1565 ****************************************************************/
1567 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1569 const char *krb5_cc_env = NULL;
1571 if (r->in.ads) {
1572 ads_destroy(&r->in.ads);
1575 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1576 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1577 unsetenv(KRB5_ENV_CCNAME);
1580 return 0;
1583 /****************************************************************
1584 ****************************************************************/
1586 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1587 struct libnet_JoinCtx **r)
1589 struct libnet_JoinCtx *ctx;
1590 const char *krb5_cc_env = NULL;
1592 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1593 if (!ctx) {
1594 return WERR_NOMEM;
1597 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1599 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1600 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1602 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1603 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1604 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1605 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1606 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1609 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1611 *r = ctx;
1613 return WERR_OK;
1616 /****************************************************************
1617 ****************************************************************/
1619 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1620 struct libnet_UnjoinCtx **r)
1622 struct libnet_UnjoinCtx *ctx;
1623 const char *krb5_cc_env = NULL;
1625 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1626 if (!ctx) {
1627 return WERR_NOMEM;
1630 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1632 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1633 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1635 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1636 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1637 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1638 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1639 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1642 *r = ctx;
1644 return WERR_OK;
1647 /****************************************************************
1648 ****************************************************************/
1650 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1651 struct libnet_JoinCtx *r)
1653 bool valid_security = false;
1654 bool valid_workgroup = false;
1655 bool valid_realm = false;
1657 /* check if configuration is already set correctly */
1659 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1661 switch (r->out.domain_is_ad) {
1662 case false:
1663 valid_security = (lp_security() == SEC_DOMAIN);
1664 if (valid_workgroup && valid_security) {
1665 /* nothing to be done */
1666 return WERR_OK;
1668 break;
1669 case true:
1670 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1671 switch (lp_security()) {
1672 case SEC_DOMAIN:
1673 case SEC_ADS:
1674 valid_security = true;
1677 if (valid_workgroup && valid_realm && valid_security) {
1678 /* nothing to be done */
1679 return WERR_OK;
1681 break;
1684 /* check if we are supposed to manipulate configuration */
1686 if (!r->in.modify_config) {
1688 char *wrong_conf = talloc_strdup(mem_ctx, "");
1690 if (!valid_workgroup) {
1691 wrong_conf = talloc_asprintf_append(wrong_conf,
1692 "\"workgroup\" set to '%s', should be '%s'",
1693 lp_workgroup(), r->out.netbios_domain_name);
1694 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1697 if (!valid_realm) {
1698 wrong_conf = talloc_asprintf_append(wrong_conf,
1699 "\"realm\" set to '%s', should be '%s'",
1700 lp_realm(), r->out.dns_domain_name);
1701 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1704 if (!valid_security) {
1705 const char *sec = NULL;
1706 switch (lp_security()) {
1707 case SEC_SHARE: sec = "share"; break;
1708 case SEC_USER: sec = "user"; break;
1709 case SEC_DOMAIN: sec = "domain"; break;
1710 case SEC_ADS: sec = "ads"; break;
1712 wrong_conf = talloc_asprintf_append(wrong_conf,
1713 "\"security\" set to '%s', should be %s",
1714 sec, r->out.domain_is_ad ?
1715 "either 'domain' or 'ads'" : "'domain'");
1716 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1719 libnet_join_set_error_string(mem_ctx, r,
1720 "Invalid configuration (%s) and configuration modification "
1721 "was not requested", wrong_conf);
1722 return WERR_CAN_NOT_COMPLETE;
1725 /* check if we are able to manipulate configuration */
1727 if (!lp_config_backend_is_registry()) {
1728 libnet_join_set_error_string(mem_ctx, r,
1729 "Configuration manipulation requested but not "
1730 "supported by backend");
1731 return WERR_NOT_SUPPORTED;
1734 return WERR_OK;
1737 /****************************************************************
1738 ****************************************************************/
1740 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1741 struct libnet_JoinCtx *r)
1743 NTSTATUS status;
1744 WERROR werr;
1745 struct cli_state *cli = NULL;
1746 #ifdef WITH_ADS
1747 ADS_STATUS ads_status;
1748 #endif /* WITH_ADS */
1750 if (!r->in.dc_name) {
1751 struct netr_DsRGetDCNameInfo *info;
1752 const char *dc;
1753 status = dsgetdcname(mem_ctx,
1754 r->in.msg_ctx,
1755 r->in.domain_name,
1756 NULL,
1757 NULL,
1758 DS_FORCE_REDISCOVERY |
1759 DS_DIRECTORY_SERVICE_REQUIRED |
1760 DS_WRITABLE_REQUIRED |
1761 DS_RETURN_DNS_NAME,
1762 &info);
1763 if (!NT_STATUS_IS_OK(status)) {
1764 libnet_join_set_error_string(mem_ctx, r,
1765 "failed to find DC for domain %s",
1766 r->in.domain_name,
1767 get_friendly_nt_error_msg(status));
1768 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1771 dc = strip_hostname(info->dc_unc);
1772 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1773 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1776 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1777 if (!NT_STATUS_IS_OK(status)) {
1778 libnet_join_set_error_string(mem_ctx, r,
1779 "failed to lookup DC info for domain '%s' over rpc: %s",
1780 r->in.domain_name, get_friendly_nt_error_msg(status));
1781 return ntstatus_to_werror(status);
1784 werr = libnet_join_check_config(mem_ctx, r);
1785 if (!W_ERROR_IS_OK(werr)) {
1786 goto done;
1789 #ifdef WITH_ADS
1790 if (r->out.domain_is_ad && r->in.account_ou) {
1792 ads_status = libnet_join_connect_ads(mem_ctx, r);
1793 if (!ADS_ERR_OK(ads_status)) {
1794 return WERR_DEFAULT_JOIN_REQUIRED;
1797 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1798 if (!ADS_ERR_OK(ads_status)) {
1799 libnet_join_set_error_string(mem_ctx, r,
1800 "failed to precreate account in ou %s: %s",
1801 r->in.account_ou,
1802 ads_errstr(ads_status));
1803 return WERR_DEFAULT_JOIN_REQUIRED;
1806 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1808 #endif /* WITH_ADS */
1810 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
1811 if (!NT_STATUS_IS_OK(status)) {
1812 libnet_join_set_error_string(mem_ctx, r,
1813 "failed to join domain '%s' over rpc: %s",
1814 r->in.domain_name, get_friendly_nt_error_msg(status));
1815 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1816 return WERR_SETUP_ALREADY_JOINED;
1818 werr = ntstatus_to_werror(status);
1819 goto done;
1822 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1823 werr = WERR_SETUP_NOT_JOINED;
1824 goto done;
1827 werr = WERR_OK;
1829 done:
1830 if (cli) {
1831 cli_shutdown(cli);
1834 return werr;
1837 /****************************************************************
1838 ****************************************************************/
1840 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
1841 struct libnet_JoinCtx *r)
1843 WERROR werr;
1844 struct libnet_UnjoinCtx *u = NULL;
1846 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
1847 if (!W_ERROR_IS_OK(werr)) {
1848 return werr;
1851 u->in.debug = r->in.debug;
1852 u->in.dc_name = r->in.dc_name;
1853 u->in.domain_name = r->in.domain_name;
1854 u->in.admin_account = r->in.admin_account;
1855 u->in.admin_password = r->in.admin_password;
1856 u->in.modify_config = r->in.modify_config;
1857 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
1858 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
1860 werr = libnet_Unjoin(mem_ctx, u);
1861 TALLOC_FREE(u);
1863 return werr;
1866 /****************************************************************
1867 ****************************************************************/
1869 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1870 struct libnet_JoinCtx *r)
1872 WERROR werr;
1874 if (r->in.debug) {
1875 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1878 werr = libnet_join_pre_processing(mem_ctx, r);
1879 if (!W_ERROR_IS_OK(werr)) {
1880 goto done;
1883 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1884 werr = libnet_DomainJoin(mem_ctx, r);
1885 if (!W_ERROR_IS_OK(werr)) {
1886 goto done;
1890 werr = libnet_join_post_processing(mem_ctx, r);
1891 if (!W_ERROR_IS_OK(werr)) {
1892 goto done;
1895 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1896 werr = libnet_join_post_verify(mem_ctx, r);
1897 if (!W_ERROR_IS_OK(werr)) {
1898 libnet_join_rollback(mem_ctx, r);
1902 done:
1903 r->out.result = werr;
1905 if (r->in.debug) {
1906 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1908 return werr;
1911 /****************************************************************
1912 ****************************************************************/
1914 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1915 struct libnet_UnjoinCtx *r)
1917 NTSTATUS status;
1919 if (!r->in.domain_sid) {
1920 struct dom_sid sid;
1921 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1922 libnet_unjoin_set_error_string(mem_ctx, r,
1923 "Unable to fetch domain sid: are we joined?");
1924 return WERR_SETUP_NOT_JOINED;
1926 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
1927 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
1930 if (!r->in.dc_name) {
1931 struct netr_DsRGetDCNameInfo *info;
1932 const char *dc;
1933 status = dsgetdcname(mem_ctx,
1934 r->in.msg_ctx,
1935 r->in.domain_name,
1936 NULL,
1937 NULL,
1938 DS_DIRECTORY_SERVICE_REQUIRED |
1939 DS_WRITABLE_REQUIRED |
1940 DS_RETURN_DNS_NAME,
1941 &info);
1942 if (!NT_STATUS_IS_OK(status)) {
1943 libnet_unjoin_set_error_string(mem_ctx, r,
1944 "failed to find DC for domain %s",
1945 r->in.domain_name,
1946 get_friendly_nt_error_msg(status));
1947 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1950 dc = strip_hostname(info->dc_unc);
1951 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1952 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1955 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
1956 if (!NT_STATUS_IS_OK(status)) {
1957 libnet_unjoin_set_error_string(mem_ctx, r,
1958 "failed to disable machine account via rpc: %s",
1959 get_friendly_nt_error_msg(status));
1960 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1961 return WERR_SETUP_NOT_JOINED;
1963 return ntstatus_to_werror(status);
1966 r->out.disabled_machine_account = true;
1968 #ifdef WITH_ADS
1969 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
1970 ADS_STATUS ads_status;
1971 libnet_unjoin_connect_ads(mem_ctx, r);
1972 ads_status = libnet_unjoin_remove_machine_acct(mem_ctx, r);
1973 if (!ADS_ERR_OK(ads_status)) {
1974 libnet_unjoin_set_error_string(mem_ctx, r,
1975 "failed to remove machine account from AD: %s",
1976 ads_errstr(ads_status));
1977 } else {
1978 r->out.deleted_machine_account = true;
1979 /* dirty hack */
1980 r->out.dns_domain_name = talloc_strdup(mem_ctx,
1981 r->in.ads->server.realm);
1982 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
1985 #endif /* WITH_ADS */
1987 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
1989 return WERR_OK;
1992 /****************************************************************
1993 ****************************************************************/
1995 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
1996 struct libnet_UnjoinCtx *r)
1998 if (!r->in.domain_name) {
1999 libnet_unjoin_set_error_string(mem_ctx, r,
2000 "No domain name defined");
2001 return WERR_INVALID_PARAM;
2004 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2005 &r->in.domain_name,
2006 &r->in.dc_name)) {
2007 libnet_unjoin_set_error_string(mem_ctx, r,
2008 "Failed to parse domain name");
2009 return WERR_INVALID_PARAM;
2012 if (IS_DC) {
2013 return WERR_SETUP_DOMAIN_CONTROLLER;
2016 if (!secrets_init()) {
2017 libnet_unjoin_set_error_string(mem_ctx, r,
2018 "Unable to open secrets database");
2019 return WERR_CAN_NOT_COMPLETE;
2022 return WERR_OK;
2025 /****************************************************************
2026 ****************************************************************/
2028 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2029 struct libnet_UnjoinCtx *r)
2031 saf_delete(r->out.netbios_domain_name);
2032 saf_delete(r->out.dns_domain_name);
2034 return libnet_unjoin_config(r);
2037 /****************************************************************
2038 ****************************************************************/
2040 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2041 struct libnet_UnjoinCtx *r)
2043 WERROR werr;
2045 if (r->in.debug) {
2046 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2049 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2050 if (!W_ERROR_IS_OK(werr)) {
2051 goto done;
2054 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2055 werr = libnet_DomainUnjoin(mem_ctx, r);
2056 if (!W_ERROR_IS_OK(werr)) {
2057 libnet_unjoin_config(r);
2058 goto done;
2062 werr = libnet_unjoin_post_processing(mem_ctx, r);
2063 if (!W_ERROR_IS_OK(werr)) {
2064 goto done;
2067 done:
2068 r->out.result = werr;
2070 if (r->in.debug) {
2071 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2074 return werr;