libnetjoin: fix Bug #5570.
[Samba.git] / source / libnet / libnet_join.c
blob86cbce02242f728bad51ea6495be9a391494dd2d
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(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 !strequal(my_fqdn, r->in.machine_name)) {
363 strlower_m(my_fqdn);
364 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
365 if (!spn) {
366 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
368 spn_array[1] = spn;
371 mods = ads_init_mods(mem_ctx);
372 if (!mods) {
373 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
376 /* fields of primary importance */
378 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
379 if (!ADS_ERR_OK(status)) {
380 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
383 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
384 spn_array);
385 if (!ADS_ERR_OK(status)) {
386 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
389 return ads_gen_mod(r->in.ads, r->out.dn, mods);
392 /****************************************************************
393 ****************************************************************/
395 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
396 struct libnet_JoinCtx *r)
398 ADS_STATUS status;
399 ADS_MODLIST mods;
401 if (!r->in.create_upn) {
402 return ADS_SUCCESS;
405 /* Find our DN */
407 status = libnet_join_find_machine_acct(mem_ctx, r);
408 if (!ADS_ERR_OK(status)) {
409 return status;
412 if (!r->in.upn) {
413 r->in.upn = talloc_asprintf(mem_ctx,
414 "host/%s@%s",
415 r->in.machine_name,
416 r->out.dns_domain_name);
417 if (!r->in.upn) {
418 return ADS_ERROR(LDAP_NO_MEMORY);
422 /* now do the mods */
424 mods = ads_init_mods(mem_ctx);
425 if (!mods) {
426 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
429 /* fields of primary importance */
431 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
432 if (!ADS_ERR_OK(status)) {
433 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
436 return ads_gen_mod(r->in.ads, r->out.dn, mods);
440 /****************************************************************
441 ****************************************************************/
443 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
444 struct libnet_JoinCtx *r)
446 ADS_STATUS status;
447 ADS_MODLIST mods;
448 char *os_sp = NULL;
450 if (!r->in.os_name || !r->in.os_version ) {
451 return ADS_SUCCESS;
454 /* Find our DN */
456 status = libnet_join_find_machine_acct(mem_ctx, r);
457 if (!ADS_ERR_OK(status)) {
458 return status;
461 /* now do the mods */
463 mods = ads_init_mods(mem_ctx);
464 if (!mods) {
465 return ADS_ERROR(LDAP_NO_MEMORY);
468 os_sp = talloc_asprintf(mem_ctx, "Samba %s", SAMBA_VERSION_STRING);
469 if (!os_sp) {
470 return ADS_ERROR(LDAP_NO_MEMORY);
473 /* fields of primary importance */
475 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
476 r->in.os_name);
477 if (!ADS_ERR_OK(status)) {
478 return status;
481 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
482 r->in.os_version);
483 if (!ADS_ERR_OK(status)) {
484 return status;
487 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
488 os_sp);
489 if (!ADS_ERR_OK(status)) {
490 return status;
493 return ads_gen_mod(r->in.ads, r->out.dn, mods);
496 /****************************************************************
497 ****************************************************************/
499 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
500 struct libnet_JoinCtx *r)
502 if (!lp_use_kerberos_keytab()) {
503 return true;
506 if (ads_keytab_create_default(r->in.ads) != 0) {
507 return false;
510 return true;
513 /****************************************************************
514 ****************************************************************/
516 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
517 struct libnet_JoinCtx *r)
519 uint32_t domain_func;
520 ADS_STATUS status;
521 const char *salt = NULL;
522 char *std_salt = NULL;
524 status = ads_domain_func_level(r->in.ads, &domain_func);
525 if (!ADS_ERR_OK(status)) {
526 libnet_join_set_error_string(mem_ctx, r,
527 "failed to determine domain functional level: %s",
528 ads_errstr(status));
529 return false;
532 /* go ahead and setup the default salt */
534 std_salt = kerberos_standard_des_salt();
535 if (!std_salt) {
536 libnet_join_set_error_string(mem_ctx, r,
537 "failed to obtain standard DES salt");
538 return false;
541 salt = talloc_strdup(mem_ctx, std_salt);
542 if (!salt) {
543 return false;
546 SAFE_FREE(std_salt);
548 /* if it's a Windows functional domain, we have to look for the UPN */
550 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
551 char *upn;
553 upn = ads_get_upn(r->in.ads, mem_ctx,
554 r->in.machine_name);
555 if (upn) {
556 salt = talloc_strdup(mem_ctx, upn);
557 if (!salt) {
558 return false;
563 return kerberos_secrets_store_des_salt(salt);
566 /****************************************************************
567 ****************************************************************/
569 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
570 struct libnet_JoinCtx *r)
572 ADS_STATUS status;
574 if (!r->in.ads) {
575 status = libnet_join_connect_ads(mem_ctx, r);
576 if (!ADS_ERR_OK(status)) {
577 return status;
581 status = libnet_join_set_machine_spn(mem_ctx, r);
582 if (!ADS_ERR_OK(status)) {
583 libnet_join_set_error_string(mem_ctx, r,
584 "failed to set machine spn: %s",
585 ads_errstr(status));
586 return status;
589 status = libnet_join_set_os_attributes(mem_ctx, r);
590 if (!ADS_ERR_OK(status)) {
591 libnet_join_set_error_string(mem_ctx, r,
592 "failed to set machine os attributes: %s",
593 ads_errstr(status));
594 return status;
597 status = libnet_join_set_machine_upn(mem_ctx, r);
598 if (!ADS_ERR_OK(status)) {
599 libnet_join_set_error_string(mem_ctx, r,
600 "failed to set machine upn: %s",
601 ads_errstr(status));
602 return status;
605 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
606 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
609 if (!libnet_join_create_keytab(mem_ctx, r)) {
610 libnet_join_set_error_string(mem_ctx, r,
611 "failed to create kerberos keytab");
612 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
615 return ADS_SUCCESS;
617 #endif /* WITH_ADS */
619 /****************************************************************
620 Store the machine password and domain SID
621 ****************************************************************/
623 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
624 struct libnet_JoinCtx *r)
626 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
627 r->out.domain_sid))
629 DEBUG(1,("Failed to save domain sid\n"));
630 return false;
633 if (!secrets_store_machine_password(r->in.machine_password,
634 r->out.netbios_domain_name,
635 r->in.secure_channel_type))
637 DEBUG(1,("Failed to save machine password\n"));
638 return false;
641 return true;
644 /****************************************************************
645 Lookup domain dc's info
646 ****************************************************************/
648 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
649 struct libnet_JoinCtx *r,
650 struct cli_state **cli)
652 struct rpc_pipe_client *pipe_hnd = NULL;
653 POLICY_HND lsa_pol;
654 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
655 union lsa_PolicyInformation *info = NULL;
657 status = cli_full_connection(cli, NULL,
658 r->in.dc_name,
659 NULL, 0,
660 "IPC$", "IPC",
661 r->in.admin_account,
662 NULL,
663 r->in.admin_password,
665 Undefined, NULL);
667 if (!NT_STATUS_IS_OK(status)) {
668 goto done;
671 pipe_hnd = cli_rpc_pipe_open_noauth(*cli, PI_LSARPC, &status);
672 if (!pipe_hnd) {
673 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
674 nt_errstr(status)));
675 goto done;
678 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
679 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
680 if (!NT_STATUS_IS_OK(status)) {
681 goto done;
684 status = rpccli_lsa_QueryInfoPolicy2(pipe_hnd, mem_ctx,
685 &lsa_pol,
686 LSA_POLICY_INFO_DNS,
687 &info);
688 if (NT_STATUS_IS_OK(status)) {
689 r->out.domain_is_ad = true;
690 r->out.netbios_domain_name = info->dns.name.string;
691 r->out.dns_domain_name = info->dns.dns_domain.string;
692 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->dns.sid);
693 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
696 if (!NT_STATUS_IS_OK(status)) {
697 status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
698 &lsa_pol,
699 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
700 &info);
701 if (!NT_STATUS_IS_OK(status)) {
702 goto done;
705 r->out.netbios_domain_name = info->account_domain.name.string;
706 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->account_domain.sid);
707 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
710 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
711 cli_rpc_pipe_close(pipe_hnd);
713 done:
714 return status;
717 /****************************************************************
718 Do the domain join
719 ****************************************************************/
721 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
722 struct libnet_JoinCtx *r,
723 struct cli_state *cli)
725 struct rpc_pipe_client *pipe_hnd = NULL;
726 POLICY_HND sam_pol, domain_pol, user_pol;
727 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
728 char *acct_name;
729 struct lsa_String lsa_acct_name;
730 uint32_t user_rid;
731 uint32_t acct_flags = ACB_WSTRUST;
732 uchar pwbuf[532];
733 struct MD5Context md5ctx;
734 uchar md5buffer[16];
735 DATA_BLOB digested_session_key;
736 uchar md4_trust_password[16];
737 struct samr_Ids user_rids;
738 struct samr_Ids name_types;
739 union samr_UserInfo user_info;
741 ZERO_STRUCT(sam_pol);
742 ZERO_STRUCT(domain_pol);
743 ZERO_STRUCT(user_pol);
745 if (!r->in.machine_password) {
746 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
747 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
750 /* Open the domain */
752 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
753 if (!pipe_hnd) {
754 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
755 nt_errstr(status)));
756 goto done;
759 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
760 pipe_hnd->cli->desthost,
761 SEC_RIGHTS_MAXIMUM_ALLOWED,
762 &sam_pol);
763 if (!NT_STATUS_IS_OK(status)) {
764 goto done;
767 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
768 &sam_pol,
769 SEC_RIGHTS_MAXIMUM_ALLOWED,
770 r->out.domain_sid,
771 &domain_pol);
772 if (!NT_STATUS_IS_OK(status)) {
773 goto done;
776 /* Create domain user */
778 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
779 strlower_m(acct_name);
781 init_lsa_String(&lsa_acct_name, acct_name);
783 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
784 uint32_t access_desired =
785 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
786 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
787 SAMR_USER_ACCESS_SET_PASSWORD |
788 SAMR_USER_ACCESS_GET_ATTRIBUTES |
789 SAMR_USER_ACCESS_SET_ATTRIBUTES;
790 uint32_t access_granted = 0;
792 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
794 DEBUG(10,("Creating account with desired access mask: %d\n",
795 access_desired));
797 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
798 &domain_pol,
799 &lsa_acct_name,
800 ACB_WSTRUST,
801 access_desired,
802 &user_pol,
803 &access_granted,
804 &user_rid);
805 if (!NT_STATUS_IS_OK(status) &&
806 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
808 DEBUG(10,("Creation of workstation account failed: %s\n",
809 nt_errstr(status)));
811 /* If NT_STATUS_ACCESS_DENIED then we have a valid
812 username/password combo but the user does not have
813 administrator access. */
815 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
816 libnet_join_set_error_string(mem_ctx, r,
817 "User specified does not have "
818 "administrator privileges");
821 goto done;
824 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
825 if (!(r->in.join_flags &
826 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
827 goto done;
831 /* We *must* do this.... don't ask... */
833 if (NT_STATUS_IS_OK(status)) {
834 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
838 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
839 &domain_pol,
841 &lsa_acct_name,
842 &user_rids,
843 &name_types);
844 if (!NT_STATUS_IS_OK(status)) {
845 goto done;
848 if (name_types.ids[0] != SID_NAME_USER) {
849 DEBUG(0,("%s is not a user account (type=%d)\n",
850 acct_name, name_types.ids[0]));
851 status = NT_STATUS_INVALID_WORKSTATION;
852 goto done;
855 user_rid = user_rids.ids[0];
857 /* Open handle on user */
859 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
860 &domain_pol,
861 SEC_RIGHTS_MAXIMUM_ALLOWED,
862 user_rid,
863 &user_pol);
864 if (!NT_STATUS_IS_OK(status)) {
865 goto done;
868 /* Create a random machine account password and generate the hash */
870 E_md4hash(r->in.machine_password, md4_trust_password);
871 encode_pw_buffer(pwbuf, r->in.machine_password, STR_UNICODE);
873 generate_random_buffer((uint8_t*)md5buffer, sizeof(md5buffer));
874 digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
876 MD5Init(&md5ctx);
877 MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
878 MD5Update(&md5ctx, cli->user_session_key.data,
879 cli->user_session_key.length);
880 MD5Final(digested_session_key.data, &md5ctx);
882 SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
883 memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
885 /* Fill in the additional account flags now */
887 acct_flags |= ACB_PWNOEXP;
888 if (r->out.domain_is_ad) {
889 #if !defined(ENCTYPE_ARCFOUR_HMAC)
890 acct_flags |= ACB_USE_DES_KEY_ONLY;
891 #endif
895 /* Set password and account flags on machine account */
897 ZERO_STRUCT(user_info.info25);
899 user_info.info25.info.fields_present = ACCT_NT_PWD_SET |
900 ACCT_LM_PWD_SET |
901 SAMR_FIELD_ACCT_FLAGS;
903 user_info.info25.info.acct_flags = acct_flags;
904 memcpy(&user_info.info25.password.data, pwbuf, sizeof(pwbuf));
906 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
907 &user_pol,
909 &user_info);
911 if (NT_STATUS_EQUAL(status, NT_STATUS(DCERPC_FAULT_INVALID_TAG))) {
913 uchar pwbuf2[516];
915 encode_pw_buffer(pwbuf2, r->in.machine_password, STR_UNICODE);
917 /* retry with level 24 */
918 init_samr_user_info24(&user_info.info24, pwbuf2, 24);
920 SamOEMhashBlob(user_info.info24.password.data, 516,
921 &cli->user_session_key);
923 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
924 &user_pol,
926 &user_info);
929 if (!NT_STATUS_IS_OK(status)) {
931 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
932 &user_pol);
934 libnet_join_set_error_string(mem_ctx, r,
935 "Failed to set password for machine account (%s)\n",
936 nt_errstr(status));
937 goto done;
940 status = NT_STATUS_OK;
942 done:
943 if (!pipe_hnd) {
944 return status;
947 if (is_valid_policy_hnd(&sam_pol)) {
948 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
950 if (is_valid_policy_hnd(&domain_pol)) {
951 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
953 if (is_valid_policy_hnd(&user_pol)) {
954 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
956 cli_rpc_pipe_close(pipe_hnd);
958 return status;
961 /****************************************************************
962 ****************************************************************/
964 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
965 const char *machine_name,
966 const char *dc_name)
968 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
969 struct cli_state *cli = NULL;
970 struct rpc_pipe_client *pipe_hnd = NULL;
971 struct rpc_pipe_client *netlogon_pipe = NULL;
972 NTSTATUS status;
973 char *machine_password = NULL;
974 char *machine_account = NULL;
976 if (!dc_name) {
977 return NT_STATUS_INVALID_PARAMETER;
980 if (!secrets_init()) {
981 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
984 machine_password = secrets_fetch_machine_password(netbios_domain_name,
985 NULL, NULL);
986 if (!machine_password) {
987 return NT_STATUS_NO_TRUST_LSA_SECRET;
990 asprintf(&machine_account, "%s$", machine_name);
991 if (!machine_account) {
992 SAFE_FREE(machine_password);
993 return NT_STATUS_NO_MEMORY;
996 status = cli_full_connection(&cli, NULL,
997 dc_name,
998 NULL, 0,
999 "IPC$", "IPC",
1000 machine_account,
1001 NULL,
1002 machine_password,
1004 Undefined, NULL);
1005 free(machine_account);
1006 free(machine_password);
1008 if (!NT_STATUS_IS_OK(status)) {
1009 status = cli_full_connection(&cli, NULL,
1010 dc_name,
1011 NULL, 0,
1012 "IPC$", "IPC",
1014 NULL,
1017 Undefined, NULL);
1020 if (!NT_STATUS_IS_OK(status)) {
1021 return status;
1024 netlogon_pipe = get_schannel_session_key(cli,
1025 netbios_domain_name,
1026 &neg_flags, &status);
1027 if (!netlogon_pipe) {
1028 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1029 cli_shutdown(cli);
1030 return NT_STATUS_OK;
1033 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1034 "key from server %s for domain %s. Error was %s\n",
1035 cli->desthost, netbios_domain_name, nt_errstr(status)));
1036 cli_shutdown(cli);
1037 return status;
1040 if (!lp_client_schannel()) {
1041 cli_shutdown(cli);
1042 return NT_STATUS_OK;
1045 pipe_hnd = cli_rpc_pipe_open_schannel_with_key(cli, PI_NETLOGON,
1046 PIPE_AUTH_LEVEL_PRIVACY,
1047 netbios_domain_name,
1048 netlogon_pipe->dc,
1049 &status);
1051 cli_shutdown(cli);
1053 if (!pipe_hnd) {
1054 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1055 "on netlogon pipe to server %s for domain %s. "
1056 "Error was %s\n",
1057 cli->desthost, netbios_domain_name, nt_errstr(status)));
1058 return status;
1061 return NT_STATUS_OK;
1064 /****************************************************************
1065 ****************************************************************/
1067 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1068 struct libnet_JoinCtx *r)
1070 NTSTATUS status;
1072 status = libnet_join_ok(r->out.netbios_domain_name,
1073 r->in.machine_name,
1074 r->in.dc_name);
1075 if (!NT_STATUS_IS_OK(status)) {
1076 libnet_join_set_error_string(mem_ctx, r,
1077 "failed to verify domain membership after joining: %s",
1078 get_friendly_nt_error_msg(status));
1079 return WERR_SETUP_NOT_JOINED;
1082 return WERR_OK;
1085 /****************************************************************
1086 ****************************************************************/
1088 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1089 struct libnet_UnjoinCtx *r)
1091 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1092 return false;
1095 if (!secrets_delete_domain_sid(lp_workgroup())) {
1096 return false;
1099 return true;
1102 /****************************************************************
1103 ****************************************************************/
1105 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1106 struct libnet_UnjoinCtx *r)
1108 struct cli_state *cli = NULL;
1109 struct rpc_pipe_client *pipe_hnd = NULL;
1110 POLICY_HND sam_pol, domain_pol, user_pol;
1111 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1112 char *acct_name;
1113 uint32_t user_rid;
1114 struct lsa_String lsa_acct_name;
1115 struct samr_Ids user_rids;
1116 struct samr_Ids name_types;
1117 union samr_UserInfo *info = NULL;
1119 ZERO_STRUCT(sam_pol);
1120 ZERO_STRUCT(domain_pol);
1121 ZERO_STRUCT(user_pol);
1123 status = cli_full_connection(&cli, NULL,
1124 r->in.dc_name,
1125 NULL, 0,
1126 "IPC$", "IPC",
1127 r->in.admin_account,
1128 NULL,
1129 r->in.admin_password,
1130 0, Undefined, NULL);
1132 if (!NT_STATUS_IS_OK(status)) {
1133 goto done;
1136 /* Open the domain */
1138 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
1139 if (!pipe_hnd) {
1140 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1141 nt_errstr(status)));
1142 goto done;
1145 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1146 pipe_hnd->cli->desthost,
1147 SEC_RIGHTS_MAXIMUM_ALLOWED,
1148 &sam_pol);
1149 if (!NT_STATUS_IS_OK(status)) {
1150 goto done;
1153 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1154 &sam_pol,
1155 SEC_RIGHTS_MAXIMUM_ALLOWED,
1156 r->in.domain_sid,
1157 &domain_pol);
1158 if (!NT_STATUS_IS_OK(status)) {
1159 goto done;
1162 /* Create domain user */
1164 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1165 strlower_m(acct_name);
1167 init_lsa_String(&lsa_acct_name, acct_name);
1169 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1170 &domain_pol,
1172 &lsa_acct_name,
1173 &user_rids,
1174 &name_types);
1176 if (!NT_STATUS_IS_OK(status)) {
1177 goto done;
1180 if (name_types.ids[0] != SID_NAME_USER) {
1181 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1182 name_types.ids[0]));
1183 status = NT_STATUS_INVALID_WORKSTATION;
1184 goto done;
1187 user_rid = user_rids.ids[0];
1189 /* Open handle on user */
1191 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1192 &domain_pol,
1193 SEC_RIGHTS_MAXIMUM_ALLOWED,
1194 user_rid,
1195 &user_pol);
1196 if (!NT_STATUS_IS_OK(status)) {
1197 goto done;
1200 /* Get user info */
1202 status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1203 &user_pol,
1205 &info);
1206 if (!NT_STATUS_IS_OK(status)) {
1207 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1208 goto done;
1211 /* now disable and setuser info */
1213 info->info16.acct_flags |= ACB_DISABLED;
1215 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1216 &user_pol,
1218 info);
1220 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1222 done:
1223 if (pipe_hnd) {
1224 if (is_valid_policy_hnd(&domain_pol)) {
1225 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1227 if (is_valid_policy_hnd(&sam_pol)) {
1228 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1230 cli_rpc_pipe_close(pipe_hnd);
1233 if (cli) {
1234 cli_shutdown(cli);
1237 return status;
1240 /****************************************************************
1241 ****************************************************************/
1243 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1245 WERROR werr;
1246 struct smbconf_ctx *ctx;
1248 werr = smbconf_init_reg(r, &ctx, NULL);
1249 if (!W_ERROR_IS_OK(werr)) {
1250 goto done;
1253 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1255 werr = smbconf_set_global_parameter(ctx, "security", "user");
1256 W_ERROR_NOT_OK_GOTO_DONE(werr);
1258 werr = smbconf_set_global_parameter(ctx, "workgroup",
1259 r->in.domain_name);
1261 smbconf_delete_global_parameter(ctx, "realm");
1262 goto done;
1265 werr = smbconf_set_global_parameter(ctx, "security", "domain");
1266 W_ERROR_NOT_OK_GOTO_DONE(werr);
1268 werr = smbconf_set_global_parameter(ctx, "workgroup",
1269 r->out.netbios_domain_name);
1270 W_ERROR_NOT_OK_GOTO_DONE(werr);
1272 if (r->out.domain_is_ad) {
1273 werr = smbconf_set_global_parameter(ctx, "security", "ads");
1274 W_ERROR_NOT_OK_GOTO_DONE(werr);
1276 werr = smbconf_set_global_parameter(ctx, "realm",
1277 r->out.dns_domain_name);
1278 W_ERROR_NOT_OK_GOTO_DONE(werr);
1281 done:
1282 smbconf_shutdown(ctx);
1283 return werr;
1286 /****************************************************************
1287 ****************************************************************/
1289 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1291 WERROR werr = WERR_OK;
1292 struct smbconf_ctx *ctx;
1294 werr = smbconf_init_reg(r, &ctx, NULL);
1295 if (!W_ERROR_IS_OK(werr)) {
1296 goto done;
1299 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1301 werr = smbconf_set_global_parameter(ctx, "security", "user");
1302 W_ERROR_NOT_OK_GOTO_DONE(werr);
1304 werr = smbconf_delete_global_parameter(ctx, "workgroup");
1305 W_ERROR_NOT_OK_GOTO_DONE(werr);
1307 smbconf_delete_global_parameter(ctx, "realm");
1310 done:
1311 smbconf_shutdown(ctx);
1312 return werr;
1315 /****************************************************************
1316 ****************************************************************/
1318 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1320 WERROR werr;
1322 if (!W_ERROR_IS_OK(r->out.result)) {
1323 return r->out.result;
1326 if (!r->in.modify_config) {
1327 return WERR_OK;
1330 werr = do_join_modify_vals_config(r);
1331 if (!W_ERROR_IS_OK(werr)) {
1332 return werr;
1335 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1337 r->out.modified_config = true;
1338 r->out.result = werr;
1340 return werr;
1343 /****************************************************************
1344 ****************************************************************/
1346 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1348 WERROR werr;
1350 if (!W_ERROR_IS_OK(r->out.result)) {
1351 return r->out.result;
1354 if (!r->in.modify_config) {
1355 return WERR_OK;
1358 werr = do_unjoin_modify_vals_config(r);
1359 if (!W_ERROR_IS_OK(werr)) {
1360 return werr;
1363 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1365 r->out.modified_config = true;
1366 r->out.result = werr;
1368 return werr;
1371 /****************************************************************
1372 ****************************************************************/
1374 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1375 const char *domain_str,
1376 const char **domain_p,
1377 const char **dc_p)
1379 char *domain = NULL;
1380 char *dc = NULL;
1381 const char *p = NULL;
1383 if (!domain_str || !domain_p || !dc_p) {
1384 return false;
1387 p = strchr_m(domain_str, '\\');
1389 if (p != NULL) {
1390 domain = talloc_strndup(mem_ctx, domain_str,
1391 PTR_DIFF(p, domain_str));
1392 dc = talloc_strdup(mem_ctx, p+1);
1393 if (!dc) {
1394 return false;
1396 } else {
1397 domain = talloc_strdup(mem_ctx, domain_str);
1398 dc = NULL;
1400 if (!domain) {
1401 return false;
1404 *domain_p = domain;
1406 if (!*dc_p && dc) {
1407 *dc_p = dc;
1410 return true;
1413 /****************************************************************
1414 ****************************************************************/
1416 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1417 struct libnet_JoinCtx *r)
1419 if (!r->in.domain_name) {
1420 libnet_join_set_error_string(mem_ctx, r,
1421 "No domain name defined");
1422 return WERR_INVALID_PARAM;
1425 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1426 &r->in.domain_name,
1427 &r->in.dc_name)) {
1428 libnet_join_set_error_string(mem_ctx, r,
1429 "Failed to parse domain name");
1430 return WERR_INVALID_PARAM;
1433 if (IS_DC) {
1434 return WERR_SETUP_DOMAIN_CONTROLLER;
1437 if (!secrets_init()) {
1438 libnet_join_set_error_string(mem_ctx, r,
1439 "Unable to open secrets database");
1440 return WERR_CAN_NOT_COMPLETE;
1443 return WERR_OK;
1446 /****************************************************************
1447 ****************************************************************/
1449 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1450 struct libnet_JoinCtx *r)
1452 WERROR werr;
1454 if (!W_ERROR_IS_OK(r->out.result)) {
1455 return r->out.result;
1458 werr = do_JoinConfig(r);
1459 if (!W_ERROR_IS_OK(werr)) {
1460 return werr;
1463 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1464 saf_store(r->in.domain_name, r->in.dc_name);
1467 return WERR_OK;
1470 /****************************************************************
1471 ****************************************************************/
1473 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1475 const char *krb5_cc_env = NULL;
1477 if (r->in.ads) {
1478 ads_destroy(&r->in.ads);
1481 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1482 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1483 unsetenv(KRB5_ENV_CCNAME);
1486 return 0;
1489 /****************************************************************
1490 ****************************************************************/
1492 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1494 const char *krb5_cc_env = NULL;
1496 if (r->in.ads) {
1497 ads_destroy(&r->in.ads);
1500 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1501 if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1502 unsetenv(KRB5_ENV_CCNAME);
1505 return 0;
1508 /****************************************************************
1509 ****************************************************************/
1511 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1512 struct libnet_JoinCtx **r)
1514 struct libnet_JoinCtx *ctx;
1515 const char *krb5_cc_env = NULL;
1517 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1518 if (!ctx) {
1519 return WERR_NOMEM;
1522 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1524 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1525 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1527 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1528 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1529 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1530 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1531 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1534 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1536 *r = ctx;
1538 return WERR_OK;
1541 /****************************************************************
1542 ****************************************************************/
1544 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1545 struct libnet_UnjoinCtx **r)
1547 struct libnet_UnjoinCtx *ctx;
1548 const char *krb5_cc_env = NULL;
1550 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1551 if (!ctx) {
1552 return WERR_NOMEM;
1555 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1557 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1558 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1560 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1561 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1562 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1563 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1564 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1567 *r = ctx;
1569 return WERR_OK;
1572 /****************************************************************
1573 ****************************************************************/
1575 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1576 struct libnet_JoinCtx *r)
1578 /* check if configuration is already set correctly */
1580 switch (r->out.domain_is_ad) {
1581 case false:
1582 if ((strequal(lp_workgroup(),
1583 r->out.netbios_domain_name)) &&
1584 (lp_security() == SEC_DOMAIN)) {
1585 /* nothing to be done */
1586 return WERR_OK;
1588 break;
1589 case true:
1590 if ((strequal(lp_workgroup(),
1591 r->out.netbios_domain_name)) &&
1592 (strequal(lp_realm(),
1593 r->out.dns_domain_name)) &&
1594 ((lp_security() == SEC_ADS) ||
1595 (lp_security() == SEC_DOMAIN))) {
1596 /* nothing to be done */
1597 return WERR_OK;
1599 break;
1602 /* check if we are supposed to manipulate configuration */
1604 if (!r->in.modify_config) {
1605 libnet_join_set_error_string(mem_ctx, r,
1606 "Invalid configuration and configuration modification "
1607 "was not requested");
1608 return WERR_CAN_NOT_COMPLETE;
1611 /* check if we are able to manipulate configuration */
1613 if (!lp_config_backend_is_registry()) {
1614 libnet_join_set_error_string(mem_ctx, r,
1615 "Configuration manipulation requested but not "
1616 "supported by backend");
1617 return WERR_NOT_SUPPORTED;
1620 return WERR_OK;
1623 /****************************************************************
1624 ****************************************************************/
1626 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1627 struct libnet_JoinCtx *r)
1629 NTSTATUS status;
1630 WERROR werr;
1631 struct cli_state *cli = NULL;
1632 #ifdef WITH_ADS
1633 ADS_STATUS ads_status;
1634 #endif /* WITH_ADS */
1636 if (!r->in.dc_name) {
1637 struct netr_DsRGetDCNameInfo *info;
1638 const char *dc;
1639 status = dsgetdcname(mem_ctx,
1640 r->in.msg_ctx,
1641 r->in.domain_name,
1642 NULL,
1643 NULL,
1644 DS_DIRECTORY_SERVICE_REQUIRED |
1645 DS_WRITABLE_REQUIRED |
1646 DS_RETURN_DNS_NAME,
1647 &info);
1648 if (!NT_STATUS_IS_OK(status)) {
1649 libnet_join_set_error_string(mem_ctx, r,
1650 "failed to find DC for domain %s",
1651 r->in.domain_name,
1652 get_friendly_nt_error_msg(status));
1653 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1656 dc = strip_hostname(info->dc_unc);
1657 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1658 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1661 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1662 if (!NT_STATUS_IS_OK(status)) {
1663 libnet_join_set_error_string(mem_ctx, r,
1664 "failed to lookup DC info for domain '%s' over rpc: %s",
1665 r->in.domain_name, get_friendly_nt_error_msg(status));
1666 return ntstatus_to_werror(status);
1669 werr = libnet_join_check_config(mem_ctx, r);
1670 if (!W_ERROR_IS_OK(werr)) {
1671 goto done;
1674 #ifdef WITH_ADS
1675 if (r->out.domain_is_ad && r->in.account_ou) {
1677 ads_status = libnet_join_connect_ads(mem_ctx, r);
1678 if (!ADS_ERR_OK(ads_status)) {
1679 return WERR_DEFAULT_JOIN_REQUIRED;
1682 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1683 if (!ADS_ERR_OK(ads_status)) {
1684 libnet_join_set_error_string(mem_ctx, r,
1685 "failed to precreate account in ou %s: %s",
1686 r->in.account_ou,
1687 ads_errstr(ads_status));
1688 return WERR_DEFAULT_JOIN_REQUIRED;
1691 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1693 #endif /* WITH_ADS */
1695 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
1696 if (!NT_STATUS_IS_OK(status)) {
1697 libnet_join_set_error_string(mem_ctx, r,
1698 "failed to join domain '%s' over rpc: %s",
1699 r->in.domain_name, get_friendly_nt_error_msg(status));
1700 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1701 return WERR_SETUP_ALREADY_JOINED;
1703 werr = ntstatus_to_werror(status);
1704 goto done;
1707 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1708 werr = WERR_SETUP_NOT_JOINED;
1709 goto done;
1712 #ifdef WITH_ADS
1713 if (r->out.domain_is_ad) {
1714 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1715 if (!ADS_ERR_OK(ads_status)) {
1716 werr = WERR_GENERAL_FAILURE;
1717 goto done;
1720 #endif /* WITH_ADS */
1722 werr = WERR_OK;
1724 done:
1725 if (cli) {
1726 cli_shutdown(cli);
1729 return werr;
1732 /****************************************************************
1733 ****************************************************************/
1735 WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
1736 struct libnet_JoinCtx *r)
1738 WERROR werr;
1739 struct libnet_UnjoinCtx *u = NULL;
1741 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
1742 if (!W_ERROR_IS_OK(werr)) {
1743 return werr;
1746 u->in.debug = r->in.debug;
1747 u->in.dc_name = r->in.dc_name;
1748 u->in.domain_name = r->in.domain_name;
1749 u->in.admin_account = r->in.admin_account;
1750 u->in.admin_password = r->in.admin_password;
1751 u->in.modify_config = r->in.modify_config;
1752 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
1753 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
1755 werr = libnet_Unjoin(mem_ctx, u);
1756 TALLOC_FREE(u);
1758 return werr;
1761 /****************************************************************
1762 ****************************************************************/
1764 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1765 struct libnet_JoinCtx *r)
1767 WERROR werr;
1769 if (r->in.debug) {
1770 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1773 werr = libnet_join_pre_processing(mem_ctx, r);
1774 if (!W_ERROR_IS_OK(werr)) {
1775 goto done;
1778 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1779 werr = libnet_DomainJoin(mem_ctx, r);
1780 if (!W_ERROR_IS_OK(werr)) {
1781 goto done;
1785 werr = libnet_join_post_processing(mem_ctx, r);
1786 if (!W_ERROR_IS_OK(werr)) {
1787 goto done;
1790 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1791 werr = libnet_join_post_verify(mem_ctx, r);
1792 if (!W_ERROR_IS_OK(werr)) {
1793 libnet_join_rollback(mem_ctx, r);
1797 done:
1798 r->out.result = werr;
1800 if (r->in.debug) {
1801 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1803 return werr;
1806 /****************************************************************
1807 ****************************************************************/
1809 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1810 struct libnet_UnjoinCtx *r)
1812 NTSTATUS status;
1814 if (!r->in.domain_sid) {
1815 struct dom_sid sid;
1816 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1817 libnet_unjoin_set_error_string(mem_ctx, r,
1818 "Unable to fetch domain sid: are we joined?");
1819 return WERR_SETUP_NOT_JOINED;
1821 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
1822 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
1825 if (!r->in.dc_name) {
1826 struct netr_DsRGetDCNameInfo *info;
1827 const char *dc;
1828 status = dsgetdcname(mem_ctx,
1829 r->in.msg_ctx,
1830 r->in.domain_name,
1831 NULL,
1832 NULL,
1833 DS_DIRECTORY_SERVICE_REQUIRED |
1834 DS_WRITABLE_REQUIRED |
1835 DS_RETURN_DNS_NAME,
1836 &info);
1837 if (!NT_STATUS_IS_OK(status)) {
1838 libnet_unjoin_set_error_string(mem_ctx, r,
1839 "failed to find DC for domain %s",
1840 r->in.domain_name,
1841 get_friendly_nt_error_msg(status));
1842 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1845 dc = strip_hostname(info->dc_unc);
1846 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1847 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1850 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
1851 if (!NT_STATUS_IS_OK(status)) {
1852 libnet_unjoin_set_error_string(mem_ctx, r,
1853 "failed to disable machine account via rpc: %s",
1854 get_friendly_nt_error_msg(status));
1855 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1856 return WERR_SETUP_NOT_JOINED;
1858 return ntstatus_to_werror(status);
1861 r->out.disabled_machine_account = true;
1863 #ifdef WITH_ADS
1864 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
1865 ADS_STATUS ads_status;
1866 libnet_unjoin_connect_ads(mem_ctx, r);
1867 ads_status = libnet_unjoin_remove_machine_acct(mem_ctx, r);
1868 if (!ADS_ERR_OK(ads_status)) {
1869 libnet_unjoin_set_error_string(mem_ctx, r,
1870 "failed to remove machine account from AD: %s",
1871 ads_errstr(ads_status));
1872 } else {
1873 r->out.deleted_machine_account = true;
1874 /* dirty hack */
1875 r->out.dns_domain_name = talloc_strdup(mem_ctx,
1876 r->in.ads->server.realm);
1877 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
1880 #endif /* WITH_ADS */
1882 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
1884 return WERR_OK;
1887 /****************************************************************
1888 ****************************************************************/
1890 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
1891 struct libnet_UnjoinCtx *r)
1893 if (!r->in.domain_name) {
1894 libnet_unjoin_set_error_string(mem_ctx, r,
1895 "No domain name defined");
1896 return WERR_INVALID_PARAM;
1899 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1900 &r->in.domain_name,
1901 &r->in.dc_name)) {
1902 libnet_unjoin_set_error_string(mem_ctx, r,
1903 "Failed to parse domain name");
1904 return WERR_INVALID_PARAM;
1907 if (IS_DC) {
1908 return WERR_SETUP_DOMAIN_CONTROLLER;
1911 if (!secrets_init()) {
1912 libnet_unjoin_set_error_string(mem_ctx, r,
1913 "Unable to open secrets database");
1914 return WERR_CAN_NOT_COMPLETE;
1917 return WERR_OK;
1920 /****************************************************************
1921 ****************************************************************/
1923 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
1924 struct libnet_UnjoinCtx *r)
1926 saf_delete(r->out.netbios_domain_name);
1927 saf_delete(r->out.dns_domain_name);
1929 return libnet_unjoin_config(r);
1932 /****************************************************************
1933 ****************************************************************/
1935 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
1936 struct libnet_UnjoinCtx *r)
1938 WERROR werr;
1940 if (r->in.debug) {
1941 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
1944 werr = libnet_unjoin_pre_processing(mem_ctx, r);
1945 if (!W_ERROR_IS_OK(werr)) {
1946 goto done;
1949 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1950 werr = libnet_DomainUnjoin(mem_ctx, r);
1951 if (!W_ERROR_IS_OK(werr)) {
1952 libnet_unjoin_config(r);
1953 goto done;
1957 werr = libnet_unjoin_post_processing(mem_ctx, r);
1958 if (!W_ERROR_IS_OK(werr)) {
1959 goto done;
1962 done:
1963 r->out.result = werr;
1965 if (r->in.debug) {
1966 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
1969 return werr;