s3-libnetjoin: fix build warning.
[Samba.git] / source / libnet / libnet_join.c
blob3b6af5e3c382c95e73348050e00170925ca7ce48
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 uchar md4_trust_password[16];
766 struct samr_Ids user_rids;
767 struct samr_Ids name_types;
768 union samr_UserInfo user_info;
770 struct samr_CryptPassword crypt_pwd;
771 struct samr_CryptPasswordEx crypt_pwd_ex;
773 ZERO_STRUCT(sam_pol);
774 ZERO_STRUCT(domain_pol);
775 ZERO_STRUCT(user_pol);
777 if (!r->in.machine_password) {
778 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
779 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
782 /* Open the domain */
784 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
785 &pipe_hnd);
786 if (!NT_STATUS_IS_OK(status)) {
787 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
788 nt_errstr(status)));
789 goto done;
792 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
793 pipe_hnd->desthost,
794 SEC_RIGHTS_MAXIMUM_ALLOWED,
795 &sam_pol);
796 if (!NT_STATUS_IS_OK(status)) {
797 goto done;
800 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
801 &sam_pol,
802 SEC_RIGHTS_MAXIMUM_ALLOWED,
803 r->out.domain_sid,
804 &domain_pol);
805 if (!NT_STATUS_IS_OK(status)) {
806 goto done;
809 /* Create domain user */
811 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
812 strlower_m(acct_name);
814 init_lsa_String(&lsa_acct_name, acct_name);
816 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
817 uint32_t access_desired =
818 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
819 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
820 SAMR_USER_ACCESS_SET_PASSWORD |
821 SAMR_USER_ACCESS_GET_ATTRIBUTES |
822 SAMR_USER_ACCESS_SET_ATTRIBUTES;
823 uint32_t access_granted = 0;
825 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
827 DEBUG(10,("Creating account with desired access mask: %d\n",
828 access_desired));
830 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
831 &domain_pol,
832 &lsa_acct_name,
833 ACB_WSTRUST,
834 access_desired,
835 &user_pol,
836 &access_granted,
837 &user_rid);
838 if (!NT_STATUS_IS_OK(status) &&
839 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
841 DEBUG(10,("Creation of workstation account failed: %s\n",
842 nt_errstr(status)));
844 /* If NT_STATUS_ACCESS_DENIED then we have a valid
845 username/password combo but the user does not have
846 administrator access. */
848 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
849 libnet_join_set_error_string(mem_ctx, r,
850 "User specified does not have "
851 "administrator privileges");
854 goto done;
857 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
858 if (!(r->in.join_flags &
859 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
860 goto done;
864 /* We *must* do this.... don't ask... */
866 if (NT_STATUS_IS_OK(status)) {
867 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
871 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
872 &domain_pol,
874 &lsa_acct_name,
875 &user_rids,
876 &name_types);
877 if (!NT_STATUS_IS_OK(status)) {
878 goto done;
881 if (name_types.ids[0] != SID_NAME_USER) {
882 DEBUG(0,("%s is not a user account (type=%d)\n",
883 acct_name, name_types.ids[0]));
884 status = NT_STATUS_INVALID_WORKSTATION;
885 goto done;
888 user_rid = user_rids.ids[0];
890 /* Open handle on user */
892 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
893 &domain_pol,
894 SEC_RIGHTS_MAXIMUM_ALLOWED,
895 user_rid,
896 &user_pol);
897 if (!NT_STATUS_IS_OK(status)) {
898 goto done;
901 /* Create a random machine account password and generate the hash */
903 E_md4hash(r->in.machine_password, md4_trust_password);
905 init_samr_CryptPasswordEx(r->in.machine_password,
906 &cli->user_session_key,
907 &crypt_pwd_ex);
909 /* Fill in the additional account flags now */
911 acct_flags |= ACB_PWNOEXP;
912 if (r->out.domain_is_ad) {
913 #if !defined(ENCTYPE_ARCFOUR_HMAC)
914 acct_flags |= ACB_USE_DES_KEY_ONLY;
915 #endif
919 /* Set password and account flags on machine account */
921 ZERO_STRUCT(user_info.info25);
923 user_info.info25.info.fields_present = ACCT_NT_PWD_SET |
924 ACCT_LM_PWD_SET |
925 SAMR_FIELD_ACCT_FLAGS;
927 user_info.info25.info.acct_flags = acct_flags;
928 memcpy(&user_info.info25.password.data, crypt_pwd_ex.data,
929 sizeof(crypt_pwd_ex.data));
931 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
932 &user_pol,
934 &user_info);
936 if (NT_STATUS_EQUAL(status, NT_STATUS(DCERPC_FAULT_INVALID_TAG))) {
938 /* retry with level 24 */
940 init_samr_CryptPassword(r->in.machine_password,
941 &cli->user_session_key,
942 &crypt_pwd);
944 init_samr_user_info24(&user_info.info24, crypt_pwd.data, 24);
946 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
947 &user_pol,
949 &user_info);
952 if (!NT_STATUS_IS_OK(status)) {
954 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
955 &user_pol);
957 libnet_join_set_error_string(mem_ctx, r,
958 "Failed to set password for machine account (%s)\n",
959 nt_errstr(status));
960 goto done;
963 status = NT_STATUS_OK;
965 done:
966 if (!pipe_hnd) {
967 return status;
970 if (is_valid_policy_hnd(&sam_pol)) {
971 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
973 if (is_valid_policy_hnd(&domain_pol)) {
974 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
976 if (is_valid_policy_hnd(&user_pol)) {
977 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
979 TALLOC_FREE(pipe_hnd);
981 return status;
984 /****************************************************************
985 ****************************************************************/
987 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
988 const char *machine_name,
989 const char *dc_name)
991 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
992 struct cli_state *cli = NULL;
993 struct rpc_pipe_client *pipe_hnd = NULL;
994 struct rpc_pipe_client *netlogon_pipe = NULL;
995 NTSTATUS status;
996 char *machine_password = NULL;
997 char *machine_account = NULL;
999 if (!dc_name) {
1000 return NT_STATUS_INVALID_PARAMETER;
1003 if (!secrets_init()) {
1004 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1007 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1008 NULL, NULL);
1009 if (!machine_password) {
1010 return NT_STATUS_NO_TRUST_LSA_SECRET;
1013 asprintf(&machine_account, "%s$", machine_name);
1014 if (!machine_account) {
1015 SAFE_FREE(machine_password);
1016 return NT_STATUS_NO_MEMORY;
1019 status = cli_full_connection(&cli, NULL,
1020 dc_name,
1021 NULL, 0,
1022 "IPC$", "IPC",
1023 machine_account,
1024 NULL,
1025 machine_password,
1027 Undefined, NULL);
1028 free(machine_account);
1029 free(machine_password);
1031 if (!NT_STATUS_IS_OK(status)) {
1032 status = cli_full_connection(&cli, NULL,
1033 dc_name,
1034 NULL, 0,
1035 "IPC$", "IPC",
1037 NULL,
1040 Undefined, NULL);
1043 if (!NT_STATUS_IS_OK(status)) {
1044 return status;
1047 status = get_schannel_session_key(cli, netbios_domain_name,
1048 &neg_flags, &netlogon_pipe);
1049 if (!NT_STATUS_IS_OK(status)) {
1050 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1051 cli_shutdown(cli);
1052 return NT_STATUS_OK;
1055 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1056 "key from server %s for domain %s. Error was %s\n",
1057 cli->desthost, netbios_domain_name, nt_errstr(status)));
1058 cli_shutdown(cli);
1059 return status;
1062 if (!lp_client_schannel()) {
1063 cli_shutdown(cli);
1064 return NT_STATUS_OK;
1067 status = cli_rpc_pipe_open_schannel_with_key(
1068 cli, &ndr_table_netlogon.syntax_id, PIPE_AUTH_LEVEL_PRIVACY,
1069 netbios_domain_name, netlogon_pipe->dc, &pipe_hnd);
1071 cli_shutdown(cli);
1073 if (!NT_STATUS_IS_OK(status)) {
1074 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1075 "on netlogon pipe to server %s for domain %s. "
1076 "Error was %s\n",
1077 cli->desthost, netbios_domain_name, nt_errstr(status)));
1078 return status;
1081 return NT_STATUS_OK;
1084 /****************************************************************
1085 ****************************************************************/
1087 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1088 struct libnet_JoinCtx *r)
1090 NTSTATUS status;
1092 status = libnet_join_ok(r->out.netbios_domain_name,
1093 r->in.machine_name,
1094 r->in.dc_name);
1095 if (!NT_STATUS_IS_OK(status)) {
1096 libnet_join_set_error_string(mem_ctx, r,
1097 "failed to verify domain membership after joining: %s",
1098 get_friendly_nt_error_msg(status));
1099 return WERR_SETUP_NOT_JOINED;
1102 return WERR_OK;
1105 /****************************************************************
1106 ****************************************************************/
1108 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1109 struct libnet_UnjoinCtx *r)
1111 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1112 return false;
1115 if (!secrets_delete_domain_sid(lp_workgroup())) {
1116 return false;
1119 return true;
1122 /****************************************************************
1123 ****************************************************************/
1125 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1126 struct libnet_UnjoinCtx *r)
1128 struct cli_state *cli = NULL;
1129 struct rpc_pipe_client *pipe_hnd = NULL;
1130 POLICY_HND sam_pol, domain_pol, user_pol;
1131 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1132 char *acct_name;
1133 uint32_t user_rid;
1134 struct lsa_String lsa_acct_name;
1135 struct samr_Ids user_rids;
1136 struct samr_Ids name_types;
1137 union samr_UserInfo *info = NULL;
1139 ZERO_STRUCT(sam_pol);
1140 ZERO_STRUCT(domain_pol);
1141 ZERO_STRUCT(user_pol);
1143 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1144 r->in.admin_account,
1145 r->in.admin_password,
1146 r->in.use_kerberos,
1147 &cli);
1148 if (!NT_STATUS_IS_OK(status)) {
1149 goto done;
1152 /* Open the domain */
1154 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1155 &pipe_hnd);
1156 if (!NT_STATUS_IS_OK(status)) {
1157 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1158 nt_errstr(status)));
1159 goto done;
1162 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1163 pipe_hnd->desthost,
1164 SEC_RIGHTS_MAXIMUM_ALLOWED,
1165 &sam_pol);
1166 if (!NT_STATUS_IS_OK(status)) {
1167 goto done;
1170 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1171 &sam_pol,
1172 SEC_RIGHTS_MAXIMUM_ALLOWED,
1173 r->in.domain_sid,
1174 &domain_pol);
1175 if (!NT_STATUS_IS_OK(status)) {
1176 goto done;
1179 /* Create domain user */
1181 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1182 strlower_m(acct_name);
1184 init_lsa_String(&lsa_acct_name, acct_name);
1186 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1187 &domain_pol,
1189 &lsa_acct_name,
1190 &user_rids,
1191 &name_types);
1193 if (!NT_STATUS_IS_OK(status)) {
1194 goto done;
1197 if (name_types.ids[0] != SID_NAME_USER) {
1198 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1199 name_types.ids[0]));
1200 status = NT_STATUS_INVALID_WORKSTATION;
1201 goto done;
1204 user_rid = user_rids.ids[0];
1206 /* Open handle on user */
1208 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1209 &domain_pol,
1210 SEC_RIGHTS_MAXIMUM_ALLOWED,
1211 user_rid,
1212 &user_pol);
1213 if (!NT_STATUS_IS_OK(status)) {
1214 goto done;
1217 /* Get user info */
1219 status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1220 &user_pol,
1222 &info);
1223 if (!NT_STATUS_IS_OK(status)) {
1224 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1225 goto done;
1228 /* now disable and setuser info */
1230 info->info16.acct_flags |= ACB_DISABLED;
1232 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1233 &user_pol,
1235 info);
1237 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1239 done:
1240 if (pipe_hnd) {
1241 if (is_valid_policy_hnd(&domain_pol)) {
1242 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1244 if (is_valid_policy_hnd(&sam_pol)) {
1245 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1247 TALLOC_FREE(pipe_hnd);
1250 if (cli) {
1251 cli_shutdown(cli);
1254 return status;
1257 /****************************************************************
1258 ****************************************************************/
1260 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1262 WERROR werr;
1263 struct smbconf_ctx *ctx;
1265 werr = smbconf_init_reg(r, &ctx, NULL);
1266 if (!W_ERROR_IS_OK(werr)) {
1267 goto done;
1270 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1272 werr = smbconf_set_global_parameter(ctx, "security", "user");
1273 W_ERROR_NOT_OK_GOTO_DONE(werr);
1275 werr = smbconf_set_global_parameter(ctx, "workgroup",
1276 r->in.domain_name);
1278 smbconf_delete_global_parameter(ctx, "realm");
1279 goto done;
1282 werr = smbconf_set_global_parameter(ctx, "security", "domain");
1283 W_ERROR_NOT_OK_GOTO_DONE(werr);
1285 werr = smbconf_set_global_parameter(ctx, "workgroup",
1286 r->out.netbios_domain_name);
1287 W_ERROR_NOT_OK_GOTO_DONE(werr);
1289 if (r->out.domain_is_ad) {
1290 werr = smbconf_set_global_parameter(ctx, "security", "ads");
1291 W_ERROR_NOT_OK_GOTO_DONE(werr);
1293 werr = smbconf_set_global_parameter(ctx, "realm",
1294 r->out.dns_domain_name);
1295 W_ERROR_NOT_OK_GOTO_DONE(werr);
1298 done:
1299 smbconf_shutdown(ctx);
1300 return werr;
1303 /****************************************************************
1304 ****************************************************************/
1306 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1308 WERROR werr = WERR_OK;
1309 struct smbconf_ctx *ctx;
1311 werr = smbconf_init_reg(r, &ctx, NULL);
1312 if (!W_ERROR_IS_OK(werr)) {
1313 goto done;
1316 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1318 werr = smbconf_set_global_parameter(ctx, "security", "user");
1319 W_ERROR_NOT_OK_GOTO_DONE(werr);
1321 werr = smbconf_delete_global_parameter(ctx, "workgroup");
1322 W_ERROR_NOT_OK_GOTO_DONE(werr);
1324 smbconf_delete_global_parameter(ctx, "realm");
1327 done:
1328 smbconf_shutdown(ctx);
1329 return werr;
1332 /****************************************************************
1333 ****************************************************************/
1335 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1337 WERROR werr;
1339 if (!W_ERROR_IS_OK(r->out.result)) {
1340 return r->out.result;
1343 if (!r->in.modify_config) {
1344 return WERR_OK;
1347 werr = do_join_modify_vals_config(r);
1348 if (!W_ERROR_IS_OK(werr)) {
1349 return werr;
1352 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1354 r->out.modified_config = true;
1355 r->out.result = werr;
1357 return werr;
1360 /****************************************************************
1361 ****************************************************************/
1363 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1365 WERROR werr;
1367 if (!W_ERROR_IS_OK(r->out.result)) {
1368 return r->out.result;
1371 if (!r->in.modify_config) {
1372 return WERR_OK;
1375 werr = do_unjoin_modify_vals_config(r);
1376 if (!W_ERROR_IS_OK(werr)) {
1377 return werr;
1380 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1382 r->out.modified_config = true;
1383 r->out.result = werr;
1385 return werr;
1388 /****************************************************************
1389 ****************************************************************/
1391 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1392 const char *domain_str,
1393 const char **domain_p,
1394 const char **dc_p)
1396 char *domain = NULL;
1397 char *dc = NULL;
1398 const char *p = NULL;
1400 if (!domain_str || !domain_p || !dc_p) {
1401 return false;
1404 p = strchr_m(domain_str, '\\');
1406 if (p != NULL) {
1407 domain = talloc_strndup(mem_ctx, domain_str,
1408 PTR_DIFF(p, domain_str));
1409 dc = talloc_strdup(mem_ctx, p+1);
1410 if (!dc) {
1411 return false;
1413 } else {
1414 domain = talloc_strdup(mem_ctx, domain_str);
1415 dc = NULL;
1417 if (!domain) {
1418 return false;
1421 *domain_p = domain;
1423 if (!*dc_p && dc) {
1424 *dc_p = dc;
1427 return true;
1430 /****************************************************************
1431 ****************************************************************/
1433 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1434 struct libnet_JoinCtx *r)
1436 if (!r->in.domain_name) {
1437 libnet_join_set_error_string(mem_ctx, r,
1438 "No domain name defined");
1439 return WERR_INVALID_PARAM;
1442 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1443 &r->in.domain_name,
1444 &r->in.dc_name)) {
1445 libnet_join_set_error_string(mem_ctx, r,
1446 "Failed to parse domain name");
1447 return WERR_INVALID_PARAM;
1450 if (IS_DC) {
1451 return WERR_SETUP_DOMAIN_CONTROLLER;
1454 if (!secrets_init()) {
1455 libnet_join_set_error_string(mem_ctx, r,
1456 "Unable to open secrets database");
1457 return WERR_CAN_NOT_COMPLETE;
1460 return WERR_OK;
1463 /****************************************************************
1464 ****************************************************************/
1466 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1468 NTSTATUS status;
1470 /* Try adding dom admins to builtin\admins. Only log failures. */
1471 status = create_builtin_administrators(domain_sid);
1472 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1473 DEBUG(10,("Unable to auto-add domain administrators to "
1474 "BUILTIN\\Administrators during join because "
1475 "winbindd must be running."));
1476 } else if (!NT_STATUS_IS_OK(status)) {
1477 DEBUG(5, ("Failed to auto-add domain administrators to "
1478 "BUILTIN\\Administrators during join: %s\n",
1479 nt_errstr(status)));
1482 /* Try adding dom users to builtin\users. Only log failures. */
1483 status = create_builtin_users(domain_sid);
1484 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1485 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1486 "during join because winbindd must be running."));
1487 } else if (!NT_STATUS_IS_OK(status)) {
1488 DEBUG(5, ("Failed to auto-add domain administrators to "
1489 "BUILTIN\\Administrators during join: %s\n",
1490 nt_errstr(status)));
1494 /****************************************************************
1495 ****************************************************************/
1497 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1498 struct libnet_JoinCtx *r)
1500 WERROR werr;
1502 if (!W_ERROR_IS_OK(r->out.result)) {
1503 return r->out.result;
1506 werr = do_JoinConfig(r);
1507 if (!W_ERROR_IS_OK(werr)) {
1508 return werr;
1511 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1512 return WERR_OK;
1515 saf_store(r->in.domain_name, r->in.dc_name);
1517 #ifdef WITH_ADS
1518 if (r->out.domain_is_ad) {
1519 ADS_STATUS ads_status;
1521 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1522 if (!ADS_ERR_OK(ads_status)) {
1523 return WERR_GENERAL_FAILURE;
1526 #endif /* WITH_ADS */
1528 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1530 return WERR_OK;
1533 /****************************************************************
1534 ****************************************************************/
1536 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1538 const char *krb5_cc_env = NULL;
1540 if (r->in.ads) {
1541 ads_destroy(&r->in.ads);
1544 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1545 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1546 unsetenv(KRB5_ENV_CCNAME);
1549 return 0;
1552 /****************************************************************
1553 ****************************************************************/
1555 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1557 const char *krb5_cc_env = NULL;
1559 if (r->in.ads) {
1560 ads_destroy(&r->in.ads);
1563 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1564 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1565 unsetenv(KRB5_ENV_CCNAME);
1568 return 0;
1571 /****************************************************************
1572 ****************************************************************/
1574 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1575 struct libnet_JoinCtx **r)
1577 struct libnet_JoinCtx *ctx;
1578 const char *krb5_cc_env = NULL;
1580 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1581 if (!ctx) {
1582 return WERR_NOMEM;
1585 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1587 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1588 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1590 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1591 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1592 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1593 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1594 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1597 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1599 *r = ctx;
1601 return WERR_OK;
1604 /****************************************************************
1605 ****************************************************************/
1607 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1608 struct libnet_UnjoinCtx **r)
1610 struct libnet_UnjoinCtx *ctx;
1611 const char *krb5_cc_env = NULL;
1613 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1614 if (!ctx) {
1615 return WERR_NOMEM;
1618 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1620 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1621 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1623 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1624 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1625 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1626 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1627 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1630 *r = ctx;
1632 return WERR_OK;
1635 /****************************************************************
1636 ****************************************************************/
1638 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1639 struct libnet_JoinCtx *r)
1641 bool valid_security = false;
1642 bool valid_workgroup = false;
1643 bool valid_realm = false;
1645 /* check if configuration is already set correctly */
1647 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1649 switch (r->out.domain_is_ad) {
1650 case false:
1651 valid_security = (lp_security() == SEC_DOMAIN);
1652 if (valid_workgroup && valid_security) {
1653 /* nothing to be done */
1654 return WERR_OK;
1656 break;
1657 case true:
1658 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1659 switch (lp_security()) {
1660 case SEC_DOMAIN:
1661 case SEC_ADS:
1662 valid_security = true;
1665 if (valid_workgroup && valid_realm && valid_security) {
1666 /* nothing to be done */
1667 return WERR_OK;
1669 break;
1672 /* check if we are supposed to manipulate configuration */
1674 if (!r->in.modify_config) {
1676 char *wrong_conf = talloc_strdup(mem_ctx, "");
1678 if (!valid_workgroup) {
1679 wrong_conf = talloc_asprintf_append(wrong_conf,
1680 "\"workgroup\" set to '%s', should be '%s'",
1681 lp_workgroup(), r->out.netbios_domain_name);
1682 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1685 if (!valid_realm) {
1686 wrong_conf = talloc_asprintf_append(wrong_conf,
1687 "\"realm\" set to '%s', should be '%s'",
1688 lp_realm(), r->out.dns_domain_name);
1689 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1692 if (!valid_security) {
1693 const char *sec = NULL;
1694 switch (lp_security()) {
1695 case SEC_SHARE: sec = "share"; break;
1696 case SEC_USER: sec = "user"; break;
1697 case SEC_DOMAIN: sec = "domain"; break;
1698 case SEC_ADS: sec = "ads"; break;
1700 wrong_conf = talloc_asprintf_append(wrong_conf,
1701 "\"security\" set to '%s', should be %s",
1702 sec, r->out.domain_is_ad ?
1703 "either 'domain' or 'ads'" : "'domain'");
1704 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1707 libnet_join_set_error_string(mem_ctx, r,
1708 "Invalid configuration (%s) and configuration modification "
1709 "was not requested", wrong_conf);
1710 return WERR_CAN_NOT_COMPLETE;
1713 /* check if we are able to manipulate configuration */
1715 if (!lp_config_backend_is_registry()) {
1716 libnet_join_set_error_string(mem_ctx, r,
1717 "Configuration manipulation requested but not "
1718 "supported by backend");
1719 return WERR_NOT_SUPPORTED;
1722 return WERR_OK;
1725 /****************************************************************
1726 ****************************************************************/
1728 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1729 struct libnet_JoinCtx *r)
1731 NTSTATUS status;
1732 WERROR werr;
1733 struct cli_state *cli = NULL;
1734 #ifdef WITH_ADS
1735 ADS_STATUS ads_status;
1736 #endif /* WITH_ADS */
1738 if (!r->in.dc_name) {
1739 struct netr_DsRGetDCNameInfo *info;
1740 const char *dc;
1741 status = dsgetdcname(mem_ctx,
1742 r->in.msg_ctx,
1743 r->in.domain_name,
1744 NULL,
1745 NULL,
1746 DS_DIRECTORY_SERVICE_REQUIRED |
1747 DS_WRITABLE_REQUIRED |
1748 DS_RETURN_DNS_NAME,
1749 &info);
1750 if (!NT_STATUS_IS_OK(status)) {
1751 libnet_join_set_error_string(mem_ctx, r,
1752 "failed to find DC for domain %s",
1753 r->in.domain_name,
1754 get_friendly_nt_error_msg(status));
1755 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1758 dc = strip_hostname(info->dc_unc);
1759 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1760 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1763 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1764 if (!NT_STATUS_IS_OK(status)) {
1765 libnet_join_set_error_string(mem_ctx, r,
1766 "failed to lookup DC info for domain '%s' over rpc: %s",
1767 r->in.domain_name, get_friendly_nt_error_msg(status));
1768 return ntstatus_to_werror(status);
1771 werr = libnet_join_check_config(mem_ctx, r);
1772 if (!W_ERROR_IS_OK(werr)) {
1773 goto done;
1776 #ifdef WITH_ADS
1777 if (r->out.domain_is_ad && r->in.account_ou) {
1779 ads_status = libnet_join_connect_ads(mem_ctx, r);
1780 if (!ADS_ERR_OK(ads_status)) {
1781 return WERR_DEFAULT_JOIN_REQUIRED;
1784 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1785 if (!ADS_ERR_OK(ads_status)) {
1786 libnet_join_set_error_string(mem_ctx, r,
1787 "failed to precreate account in ou %s: %s",
1788 r->in.account_ou,
1789 ads_errstr(ads_status));
1790 return WERR_DEFAULT_JOIN_REQUIRED;
1793 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1795 #endif /* WITH_ADS */
1797 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
1798 if (!NT_STATUS_IS_OK(status)) {
1799 libnet_join_set_error_string(mem_ctx, r,
1800 "failed to join domain '%s' over rpc: %s",
1801 r->in.domain_name, get_friendly_nt_error_msg(status));
1802 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1803 return WERR_SETUP_ALREADY_JOINED;
1805 werr = ntstatus_to_werror(status);
1806 goto done;
1809 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1810 werr = WERR_SETUP_NOT_JOINED;
1811 goto done;
1814 werr = WERR_OK;
1816 done:
1817 if (cli) {
1818 cli_shutdown(cli);
1821 return werr;
1824 /****************************************************************
1825 ****************************************************************/
1827 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
1828 struct libnet_JoinCtx *r)
1830 WERROR werr;
1831 struct libnet_UnjoinCtx *u = NULL;
1833 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
1834 if (!W_ERROR_IS_OK(werr)) {
1835 return werr;
1838 u->in.debug = r->in.debug;
1839 u->in.dc_name = r->in.dc_name;
1840 u->in.domain_name = r->in.domain_name;
1841 u->in.admin_account = r->in.admin_account;
1842 u->in.admin_password = r->in.admin_password;
1843 u->in.modify_config = r->in.modify_config;
1844 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
1845 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
1847 werr = libnet_Unjoin(mem_ctx, u);
1848 TALLOC_FREE(u);
1850 return werr;
1853 /****************************************************************
1854 ****************************************************************/
1856 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1857 struct libnet_JoinCtx *r)
1859 WERROR werr;
1861 if (r->in.debug) {
1862 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1865 werr = libnet_join_pre_processing(mem_ctx, r);
1866 if (!W_ERROR_IS_OK(werr)) {
1867 goto done;
1870 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1871 werr = libnet_DomainJoin(mem_ctx, r);
1872 if (!W_ERROR_IS_OK(werr)) {
1873 goto done;
1877 werr = libnet_join_post_processing(mem_ctx, r);
1878 if (!W_ERROR_IS_OK(werr)) {
1879 goto done;
1882 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1883 werr = libnet_join_post_verify(mem_ctx, r);
1884 if (!W_ERROR_IS_OK(werr)) {
1885 libnet_join_rollback(mem_ctx, r);
1889 done:
1890 r->out.result = werr;
1892 if (r->in.debug) {
1893 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1895 return werr;
1898 /****************************************************************
1899 ****************************************************************/
1901 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1902 struct libnet_UnjoinCtx *r)
1904 NTSTATUS status;
1906 if (!r->in.domain_sid) {
1907 struct dom_sid sid;
1908 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1909 libnet_unjoin_set_error_string(mem_ctx, r,
1910 "Unable to fetch domain sid: are we joined?");
1911 return WERR_SETUP_NOT_JOINED;
1913 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
1914 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
1917 if (!r->in.dc_name) {
1918 struct netr_DsRGetDCNameInfo *info;
1919 const char *dc;
1920 status = dsgetdcname(mem_ctx,
1921 r->in.msg_ctx,
1922 r->in.domain_name,
1923 NULL,
1924 NULL,
1925 DS_DIRECTORY_SERVICE_REQUIRED |
1926 DS_WRITABLE_REQUIRED |
1927 DS_RETURN_DNS_NAME,
1928 &info);
1929 if (!NT_STATUS_IS_OK(status)) {
1930 libnet_unjoin_set_error_string(mem_ctx, r,
1931 "failed to find DC for domain %s",
1932 r->in.domain_name,
1933 get_friendly_nt_error_msg(status));
1934 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1937 dc = strip_hostname(info->dc_unc);
1938 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1939 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1942 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
1943 if (!NT_STATUS_IS_OK(status)) {
1944 libnet_unjoin_set_error_string(mem_ctx, r,
1945 "failed to disable machine account via rpc: %s",
1946 get_friendly_nt_error_msg(status));
1947 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1948 return WERR_SETUP_NOT_JOINED;
1950 return ntstatus_to_werror(status);
1953 r->out.disabled_machine_account = true;
1955 #ifdef WITH_ADS
1956 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
1957 ADS_STATUS ads_status;
1958 libnet_unjoin_connect_ads(mem_ctx, r);
1959 ads_status = libnet_unjoin_remove_machine_acct(mem_ctx, r);
1960 if (!ADS_ERR_OK(ads_status)) {
1961 libnet_unjoin_set_error_string(mem_ctx, r,
1962 "failed to remove machine account from AD: %s",
1963 ads_errstr(ads_status));
1964 } else {
1965 r->out.deleted_machine_account = true;
1966 /* dirty hack */
1967 r->out.dns_domain_name = talloc_strdup(mem_ctx,
1968 r->in.ads->server.realm);
1969 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
1972 #endif /* WITH_ADS */
1974 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
1976 return WERR_OK;
1979 /****************************************************************
1980 ****************************************************************/
1982 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
1983 struct libnet_UnjoinCtx *r)
1985 if (!r->in.domain_name) {
1986 libnet_unjoin_set_error_string(mem_ctx, r,
1987 "No domain name defined");
1988 return WERR_INVALID_PARAM;
1991 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1992 &r->in.domain_name,
1993 &r->in.dc_name)) {
1994 libnet_unjoin_set_error_string(mem_ctx, r,
1995 "Failed to parse domain name");
1996 return WERR_INVALID_PARAM;
1999 if (IS_DC) {
2000 return WERR_SETUP_DOMAIN_CONTROLLER;
2003 if (!secrets_init()) {
2004 libnet_unjoin_set_error_string(mem_ctx, r,
2005 "Unable to open secrets database");
2006 return WERR_CAN_NOT_COMPLETE;
2009 return WERR_OK;
2012 /****************************************************************
2013 ****************************************************************/
2015 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2016 struct libnet_UnjoinCtx *r)
2018 saf_delete(r->out.netbios_domain_name);
2019 saf_delete(r->out.dns_domain_name);
2021 return libnet_unjoin_config(r);
2024 /****************************************************************
2025 ****************************************************************/
2027 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2028 struct libnet_UnjoinCtx *r)
2030 WERROR werr;
2032 if (r->in.debug) {
2033 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2036 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2037 if (!W_ERROR_IS_OK(werr)) {
2038 goto done;
2041 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2042 werr = libnet_DomainUnjoin(mem_ctx, r);
2043 if (!W_ERROR_IS_OK(werr)) {
2044 libnet_unjoin_config(r);
2045 goto done;
2049 werr = libnet_unjoin_post_processing(mem_ctx, r);
2050 if (!W_ERROR_IS_OK(werr)) {
2051 goto done;
2054 done:
2055 r->out.result = werr;
2057 if (r->in.debug) {
2058 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2061 return werr;