s3: Rename new parameter "ldap ref follow" to "ldap follow referral".
[Samba/gebeck_regimport.git] / source3 / libnet / libnet_join.c
blobaa5f54adaf6035fd7bcfb3934c1a102ee51fc4c8
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"
23 #include "libcli/auth/libcli_auth.h"
25 /****************************************************************
26 ****************************************************************/
28 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
29 do { \
30 char *str = NULL; \
31 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
32 DEBUG(1,("libnet_Join:\n%s", str)); \
33 TALLOC_FREE(str); \
34 } while (0)
36 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
37 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
38 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
39 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
41 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
42 do { \
43 char *str = NULL; \
44 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
45 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
46 TALLOC_FREE(str); \
47 } while (0)
49 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
50 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
51 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
52 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
54 /****************************************************************
55 ****************************************************************/
57 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
58 struct libnet_JoinCtx *r,
59 const char *format, ...)
61 va_list args;
63 if (r->out.error_string) {
64 return;
67 va_start(args, format);
68 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
69 va_end(args);
72 /****************************************************************
73 ****************************************************************/
75 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
76 struct libnet_UnjoinCtx *r,
77 const char *format, ...)
79 va_list args;
81 if (r->out.error_string) {
82 return;
85 va_start(args, format);
86 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
87 va_end(args);
90 #ifdef WITH_ADS
92 /****************************************************************
93 ****************************************************************/
95 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
96 const char *netbios_domain_name,
97 const char *dc_name,
98 const char *user_name,
99 const char *password,
100 ADS_STRUCT **ads)
102 ADS_STATUS status;
103 ADS_STRUCT *my_ads = NULL;
105 my_ads = ads_init(dns_domain_name,
106 netbios_domain_name,
107 dc_name);
108 if (!my_ads) {
109 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
112 if (user_name) {
113 SAFE_FREE(my_ads->auth.user_name);
114 my_ads->auth.user_name = SMB_STRDUP(user_name);
117 if (password) {
118 SAFE_FREE(my_ads->auth.password);
119 my_ads->auth.password = SMB_STRDUP(password);
122 status = ads_connect_user_creds(my_ads);
123 if (!ADS_ERR_OK(status)) {
124 ads_destroy(&my_ads);
125 return status;
128 *ads = my_ads;
129 return ADS_SUCCESS;
132 /****************************************************************
133 ****************************************************************/
135 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
136 struct libnet_JoinCtx *r)
138 ADS_STATUS status;
140 status = libnet_connect_ads(r->out.dns_domain_name,
141 r->out.netbios_domain_name,
142 r->in.dc_name,
143 r->in.admin_account,
144 r->in.admin_password,
145 &r->in.ads);
146 if (!ADS_ERR_OK(status)) {
147 libnet_join_set_error_string(mem_ctx, r,
148 "failed to connect to AD: %s",
149 ads_errstr(status));
150 return status;
153 if (!r->out.netbios_domain_name) {
154 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
155 r->in.ads->server.workgroup);
156 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
159 if (!r->out.dns_domain_name) {
160 r->out.dns_domain_name = talloc_strdup(mem_ctx,
161 r->in.ads->config.realm);
162 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
165 r->out.domain_is_ad = true;
167 return ADS_SUCCESS;
170 /****************************************************************
171 ****************************************************************/
173 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
174 struct libnet_UnjoinCtx *r)
176 ADS_STATUS status;
178 status = libnet_connect_ads(r->in.domain_name,
179 r->in.domain_name,
180 r->in.dc_name,
181 r->in.admin_account,
182 r->in.admin_password,
183 &r->in.ads);
184 if (!ADS_ERR_OK(status)) {
185 libnet_unjoin_set_error_string(mem_ctx, r,
186 "failed to connect to AD: %s",
187 ads_errstr(status));
190 return status;
193 /****************************************************************
194 join a domain using ADS (LDAP mods)
195 ****************************************************************/
197 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
198 struct libnet_JoinCtx *r)
200 ADS_STATUS status;
201 LDAPMessage *res = NULL;
202 const char *attrs[] = { "dn", NULL };
203 bool moved = false;
205 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
206 if (!ADS_ERR_OK(status)) {
207 return status;
210 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
211 if (!ADS_ERR_OK(status)) {
212 return status;
215 if (ads_count_replies(r->in.ads, res) != 1) {
216 ads_msgfree(r->in.ads, res);
217 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
220 ads_msgfree(r->in.ads, res);
222 /* Attempt to create the machine account and bail if this fails.
223 Assume that the admin wants exactly what they requested */
225 status = ads_create_machine_acct(r->in.ads,
226 r->in.machine_name,
227 r->in.account_ou);
229 if (ADS_ERR_OK(status)) {
230 DEBUG(1,("machine account creation created\n"));
231 return status;
232 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
233 (status.err.rc == LDAP_ALREADY_EXISTS)) {
234 status = ADS_SUCCESS;
237 if (!ADS_ERR_OK(status)) {
238 DEBUG(1,("machine account creation failed\n"));
239 return status;
242 status = ads_move_machine_acct(r->in.ads,
243 r->in.machine_name,
244 r->in.account_ou,
245 &moved);
246 if (!ADS_ERR_OK(status)) {
247 DEBUG(1,("failure to locate/move pre-existing "
248 "machine account\n"));
249 return status;
252 DEBUG(1,("The machine account %s the specified OU.\n",
253 moved ? "was moved into" : "already exists in"));
255 return status;
258 /****************************************************************
259 ****************************************************************/
261 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
262 struct libnet_UnjoinCtx *r)
264 ADS_STATUS status;
266 if (!r->in.ads) {
267 status = libnet_unjoin_connect_ads(mem_ctx, r);
268 if (!ADS_ERR_OK(status)) {
269 libnet_unjoin_set_error_string(mem_ctx, r,
270 "failed to connect to AD: %s",
271 ads_errstr(status));
272 return status;
276 status = ads_leave_realm(r->in.ads, r->in.machine_name);
277 if (!ADS_ERR_OK(status)) {
278 libnet_unjoin_set_error_string(mem_ctx, r,
279 "failed to leave realm: %s",
280 ads_errstr(status));
281 return status;
284 return ADS_SUCCESS;
287 /****************************************************************
288 ****************************************************************/
290 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
291 struct libnet_JoinCtx *r)
293 ADS_STATUS status;
294 LDAPMessage *res = NULL;
295 char *dn = NULL;
297 if (!r->in.machine_name) {
298 return ADS_ERROR(LDAP_NO_MEMORY);
301 status = ads_find_machine_acct(r->in.ads,
302 &res,
303 r->in.machine_name);
304 if (!ADS_ERR_OK(status)) {
305 return status;
308 if (ads_count_replies(r->in.ads, res) != 1) {
309 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
310 goto done;
313 dn = ads_get_dn(r->in.ads, mem_ctx, res);
314 if (!dn) {
315 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
316 goto done;
319 r->out.dn = talloc_strdup(mem_ctx, dn);
320 if (!r->out.dn) {
321 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
322 goto done;
325 done:
326 ads_msgfree(r->in.ads, res);
327 TALLOC_FREE(dn);
329 return status;
332 /****************************************************************
333 Set a machines dNSHostName and servicePrincipalName attributes
334 ****************************************************************/
336 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
337 struct libnet_JoinCtx *r)
339 ADS_STATUS status;
340 ADS_MODLIST mods;
341 fstring my_fqdn;
342 const char *spn_array[3] = {NULL, NULL, NULL};
343 char *spn = NULL;
345 /* Find our DN */
347 status = libnet_join_find_machine_acct(mem_ctx, r);
348 if (!ADS_ERR_OK(status)) {
349 return status;
352 /* Windows only creates HOST/shortname & HOST/fqdn. */
354 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
355 if (!spn) {
356 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
358 strupper_m(spn);
359 spn_array[0] = spn;
361 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
362 || (strchr(my_fqdn, '.') == NULL)) {
363 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
364 r->out.dns_domain_name);
367 strlower_m(my_fqdn);
369 if (!strequal(my_fqdn, r->in.machine_name)) {
370 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
371 if (!spn) {
372 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
374 spn_array[1] = spn;
377 mods = ads_init_mods(mem_ctx);
378 if (!mods) {
379 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
382 /* fields of primary importance */
384 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
385 if (!ADS_ERR_OK(status)) {
386 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
389 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
390 spn_array);
391 if (!ADS_ERR_OK(status)) {
392 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
395 return ads_gen_mod(r->in.ads, r->out.dn, mods);
398 /****************************************************************
399 ****************************************************************/
401 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
402 struct libnet_JoinCtx *r)
404 ADS_STATUS status;
405 ADS_MODLIST mods;
407 if (!r->in.create_upn) {
408 return ADS_SUCCESS;
411 /* Find our DN */
413 status = libnet_join_find_machine_acct(mem_ctx, r);
414 if (!ADS_ERR_OK(status)) {
415 return status;
418 if (!r->in.upn) {
419 r->in.upn = talloc_asprintf(mem_ctx,
420 "host/%s@%s",
421 r->in.machine_name,
422 r->out.dns_domain_name);
423 if (!r->in.upn) {
424 return ADS_ERROR(LDAP_NO_MEMORY);
428 /* now do the mods */
430 mods = ads_init_mods(mem_ctx);
431 if (!mods) {
432 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
435 /* fields of primary importance */
437 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
438 if (!ADS_ERR_OK(status)) {
439 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
442 return ads_gen_mod(r->in.ads, r->out.dn, mods);
446 /****************************************************************
447 ****************************************************************/
449 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
450 struct libnet_JoinCtx *r)
452 ADS_STATUS status;
453 ADS_MODLIST mods;
454 char *os_sp = NULL;
456 if (!r->in.os_name || !r->in.os_version ) {
457 return ADS_SUCCESS;
460 /* Find our DN */
462 status = libnet_join_find_machine_acct(mem_ctx, r);
463 if (!ADS_ERR_OK(status)) {
464 return status;
467 /* now do the mods */
469 mods = ads_init_mods(mem_ctx);
470 if (!mods) {
471 return ADS_ERROR(LDAP_NO_MEMORY);
474 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
475 if (!os_sp) {
476 return ADS_ERROR(LDAP_NO_MEMORY);
479 /* fields of primary importance */
481 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
482 r->in.os_name);
483 if (!ADS_ERR_OK(status)) {
484 return status;
487 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
488 r->in.os_version);
489 if (!ADS_ERR_OK(status)) {
490 return status;
493 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
494 os_sp);
495 if (!ADS_ERR_OK(status)) {
496 return status;
499 return ads_gen_mod(r->in.ads, r->out.dn, mods);
502 /****************************************************************
503 ****************************************************************/
505 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
506 struct libnet_JoinCtx *r)
508 if (!USE_SYSTEM_KEYTAB) {
509 return true;
512 if (ads_keytab_create_default(r->in.ads) != 0) {
513 return false;
516 return true;
519 /****************************************************************
520 ****************************************************************/
522 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
523 struct libnet_JoinCtx *r)
525 uint32_t domain_func;
526 ADS_STATUS status;
527 const char *salt = NULL;
528 char *std_salt = NULL;
530 status = ads_domain_func_level(r->in.ads, &domain_func);
531 if (!ADS_ERR_OK(status)) {
532 libnet_join_set_error_string(mem_ctx, r,
533 "failed to determine domain functional level: %s",
534 ads_errstr(status));
535 return false;
538 /* go ahead and setup the default salt */
540 std_salt = kerberos_standard_des_salt();
541 if (!std_salt) {
542 libnet_join_set_error_string(mem_ctx, r,
543 "failed to obtain standard DES salt");
544 return false;
547 salt = talloc_strdup(mem_ctx, std_salt);
548 if (!salt) {
549 return false;
552 SAFE_FREE(std_salt);
554 /* if it's a Windows functional domain, we have to look for the UPN */
556 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
557 char *upn;
559 upn = ads_get_upn(r->in.ads, mem_ctx,
560 r->in.machine_name);
561 if (upn) {
562 salt = talloc_strdup(mem_ctx, upn);
563 if (!salt) {
564 return false;
569 return kerberos_secrets_store_des_salt(salt);
572 /****************************************************************
573 ****************************************************************/
575 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
576 struct libnet_JoinCtx *r)
578 ADS_STATUS status;
580 if (!r->in.ads) {
581 status = libnet_join_connect_ads(mem_ctx, r);
582 if (!ADS_ERR_OK(status)) {
583 return status;
587 status = libnet_join_set_machine_spn(mem_ctx, r);
588 if (!ADS_ERR_OK(status)) {
589 libnet_join_set_error_string(mem_ctx, r,
590 "failed to set machine spn: %s",
591 ads_errstr(status));
592 return status;
595 status = libnet_join_set_os_attributes(mem_ctx, r);
596 if (!ADS_ERR_OK(status)) {
597 libnet_join_set_error_string(mem_ctx, r,
598 "failed to set machine os attributes: %s",
599 ads_errstr(status));
600 return status;
603 status = libnet_join_set_machine_upn(mem_ctx, r);
604 if (!ADS_ERR_OK(status)) {
605 libnet_join_set_error_string(mem_ctx, r,
606 "failed to set machine upn: %s",
607 ads_errstr(status));
608 return status;
611 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
612 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
615 if (!libnet_join_create_keytab(mem_ctx, r)) {
616 libnet_join_set_error_string(mem_ctx, r,
617 "failed to create kerberos keytab");
618 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
621 return ADS_SUCCESS;
623 #endif /* WITH_ADS */
625 /****************************************************************
626 Store the machine password and domain SID
627 ****************************************************************/
629 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
630 struct libnet_JoinCtx *r)
632 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
633 r->out.domain_sid))
635 DEBUG(1,("Failed to save domain sid\n"));
636 return false;
639 if (!secrets_store_machine_password(r->in.machine_password,
640 r->out.netbios_domain_name,
641 r->in.secure_channel_type))
643 DEBUG(1,("Failed to save machine password\n"));
644 return false;
647 return true;
650 /****************************************************************
651 Connect dc's IPC$ share
652 ****************************************************************/
654 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
655 const char *user,
656 const char *pass,
657 bool use_kerberos,
658 struct cli_state **cli)
660 int flags = 0;
662 if (use_kerberos) {
663 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
666 if (use_kerberos && pass) {
667 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
670 return cli_full_connection(cli, NULL,
672 NULL, 0,
673 "IPC$", "IPC",
674 user,
675 NULL,
676 pass,
677 flags,
678 Undefined, NULL);
681 /****************************************************************
682 Lookup domain dc's info
683 ****************************************************************/
685 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
686 struct libnet_JoinCtx *r,
687 struct cli_state **cli)
689 struct rpc_pipe_client *pipe_hnd = NULL;
690 struct policy_handle lsa_pol;
691 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
692 union lsa_PolicyInformation *info = NULL;
694 status = libnet_join_connect_dc_ipc(r->in.dc_name,
695 r->in.admin_account,
696 r->in.admin_password,
697 r->in.use_kerberos,
698 cli);
699 if (!NT_STATUS_IS_OK(status)) {
700 goto done;
703 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
704 &pipe_hnd);
705 if (!NT_STATUS_IS_OK(status)) {
706 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
707 nt_errstr(status)));
708 goto done;
711 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
712 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
713 if (!NT_STATUS_IS_OK(status)) {
714 goto done;
717 status = rpccli_lsa_QueryInfoPolicy2(pipe_hnd, mem_ctx,
718 &lsa_pol,
719 LSA_POLICY_INFO_DNS,
720 &info);
721 if (NT_STATUS_IS_OK(status)) {
722 r->out.domain_is_ad = true;
723 r->out.netbios_domain_name = info->dns.name.string;
724 r->out.dns_domain_name = info->dns.dns_domain.string;
725 r->out.forest_name = info->dns.dns_forest.string;
726 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->dns.sid);
727 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
730 if (!NT_STATUS_IS_OK(status)) {
731 status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
732 &lsa_pol,
733 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
734 &info);
735 if (!NT_STATUS_IS_OK(status)) {
736 goto done;
739 r->out.netbios_domain_name = info->account_domain.name.string;
740 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->account_domain.sid);
741 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
744 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
745 TALLOC_FREE(pipe_hnd);
747 done:
748 return status;
751 /****************************************************************
752 Do the domain join unsecure
753 ****************************************************************/
755 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
756 struct libnet_JoinCtx *r,
757 struct cli_state *cli)
759 struct rpc_pipe_client *pipe_hnd = NULL;
760 unsigned char orig_trust_passwd_hash[16];
761 unsigned char new_trust_passwd_hash[16];
762 fstring trust_passwd;
763 NTSTATUS status;
765 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
766 &pipe_hnd);
767 if (!NT_STATUS_IS_OK(status)) {
768 return status;
771 if (!r->in.machine_password) {
772 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
773 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
776 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
778 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
779 fstrcpy(trust_passwd, r->in.admin_password);
780 strlower_m(trust_passwd);
783 * Machine names can be 15 characters, but the max length on
784 * a password is 14. --jerry
787 trust_passwd[14] = '\0';
789 E_md4hash(trust_passwd, orig_trust_passwd_hash);
791 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
792 r->in.machine_name,
793 orig_trust_passwd_hash,
794 r->in.machine_password,
795 new_trust_passwd_hash,
796 r->in.secure_channel_type);
798 return status;
801 /****************************************************************
802 Do the domain join
803 ****************************************************************/
805 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
806 struct libnet_JoinCtx *r,
807 struct cli_state *cli)
809 struct rpc_pipe_client *pipe_hnd = NULL;
810 struct policy_handle sam_pol, domain_pol, user_pol;
811 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
812 char *acct_name;
813 struct lsa_String lsa_acct_name;
814 uint32_t user_rid;
815 uint32_t acct_flags = ACB_WSTRUST;
816 struct samr_Ids user_rids;
817 struct samr_Ids name_types;
818 union samr_UserInfo user_info;
820 struct samr_CryptPassword crypt_pwd;
821 struct samr_CryptPasswordEx crypt_pwd_ex;
823 ZERO_STRUCT(sam_pol);
824 ZERO_STRUCT(domain_pol);
825 ZERO_STRUCT(user_pol);
827 switch (r->in.secure_channel_type) {
828 case SEC_CHAN_WKSTA:
829 acct_flags = ACB_WSTRUST;
830 break;
831 case SEC_CHAN_BDC:
832 acct_flags = ACB_SVRTRUST;
833 break;
834 default:
835 return NT_STATUS_INVALID_PARAMETER;
838 if (!r->in.machine_password) {
839 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
840 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
843 /* Open the domain */
845 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
846 &pipe_hnd);
847 if (!NT_STATUS_IS_OK(status)) {
848 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
849 nt_errstr(status)));
850 goto done;
853 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
854 pipe_hnd->desthost,
855 SAMR_ACCESS_ENUM_DOMAINS
856 | SAMR_ACCESS_LOOKUP_DOMAIN,
857 &sam_pol);
858 if (!NT_STATUS_IS_OK(status)) {
859 goto done;
862 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
863 &sam_pol,
864 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
865 | SAMR_DOMAIN_ACCESS_CREATE_USER
866 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
867 r->out.domain_sid,
868 &domain_pol);
869 if (!NT_STATUS_IS_OK(status)) {
870 goto done;
873 /* Create domain user */
875 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
876 strlower_m(acct_name);
878 init_lsa_String(&lsa_acct_name, acct_name);
880 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
881 uint32_t access_desired =
882 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
883 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
884 SAMR_USER_ACCESS_SET_PASSWORD |
885 SAMR_USER_ACCESS_GET_ATTRIBUTES |
886 SAMR_USER_ACCESS_SET_ATTRIBUTES;
887 uint32_t access_granted = 0;
889 DEBUG(10,("Creating account with desired access mask: %d\n",
890 access_desired));
892 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
893 &domain_pol,
894 &lsa_acct_name,
895 acct_flags,
896 access_desired,
897 &user_pol,
898 &access_granted,
899 &user_rid);
900 if (!NT_STATUS_IS_OK(status) &&
901 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
903 DEBUG(10,("Creation of workstation account failed: %s\n",
904 nt_errstr(status)));
906 /* If NT_STATUS_ACCESS_DENIED then we have a valid
907 username/password combo but the user does not have
908 administrator access. */
910 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
911 libnet_join_set_error_string(mem_ctx, r,
912 "User specified does not have "
913 "administrator privileges");
916 goto done;
919 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
920 if (!(r->in.join_flags &
921 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
922 goto done;
926 /* We *must* do this.... don't ask... */
928 if (NT_STATUS_IS_OK(status)) {
929 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
933 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
934 &domain_pol,
936 &lsa_acct_name,
937 &user_rids,
938 &name_types);
939 if (!NT_STATUS_IS_OK(status)) {
940 goto done;
943 if (name_types.ids[0] != SID_NAME_USER) {
944 DEBUG(0,("%s is not a user account (type=%d)\n",
945 acct_name, name_types.ids[0]));
946 status = NT_STATUS_INVALID_WORKSTATION;
947 goto done;
950 user_rid = user_rids.ids[0];
952 /* Open handle on user */
954 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
955 &domain_pol,
956 SEC_FLAG_MAXIMUM_ALLOWED,
957 user_rid,
958 &user_pol);
959 if (!NT_STATUS_IS_OK(status)) {
960 goto done;
963 /* Fill in the additional account flags now */
965 acct_flags |= ACB_PWNOEXP;
966 if (r->out.domain_is_ad) {
967 #if !defined(ENCTYPE_ARCFOUR_HMAC)
968 acct_flags |= ACB_USE_DES_KEY_ONLY;
969 #endif
973 /* Set account flags on machine account */
974 ZERO_STRUCT(user_info.info16);
975 user_info.info16.acct_flags = acct_flags;
977 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
978 &user_pol,
980 &user_info);
982 if (!NT_STATUS_IS_OK(status)) {
984 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
985 &user_pol);
987 libnet_join_set_error_string(mem_ctx, r,
988 "Failed to set account flags for machine account (%s)\n",
989 nt_errstr(status));
990 goto done;
993 /* Set password on machine account - first try level 26 */
995 init_samr_CryptPasswordEx(r->in.machine_password,
996 &cli->user_session_key,
997 &crypt_pwd_ex);
999 user_info.info26.password = crypt_pwd_ex;
1000 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1002 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
1003 &user_pol,
1005 &user_info);
1007 if (NT_STATUS_EQUAL(status, NT_STATUS(DCERPC_FAULT_INVALID_TAG))) {
1009 /* retry with level 24 */
1011 init_samr_CryptPassword(r->in.machine_password,
1012 &cli->user_session_key,
1013 &crypt_pwd);
1015 user_info.info24.password = crypt_pwd;
1016 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1018 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
1019 &user_pol,
1021 &user_info);
1024 if (!NT_STATUS_IS_OK(status)) {
1026 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
1027 &user_pol);
1029 libnet_join_set_error_string(mem_ctx, r,
1030 "Failed to set password for machine account (%s)\n",
1031 nt_errstr(status));
1032 goto done;
1035 status = NT_STATUS_OK;
1037 done:
1038 if (!pipe_hnd) {
1039 return status;
1042 if (is_valid_policy_hnd(&sam_pol)) {
1043 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1045 if (is_valid_policy_hnd(&domain_pol)) {
1046 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1048 if (is_valid_policy_hnd(&user_pol)) {
1049 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1051 TALLOC_FREE(pipe_hnd);
1053 return status;
1056 /****************************************************************
1057 ****************************************************************/
1059 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1060 const char *machine_name,
1061 const char *dc_name)
1063 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1064 struct cli_state *cli = NULL;
1065 struct rpc_pipe_client *pipe_hnd = NULL;
1066 struct rpc_pipe_client *netlogon_pipe = NULL;
1067 NTSTATUS status;
1068 char *machine_password = NULL;
1069 char *machine_account = NULL;
1071 if (!dc_name) {
1072 return NT_STATUS_INVALID_PARAMETER;
1075 if (!secrets_init()) {
1076 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1079 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1080 NULL, NULL);
1081 if (!machine_password) {
1082 return NT_STATUS_NO_TRUST_LSA_SECRET;
1085 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1086 SAFE_FREE(machine_password);
1087 return NT_STATUS_NO_MEMORY;
1090 status = cli_full_connection(&cli, NULL,
1091 dc_name,
1092 NULL, 0,
1093 "IPC$", "IPC",
1094 machine_account,
1095 NULL,
1096 machine_password,
1098 Undefined, NULL);
1099 free(machine_account);
1100 free(machine_password);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 status = cli_full_connection(&cli, NULL,
1104 dc_name,
1105 NULL, 0,
1106 "IPC$", "IPC",
1108 NULL,
1111 Undefined, NULL);
1114 if (!NT_STATUS_IS_OK(status)) {
1115 return status;
1118 status = get_schannel_session_key(cli, netbios_domain_name,
1119 &neg_flags, &netlogon_pipe);
1120 if (!NT_STATUS_IS_OK(status)) {
1121 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1122 cli_shutdown(cli);
1123 return NT_STATUS_OK;
1126 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1127 "key from server %s for domain %s. Error was %s\n",
1128 cli->desthost, netbios_domain_name, nt_errstr(status)));
1129 cli_shutdown(cli);
1130 return status;
1133 if (!lp_client_schannel()) {
1134 cli_shutdown(cli);
1135 return NT_STATUS_OK;
1138 status = cli_rpc_pipe_open_schannel_with_key(
1139 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
1140 DCERPC_AUTH_LEVEL_PRIVACY,
1141 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1143 cli_shutdown(cli);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1147 "on netlogon pipe to server %s for domain %s. "
1148 "Error was %s\n",
1149 cli->desthost, netbios_domain_name, nt_errstr(status)));
1150 return status;
1153 return NT_STATUS_OK;
1156 /****************************************************************
1157 ****************************************************************/
1159 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1160 struct libnet_JoinCtx *r)
1162 NTSTATUS status;
1164 status = libnet_join_ok(r->out.netbios_domain_name,
1165 r->in.machine_name,
1166 r->in.dc_name);
1167 if (!NT_STATUS_IS_OK(status)) {
1168 libnet_join_set_error_string(mem_ctx, r,
1169 "failed to verify domain membership after joining: %s",
1170 get_friendly_nt_error_msg(status));
1171 return WERR_SETUP_NOT_JOINED;
1174 return WERR_OK;
1177 /****************************************************************
1178 ****************************************************************/
1180 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1181 struct libnet_UnjoinCtx *r)
1183 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1184 return false;
1187 if (!secrets_delete_domain_sid(lp_workgroup())) {
1188 return false;
1191 return true;
1194 /****************************************************************
1195 ****************************************************************/
1197 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1198 struct libnet_UnjoinCtx *r)
1200 struct cli_state *cli = NULL;
1201 struct rpc_pipe_client *pipe_hnd = NULL;
1202 struct policy_handle sam_pol, domain_pol, user_pol;
1203 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1204 char *acct_name;
1205 uint32_t user_rid;
1206 struct lsa_String lsa_acct_name;
1207 struct samr_Ids user_rids;
1208 struct samr_Ids name_types;
1209 union samr_UserInfo *info = NULL;
1211 ZERO_STRUCT(sam_pol);
1212 ZERO_STRUCT(domain_pol);
1213 ZERO_STRUCT(user_pol);
1215 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1216 r->in.admin_account,
1217 r->in.admin_password,
1218 r->in.use_kerberos,
1219 &cli);
1220 if (!NT_STATUS_IS_OK(status)) {
1221 goto done;
1224 /* Open the domain */
1226 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1227 &pipe_hnd);
1228 if (!NT_STATUS_IS_OK(status)) {
1229 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1230 nt_errstr(status)));
1231 goto done;
1234 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1235 pipe_hnd->desthost,
1236 SEC_FLAG_MAXIMUM_ALLOWED,
1237 &sam_pol);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 goto done;
1242 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1243 &sam_pol,
1244 SEC_FLAG_MAXIMUM_ALLOWED,
1245 r->in.domain_sid,
1246 &domain_pol);
1247 if (!NT_STATUS_IS_OK(status)) {
1248 goto done;
1251 /* Create domain user */
1253 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1254 strlower_m(acct_name);
1256 init_lsa_String(&lsa_acct_name, acct_name);
1258 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1259 &domain_pol,
1261 &lsa_acct_name,
1262 &user_rids,
1263 &name_types);
1265 if (!NT_STATUS_IS_OK(status)) {
1266 goto done;
1269 if (name_types.ids[0] != SID_NAME_USER) {
1270 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1271 name_types.ids[0]));
1272 status = NT_STATUS_INVALID_WORKSTATION;
1273 goto done;
1276 user_rid = user_rids.ids[0];
1278 /* Open handle on user */
1280 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1281 &domain_pol,
1282 SEC_FLAG_MAXIMUM_ALLOWED,
1283 user_rid,
1284 &user_pol);
1285 if (!NT_STATUS_IS_OK(status)) {
1286 goto done;
1289 /* Get user info */
1291 status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1292 &user_pol,
1294 &info);
1295 if (!NT_STATUS_IS_OK(status)) {
1296 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1297 goto done;
1300 /* now disable and setuser info */
1302 info->info16.acct_flags |= ACB_DISABLED;
1304 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1305 &user_pol,
1307 info);
1309 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1311 done:
1312 if (pipe_hnd) {
1313 if (is_valid_policy_hnd(&domain_pol)) {
1314 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1316 if (is_valid_policy_hnd(&sam_pol)) {
1317 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1319 TALLOC_FREE(pipe_hnd);
1322 if (cli) {
1323 cli_shutdown(cli);
1326 return status;
1329 /****************************************************************
1330 ****************************************************************/
1332 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1334 WERROR werr;
1335 struct smbconf_ctx *ctx;
1337 werr = smbconf_init_reg(r, &ctx, NULL);
1338 if (!W_ERROR_IS_OK(werr)) {
1339 goto done;
1342 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1344 werr = smbconf_set_global_parameter(ctx, "security", "user");
1345 W_ERROR_NOT_OK_GOTO_DONE(werr);
1347 werr = smbconf_set_global_parameter(ctx, "workgroup",
1348 r->in.domain_name);
1350 smbconf_delete_global_parameter(ctx, "realm");
1351 goto done;
1354 werr = smbconf_set_global_parameter(ctx, "security", "domain");
1355 W_ERROR_NOT_OK_GOTO_DONE(werr);
1357 werr = smbconf_set_global_parameter(ctx, "workgroup",
1358 r->out.netbios_domain_name);
1359 W_ERROR_NOT_OK_GOTO_DONE(werr);
1361 if (r->out.domain_is_ad) {
1362 werr = smbconf_set_global_parameter(ctx, "security", "ads");
1363 W_ERROR_NOT_OK_GOTO_DONE(werr);
1365 werr = smbconf_set_global_parameter(ctx, "realm",
1366 r->out.dns_domain_name);
1367 W_ERROR_NOT_OK_GOTO_DONE(werr);
1370 done:
1371 smbconf_shutdown(ctx);
1372 return werr;
1375 /****************************************************************
1376 ****************************************************************/
1378 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1380 WERROR werr = WERR_OK;
1381 struct smbconf_ctx *ctx;
1383 werr = smbconf_init_reg(r, &ctx, NULL);
1384 if (!W_ERROR_IS_OK(werr)) {
1385 goto done;
1388 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1390 werr = smbconf_set_global_parameter(ctx, "security", "user");
1391 W_ERROR_NOT_OK_GOTO_DONE(werr);
1393 werr = smbconf_delete_global_parameter(ctx, "workgroup");
1394 W_ERROR_NOT_OK_GOTO_DONE(werr);
1396 smbconf_delete_global_parameter(ctx, "realm");
1399 done:
1400 smbconf_shutdown(ctx);
1401 return werr;
1404 /****************************************************************
1405 ****************************************************************/
1407 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1409 WERROR werr;
1411 if (!W_ERROR_IS_OK(r->out.result)) {
1412 return r->out.result;
1415 if (!r->in.modify_config) {
1416 return WERR_OK;
1419 werr = do_join_modify_vals_config(r);
1420 if (!W_ERROR_IS_OK(werr)) {
1421 return werr;
1424 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1426 r->out.modified_config = true;
1427 r->out.result = werr;
1429 return werr;
1432 /****************************************************************
1433 ****************************************************************/
1435 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1437 WERROR werr;
1439 if (!W_ERROR_IS_OK(r->out.result)) {
1440 return r->out.result;
1443 if (!r->in.modify_config) {
1444 return WERR_OK;
1447 werr = do_unjoin_modify_vals_config(r);
1448 if (!W_ERROR_IS_OK(werr)) {
1449 return werr;
1452 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1454 r->out.modified_config = true;
1455 r->out.result = werr;
1457 return werr;
1460 /****************************************************************
1461 ****************************************************************/
1463 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1464 const char *domain_str,
1465 const char **domain_p,
1466 const char **dc_p)
1468 char *domain = NULL;
1469 char *dc = NULL;
1470 const char *p = NULL;
1472 if (!domain_str || !domain_p || !dc_p) {
1473 return false;
1476 p = strchr_m(domain_str, '\\');
1478 if (p != NULL) {
1479 domain = talloc_strndup(mem_ctx, domain_str,
1480 PTR_DIFF(p, domain_str));
1481 dc = talloc_strdup(mem_ctx, p+1);
1482 if (!dc) {
1483 return false;
1485 } else {
1486 domain = talloc_strdup(mem_ctx, domain_str);
1487 dc = NULL;
1489 if (!domain) {
1490 return false;
1493 *domain_p = domain;
1495 if (!*dc_p && dc) {
1496 *dc_p = dc;
1499 return true;
1502 /****************************************************************
1503 ****************************************************************/
1505 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1506 struct libnet_JoinCtx *r)
1508 if (!r->in.domain_name) {
1509 libnet_join_set_error_string(mem_ctx, r,
1510 "No domain name defined");
1511 return WERR_INVALID_PARAM;
1514 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1515 &r->in.domain_name,
1516 &r->in.dc_name)) {
1517 libnet_join_set_error_string(mem_ctx, r,
1518 "Failed to parse domain name");
1519 return WERR_INVALID_PARAM;
1522 if (IS_DC) {
1523 return WERR_SETUP_DOMAIN_CONTROLLER;
1526 if (!secrets_init()) {
1527 libnet_join_set_error_string(mem_ctx, r,
1528 "Unable to open secrets database");
1529 return WERR_CAN_NOT_COMPLETE;
1532 return WERR_OK;
1535 /****************************************************************
1536 ****************************************************************/
1538 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1540 NTSTATUS status;
1542 /* Try adding dom admins to builtin\admins. Only log failures. */
1543 status = create_builtin_administrators(domain_sid);
1544 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1545 DEBUG(10,("Unable to auto-add domain administrators to "
1546 "BUILTIN\\Administrators during join because "
1547 "winbindd must be running."));
1548 } else if (!NT_STATUS_IS_OK(status)) {
1549 DEBUG(5, ("Failed to auto-add domain administrators to "
1550 "BUILTIN\\Administrators during join: %s\n",
1551 nt_errstr(status)));
1554 /* Try adding dom users to builtin\users. Only log failures. */
1555 status = create_builtin_users(domain_sid);
1556 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1557 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1558 "during join because winbindd must be running."));
1559 } else if (!NT_STATUS_IS_OK(status)) {
1560 DEBUG(5, ("Failed to auto-add domain administrators to "
1561 "BUILTIN\\Administrators during join: %s\n",
1562 nt_errstr(status)));
1566 /****************************************************************
1567 ****************************************************************/
1569 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1570 struct libnet_JoinCtx *r)
1572 WERROR werr;
1574 if (!W_ERROR_IS_OK(r->out.result)) {
1575 return r->out.result;
1578 werr = do_JoinConfig(r);
1579 if (!W_ERROR_IS_OK(werr)) {
1580 return werr;
1583 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1584 return WERR_OK;
1587 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1588 if (r->out.dns_domain_name) {
1589 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1592 #ifdef WITH_ADS
1593 if (r->out.domain_is_ad &&
1594 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1595 ADS_STATUS ads_status;
1597 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1598 if (!ADS_ERR_OK(ads_status)) {
1599 return WERR_GENERAL_FAILURE;
1602 #endif /* WITH_ADS */
1604 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1606 return WERR_OK;
1609 /****************************************************************
1610 ****************************************************************/
1612 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1614 const char *krb5_cc_env = NULL;
1616 if (r->in.ads) {
1617 ads_destroy(&r->in.ads);
1620 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1621 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1622 unsetenv(KRB5_ENV_CCNAME);
1625 return 0;
1628 /****************************************************************
1629 ****************************************************************/
1631 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1633 const char *krb5_cc_env = NULL;
1635 if (r->in.ads) {
1636 ads_destroy(&r->in.ads);
1639 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1640 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1641 unsetenv(KRB5_ENV_CCNAME);
1644 return 0;
1647 /****************************************************************
1648 ****************************************************************/
1650 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1651 struct libnet_JoinCtx **r)
1653 struct libnet_JoinCtx *ctx;
1654 const char *krb5_cc_env = NULL;
1656 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1657 if (!ctx) {
1658 return WERR_NOMEM;
1661 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1663 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1664 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1666 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1667 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1668 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1669 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1670 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1673 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1675 *r = ctx;
1677 return WERR_OK;
1680 /****************************************************************
1681 ****************************************************************/
1683 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1684 struct libnet_UnjoinCtx **r)
1686 struct libnet_UnjoinCtx *ctx;
1687 const char *krb5_cc_env = NULL;
1689 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1690 if (!ctx) {
1691 return WERR_NOMEM;
1694 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1696 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1697 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1699 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1700 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1701 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1702 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1703 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1706 *r = ctx;
1708 return WERR_OK;
1711 /****************************************************************
1712 ****************************************************************/
1714 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1715 struct libnet_JoinCtx *r)
1717 bool valid_security = false;
1718 bool valid_workgroup = false;
1719 bool valid_realm = false;
1721 /* check if configuration is already set correctly */
1723 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1725 switch (r->out.domain_is_ad) {
1726 case false:
1727 valid_security = (lp_security() == SEC_DOMAIN);
1728 if (valid_workgroup && valid_security) {
1729 /* nothing to be done */
1730 return WERR_OK;
1732 break;
1733 case true:
1734 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1735 switch (lp_security()) {
1736 case SEC_DOMAIN:
1737 case SEC_ADS:
1738 valid_security = true;
1741 if (valid_workgroup && valid_realm && valid_security) {
1742 /* nothing to be done */
1743 return WERR_OK;
1745 break;
1748 /* check if we are supposed to manipulate configuration */
1750 if (!r->in.modify_config) {
1752 char *wrong_conf = talloc_strdup(mem_ctx, "");
1754 if (!valid_workgroup) {
1755 wrong_conf = talloc_asprintf_append(wrong_conf,
1756 "\"workgroup\" set to '%s', should be '%s'",
1757 lp_workgroup(), r->out.netbios_domain_name);
1758 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1761 if (!valid_realm) {
1762 wrong_conf = talloc_asprintf_append(wrong_conf,
1763 "\"realm\" set to '%s', should be '%s'",
1764 lp_realm(), r->out.dns_domain_name);
1765 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1768 if (!valid_security) {
1769 const char *sec = NULL;
1770 switch (lp_security()) {
1771 case SEC_SHARE: sec = "share"; break;
1772 case SEC_USER: sec = "user"; break;
1773 case SEC_DOMAIN: sec = "domain"; break;
1774 case SEC_ADS: sec = "ads"; break;
1776 wrong_conf = talloc_asprintf_append(wrong_conf,
1777 "\"security\" set to '%s', should be %s",
1778 sec, r->out.domain_is_ad ?
1779 "either 'domain' or 'ads'" : "'domain'");
1780 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1783 libnet_join_set_error_string(mem_ctx, r,
1784 "Invalid configuration (%s) and configuration modification "
1785 "was not requested", wrong_conf);
1786 return WERR_CAN_NOT_COMPLETE;
1789 /* check if we are able to manipulate configuration */
1791 if (!lp_config_backend_is_registry()) {
1792 libnet_join_set_error_string(mem_ctx, r,
1793 "Configuration manipulation requested but not "
1794 "supported by backend");
1795 return WERR_NOT_SUPPORTED;
1798 return WERR_OK;
1801 /****************************************************************
1802 ****************************************************************/
1804 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1805 struct libnet_JoinCtx *r)
1807 NTSTATUS status;
1808 WERROR werr;
1809 struct cli_state *cli = NULL;
1810 #ifdef WITH_ADS
1811 ADS_STATUS ads_status;
1812 #endif /* WITH_ADS */
1814 if (!r->in.dc_name) {
1815 struct netr_DsRGetDCNameInfo *info;
1816 const char *dc;
1817 status = dsgetdcname(mem_ctx,
1818 r->in.msg_ctx,
1819 r->in.domain_name,
1820 NULL,
1821 NULL,
1822 DS_FORCE_REDISCOVERY |
1823 DS_DIRECTORY_SERVICE_REQUIRED |
1824 DS_WRITABLE_REQUIRED |
1825 DS_RETURN_DNS_NAME,
1826 &info);
1827 if (!NT_STATUS_IS_OK(status)) {
1828 libnet_join_set_error_string(mem_ctx, r,
1829 "failed to find DC for domain %s",
1830 r->in.domain_name,
1831 get_friendly_nt_error_msg(status));
1832 return WERR_DCNOTFOUND;
1835 dc = strip_hostname(info->dc_unc);
1836 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1837 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1840 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1841 if (!NT_STATUS_IS_OK(status)) {
1842 libnet_join_set_error_string(mem_ctx, r,
1843 "failed to lookup DC info for domain '%s' over rpc: %s",
1844 r->in.domain_name, get_friendly_nt_error_msg(status));
1845 return ntstatus_to_werror(status);
1848 werr = libnet_join_check_config(mem_ctx, r);
1849 if (!W_ERROR_IS_OK(werr)) {
1850 goto done;
1853 #ifdef WITH_ADS
1854 if (r->out.domain_is_ad && r->in.account_ou &&
1855 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1857 ads_status = libnet_join_connect_ads(mem_ctx, r);
1858 if (!ADS_ERR_OK(ads_status)) {
1859 return WERR_DEFAULT_JOIN_REQUIRED;
1862 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1863 if (!ADS_ERR_OK(ads_status)) {
1864 libnet_join_set_error_string(mem_ctx, r,
1865 "failed to precreate account in ou %s: %s",
1866 r->in.account_ou,
1867 ads_errstr(ads_status));
1868 return WERR_DEFAULT_JOIN_REQUIRED;
1871 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1873 #endif /* WITH_ADS */
1875 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
1876 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
1877 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
1878 } else {
1879 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
1881 if (!NT_STATUS_IS_OK(status)) {
1882 libnet_join_set_error_string(mem_ctx, r,
1883 "failed to join domain '%s' over rpc: %s",
1884 r->in.domain_name, get_friendly_nt_error_msg(status));
1885 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1886 return WERR_SETUP_ALREADY_JOINED;
1888 werr = ntstatus_to_werror(status);
1889 goto done;
1892 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1893 werr = WERR_SETUP_NOT_JOINED;
1894 goto done;
1897 werr = WERR_OK;
1899 done:
1900 if (cli) {
1901 cli_shutdown(cli);
1904 return werr;
1907 /****************************************************************
1908 ****************************************************************/
1910 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
1911 struct libnet_JoinCtx *r)
1913 WERROR werr;
1914 struct libnet_UnjoinCtx *u = NULL;
1916 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
1917 if (!W_ERROR_IS_OK(werr)) {
1918 return werr;
1921 u->in.debug = r->in.debug;
1922 u->in.dc_name = r->in.dc_name;
1923 u->in.domain_name = r->in.domain_name;
1924 u->in.admin_account = r->in.admin_account;
1925 u->in.admin_password = r->in.admin_password;
1926 u->in.modify_config = r->in.modify_config;
1927 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
1928 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
1930 werr = libnet_Unjoin(mem_ctx, u);
1931 TALLOC_FREE(u);
1933 return werr;
1936 /****************************************************************
1937 ****************************************************************/
1939 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1940 struct libnet_JoinCtx *r)
1942 WERROR werr;
1944 if (r->in.debug) {
1945 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1948 werr = libnet_join_pre_processing(mem_ctx, r);
1949 if (!W_ERROR_IS_OK(werr)) {
1950 goto done;
1953 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1954 werr = libnet_DomainJoin(mem_ctx, r);
1955 if (!W_ERROR_IS_OK(werr)) {
1956 goto done;
1960 werr = libnet_join_post_processing(mem_ctx, r);
1961 if (!W_ERROR_IS_OK(werr)) {
1962 goto done;
1965 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1966 werr = libnet_join_post_verify(mem_ctx, r);
1967 if (!W_ERROR_IS_OK(werr)) {
1968 libnet_join_rollback(mem_ctx, r);
1972 done:
1973 r->out.result = werr;
1975 if (r->in.debug) {
1976 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1978 return werr;
1981 /****************************************************************
1982 ****************************************************************/
1984 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1985 struct libnet_UnjoinCtx *r)
1987 NTSTATUS status;
1989 if (!r->in.domain_sid) {
1990 struct dom_sid sid;
1991 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1992 libnet_unjoin_set_error_string(mem_ctx, r,
1993 "Unable to fetch domain sid: are we joined?");
1994 return WERR_SETUP_NOT_JOINED;
1996 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
1997 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2000 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2001 !r->in.delete_machine_account) {
2002 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2003 return WERR_OK;
2006 if (!r->in.dc_name) {
2007 struct netr_DsRGetDCNameInfo *info;
2008 const char *dc;
2009 status = dsgetdcname(mem_ctx,
2010 r->in.msg_ctx,
2011 r->in.domain_name,
2012 NULL,
2013 NULL,
2014 DS_DIRECTORY_SERVICE_REQUIRED |
2015 DS_WRITABLE_REQUIRED |
2016 DS_RETURN_DNS_NAME,
2017 &info);
2018 if (!NT_STATUS_IS_OK(status)) {
2019 libnet_unjoin_set_error_string(mem_ctx, r,
2020 "failed to find DC for domain %s",
2021 r->in.domain_name,
2022 get_friendly_nt_error_msg(status));
2023 return WERR_DCNOTFOUND;
2026 dc = strip_hostname(info->dc_unc);
2027 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2028 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2031 #ifdef WITH_ADS
2032 /* for net ads leave, try to delete the account. If it works,
2033 no sense in disabling. If it fails, we can still try to
2034 disable it. jmcd */
2036 if (r->in.delete_machine_account) {
2037 ADS_STATUS ads_status;
2038 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2039 if (ADS_ERR_OK(ads_status)) {
2040 /* dirty hack */
2041 r->out.dns_domain_name =
2042 talloc_strdup(mem_ctx,
2043 r->in.ads->server.realm);
2044 ads_status =
2045 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2047 if (!ADS_ERR_OK(ads_status)) {
2048 libnet_unjoin_set_error_string(mem_ctx, r,
2049 "failed to remove machine account from AD: %s",
2050 ads_errstr(ads_status));
2051 } else {
2052 r->out.deleted_machine_account = true;
2053 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2054 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2055 return WERR_OK;
2058 #endif /* WITH_ADS */
2060 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2061 "disable". */
2062 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2063 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2064 if (!NT_STATUS_IS_OK(status)) {
2065 libnet_unjoin_set_error_string(mem_ctx, r,
2066 "failed to disable machine account via rpc: %s",
2067 get_friendly_nt_error_msg(status));
2068 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2069 return WERR_SETUP_NOT_JOINED;
2071 return ntstatus_to_werror(status);
2074 r->out.disabled_machine_account = true;
2077 /* If disable succeeded or was not requested at all, we
2078 should be getting rid of our end of things */
2080 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2082 return WERR_OK;
2085 /****************************************************************
2086 ****************************************************************/
2088 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2089 struct libnet_UnjoinCtx *r)
2091 if (!r->in.domain_name) {
2092 libnet_unjoin_set_error_string(mem_ctx, r,
2093 "No domain name defined");
2094 return WERR_INVALID_PARAM;
2097 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2098 &r->in.domain_name,
2099 &r->in.dc_name)) {
2100 libnet_unjoin_set_error_string(mem_ctx, r,
2101 "Failed to parse domain name");
2102 return WERR_INVALID_PARAM;
2105 if (IS_DC) {
2106 return WERR_SETUP_DOMAIN_CONTROLLER;
2109 if (!secrets_init()) {
2110 libnet_unjoin_set_error_string(mem_ctx, r,
2111 "Unable to open secrets database");
2112 return WERR_CAN_NOT_COMPLETE;
2115 return WERR_OK;
2118 /****************************************************************
2119 ****************************************************************/
2121 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2122 struct libnet_UnjoinCtx *r)
2124 saf_delete(r->out.netbios_domain_name);
2125 saf_delete(r->out.dns_domain_name);
2127 return libnet_unjoin_config(r);
2130 /****************************************************************
2131 ****************************************************************/
2133 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2134 struct libnet_UnjoinCtx *r)
2136 WERROR werr;
2138 if (r->in.debug) {
2139 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2142 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2143 if (!W_ERROR_IS_OK(werr)) {
2144 goto done;
2147 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2148 werr = libnet_DomainUnjoin(mem_ctx, r);
2149 if (!W_ERROR_IS_OK(werr)) {
2150 libnet_unjoin_config(r);
2151 goto done;
2155 werr = libnet_unjoin_post_processing(mem_ctx, r);
2156 if (!W_ERROR_IS_OK(werr)) {
2157 goto done;
2160 done:
2161 r->out.result = werr;
2163 if (r->in.debug) {
2164 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2167 return werr;