s4-scripting: samba-tool: Fix domain info usage message
[Samba/vl.git] / source3 / libnet / libnet_join.c
blob5a36d3422d536d7338041c0f13aa3e61de4ea465
1 /*
2 * Unix SMB/CIFS implementation.
3 * libnet Join Support
4 * Copyright (C) Gerald (Jerry) Carter 2006
5 * Copyright (C) Guenther Deschner 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "ads.h"
23 #include "librpc/gen_ndr/ndr_libnet_join.h"
24 #include "libnet/libnet_join.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_samr_c.h"
27 #include "rpc_client/init_samr.h"
28 #include "../librpc/gen_ndr/ndr_lsa_c.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../librpc/gen_ndr/ndr_netlogon.h"
31 #include "rpc_client/cli_netlogon.h"
32 #include "lib/smbconf/smbconf.h"
33 #include "lib/smbconf/smbconf_reg.h"
34 #include "../libds/common/flags.h"
35 #include "secrets.h"
36 #include "rpc_client/init_lsa.h"
37 #include "rpc_client/cli_pipe.h"
38 #include "../libcli/security/security.h"
39 #include "passdb.h"
40 #include "libsmb/libsmb.h"
42 /****************************************************************
43 ****************************************************************/
45 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
46 do { \
47 char *str = NULL; \
48 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
49 DEBUG(1,("libnet_Join:\n%s", str)); \
50 TALLOC_FREE(str); \
51 } while (0)
53 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
54 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
55 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
56 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
58 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
59 do { \
60 char *str = NULL; \
61 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
62 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
63 TALLOC_FREE(str); \
64 } while (0)
66 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
67 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
68 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
69 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
71 /****************************************************************
72 ****************************************************************/
74 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
75 struct libnet_JoinCtx *r,
76 const char *format, ...)
78 va_list args;
80 if (r->out.error_string) {
81 return;
84 va_start(args, format);
85 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
86 va_end(args);
89 /****************************************************************
90 ****************************************************************/
92 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
93 struct libnet_UnjoinCtx *r,
94 const char *format, ...)
96 va_list args;
98 if (r->out.error_string) {
99 return;
102 va_start(args, format);
103 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
104 va_end(args);
107 #ifdef HAVE_ADS
109 /****************************************************************
110 ****************************************************************/
112 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
113 const char *netbios_domain_name,
114 const char *dc_name,
115 const char *user_name,
116 const char *password,
117 ADS_STRUCT **ads)
119 ADS_STATUS status;
120 ADS_STRUCT *my_ads = NULL;
121 char *cp;
123 my_ads = ads_init(dns_domain_name,
124 netbios_domain_name,
125 dc_name);
126 if (!my_ads) {
127 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
130 if (user_name) {
131 SAFE_FREE(my_ads->auth.user_name);
132 my_ads->auth.user_name = SMB_STRDUP(user_name);
133 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
134 *cp++ = '\0';
135 SAFE_FREE(my_ads->auth.realm);
136 my_ads->auth.realm = smb_xstrdup(cp);
137 strupper_m(my_ads->auth.realm);
141 if (password) {
142 SAFE_FREE(my_ads->auth.password);
143 my_ads->auth.password = SMB_STRDUP(password);
146 status = ads_connect_user_creds(my_ads);
147 if (!ADS_ERR_OK(status)) {
148 ads_destroy(&my_ads);
149 return status;
152 *ads = my_ads;
153 return ADS_SUCCESS;
156 /****************************************************************
157 ****************************************************************/
159 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
160 struct libnet_JoinCtx *r)
162 ADS_STATUS status;
164 status = libnet_connect_ads(r->out.dns_domain_name,
165 r->out.netbios_domain_name,
166 r->in.dc_name,
167 r->in.admin_account,
168 r->in.admin_password,
169 &r->in.ads);
170 if (!ADS_ERR_OK(status)) {
171 libnet_join_set_error_string(mem_ctx, r,
172 "failed to connect to AD: %s",
173 ads_errstr(status));
174 return status;
177 if (!r->out.netbios_domain_name) {
178 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
179 r->in.ads->server.workgroup);
180 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
183 if (!r->out.dns_domain_name) {
184 r->out.dns_domain_name = talloc_strdup(mem_ctx,
185 r->in.ads->config.realm);
186 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
189 r->out.domain_is_ad = true;
191 return ADS_SUCCESS;
194 /****************************************************************
195 ****************************************************************/
197 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
198 struct libnet_UnjoinCtx *r)
200 ADS_STATUS status;
202 status = libnet_connect_ads(r->in.domain_name,
203 r->in.domain_name,
204 r->in.dc_name,
205 r->in.admin_account,
206 r->in.admin_password,
207 &r->in.ads);
208 if (!ADS_ERR_OK(status)) {
209 libnet_unjoin_set_error_string(mem_ctx, r,
210 "failed to connect to AD: %s",
211 ads_errstr(status));
214 return status;
217 /****************************************************************
218 join a domain using ADS (LDAP mods)
219 ****************************************************************/
221 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
222 struct libnet_JoinCtx *r)
224 ADS_STATUS status;
225 LDAPMessage *res = NULL;
226 const char *attrs[] = { "dn", NULL };
227 bool moved = false;
229 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
230 if (!ADS_ERR_OK(status)) {
231 return status;
234 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
235 if (!ADS_ERR_OK(status)) {
236 return status;
239 if (ads_count_replies(r->in.ads, res) != 1) {
240 ads_msgfree(r->in.ads, res);
241 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
244 ads_msgfree(r->in.ads, res);
246 /* Attempt to create the machine account and bail if this fails.
247 Assume that the admin wants exactly what they requested */
249 status = ads_create_machine_acct(r->in.ads,
250 r->in.machine_name,
251 r->in.account_ou);
253 if (ADS_ERR_OK(status)) {
254 DEBUG(1,("machine account creation created\n"));
255 return status;
256 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
257 (status.err.rc == LDAP_ALREADY_EXISTS)) {
258 status = ADS_SUCCESS;
261 if (!ADS_ERR_OK(status)) {
262 DEBUG(1,("machine account creation failed\n"));
263 return status;
266 status = ads_move_machine_acct(r->in.ads,
267 r->in.machine_name,
268 r->in.account_ou,
269 &moved);
270 if (!ADS_ERR_OK(status)) {
271 DEBUG(1,("failure to locate/move pre-existing "
272 "machine account\n"));
273 return status;
276 DEBUG(1,("The machine account %s the specified OU.\n",
277 moved ? "was moved into" : "already exists in"));
279 return status;
282 /****************************************************************
283 ****************************************************************/
285 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
286 struct libnet_UnjoinCtx *r)
288 ADS_STATUS status;
290 if (!r->in.ads) {
291 status = libnet_unjoin_connect_ads(mem_ctx, r);
292 if (!ADS_ERR_OK(status)) {
293 libnet_unjoin_set_error_string(mem_ctx, r,
294 "failed to connect to AD: %s",
295 ads_errstr(status));
296 return status;
300 status = ads_leave_realm(r->in.ads, r->in.machine_name);
301 if (!ADS_ERR_OK(status)) {
302 libnet_unjoin_set_error_string(mem_ctx, r,
303 "failed to leave realm: %s",
304 ads_errstr(status));
305 return status;
308 return ADS_SUCCESS;
311 /****************************************************************
312 ****************************************************************/
314 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
315 struct libnet_JoinCtx *r)
317 ADS_STATUS status;
318 LDAPMessage *res = NULL;
319 char *dn = NULL;
321 if (!r->in.machine_name) {
322 return ADS_ERROR(LDAP_NO_MEMORY);
325 status = ads_find_machine_acct(r->in.ads,
326 &res,
327 r->in.machine_name);
328 if (!ADS_ERR_OK(status)) {
329 return status;
332 if (ads_count_replies(r->in.ads, res) != 1) {
333 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
334 goto done;
337 dn = ads_get_dn(r->in.ads, mem_ctx, res);
338 if (!dn) {
339 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
340 goto done;
343 r->out.dn = talloc_strdup(mem_ctx, dn);
344 if (!r->out.dn) {
345 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
346 goto done;
349 done:
350 ads_msgfree(r->in.ads, res);
351 TALLOC_FREE(dn);
353 return status;
356 /****************************************************************
357 Set a machines dNSHostName and servicePrincipalName attributes
358 ****************************************************************/
360 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
361 struct libnet_JoinCtx *r)
363 ADS_STATUS status;
364 ADS_MODLIST mods;
365 fstring my_fqdn;
366 const char *spn_array[3] = {NULL, NULL, NULL};
367 char *spn = NULL;
369 /* Find our DN */
371 status = libnet_join_find_machine_acct(mem_ctx, r);
372 if (!ADS_ERR_OK(status)) {
373 return status;
376 /* Windows only creates HOST/shortname & HOST/fqdn. */
378 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
379 if (!spn) {
380 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
382 strupper_m(spn);
383 spn_array[0] = spn;
385 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
386 || (strchr(my_fqdn, '.') == NULL)) {
387 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
388 r->out.dns_domain_name);
391 strlower_m(my_fqdn);
393 if (!strequal(my_fqdn, r->in.machine_name)) {
394 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
395 if (!spn) {
396 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
398 spn_array[1] = spn;
401 mods = ads_init_mods(mem_ctx);
402 if (!mods) {
403 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
406 /* fields of primary importance */
408 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
409 if (!ADS_ERR_OK(status)) {
410 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
413 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
414 spn_array);
415 if (!ADS_ERR_OK(status)) {
416 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
419 return ads_gen_mod(r->in.ads, r->out.dn, mods);
422 /****************************************************************
423 ****************************************************************/
425 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
426 struct libnet_JoinCtx *r)
428 ADS_STATUS status;
429 ADS_MODLIST mods;
431 if (!r->in.create_upn) {
432 return ADS_SUCCESS;
435 /* Find our DN */
437 status = libnet_join_find_machine_acct(mem_ctx, r);
438 if (!ADS_ERR_OK(status)) {
439 return status;
442 if (!r->in.upn) {
443 r->in.upn = talloc_asprintf(mem_ctx,
444 "host/%s@%s",
445 r->in.machine_name,
446 r->out.dns_domain_name);
447 if (!r->in.upn) {
448 return ADS_ERROR(LDAP_NO_MEMORY);
452 /* now do the mods */
454 mods = ads_init_mods(mem_ctx);
455 if (!mods) {
456 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
459 /* fields of primary importance */
461 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
462 if (!ADS_ERR_OK(status)) {
463 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
466 return ads_gen_mod(r->in.ads, r->out.dn, mods);
470 /****************************************************************
471 ****************************************************************/
473 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
474 struct libnet_JoinCtx *r)
476 ADS_STATUS status;
477 ADS_MODLIST mods;
478 char *os_sp = NULL;
480 if (!r->in.os_name || !r->in.os_version ) {
481 return ADS_SUCCESS;
484 /* Find our DN */
486 status = libnet_join_find_machine_acct(mem_ctx, r);
487 if (!ADS_ERR_OK(status)) {
488 return status;
491 /* now do the mods */
493 mods = ads_init_mods(mem_ctx);
494 if (!mods) {
495 return ADS_ERROR(LDAP_NO_MEMORY);
498 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
499 if (!os_sp) {
500 return ADS_ERROR(LDAP_NO_MEMORY);
503 /* fields of primary importance */
505 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
506 r->in.os_name);
507 if (!ADS_ERR_OK(status)) {
508 return status;
511 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
512 r->in.os_version);
513 if (!ADS_ERR_OK(status)) {
514 return status;
517 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
518 os_sp);
519 if (!ADS_ERR_OK(status)) {
520 return status;
523 return ads_gen_mod(r->in.ads, r->out.dn, mods);
526 /****************************************************************
527 ****************************************************************/
529 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
530 struct libnet_JoinCtx *r)
532 if (!USE_SYSTEM_KEYTAB) {
533 return true;
536 if (ads_keytab_create_default(r->in.ads) != 0) {
537 return false;
540 return true;
543 /****************************************************************
544 ****************************************************************/
546 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
547 struct libnet_JoinCtx *r)
549 uint32_t domain_func;
550 ADS_STATUS status;
551 const char *salt = NULL;
552 char *std_salt = NULL;
554 status = ads_domain_func_level(r->in.ads, &domain_func);
555 if (!ADS_ERR_OK(status)) {
556 libnet_join_set_error_string(mem_ctx, r,
557 "failed to determine domain functional level: %s",
558 ads_errstr(status));
559 return false;
562 /* go ahead and setup the default salt */
564 std_salt = kerberos_standard_des_salt();
565 if (!std_salt) {
566 libnet_join_set_error_string(mem_ctx, r,
567 "failed to obtain standard DES salt");
568 return false;
571 salt = talloc_strdup(mem_ctx, std_salt);
572 if (!salt) {
573 return false;
576 SAFE_FREE(std_salt);
578 /* if it's a Windows functional domain, we have to look for the UPN */
580 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
581 char *upn;
583 upn = ads_get_upn(r->in.ads, mem_ctx,
584 r->in.machine_name);
585 if (upn) {
586 salt = talloc_strdup(mem_ctx, upn);
587 if (!salt) {
588 return false;
593 return kerberos_secrets_store_des_salt(salt);
596 /****************************************************************
597 ****************************************************************/
599 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
600 struct libnet_JoinCtx *r)
602 ADS_STATUS status;
604 if (!r->in.ads) {
605 status = libnet_join_connect_ads(mem_ctx, r);
606 if (!ADS_ERR_OK(status)) {
607 return status;
611 status = libnet_join_set_machine_spn(mem_ctx, r);
612 if (!ADS_ERR_OK(status)) {
613 libnet_join_set_error_string(mem_ctx, r,
614 "failed to set machine spn: %s",
615 ads_errstr(status));
616 return status;
619 status = libnet_join_set_os_attributes(mem_ctx, r);
620 if (!ADS_ERR_OK(status)) {
621 libnet_join_set_error_string(mem_ctx, r,
622 "failed to set machine os attributes: %s",
623 ads_errstr(status));
624 return status;
627 status = libnet_join_set_machine_upn(mem_ctx, r);
628 if (!ADS_ERR_OK(status)) {
629 libnet_join_set_error_string(mem_ctx, r,
630 "failed to set machine upn: %s",
631 ads_errstr(status));
632 return status;
635 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
636 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
639 if (!libnet_join_create_keytab(mem_ctx, r)) {
640 libnet_join_set_error_string(mem_ctx, r,
641 "failed to create kerberos keytab");
642 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
645 return ADS_SUCCESS;
647 #endif /* HAVE_ADS */
649 /****************************************************************
650 Store the machine password and domain SID
651 ****************************************************************/
653 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
654 struct libnet_JoinCtx *r)
656 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
657 r->out.domain_sid))
659 DEBUG(1,("Failed to save domain sid\n"));
660 return false;
663 if (!secrets_store_machine_password(r->in.machine_password,
664 r->out.netbios_domain_name,
665 r->in.secure_channel_type))
667 DEBUG(1,("Failed to save machine password\n"));
668 return false;
671 return true;
674 /****************************************************************
675 Connect dc's IPC$ share
676 ****************************************************************/
678 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
679 const char *user,
680 const char *pass,
681 bool use_kerberos,
682 struct cli_state **cli)
684 int flags = 0;
686 if (use_kerberos) {
687 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
690 if (use_kerberos && pass) {
691 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
694 return cli_full_connection(cli, NULL,
696 NULL, 0,
697 "IPC$", "IPC",
698 user,
699 NULL,
700 pass,
701 flags,
702 SMB_SIGNING_DEFAULT);
705 /****************************************************************
706 Lookup domain dc's info
707 ****************************************************************/
709 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
710 struct libnet_JoinCtx *r,
711 struct cli_state **cli)
713 struct rpc_pipe_client *pipe_hnd = NULL;
714 struct policy_handle lsa_pol;
715 NTSTATUS status, result;
716 union lsa_PolicyInformation *info = NULL;
717 struct dcerpc_binding_handle *b;
719 status = libnet_join_connect_dc_ipc(r->in.dc_name,
720 r->in.admin_account,
721 r->in.admin_password,
722 r->in.use_kerberos,
723 cli);
724 if (!NT_STATUS_IS_OK(status)) {
725 goto done;
728 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
729 &pipe_hnd);
730 if (!NT_STATUS_IS_OK(status)) {
731 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
732 nt_errstr(status)));
733 goto done;
736 b = pipe_hnd->binding_handle;
738 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
739 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
740 if (!NT_STATUS_IS_OK(status)) {
741 goto done;
744 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
745 &lsa_pol,
746 LSA_POLICY_INFO_DNS,
747 &info,
748 &result);
749 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
750 r->out.domain_is_ad = true;
751 r->out.netbios_domain_name = info->dns.name.string;
752 r->out.dns_domain_name = info->dns.dns_domain.string;
753 r->out.forest_name = info->dns.dns_forest.string;
754 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
755 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
758 if (!NT_STATUS_IS_OK(status)) {
759 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
760 &lsa_pol,
761 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
762 &info,
763 &result);
764 if (!NT_STATUS_IS_OK(status)) {
765 goto done;
767 if (!NT_STATUS_IS_OK(result)) {
768 status = result;
769 goto done;
772 r->out.netbios_domain_name = info->account_domain.name.string;
773 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
774 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
777 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
778 TALLOC_FREE(pipe_hnd);
780 done:
781 return status;
784 /****************************************************************
785 Do the domain join unsecure
786 ****************************************************************/
788 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
789 struct libnet_JoinCtx *r,
790 struct cli_state *cli)
792 struct rpc_pipe_client *pipe_hnd = NULL;
793 unsigned char orig_trust_passwd_hash[16];
794 unsigned char new_trust_passwd_hash[16];
795 fstring trust_passwd;
796 NTSTATUS status;
798 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
799 &pipe_hnd);
800 if (!NT_STATUS_IS_OK(status)) {
801 return status;
804 if (!r->in.machine_password) {
805 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
806 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
809 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
811 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
812 fstrcpy(trust_passwd, r->in.admin_password);
813 strlower_m(trust_passwd);
816 * Machine names can be 15 characters, but the max length on
817 * a password is 14. --jerry
820 trust_passwd[14] = '\0';
822 E_md4hash(trust_passwd, orig_trust_passwd_hash);
824 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
825 r->in.machine_name,
826 orig_trust_passwd_hash,
827 r->in.machine_password,
828 new_trust_passwd_hash,
829 r->in.secure_channel_type);
831 return status;
834 /****************************************************************
835 Do the domain join
836 ****************************************************************/
838 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
839 struct libnet_JoinCtx *r,
840 struct cli_state *cli)
842 struct rpc_pipe_client *pipe_hnd = NULL;
843 struct policy_handle sam_pol, domain_pol, user_pol;
844 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
845 char *acct_name;
846 struct lsa_String lsa_acct_name;
847 uint32_t user_rid;
848 uint32_t acct_flags = ACB_WSTRUST;
849 struct samr_Ids user_rids;
850 struct samr_Ids name_types;
851 union samr_UserInfo user_info;
852 struct dcerpc_binding_handle *b = NULL;
854 struct samr_CryptPassword crypt_pwd;
855 struct samr_CryptPasswordEx crypt_pwd_ex;
857 ZERO_STRUCT(sam_pol);
858 ZERO_STRUCT(domain_pol);
859 ZERO_STRUCT(user_pol);
861 switch (r->in.secure_channel_type) {
862 case SEC_CHAN_WKSTA:
863 acct_flags = ACB_WSTRUST;
864 break;
865 case SEC_CHAN_BDC:
866 acct_flags = ACB_SVRTRUST;
867 break;
868 default:
869 return NT_STATUS_INVALID_PARAMETER;
872 if (!r->in.machine_password) {
873 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
874 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
877 /* Open the domain */
879 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
880 &pipe_hnd);
881 if (!NT_STATUS_IS_OK(status)) {
882 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
883 nt_errstr(status)));
884 goto done;
887 b = pipe_hnd->binding_handle;
889 status = dcerpc_samr_Connect2(b, mem_ctx,
890 pipe_hnd->desthost,
891 SAMR_ACCESS_ENUM_DOMAINS
892 | SAMR_ACCESS_LOOKUP_DOMAIN,
893 &sam_pol,
894 &result);
895 if (!NT_STATUS_IS_OK(status)) {
896 goto done;
898 if (!NT_STATUS_IS_OK(result)) {
899 status = result;
900 goto done;
903 status = dcerpc_samr_OpenDomain(b, mem_ctx,
904 &sam_pol,
905 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
906 | SAMR_DOMAIN_ACCESS_CREATE_USER
907 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
908 r->out.domain_sid,
909 &domain_pol,
910 &result);
911 if (!NT_STATUS_IS_OK(status)) {
912 goto done;
914 if (!NT_STATUS_IS_OK(result)) {
915 status = result;
916 goto done;
919 /* Create domain user */
921 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
922 strlower_m(acct_name);
924 init_lsa_String(&lsa_acct_name, acct_name);
926 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
927 uint32_t access_desired =
928 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
929 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
930 SAMR_USER_ACCESS_SET_PASSWORD |
931 SAMR_USER_ACCESS_GET_ATTRIBUTES |
932 SAMR_USER_ACCESS_SET_ATTRIBUTES;
933 uint32_t access_granted = 0;
935 DEBUG(10,("Creating account with desired access mask: %d\n",
936 access_desired));
938 status = dcerpc_samr_CreateUser2(b, mem_ctx,
939 &domain_pol,
940 &lsa_acct_name,
941 acct_flags,
942 access_desired,
943 &user_pol,
944 &access_granted,
945 &user_rid,
946 &result);
947 if (!NT_STATUS_IS_OK(status)) {
948 goto done;
951 status = result;
952 if (!NT_STATUS_IS_OK(status) &&
953 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
955 DEBUG(10,("Creation of workstation account failed: %s\n",
956 nt_errstr(status)));
958 /* If NT_STATUS_ACCESS_DENIED then we have a valid
959 username/password combo but the user does not have
960 administrator access. */
962 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
963 libnet_join_set_error_string(mem_ctx, r,
964 "User specified does not have "
965 "administrator privileges");
968 goto done;
971 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
972 if (!(r->in.join_flags &
973 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
974 goto done;
978 /* We *must* do this.... don't ask... */
980 if (NT_STATUS_IS_OK(status)) {
981 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
985 status = dcerpc_samr_LookupNames(b, mem_ctx,
986 &domain_pol,
988 &lsa_acct_name,
989 &user_rids,
990 &name_types,
991 &result);
992 if (!NT_STATUS_IS_OK(status)) {
993 goto done;
995 if (!NT_STATUS_IS_OK(result)) {
996 status = result;
997 goto done;
1000 if (name_types.ids[0] != SID_NAME_USER) {
1001 DEBUG(0,("%s is not a user account (type=%d)\n",
1002 acct_name, name_types.ids[0]));
1003 status = NT_STATUS_INVALID_WORKSTATION;
1004 goto done;
1007 user_rid = user_rids.ids[0];
1009 /* Open handle on user */
1011 status = dcerpc_samr_OpenUser(b, mem_ctx,
1012 &domain_pol,
1013 SEC_FLAG_MAXIMUM_ALLOWED,
1014 user_rid,
1015 &user_pol,
1016 &result);
1017 if (!NT_STATUS_IS_OK(status)) {
1018 goto done;
1020 if (!NT_STATUS_IS_OK(result)) {
1021 status = result;
1022 goto done;
1025 /* Fill in the additional account flags now */
1027 acct_flags |= ACB_PWNOEXP;
1029 /* Set account flags on machine account */
1030 ZERO_STRUCT(user_info.info16);
1031 user_info.info16.acct_flags = acct_flags;
1033 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1034 &user_pol,
1036 &user_info,
1037 &result);
1038 if (!NT_STATUS_IS_OK(status)) {
1039 dcerpc_samr_DeleteUser(b, mem_ctx,
1040 &user_pol,
1041 &result);
1043 libnet_join_set_error_string(mem_ctx, r,
1044 "Failed to set account flags for machine account (%s)\n",
1045 nt_errstr(status));
1046 goto done;
1049 if (!NT_STATUS_IS_OK(result)) {
1050 status = result;
1052 dcerpc_samr_DeleteUser(b, mem_ctx,
1053 &user_pol,
1054 &result);
1056 libnet_join_set_error_string(mem_ctx, r,
1057 "Failed to set account flags for machine account (%s)\n",
1058 nt_errstr(status));
1059 goto done;
1062 /* Set password on machine account - first try level 26 */
1064 init_samr_CryptPasswordEx(r->in.machine_password,
1065 &cli->user_session_key,
1066 &crypt_pwd_ex);
1068 user_info.info26.password = crypt_pwd_ex;
1069 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1071 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1072 &user_pol,
1074 &user_info,
1075 &result);
1077 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1079 /* retry with level 24 */
1081 init_samr_CryptPassword(r->in.machine_password,
1082 &cli->user_session_key,
1083 &crypt_pwd);
1085 user_info.info24.password = crypt_pwd;
1086 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1088 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1089 &user_pol,
1091 &user_info,
1092 &result);
1095 if (!NT_STATUS_IS_OK(status)) {
1097 dcerpc_samr_DeleteUser(b, mem_ctx,
1098 &user_pol,
1099 &result);
1101 libnet_join_set_error_string(mem_ctx, r,
1102 "Failed to set password for machine account (%s)\n",
1103 nt_errstr(status));
1104 goto done;
1106 if (!NT_STATUS_IS_OK(result)) {
1107 status = result;
1109 dcerpc_samr_DeleteUser(b, mem_ctx,
1110 &user_pol,
1111 &result);
1113 libnet_join_set_error_string(mem_ctx, r,
1114 "Failed to set password for machine account (%s)\n",
1115 nt_errstr(status));
1116 goto done;
1119 status = NT_STATUS_OK;
1121 done:
1122 if (!pipe_hnd) {
1123 return status;
1126 if (is_valid_policy_hnd(&sam_pol)) {
1127 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1129 if (is_valid_policy_hnd(&domain_pol)) {
1130 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1132 if (is_valid_policy_hnd(&user_pol)) {
1133 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1135 TALLOC_FREE(pipe_hnd);
1137 return status;
1140 /****************************************************************
1141 ****************************************************************/
1143 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1144 const char *machine_name,
1145 const char *dc_name)
1147 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1148 struct cli_state *cli = NULL;
1149 struct rpc_pipe_client *pipe_hnd = NULL;
1150 struct rpc_pipe_client *netlogon_pipe = NULL;
1151 NTSTATUS status;
1152 char *machine_password = NULL;
1153 char *machine_account = NULL;
1155 if (!dc_name) {
1156 return NT_STATUS_INVALID_PARAMETER;
1159 if (!secrets_init()) {
1160 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1163 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1164 NULL, NULL);
1165 if (!machine_password) {
1166 return NT_STATUS_NO_TRUST_LSA_SECRET;
1169 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1170 SAFE_FREE(machine_password);
1171 return NT_STATUS_NO_MEMORY;
1174 status = cli_full_connection(&cli, NULL,
1175 dc_name,
1176 NULL, 0,
1177 "IPC$", "IPC",
1178 machine_account,
1179 NULL,
1180 machine_password,
1182 SMB_SIGNING_DEFAULT);
1183 free(machine_account);
1184 free(machine_password);
1186 if (!NT_STATUS_IS_OK(status)) {
1187 status = cli_full_connection(&cli, NULL,
1188 dc_name,
1189 NULL, 0,
1190 "IPC$", "IPC",
1192 NULL,
1195 SMB_SIGNING_DEFAULT);
1198 if (!NT_STATUS_IS_OK(status)) {
1199 return status;
1202 status = get_schannel_session_key(cli, netbios_domain_name,
1203 &neg_flags, &netlogon_pipe);
1204 if (!NT_STATUS_IS_OK(status)) {
1205 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1206 cli_shutdown(cli);
1207 return NT_STATUS_OK;
1210 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1211 "key from server %s for domain %s. Error was %s\n",
1212 cli_state_remote_name(cli),
1213 netbios_domain_name, nt_errstr(status)));
1214 cli_shutdown(cli);
1215 return status;
1218 if (!lp_client_schannel()) {
1219 cli_shutdown(cli);
1220 return NT_STATUS_OK;
1223 status = cli_rpc_pipe_open_schannel_with_key(
1224 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
1225 DCERPC_AUTH_LEVEL_PRIVACY,
1226 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1228 cli_shutdown(cli);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1232 "on netlogon pipe to server %s for domain %s. "
1233 "Error was %s\n",
1234 cli_state_remote_name(cli),
1235 netbios_domain_name, nt_errstr(status)));
1236 return status;
1239 return NT_STATUS_OK;
1242 /****************************************************************
1243 ****************************************************************/
1245 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1246 struct libnet_JoinCtx *r)
1248 NTSTATUS status;
1250 status = libnet_join_ok(r->out.netbios_domain_name,
1251 r->in.machine_name,
1252 r->in.dc_name);
1253 if (!NT_STATUS_IS_OK(status)) {
1254 libnet_join_set_error_string(mem_ctx, r,
1255 "failed to verify domain membership after joining: %s",
1256 get_friendly_nt_error_msg(status));
1257 return WERR_SETUP_NOT_JOINED;
1260 return WERR_OK;
1263 /****************************************************************
1264 ****************************************************************/
1266 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1267 struct libnet_UnjoinCtx *r)
1269 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1270 return false;
1273 if (!secrets_delete_domain_sid(lp_workgroup())) {
1274 return false;
1277 return true;
1280 /****************************************************************
1281 ****************************************************************/
1283 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1284 struct libnet_UnjoinCtx *r)
1286 struct cli_state *cli = NULL;
1287 struct rpc_pipe_client *pipe_hnd = NULL;
1288 struct policy_handle sam_pol, domain_pol, user_pol;
1289 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1290 char *acct_name;
1291 uint32_t user_rid;
1292 struct lsa_String lsa_acct_name;
1293 struct samr_Ids user_rids;
1294 struct samr_Ids name_types;
1295 union samr_UserInfo *info = NULL;
1296 struct dcerpc_binding_handle *b = NULL;
1298 ZERO_STRUCT(sam_pol);
1299 ZERO_STRUCT(domain_pol);
1300 ZERO_STRUCT(user_pol);
1302 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1303 r->in.admin_account,
1304 r->in.admin_password,
1305 r->in.use_kerberos,
1306 &cli);
1307 if (!NT_STATUS_IS_OK(status)) {
1308 goto done;
1311 /* Open the domain */
1313 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1314 &pipe_hnd);
1315 if (!NT_STATUS_IS_OK(status)) {
1316 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1317 nt_errstr(status)));
1318 goto done;
1321 b = pipe_hnd->binding_handle;
1323 status = dcerpc_samr_Connect2(b, mem_ctx,
1324 pipe_hnd->desthost,
1325 SEC_FLAG_MAXIMUM_ALLOWED,
1326 &sam_pol,
1327 &result);
1328 if (!NT_STATUS_IS_OK(status)) {
1329 goto done;
1331 if (!NT_STATUS_IS_OK(result)) {
1332 status = result;
1333 goto done;
1336 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1337 &sam_pol,
1338 SEC_FLAG_MAXIMUM_ALLOWED,
1339 r->in.domain_sid,
1340 &domain_pol,
1341 &result);
1342 if (!NT_STATUS_IS_OK(status)) {
1343 goto done;
1345 if (!NT_STATUS_IS_OK(result)) {
1346 status = result;
1347 goto done;
1350 /* Create domain user */
1352 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1353 strlower_m(acct_name);
1355 init_lsa_String(&lsa_acct_name, acct_name);
1357 status = dcerpc_samr_LookupNames(b, mem_ctx,
1358 &domain_pol,
1360 &lsa_acct_name,
1361 &user_rids,
1362 &name_types,
1363 &result);
1365 if (!NT_STATUS_IS_OK(status)) {
1366 goto done;
1368 if (!NT_STATUS_IS_OK(result)) {
1369 status = result;
1370 goto done;
1373 if (name_types.ids[0] != SID_NAME_USER) {
1374 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1375 name_types.ids[0]));
1376 status = NT_STATUS_INVALID_WORKSTATION;
1377 goto done;
1380 user_rid = user_rids.ids[0];
1382 /* Open handle on user */
1384 status = dcerpc_samr_OpenUser(b, mem_ctx,
1385 &domain_pol,
1386 SEC_FLAG_MAXIMUM_ALLOWED,
1387 user_rid,
1388 &user_pol,
1389 &result);
1390 if (!NT_STATUS_IS_OK(status)) {
1391 goto done;
1393 if (!NT_STATUS_IS_OK(result)) {
1394 status = result;
1395 goto done;
1398 /* Get user info */
1400 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1401 &user_pol,
1403 &info,
1404 &result);
1405 if (!NT_STATUS_IS_OK(status)) {
1406 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1407 goto done;
1409 if (!NT_STATUS_IS_OK(result)) {
1410 status = result;
1411 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1412 goto done;
1415 /* now disable and setuser info */
1417 info->info16.acct_flags |= ACB_DISABLED;
1419 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1420 &user_pol,
1422 info,
1423 &result);
1424 if (!NT_STATUS_IS_OK(status)) {
1425 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1426 goto done;
1428 if (!NT_STATUS_IS_OK(result)) {
1429 status = result;
1430 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1431 goto done;
1433 status = result;
1434 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1436 done:
1437 if (pipe_hnd && b) {
1438 if (is_valid_policy_hnd(&domain_pol)) {
1439 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1441 if (is_valid_policy_hnd(&sam_pol)) {
1442 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1444 TALLOC_FREE(pipe_hnd);
1447 if (cli) {
1448 cli_shutdown(cli);
1451 return status;
1454 /****************************************************************
1455 ****************************************************************/
1457 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1459 WERROR werr = WERR_OK;
1460 sbcErr err;
1461 struct smbconf_ctx *ctx;
1463 err = smbconf_init_reg(r, &ctx, NULL);
1464 if (!SBC_ERROR_IS_OK(err)) {
1465 werr = WERR_NO_SUCH_SERVICE;
1466 goto done;
1469 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1471 err = smbconf_set_global_parameter(ctx, "security", "user");
1472 if (!SBC_ERROR_IS_OK(err)) {
1473 werr = WERR_NO_SUCH_SERVICE;
1474 goto done;
1477 err = smbconf_set_global_parameter(ctx, "workgroup",
1478 r->in.domain_name);
1479 if (!SBC_ERROR_IS_OK(err)) {
1480 werr = WERR_NO_SUCH_SERVICE;
1481 goto done;
1484 smbconf_delete_global_parameter(ctx, "realm");
1485 goto done;
1488 err = smbconf_set_global_parameter(ctx, "security", "domain");
1489 if (!SBC_ERROR_IS_OK(err)) {
1490 werr = WERR_NO_SUCH_SERVICE;
1491 goto done;
1494 err = smbconf_set_global_parameter(ctx, "workgroup",
1495 r->out.netbios_domain_name);
1496 if (!SBC_ERROR_IS_OK(err)) {
1497 werr = WERR_NO_SUCH_SERVICE;
1498 goto done;
1501 if (r->out.domain_is_ad) {
1502 err = smbconf_set_global_parameter(ctx, "security", "ads");
1503 if (!SBC_ERROR_IS_OK(err)) {
1504 werr = WERR_NO_SUCH_SERVICE;
1505 goto done;
1508 err = smbconf_set_global_parameter(ctx, "realm",
1509 r->out.dns_domain_name);
1510 if (!SBC_ERROR_IS_OK(err)) {
1511 werr = WERR_NO_SUCH_SERVICE;
1512 goto done;
1516 done:
1517 smbconf_shutdown(ctx);
1518 return werr;
1521 /****************************************************************
1522 ****************************************************************/
1524 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1526 WERROR werr = WERR_OK;
1527 sbcErr err;
1528 struct smbconf_ctx *ctx;
1530 err = smbconf_init_reg(r, &ctx, NULL);
1531 if (!SBC_ERROR_IS_OK(err)) {
1532 werr = WERR_NO_SUCH_SERVICE;
1533 goto done;
1536 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1538 err = smbconf_set_global_parameter(ctx, "security", "user");
1539 if (!SBC_ERROR_IS_OK(err)) {
1540 werr = WERR_NO_SUCH_SERVICE;
1541 goto done;
1544 err = smbconf_delete_global_parameter(ctx, "workgroup");
1545 if (!SBC_ERROR_IS_OK(err)) {
1546 werr = WERR_NO_SUCH_SERVICE;
1547 goto done;
1550 smbconf_delete_global_parameter(ctx, "realm");
1553 done:
1554 smbconf_shutdown(ctx);
1555 return werr;
1558 /****************************************************************
1559 ****************************************************************/
1561 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1563 WERROR werr;
1565 if (!W_ERROR_IS_OK(r->out.result)) {
1566 return r->out.result;
1569 if (!r->in.modify_config) {
1570 return WERR_OK;
1573 werr = do_join_modify_vals_config(r);
1574 if (!W_ERROR_IS_OK(werr)) {
1575 return werr;
1578 lp_load_global(get_dyn_CONFIGFILE());
1580 r->out.modified_config = true;
1581 r->out.result = werr;
1583 return werr;
1586 /****************************************************************
1587 ****************************************************************/
1589 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1591 WERROR werr;
1593 if (!W_ERROR_IS_OK(r->out.result)) {
1594 return r->out.result;
1597 if (!r->in.modify_config) {
1598 return WERR_OK;
1601 werr = do_unjoin_modify_vals_config(r);
1602 if (!W_ERROR_IS_OK(werr)) {
1603 return werr;
1606 lp_load_global(get_dyn_CONFIGFILE());
1608 r->out.modified_config = true;
1609 r->out.result = werr;
1611 return werr;
1614 /****************************************************************
1615 ****************************************************************/
1617 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1618 const char *domain_str,
1619 const char **domain_p,
1620 const char **dc_p)
1622 char *domain = NULL;
1623 char *dc = NULL;
1624 const char *p = NULL;
1626 if (!domain_str || !domain_p || !dc_p) {
1627 return false;
1630 p = strchr_m(domain_str, '\\');
1632 if (p != NULL) {
1633 domain = talloc_strndup(mem_ctx, domain_str,
1634 PTR_DIFF(p, domain_str));
1635 dc = talloc_strdup(mem_ctx, p+1);
1636 if (!dc) {
1637 return false;
1639 } else {
1640 domain = talloc_strdup(mem_ctx, domain_str);
1641 dc = NULL;
1643 if (!domain) {
1644 return false;
1647 *domain_p = domain;
1649 if (!*dc_p && dc) {
1650 *dc_p = dc;
1653 return true;
1656 /****************************************************************
1657 ****************************************************************/
1659 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1660 struct libnet_JoinCtx *r)
1662 if (!r->in.domain_name) {
1663 libnet_join_set_error_string(mem_ctx, r,
1664 "No domain name defined");
1665 return WERR_INVALID_PARAM;
1668 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1669 &r->in.domain_name,
1670 &r->in.dc_name)) {
1671 libnet_join_set_error_string(mem_ctx, r,
1672 "Failed to parse domain name");
1673 return WERR_INVALID_PARAM;
1676 if (IS_DC) {
1677 return WERR_SETUP_DOMAIN_CONTROLLER;
1680 if (!secrets_init()) {
1681 libnet_join_set_error_string(mem_ctx, r,
1682 "Unable to open secrets database");
1683 return WERR_CAN_NOT_COMPLETE;
1686 return WERR_OK;
1689 /****************************************************************
1690 ****************************************************************/
1692 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1694 NTSTATUS status;
1696 /* Try adding dom admins to builtin\admins. Only log failures. */
1697 status = create_builtin_administrators(domain_sid);
1698 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1699 DEBUG(10,("Unable to auto-add domain administrators to "
1700 "BUILTIN\\Administrators during join because "
1701 "winbindd must be running.\n"));
1702 } else if (!NT_STATUS_IS_OK(status)) {
1703 DEBUG(5, ("Failed to auto-add domain administrators to "
1704 "BUILTIN\\Administrators during join: %s\n",
1705 nt_errstr(status)));
1708 /* Try adding dom users to builtin\users. Only log failures. */
1709 status = create_builtin_users(domain_sid);
1710 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1711 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1712 "during join because winbindd must be running.\n"));
1713 } else if (!NT_STATUS_IS_OK(status)) {
1714 DEBUG(5, ("Failed to auto-add domain administrators to "
1715 "BUILTIN\\Administrators during join: %s\n",
1716 nt_errstr(status)));
1720 /****************************************************************
1721 ****************************************************************/
1723 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1724 struct libnet_JoinCtx *r)
1726 WERROR werr;
1728 if (!W_ERROR_IS_OK(r->out.result)) {
1729 return r->out.result;
1732 werr = do_JoinConfig(r);
1733 if (!W_ERROR_IS_OK(werr)) {
1734 return werr;
1737 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1738 return WERR_OK;
1741 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1742 if (r->out.dns_domain_name) {
1743 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1746 #ifdef HAVE_ADS
1747 if (r->out.domain_is_ad &&
1748 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1749 ADS_STATUS ads_status;
1751 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1752 if (!ADS_ERR_OK(ads_status)) {
1753 return WERR_GENERAL_FAILURE;
1756 #endif /* HAVE_ADS */
1758 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1760 return WERR_OK;
1763 /****************************************************************
1764 ****************************************************************/
1766 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1768 if (r->in.ads) {
1769 ads_destroy(&r->in.ads);
1772 return 0;
1775 /****************************************************************
1776 ****************************************************************/
1778 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1780 if (r->in.ads) {
1781 ads_destroy(&r->in.ads);
1784 return 0;
1787 /****************************************************************
1788 ****************************************************************/
1790 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1791 struct libnet_JoinCtx **r)
1793 struct libnet_JoinCtx *ctx;
1795 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1796 if (!ctx) {
1797 return WERR_NOMEM;
1800 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1802 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1803 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1805 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1807 *r = ctx;
1809 return WERR_OK;
1812 /****************************************************************
1813 ****************************************************************/
1815 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1816 struct libnet_UnjoinCtx **r)
1818 struct libnet_UnjoinCtx *ctx;
1820 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1821 if (!ctx) {
1822 return WERR_NOMEM;
1825 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1827 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1828 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1830 *r = ctx;
1832 return WERR_OK;
1835 /****************************************************************
1836 ****************************************************************/
1838 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1839 struct libnet_JoinCtx *r)
1841 bool valid_security = false;
1842 bool valid_workgroup = false;
1843 bool valid_realm = false;
1845 /* check if configuration is already set correctly */
1847 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1849 switch (r->out.domain_is_ad) {
1850 case false:
1851 valid_security = (lp_security() == SEC_DOMAIN);
1852 if (valid_workgroup && valid_security) {
1853 /* nothing to be done */
1854 return WERR_OK;
1856 break;
1857 case true:
1858 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1859 switch (lp_security()) {
1860 case SEC_DOMAIN:
1861 case SEC_ADS:
1862 valid_security = true;
1865 if (valid_workgroup && valid_realm && valid_security) {
1866 /* nothing to be done */
1867 return WERR_OK;
1869 break;
1872 /* check if we are supposed to manipulate configuration */
1874 if (!r->in.modify_config) {
1876 char *wrong_conf = talloc_strdup(mem_ctx, "");
1878 if (!valid_workgroup) {
1879 wrong_conf = talloc_asprintf_append(wrong_conf,
1880 "\"workgroup\" set to '%s', should be '%s'",
1881 lp_workgroup(), r->out.netbios_domain_name);
1882 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1885 if (!valid_realm) {
1886 wrong_conf = talloc_asprintf_append(wrong_conf,
1887 "\"realm\" set to '%s', should be '%s'",
1888 lp_realm(), r->out.dns_domain_name);
1889 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1892 if (!valid_security) {
1893 const char *sec = NULL;
1894 switch (lp_security()) {
1895 case SEC_SHARE: sec = "share"; break;
1896 case SEC_USER: sec = "user"; break;
1897 case SEC_DOMAIN: sec = "domain"; break;
1898 case SEC_ADS: sec = "ads"; break;
1900 wrong_conf = talloc_asprintf_append(wrong_conf,
1901 "\"security\" set to '%s', should be %s",
1902 sec, r->out.domain_is_ad ?
1903 "either 'domain' or 'ads'" : "'domain'");
1904 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1907 libnet_join_set_error_string(mem_ctx, r,
1908 "Invalid configuration (%s) and configuration modification "
1909 "was not requested", wrong_conf);
1910 return WERR_CAN_NOT_COMPLETE;
1913 /* check if we are able to manipulate configuration */
1915 if (!lp_config_backend_is_registry()) {
1916 libnet_join_set_error_string(mem_ctx, r,
1917 "Configuration manipulation requested but not "
1918 "supported by backend");
1919 return WERR_NOT_SUPPORTED;
1922 return WERR_OK;
1925 /****************************************************************
1926 ****************************************************************/
1928 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1929 struct libnet_JoinCtx *r)
1931 NTSTATUS status;
1932 WERROR werr;
1933 struct cli_state *cli = NULL;
1934 #ifdef HAVE_ADS
1935 ADS_STATUS ads_status;
1936 #endif /* HAVE_ADS */
1938 if (!r->in.dc_name) {
1939 struct netr_DsRGetDCNameInfo *info;
1940 const char *dc;
1941 status = dsgetdcname(mem_ctx,
1942 r->in.msg_ctx,
1943 r->in.domain_name,
1944 NULL,
1945 NULL,
1946 DS_FORCE_REDISCOVERY |
1947 DS_DIRECTORY_SERVICE_REQUIRED |
1948 DS_WRITABLE_REQUIRED |
1949 DS_RETURN_DNS_NAME,
1950 &info);
1951 if (!NT_STATUS_IS_OK(status)) {
1952 libnet_join_set_error_string(mem_ctx, r,
1953 "failed to find DC for domain %s",
1954 r->in.domain_name,
1955 get_friendly_nt_error_msg(status));
1956 return WERR_DCNOTFOUND;
1959 dc = strip_hostname(info->dc_unc);
1960 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1961 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1964 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1965 if (!NT_STATUS_IS_OK(status)) {
1966 libnet_join_set_error_string(mem_ctx, r,
1967 "failed to lookup DC info for domain '%s' over rpc: %s",
1968 r->in.domain_name, get_friendly_nt_error_msg(status));
1969 return ntstatus_to_werror(status);
1972 werr = libnet_join_check_config(mem_ctx, r);
1973 if (!W_ERROR_IS_OK(werr)) {
1974 goto done;
1977 #ifdef HAVE_ADS
1979 create_local_private_krb5_conf_for_domain(
1980 r->out.dns_domain_name, r->out.netbios_domain_name,
1981 NULL, cli_state_remote_sockaddr(cli),
1982 cli_state_remote_name(cli));
1984 if (r->out.domain_is_ad && r->in.account_ou &&
1985 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1987 ads_status = libnet_join_connect_ads(mem_ctx, r);
1988 if (!ADS_ERR_OK(ads_status)) {
1989 return WERR_DEFAULT_JOIN_REQUIRED;
1992 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1993 if (!ADS_ERR_OK(ads_status)) {
1994 libnet_join_set_error_string(mem_ctx, r,
1995 "failed to precreate account in ou %s: %s",
1996 r->in.account_ou,
1997 ads_errstr(ads_status));
1998 return WERR_DEFAULT_JOIN_REQUIRED;
2001 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2003 #endif /* HAVE_ADS */
2005 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2006 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2007 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2008 } else {
2009 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2011 if (!NT_STATUS_IS_OK(status)) {
2012 libnet_join_set_error_string(mem_ctx, r,
2013 "failed to join domain '%s' over rpc: %s",
2014 r->in.domain_name, get_friendly_nt_error_msg(status));
2015 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2016 return WERR_SETUP_ALREADY_JOINED;
2018 werr = ntstatus_to_werror(status);
2019 goto done;
2022 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2023 werr = WERR_SETUP_NOT_JOINED;
2024 goto done;
2027 werr = WERR_OK;
2029 done:
2030 if (cli) {
2031 cli_shutdown(cli);
2034 return werr;
2037 /****************************************************************
2038 ****************************************************************/
2040 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2041 struct libnet_JoinCtx *r)
2043 WERROR werr;
2044 struct libnet_UnjoinCtx *u = NULL;
2046 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2047 if (!W_ERROR_IS_OK(werr)) {
2048 return werr;
2051 u->in.debug = r->in.debug;
2052 u->in.dc_name = r->in.dc_name;
2053 u->in.domain_name = r->in.domain_name;
2054 u->in.admin_account = r->in.admin_account;
2055 u->in.admin_password = r->in.admin_password;
2056 u->in.modify_config = r->in.modify_config;
2057 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2058 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2060 werr = libnet_Unjoin(mem_ctx, u);
2061 TALLOC_FREE(u);
2063 return werr;
2066 /****************************************************************
2067 ****************************************************************/
2069 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2070 struct libnet_JoinCtx *r)
2072 WERROR werr;
2074 if (r->in.debug) {
2075 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2078 ZERO_STRUCT(r->out);
2080 werr = libnet_join_pre_processing(mem_ctx, r);
2081 if (!W_ERROR_IS_OK(werr)) {
2082 goto done;
2085 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2086 werr = libnet_DomainJoin(mem_ctx, r);
2087 if (!W_ERROR_IS_OK(werr)) {
2088 goto done;
2092 werr = libnet_join_post_processing(mem_ctx, r);
2093 if (!W_ERROR_IS_OK(werr)) {
2094 goto done;
2097 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2098 werr = libnet_join_post_verify(mem_ctx, r);
2099 if (!W_ERROR_IS_OK(werr)) {
2100 libnet_join_rollback(mem_ctx, r);
2104 done:
2105 r->out.result = werr;
2107 if (r->in.debug) {
2108 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2110 return werr;
2113 /****************************************************************
2114 ****************************************************************/
2116 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2117 struct libnet_UnjoinCtx *r)
2119 NTSTATUS status;
2121 if (!r->in.domain_sid) {
2122 struct dom_sid sid;
2123 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2124 libnet_unjoin_set_error_string(mem_ctx, r,
2125 "Unable to fetch domain sid: are we joined?");
2126 return WERR_SETUP_NOT_JOINED;
2128 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2129 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2132 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2133 !r->in.delete_machine_account) {
2134 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2135 return WERR_OK;
2138 if (!r->in.dc_name) {
2139 struct netr_DsRGetDCNameInfo *info;
2140 const char *dc;
2141 status = dsgetdcname(mem_ctx,
2142 r->in.msg_ctx,
2143 r->in.domain_name,
2144 NULL,
2145 NULL,
2146 DS_DIRECTORY_SERVICE_REQUIRED |
2147 DS_WRITABLE_REQUIRED |
2148 DS_RETURN_DNS_NAME,
2149 &info);
2150 if (!NT_STATUS_IS_OK(status)) {
2151 libnet_unjoin_set_error_string(mem_ctx, r,
2152 "failed to find DC for domain %s",
2153 r->in.domain_name,
2154 get_friendly_nt_error_msg(status));
2155 return WERR_DCNOTFOUND;
2158 dc = strip_hostname(info->dc_unc);
2159 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2160 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2163 #ifdef HAVE_ADS
2164 /* for net ads leave, try to delete the account. If it works,
2165 no sense in disabling. If it fails, we can still try to
2166 disable it. jmcd */
2168 if (r->in.delete_machine_account) {
2169 ADS_STATUS ads_status;
2170 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2171 if (ADS_ERR_OK(ads_status)) {
2172 /* dirty hack */
2173 r->out.dns_domain_name =
2174 talloc_strdup(mem_ctx,
2175 r->in.ads->server.realm);
2176 ads_status =
2177 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2179 if (!ADS_ERR_OK(ads_status)) {
2180 libnet_unjoin_set_error_string(mem_ctx, r,
2181 "failed to remove machine account from AD: %s",
2182 ads_errstr(ads_status));
2183 } else {
2184 r->out.deleted_machine_account = true;
2185 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2186 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2187 return WERR_OK;
2190 #endif /* HAVE_ADS */
2192 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2193 "disable". */
2194 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2195 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2196 if (!NT_STATUS_IS_OK(status)) {
2197 libnet_unjoin_set_error_string(mem_ctx, r,
2198 "failed to disable machine account via rpc: %s",
2199 get_friendly_nt_error_msg(status));
2200 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2201 return WERR_SETUP_NOT_JOINED;
2203 return ntstatus_to_werror(status);
2206 r->out.disabled_machine_account = true;
2209 /* If disable succeeded or was not requested at all, we
2210 should be getting rid of our end of things */
2212 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2214 return WERR_OK;
2217 /****************************************************************
2218 ****************************************************************/
2220 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2221 struct libnet_UnjoinCtx *r)
2223 if (!r->in.domain_name) {
2224 libnet_unjoin_set_error_string(mem_ctx, r,
2225 "No domain name defined");
2226 return WERR_INVALID_PARAM;
2229 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2230 &r->in.domain_name,
2231 &r->in.dc_name)) {
2232 libnet_unjoin_set_error_string(mem_ctx, r,
2233 "Failed to parse domain name");
2234 return WERR_INVALID_PARAM;
2237 if (IS_DC) {
2238 return WERR_SETUP_DOMAIN_CONTROLLER;
2241 if (!secrets_init()) {
2242 libnet_unjoin_set_error_string(mem_ctx, r,
2243 "Unable to open secrets database");
2244 return WERR_CAN_NOT_COMPLETE;
2247 return WERR_OK;
2250 /****************************************************************
2251 ****************************************************************/
2253 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2254 struct libnet_UnjoinCtx *r)
2256 saf_delete(r->out.netbios_domain_name);
2257 saf_delete(r->out.dns_domain_name);
2259 return libnet_unjoin_config(r);
2262 /****************************************************************
2263 ****************************************************************/
2265 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2266 struct libnet_UnjoinCtx *r)
2268 WERROR werr;
2270 if (r->in.debug) {
2271 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2274 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2275 if (!W_ERROR_IS_OK(werr)) {
2276 goto done;
2279 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2280 werr = libnet_DomainUnjoin(mem_ctx, r);
2281 if (!W_ERROR_IS_OK(werr)) {
2282 libnet_unjoin_config(r);
2283 goto done;
2287 werr = libnet_unjoin_post_processing(mem_ctx, r);
2288 if (!W_ERROR_IS_OK(werr)) {
2289 goto done;
2292 done:
2293 r->out.result = werr;
2295 if (r->in.debug) {
2296 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2299 return werr;