s3:selftest: introduce a variable for binpath('smbtorture3') in tests.py
[Samba/gebeck_regimport.git] / source3 / libnet / libnet_join.c
blobc549b586cb5a544cf3afad909deddc497b8de3a0
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"
41 #include "../libcli/smb/smbXcli_base.h"
43 /****************************************************************
44 ****************************************************************/
46 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
47 do { \
48 char *str = NULL; \
49 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
50 DEBUG(1,("libnet_Join:\n%s", str)); \
51 TALLOC_FREE(str); \
52 } while (0)
54 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
55 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
56 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
57 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
59 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
60 do { \
61 char *str = NULL; \
62 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
63 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
64 TALLOC_FREE(str); \
65 } while (0)
67 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
68 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
69 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
70 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
72 /****************************************************************
73 ****************************************************************/
75 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
76 struct libnet_JoinCtx *r,
77 const char *format, ...)
79 va_list args;
81 if (r->out.error_string) {
82 return;
85 va_start(args, format);
86 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
87 va_end(args);
90 /****************************************************************
91 ****************************************************************/
93 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
94 struct libnet_UnjoinCtx *r,
95 const char *format, ...)
97 va_list args;
99 if (r->out.error_string) {
100 return;
103 va_start(args, format);
104 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
105 va_end(args);
108 #ifdef HAVE_ADS
110 /****************************************************************
111 ****************************************************************/
113 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
114 const char *netbios_domain_name,
115 const char *dc_name,
116 const char *user_name,
117 const char *password,
118 ADS_STRUCT **ads)
120 ADS_STATUS status;
121 ADS_STRUCT *my_ads = NULL;
122 char *cp;
124 my_ads = ads_init(dns_domain_name,
125 netbios_domain_name,
126 dc_name);
127 if (!my_ads) {
128 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
131 if (user_name) {
132 SAFE_FREE(my_ads->auth.user_name);
133 my_ads->auth.user_name = SMB_STRDUP(user_name);
134 if ((cp = strchr_m(my_ads->auth.user_name, '@'))!=0) {
135 *cp++ = '\0';
136 SAFE_FREE(my_ads->auth.realm);
137 my_ads->auth.realm = smb_xstrdup(cp);
138 strupper_m(my_ads->auth.realm);
142 if (password) {
143 SAFE_FREE(my_ads->auth.password);
144 my_ads->auth.password = SMB_STRDUP(password);
147 status = ads_connect_user_creds(my_ads);
148 if (!ADS_ERR_OK(status)) {
149 ads_destroy(&my_ads);
150 return status;
153 *ads = my_ads;
154 return ADS_SUCCESS;
157 /****************************************************************
158 ****************************************************************/
160 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
161 struct libnet_JoinCtx *r)
163 ADS_STATUS status;
165 status = libnet_connect_ads(r->out.dns_domain_name,
166 r->out.netbios_domain_name,
167 r->in.dc_name,
168 r->in.admin_account,
169 r->in.admin_password,
170 &r->in.ads);
171 if (!ADS_ERR_OK(status)) {
172 libnet_join_set_error_string(mem_ctx, r,
173 "failed to connect to AD: %s",
174 ads_errstr(status));
175 return status;
178 if (!r->out.netbios_domain_name) {
179 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
180 r->in.ads->server.workgroup);
181 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
184 if (!r->out.dns_domain_name) {
185 r->out.dns_domain_name = talloc_strdup(mem_ctx,
186 r->in.ads->config.realm);
187 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
190 r->out.domain_is_ad = true;
192 return ADS_SUCCESS;
195 /****************************************************************
196 ****************************************************************/
198 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
199 struct libnet_UnjoinCtx *r)
201 ADS_STATUS status;
203 status = libnet_connect_ads(r->in.domain_name,
204 r->in.domain_name,
205 r->in.dc_name,
206 r->in.admin_account,
207 r->in.admin_password,
208 &r->in.ads);
209 if (!ADS_ERR_OK(status)) {
210 libnet_unjoin_set_error_string(mem_ctx, r,
211 "failed to connect to AD: %s",
212 ads_errstr(status));
215 return status;
218 /****************************************************************
219 join a domain using ADS (LDAP mods)
220 ****************************************************************/
222 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
223 struct libnet_JoinCtx *r)
225 ADS_STATUS status;
226 LDAPMessage *res = NULL;
227 const char *attrs[] = { "dn", NULL };
228 bool moved = false;
230 status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
231 if (!ADS_ERR_OK(status)) {
232 return status;
235 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
236 if (!ADS_ERR_OK(status)) {
237 return status;
240 if (ads_count_replies(r->in.ads, res) != 1) {
241 ads_msgfree(r->in.ads, res);
242 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
245 ads_msgfree(r->in.ads, res);
247 /* Attempt to create the machine account and bail if this fails.
248 Assume that the admin wants exactly what they requested */
250 status = ads_create_machine_acct(r->in.ads,
251 r->in.machine_name,
252 r->in.account_ou);
254 if (ADS_ERR_OK(status)) {
255 DEBUG(1,("machine account creation created\n"));
256 return status;
257 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
258 (status.err.rc == LDAP_ALREADY_EXISTS)) {
259 status = ADS_SUCCESS;
262 if (!ADS_ERR_OK(status)) {
263 DEBUG(1,("machine account creation failed\n"));
264 return status;
267 status = ads_move_machine_acct(r->in.ads,
268 r->in.machine_name,
269 r->in.account_ou,
270 &moved);
271 if (!ADS_ERR_OK(status)) {
272 DEBUG(1,("failure to locate/move pre-existing "
273 "machine account\n"));
274 return status;
277 DEBUG(1,("The machine account %s the specified OU.\n",
278 moved ? "was moved into" : "already exists in"));
280 return status;
283 /****************************************************************
284 ****************************************************************/
286 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
287 struct libnet_UnjoinCtx *r)
289 ADS_STATUS status;
291 if (!r->in.ads) {
292 status = libnet_unjoin_connect_ads(mem_ctx, r);
293 if (!ADS_ERR_OK(status)) {
294 libnet_unjoin_set_error_string(mem_ctx, r,
295 "failed to connect to AD: %s",
296 ads_errstr(status));
297 return status;
301 status = ads_leave_realm(r->in.ads, r->in.machine_name);
302 if (!ADS_ERR_OK(status)) {
303 libnet_unjoin_set_error_string(mem_ctx, r,
304 "failed to leave realm: %s",
305 ads_errstr(status));
306 return status;
309 return ADS_SUCCESS;
312 /****************************************************************
313 ****************************************************************/
315 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
316 struct libnet_JoinCtx *r)
318 ADS_STATUS status;
319 LDAPMessage *res = NULL;
320 char *dn = NULL;
322 if (!r->in.machine_name) {
323 return ADS_ERROR(LDAP_NO_MEMORY);
326 status = ads_find_machine_acct(r->in.ads,
327 &res,
328 r->in.machine_name);
329 if (!ADS_ERR_OK(status)) {
330 return status;
333 if (ads_count_replies(r->in.ads, res) != 1) {
334 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
335 goto done;
338 dn = ads_get_dn(r->in.ads, mem_ctx, res);
339 if (!dn) {
340 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
341 goto done;
344 r->out.dn = talloc_strdup(mem_ctx, dn);
345 if (!r->out.dn) {
346 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
347 goto done;
350 done:
351 ads_msgfree(r->in.ads, res);
352 TALLOC_FREE(dn);
354 return status;
357 /****************************************************************
358 Set a machines dNSHostName and servicePrincipalName attributes
359 ****************************************************************/
361 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
362 struct libnet_JoinCtx *r)
364 ADS_STATUS status;
365 ADS_MODLIST mods;
366 fstring my_fqdn;
367 const char *spn_array[3] = {NULL, NULL, NULL};
368 char *spn = NULL;
370 /* Find our DN */
372 status = libnet_join_find_machine_acct(mem_ctx, r);
373 if (!ADS_ERR_OK(status)) {
374 return status;
377 /* Windows only creates HOST/shortname & HOST/fqdn. */
379 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
380 if (!spn) {
381 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
383 strupper_m(spn);
384 spn_array[0] = spn;
386 if (!name_to_fqdn(my_fqdn, r->in.machine_name)
387 || (strchr(my_fqdn, '.') == NULL)) {
388 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
389 r->out.dns_domain_name);
392 strlower_m(my_fqdn);
394 if (!strequal(my_fqdn, r->in.machine_name)) {
395 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
396 if (!spn) {
397 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
399 spn_array[1] = spn;
402 mods = ads_init_mods(mem_ctx);
403 if (!mods) {
404 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
407 /* fields of primary importance */
409 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
410 if (!ADS_ERR_OK(status)) {
411 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
414 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
415 spn_array);
416 if (!ADS_ERR_OK(status)) {
417 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
420 return ads_gen_mod(r->in.ads, r->out.dn, mods);
423 /****************************************************************
424 ****************************************************************/
426 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
427 struct libnet_JoinCtx *r)
429 ADS_STATUS status;
430 ADS_MODLIST mods;
432 if (!r->in.create_upn) {
433 return ADS_SUCCESS;
436 /* Find our DN */
438 status = libnet_join_find_machine_acct(mem_ctx, r);
439 if (!ADS_ERR_OK(status)) {
440 return status;
443 if (!r->in.upn) {
444 r->in.upn = talloc_asprintf(mem_ctx,
445 "host/%s@%s",
446 r->in.machine_name,
447 r->out.dns_domain_name);
448 if (!r->in.upn) {
449 return ADS_ERROR(LDAP_NO_MEMORY);
453 /* now do the mods */
455 mods = ads_init_mods(mem_ctx);
456 if (!mods) {
457 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
460 /* fields of primary importance */
462 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
463 if (!ADS_ERR_OK(status)) {
464 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
467 return ads_gen_mod(r->in.ads, r->out.dn, mods);
471 /****************************************************************
472 ****************************************************************/
474 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
475 struct libnet_JoinCtx *r)
477 ADS_STATUS status;
478 ADS_MODLIST mods;
479 char *os_sp = NULL;
481 if (!r->in.os_name || !r->in.os_version ) {
482 return ADS_SUCCESS;
485 /* Find our DN */
487 status = libnet_join_find_machine_acct(mem_ctx, r);
488 if (!ADS_ERR_OK(status)) {
489 return status;
492 /* now do the mods */
494 mods = ads_init_mods(mem_ctx);
495 if (!mods) {
496 return ADS_ERROR(LDAP_NO_MEMORY);
499 os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
500 if (!os_sp) {
501 return ADS_ERROR(LDAP_NO_MEMORY);
504 /* fields of primary importance */
506 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
507 r->in.os_name);
508 if (!ADS_ERR_OK(status)) {
509 return status;
512 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
513 r->in.os_version);
514 if (!ADS_ERR_OK(status)) {
515 return status;
518 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
519 os_sp);
520 if (!ADS_ERR_OK(status)) {
521 return status;
524 return ads_gen_mod(r->in.ads, r->out.dn, mods);
527 /****************************************************************
528 ****************************************************************/
530 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
531 struct libnet_JoinCtx *r)
533 if (!USE_SYSTEM_KEYTAB) {
534 return true;
537 if (ads_keytab_create_default(r->in.ads) != 0) {
538 return false;
541 return true;
544 /****************************************************************
545 ****************************************************************/
547 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
548 struct libnet_JoinCtx *r)
550 uint32_t domain_func;
551 ADS_STATUS status;
552 const char *salt = NULL;
553 char *std_salt = NULL;
555 status = ads_domain_func_level(r->in.ads, &domain_func);
556 if (!ADS_ERR_OK(status)) {
557 libnet_join_set_error_string(mem_ctx, r,
558 "failed to determine domain functional level: %s",
559 ads_errstr(status));
560 return false;
563 /* go ahead and setup the default salt */
565 std_salt = kerberos_standard_des_salt();
566 if (!std_salt) {
567 libnet_join_set_error_string(mem_ctx, r,
568 "failed to obtain standard DES salt");
569 return false;
572 salt = talloc_strdup(mem_ctx, std_salt);
573 if (!salt) {
574 return false;
577 SAFE_FREE(std_salt);
579 /* if it's a Windows functional domain, we have to look for the UPN */
581 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
582 char *upn;
584 upn = ads_get_upn(r->in.ads, mem_ctx,
585 r->in.machine_name);
586 if (upn) {
587 salt = talloc_strdup(mem_ctx, upn);
588 if (!salt) {
589 return false;
594 return kerberos_secrets_store_des_salt(salt);
597 /****************************************************************
598 ****************************************************************/
600 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
601 struct libnet_JoinCtx *r)
603 ADS_STATUS status;
605 if (!r->in.ads) {
606 status = libnet_join_connect_ads(mem_ctx, r);
607 if (!ADS_ERR_OK(status)) {
608 return status;
612 status = libnet_join_set_machine_spn(mem_ctx, r);
613 if (!ADS_ERR_OK(status)) {
614 libnet_join_set_error_string(mem_ctx, r,
615 "failed to set machine spn: %s",
616 ads_errstr(status));
617 return status;
620 status = libnet_join_set_os_attributes(mem_ctx, r);
621 if (!ADS_ERR_OK(status)) {
622 libnet_join_set_error_string(mem_ctx, r,
623 "failed to set machine os attributes: %s",
624 ads_errstr(status));
625 return status;
628 status = libnet_join_set_machine_upn(mem_ctx, r);
629 if (!ADS_ERR_OK(status)) {
630 libnet_join_set_error_string(mem_ctx, r,
631 "failed to set machine upn: %s",
632 ads_errstr(status));
633 return status;
636 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
637 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
640 if (!libnet_join_create_keytab(mem_ctx, r)) {
641 libnet_join_set_error_string(mem_ctx, r,
642 "failed to create kerberos keytab");
643 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
646 return ADS_SUCCESS;
648 #endif /* HAVE_ADS */
650 /****************************************************************
651 Store the machine password and domain SID
652 ****************************************************************/
654 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
655 struct libnet_JoinCtx *r)
657 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
658 r->out.domain_sid))
660 DEBUG(1,("Failed to save domain sid\n"));
661 return false;
664 if (!secrets_store_machine_password(r->in.machine_password,
665 r->out.netbios_domain_name,
666 r->in.secure_channel_type))
668 DEBUG(1,("Failed to save machine password\n"));
669 return false;
672 return true;
675 /****************************************************************
676 Connect dc's IPC$ share
677 ****************************************************************/
679 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
680 const char *user,
681 const char *pass,
682 bool use_kerberos,
683 struct cli_state **cli)
685 int flags = 0;
687 if (use_kerberos) {
688 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
691 if (use_kerberos && pass) {
692 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
695 return cli_full_connection(cli, NULL,
697 NULL, 0,
698 "IPC$", "IPC",
699 user,
700 NULL,
701 pass,
702 flags,
703 SMB_SIGNING_DEFAULT);
706 /****************************************************************
707 Lookup domain dc's info
708 ****************************************************************/
710 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
711 struct libnet_JoinCtx *r,
712 struct cli_state **cli)
714 struct rpc_pipe_client *pipe_hnd = NULL;
715 struct policy_handle lsa_pol;
716 NTSTATUS status, result;
717 union lsa_PolicyInformation *info = NULL;
718 struct dcerpc_binding_handle *b;
720 status = libnet_join_connect_dc_ipc(r->in.dc_name,
721 r->in.admin_account,
722 r->in.admin_password,
723 r->in.use_kerberos,
724 cli);
725 if (!NT_STATUS_IS_OK(status)) {
726 goto done;
729 status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
730 &pipe_hnd);
731 if (!NT_STATUS_IS_OK(status)) {
732 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
733 nt_errstr(status)));
734 goto done;
737 b = pipe_hnd->binding_handle;
739 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
740 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
741 if (!NT_STATUS_IS_OK(status)) {
742 goto done;
745 status = dcerpc_lsa_QueryInfoPolicy2(b, mem_ctx,
746 &lsa_pol,
747 LSA_POLICY_INFO_DNS,
748 &info,
749 &result);
750 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
751 r->out.domain_is_ad = true;
752 r->out.netbios_domain_name = info->dns.name.string;
753 r->out.dns_domain_name = info->dns.dns_domain.string;
754 r->out.forest_name = info->dns.dns_forest.string;
755 r->out.domain_sid = dom_sid_dup(mem_ctx, info->dns.sid);
756 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
759 if (!NT_STATUS_IS_OK(status)) {
760 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
761 &lsa_pol,
762 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
763 &info,
764 &result);
765 if (!NT_STATUS_IS_OK(status)) {
766 goto done;
768 if (!NT_STATUS_IS_OK(result)) {
769 status = result;
770 goto done;
773 r->out.netbios_domain_name = info->account_domain.name.string;
774 r->out.domain_sid = dom_sid_dup(mem_ctx, info->account_domain.sid);
775 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
778 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
779 TALLOC_FREE(pipe_hnd);
781 done:
782 return status;
785 /****************************************************************
786 Do the domain join unsecure
787 ****************************************************************/
789 static NTSTATUS libnet_join_joindomain_rpc_unsecure(TALLOC_CTX *mem_ctx,
790 struct libnet_JoinCtx *r,
791 struct cli_state *cli)
793 struct rpc_pipe_client *pipe_hnd = NULL;
794 unsigned char orig_trust_passwd_hash[16];
795 unsigned char new_trust_passwd_hash[16];
796 fstring trust_passwd;
797 NTSTATUS status;
799 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
800 &pipe_hnd);
801 if (!NT_STATUS_IS_OK(status)) {
802 return status;
805 if (!r->in.machine_password) {
806 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
807 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
810 E_md4hash(r->in.machine_password, new_trust_passwd_hash);
812 /* according to WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED */
813 fstrcpy(trust_passwd, r->in.admin_password);
814 strlower_m(trust_passwd);
817 * Machine names can be 15 characters, but the max length on
818 * a password is 14. --jerry
821 trust_passwd[14] = '\0';
823 E_md4hash(trust_passwd, orig_trust_passwd_hash);
825 status = rpccli_netlogon_set_trust_password(pipe_hnd, mem_ctx,
826 r->in.machine_name,
827 orig_trust_passwd_hash,
828 r->in.machine_password,
829 new_trust_passwd_hash,
830 r->in.secure_channel_type);
832 return status;
835 /****************************************************************
836 Do the domain join
837 ****************************************************************/
839 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
840 struct libnet_JoinCtx *r,
841 struct cli_state *cli)
843 struct rpc_pipe_client *pipe_hnd = NULL;
844 struct policy_handle sam_pol, domain_pol, user_pol;
845 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
846 char *acct_name;
847 struct lsa_String lsa_acct_name;
848 uint32_t user_rid;
849 uint32_t acct_flags = ACB_WSTRUST;
850 struct samr_Ids user_rids;
851 struct samr_Ids name_types;
852 union samr_UserInfo user_info;
853 struct dcerpc_binding_handle *b = NULL;
855 struct samr_CryptPassword crypt_pwd;
856 struct samr_CryptPasswordEx crypt_pwd_ex;
858 ZERO_STRUCT(sam_pol);
859 ZERO_STRUCT(domain_pol);
860 ZERO_STRUCT(user_pol);
862 switch (r->in.secure_channel_type) {
863 case SEC_CHAN_WKSTA:
864 acct_flags = ACB_WSTRUST;
865 break;
866 case SEC_CHAN_BDC:
867 acct_flags = ACB_SVRTRUST;
868 break;
869 default:
870 return NT_STATUS_INVALID_PARAMETER;
873 if (!r->in.machine_password) {
874 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
875 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
878 /* Open the domain */
880 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
881 &pipe_hnd);
882 if (!NT_STATUS_IS_OK(status)) {
883 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
884 nt_errstr(status)));
885 goto done;
888 b = pipe_hnd->binding_handle;
890 status = dcerpc_samr_Connect2(b, mem_ctx,
891 pipe_hnd->desthost,
892 SAMR_ACCESS_ENUM_DOMAINS
893 | SAMR_ACCESS_LOOKUP_DOMAIN,
894 &sam_pol,
895 &result);
896 if (!NT_STATUS_IS_OK(status)) {
897 goto done;
899 if (!NT_STATUS_IS_OK(result)) {
900 status = result;
901 goto done;
904 status = dcerpc_samr_OpenDomain(b, mem_ctx,
905 &sam_pol,
906 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
907 | SAMR_DOMAIN_ACCESS_CREATE_USER
908 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
909 r->out.domain_sid,
910 &domain_pol,
911 &result);
912 if (!NT_STATUS_IS_OK(status)) {
913 goto done;
915 if (!NT_STATUS_IS_OK(result)) {
916 status = result;
917 goto done;
920 /* Create domain user */
922 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
923 strlower_m(acct_name);
925 init_lsa_String(&lsa_acct_name, acct_name);
927 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
928 uint32_t access_desired =
929 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
930 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
931 SAMR_USER_ACCESS_SET_PASSWORD |
932 SAMR_USER_ACCESS_GET_ATTRIBUTES |
933 SAMR_USER_ACCESS_SET_ATTRIBUTES;
934 uint32_t access_granted = 0;
936 DEBUG(10,("Creating account with desired access mask: %d\n",
937 access_desired));
939 status = dcerpc_samr_CreateUser2(b, mem_ctx,
940 &domain_pol,
941 &lsa_acct_name,
942 acct_flags,
943 access_desired,
944 &user_pol,
945 &access_granted,
946 &user_rid,
947 &result);
948 if (!NT_STATUS_IS_OK(status)) {
949 goto done;
952 status = result;
953 if (!NT_STATUS_IS_OK(status) &&
954 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
956 DEBUG(10,("Creation of workstation account failed: %s\n",
957 nt_errstr(status)));
959 /* If NT_STATUS_ACCESS_DENIED then we have a valid
960 username/password combo but the user does not have
961 administrator access. */
963 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
964 libnet_join_set_error_string(mem_ctx, r,
965 "User specified does not have "
966 "administrator privileges");
969 goto done;
972 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
973 if (!(r->in.join_flags &
974 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
975 goto done;
979 /* We *must* do this.... don't ask... */
981 if (NT_STATUS_IS_OK(status)) {
982 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
986 status = dcerpc_samr_LookupNames(b, mem_ctx,
987 &domain_pol,
989 &lsa_acct_name,
990 &user_rids,
991 &name_types,
992 &result);
993 if (!NT_STATUS_IS_OK(status)) {
994 goto done;
996 if (!NT_STATUS_IS_OK(result)) {
997 status = result;
998 goto done;
1001 if (name_types.ids[0] != SID_NAME_USER) {
1002 DEBUG(0,("%s is not a user account (type=%d)\n",
1003 acct_name, name_types.ids[0]));
1004 status = NT_STATUS_INVALID_WORKSTATION;
1005 goto done;
1008 user_rid = user_rids.ids[0];
1010 /* Open handle on user */
1012 status = dcerpc_samr_OpenUser(b, mem_ctx,
1013 &domain_pol,
1014 SEC_FLAG_MAXIMUM_ALLOWED,
1015 user_rid,
1016 &user_pol,
1017 &result);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 goto done;
1021 if (!NT_STATUS_IS_OK(result)) {
1022 status = result;
1023 goto done;
1026 /* Fill in the additional account flags now */
1028 acct_flags |= ACB_PWNOEXP;
1030 /* Set account flags on machine account */
1031 ZERO_STRUCT(user_info.info16);
1032 user_info.info16.acct_flags = acct_flags;
1034 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1035 &user_pol,
1037 &user_info,
1038 &result);
1039 if (!NT_STATUS_IS_OK(status)) {
1040 dcerpc_samr_DeleteUser(b, mem_ctx,
1041 &user_pol,
1042 &result);
1044 libnet_join_set_error_string(mem_ctx, r,
1045 "Failed to set account flags for machine account (%s)\n",
1046 nt_errstr(status));
1047 goto done;
1050 if (!NT_STATUS_IS_OK(result)) {
1051 status = result;
1053 dcerpc_samr_DeleteUser(b, mem_ctx,
1054 &user_pol,
1055 &result);
1057 libnet_join_set_error_string(mem_ctx, r,
1058 "Failed to set account flags for machine account (%s)\n",
1059 nt_errstr(status));
1060 goto done;
1063 /* Set password on machine account - first try level 26 */
1065 init_samr_CryptPasswordEx(r->in.machine_password,
1066 &cli->user_session_key,
1067 &crypt_pwd_ex);
1069 user_info.info26.password = crypt_pwd_ex;
1070 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1072 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1073 &user_pol,
1075 &user_info,
1076 &result);
1078 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1080 /* retry with level 24 */
1082 init_samr_CryptPassword(r->in.machine_password,
1083 &cli->user_session_key,
1084 &crypt_pwd);
1086 user_info.info24.password = crypt_pwd;
1087 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1089 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1090 &user_pol,
1092 &user_info,
1093 &result);
1096 if (!NT_STATUS_IS_OK(status)) {
1098 dcerpc_samr_DeleteUser(b, mem_ctx,
1099 &user_pol,
1100 &result);
1102 libnet_join_set_error_string(mem_ctx, r,
1103 "Failed to set password for machine account (%s)\n",
1104 nt_errstr(status));
1105 goto done;
1107 if (!NT_STATUS_IS_OK(result)) {
1108 status = result;
1110 dcerpc_samr_DeleteUser(b, mem_ctx,
1111 &user_pol,
1112 &result);
1114 libnet_join_set_error_string(mem_ctx, r,
1115 "Failed to set password for machine account (%s)\n",
1116 nt_errstr(status));
1117 goto done;
1120 status = NT_STATUS_OK;
1122 done:
1123 if (!pipe_hnd) {
1124 return status;
1127 if (is_valid_policy_hnd(&sam_pol)) {
1128 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1130 if (is_valid_policy_hnd(&domain_pol)) {
1131 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1133 if (is_valid_policy_hnd(&user_pol)) {
1134 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1136 TALLOC_FREE(pipe_hnd);
1138 return status;
1141 /****************************************************************
1142 ****************************************************************/
1144 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1145 const char *machine_name,
1146 const char *dc_name)
1148 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1149 struct cli_state *cli = NULL;
1150 struct rpc_pipe_client *pipe_hnd = NULL;
1151 struct rpc_pipe_client *netlogon_pipe = NULL;
1152 NTSTATUS status;
1153 char *machine_password = NULL;
1154 char *machine_account = NULL;
1156 if (!dc_name) {
1157 return NT_STATUS_INVALID_PARAMETER;
1160 if (!secrets_init()) {
1161 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1164 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1165 NULL, NULL);
1166 if (!machine_password) {
1167 return NT_STATUS_NO_TRUST_LSA_SECRET;
1170 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1171 SAFE_FREE(machine_password);
1172 return NT_STATUS_NO_MEMORY;
1175 status = cli_full_connection(&cli, NULL,
1176 dc_name,
1177 NULL, 0,
1178 "IPC$", "IPC",
1179 machine_account,
1180 NULL,
1181 machine_password,
1183 SMB_SIGNING_DEFAULT);
1184 free(machine_account);
1185 free(machine_password);
1187 if (!NT_STATUS_IS_OK(status)) {
1188 status = cli_full_connection(&cli, NULL,
1189 dc_name,
1190 NULL, 0,
1191 "IPC$", "IPC",
1193 NULL,
1196 SMB_SIGNING_DEFAULT);
1199 if (!NT_STATUS_IS_OK(status)) {
1200 return status;
1203 status = get_schannel_session_key(cli, netbios_domain_name,
1204 &neg_flags, &netlogon_pipe);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1207 cli_shutdown(cli);
1208 return NT_STATUS_OK;
1211 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1212 "key from server %s for domain %s. Error was %s\n",
1213 smbXcli_conn_remote_name(cli->conn),
1214 netbios_domain_name, nt_errstr(status)));
1215 cli_shutdown(cli);
1216 return status;
1219 if (!lp_client_schannel()) {
1220 cli_shutdown(cli);
1221 return NT_STATUS_OK;
1224 status = cli_rpc_pipe_open_schannel_with_key(
1225 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
1226 DCERPC_AUTH_LEVEL_PRIVACY,
1227 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1229 cli_shutdown(cli);
1231 if (!NT_STATUS_IS_OK(status)) {
1232 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1233 "on netlogon pipe to server %s for domain %s. "
1234 "Error was %s\n",
1235 smbXcli_conn_remote_name(cli->conn),
1236 netbios_domain_name, nt_errstr(status)));
1237 return status;
1240 return NT_STATUS_OK;
1243 /****************************************************************
1244 ****************************************************************/
1246 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1247 struct libnet_JoinCtx *r)
1249 NTSTATUS status;
1251 status = libnet_join_ok(r->out.netbios_domain_name,
1252 r->in.machine_name,
1253 r->in.dc_name);
1254 if (!NT_STATUS_IS_OK(status)) {
1255 libnet_join_set_error_string(mem_ctx, r,
1256 "failed to verify domain membership after joining: %s",
1257 get_friendly_nt_error_msg(status));
1258 return WERR_SETUP_NOT_JOINED;
1261 return WERR_OK;
1264 /****************************************************************
1265 ****************************************************************/
1267 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1268 struct libnet_UnjoinCtx *r)
1270 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1271 return false;
1274 if (!secrets_delete_domain_sid(lp_workgroup())) {
1275 return false;
1278 return true;
1281 /****************************************************************
1282 ****************************************************************/
1284 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1285 struct libnet_UnjoinCtx *r)
1287 struct cli_state *cli = NULL;
1288 struct rpc_pipe_client *pipe_hnd = NULL;
1289 struct policy_handle sam_pol, domain_pol, user_pol;
1290 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1291 char *acct_name;
1292 uint32_t user_rid;
1293 struct lsa_String lsa_acct_name;
1294 struct samr_Ids user_rids;
1295 struct samr_Ids name_types;
1296 union samr_UserInfo *info = NULL;
1297 struct dcerpc_binding_handle *b = NULL;
1299 ZERO_STRUCT(sam_pol);
1300 ZERO_STRUCT(domain_pol);
1301 ZERO_STRUCT(user_pol);
1303 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1304 r->in.admin_account,
1305 r->in.admin_password,
1306 r->in.use_kerberos,
1307 &cli);
1308 if (!NT_STATUS_IS_OK(status)) {
1309 goto done;
1312 /* Open the domain */
1314 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1315 &pipe_hnd);
1316 if (!NT_STATUS_IS_OK(status)) {
1317 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1318 nt_errstr(status)));
1319 goto done;
1322 b = pipe_hnd->binding_handle;
1324 status = dcerpc_samr_Connect2(b, mem_ctx,
1325 pipe_hnd->desthost,
1326 SEC_FLAG_MAXIMUM_ALLOWED,
1327 &sam_pol,
1328 &result);
1329 if (!NT_STATUS_IS_OK(status)) {
1330 goto done;
1332 if (!NT_STATUS_IS_OK(result)) {
1333 status = result;
1334 goto done;
1337 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1338 &sam_pol,
1339 SEC_FLAG_MAXIMUM_ALLOWED,
1340 r->in.domain_sid,
1341 &domain_pol,
1342 &result);
1343 if (!NT_STATUS_IS_OK(status)) {
1344 goto done;
1346 if (!NT_STATUS_IS_OK(result)) {
1347 status = result;
1348 goto done;
1351 /* Create domain user */
1353 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1354 strlower_m(acct_name);
1356 init_lsa_String(&lsa_acct_name, acct_name);
1358 status = dcerpc_samr_LookupNames(b, mem_ctx,
1359 &domain_pol,
1361 &lsa_acct_name,
1362 &user_rids,
1363 &name_types,
1364 &result);
1366 if (!NT_STATUS_IS_OK(status)) {
1367 goto done;
1369 if (!NT_STATUS_IS_OK(result)) {
1370 status = result;
1371 goto done;
1374 if (name_types.ids[0] != SID_NAME_USER) {
1375 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1376 name_types.ids[0]));
1377 status = NT_STATUS_INVALID_WORKSTATION;
1378 goto done;
1381 user_rid = user_rids.ids[0];
1383 /* Open handle on user */
1385 status = dcerpc_samr_OpenUser(b, mem_ctx,
1386 &domain_pol,
1387 SEC_FLAG_MAXIMUM_ALLOWED,
1388 user_rid,
1389 &user_pol,
1390 &result);
1391 if (!NT_STATUS_IS_OK(status)) {
1392 goto done;
1394 if (!NT_STATUS_IS_OK(result)) {
1395 status = result;
1396 goto done;
1399 /* Get user info */
1401 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1402 &user_pol,
1404 &info,
1405 &result);
1406 if (!NT_STATUS_IS_OK(status)) {
1407 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1408 goto done;
1410 if (!NT_STATUS_IS_OK(result)) {
1411 status = result;
1412 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1413 goto done;
1416 /* now disable and setuser info */
1418 info->info16.acct_flags |= ACB_DISABLED;
1420 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1421 &user_pol,
1423 info,
1424 &result);
1425 if (!NT_STATUS_IS_OK(status)) {
1426 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1427 goto done;
1429 if (!NT_STATUS_IS_OK(result)) {
1430 status = result;
1431 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1432 goto done;
1434 status = result;
1435 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1437 done:
1438 if (pipe_hnd && b) {
1439 if (is_valid_policy_hnd(&domain_pol)) {
1440 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1442 if (is_valid_policy_hnd(&sam_pol)) {
1443 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1445 TALLOC_FREE(pipe_hnd);
1448 if (cli) {
1449 cli_shutdown(cli);
1452 return status;
1455 /****************************************************************
1456 ****************************************************************/
1458 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1460 WERROR werr = WERR_OK;
1461 sbcErr err;
1462 struct smbconf_ctx *ctx;
1464 err = smbconf_init_reg(r, &ctx, NULL);
1465 if (!SBC_ERROR_IS_OK(err)) {
1466 werr = WERR_NO_SUCH_SERVICE;
1467 goto done;
1470 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1472 err = smbconf_set_global_parameter(ctx, "security", "user");
1473 if (!SBC_ERROR_IS_OK(err)) {
1474 werr = WERR_NO_SUCH_SERVICE;
1475 goto done;
1478 err = smbconf_set_global_parameter(ctx, "workgroup",
1479 r->in.domain_name);
1480 if (!SBC_ERROR_IS_OK(err)) {
1481 werr = WERR_NO_SUCH_SERVICE;
1482 goto done;
1485 smbconf_delete_global_parameter(ctx, "realm");
1486 goto done;
1489 err = smbconf_set_global_parameter(ctx, "security", "domain");
1490 if (!SBC_ERROR_IS_OK(err)) {
1491 werr = WERR_NO_SUCH_SERVICE;
1492 goto done;
1495 err = smbconf_set_global_parameter(ctx, "workgroup",
1496 r->out.netbios_domain_name);
1497 if (!SBC_ERROR_IS_OK(err)) {
1498 werr = WERR_NO_SUCH_SERVICE;
1499 goto done;
1502 if (r->out.domain_is_ad) {
1503 err = smbconf_set_global_parameter(ctx, "security", "ads");
1504 if (!SBC_ERROR_IS_OK(err)) {
1505 werr = WERR_NO_SUCH_SERVICE;
1506 goto done;
1509 err = smbconf_set_global_parameter(ctx, "realm",
1510 r->out.dns_domain_name);
1511 if (!SBC_ERROR_IS_OK(err)) {
1512 werr = WERR_NO_SUCH_SERVICE;
1513 goto done;
1517 done:
1518 smbconf_shutdown(ctx);
1519 return werr;
1522 /****************************************************************
1523 ****************************************************************/
1525 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1527 WERROR werr = WERR_OK;
1528 sbcErr err;
1529 struct smbconf_ctx *ctx;
1531 err = smbconf_init_reg(r, &ctx, NULL);
1532 if (!SBC_ERROR_IS_OK(err)) {
1533 werr = WERR_NO_SUCH_SERVICE;
1534 goto done;
1537 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1539 err = smbconf_set_global_parameter(ctx, "security", "user");
1540 if (!SBC_ERROR_IS_OK(err)) {
1541 werr = WERR_NO_SUCH_SERVICE;
1542 goto done;
1545 err = smbconf_delete_global_parameter(ctx, "workgroup");
1546 if (!SBC_ERROR_IS_OK(err)) {
1547 werr = WERR_NO_SUCH_SERVICE;
1548 goto done;
1551 smbconf_delete_global_parameter(ctx, "realm");
1554 done:
1555 smbconf_shutdown(ctx);
1556 return werr;
1559 /****************************************************************
1560 ****************************************************************/
1562 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1564 WERROR werr;
1566 if (!W_ERROR_IS_OK(r->out.result)) {
1567 return r->out.result;
1570 if (!r->in.modify_config) {
1571 return WERR_OK;
1574 werr = do_join_modify_vals_config(r);
1575 if (!W_ERROR_IS_OK(werr)) {
1576 return werr;
1579 lp_load_global(get_dyn_CONFIGFILE());
1581 r->out.modified_config = true;
1582 r->out.result = werr;
1584 return werr;
1587 /****************************************************************
1588 ****************************************************************/
1590 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1592 WERROR werr;
1594 if (!W_ERROR_IS_OK(r->out.result)) {
1595 return r->out.result;
1598 if (!r->in.modify_config) {
1599 return WERR_OK;
1602 werr = do_unjoin_modify_vals_config(r);
1603 if (!W_ERROR_IS_OK(werr)) {
1604 return werr;
1607 lp_load_global(get_dyn_CONFIGFILE());
1609 r->out.modified_config = true;
1610 r->out.result = werr;
1612 return werr;
1615 /****************************************************************
1616 ****************************************************************/
1618 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1619 const char *domain_str,
1620 const char **domain_p,
1621 const char **dc_p)
1623 char *domain = NULL;
1624 char *dc = NULL;
1625 const char *p = NULL;
1627 if (!domain_str || !domain_p || !dc_p) {
1628 return false;
1631 p = strchr_m(domain_str, '\\');
1633 if (p != NULL) {
1634 domain = talloc_strndup(mem_ctx, domain_str,
1635 PTR_DIFF(p, domain_str));
1636 dc = talloc_strdup(mem_ctx, p+1);
1637 if (!dc) {
1638 return false;
1640 } else {
1641 domain = talloc_strdup(mem_ctx, domain_str);
1642 dc = NULL;
1644 if (!domain) {
1645 return false;
1648 *domain_p = domain;
1650 if (!*dc_p && dc) {
1651 *dc_p = dc;
1654 return true;
1657 /****************************************************************
1658 ****************************************************************/
1660 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1661 struct libnet_JoinCtx *r)
1663 if (!r->in.domain_name) {
1664 libnet_join_set_error_string(mem_ctx, r,
1665 "No domain name defined");
1666 return WERR_INVALID_PARAM;
1669 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1670 &r->in.domain_name,
1671 &r->in.dc_name)) {
1672 libnet_join_set_error_string(mem_ctx, r,
1673 "Failed to parse domain name");
1674 return WERR_INVALID_PARAM;
1677 if (IS_DC) {
1678 return WERR_SETUP_DOMAIN_CONTROLLER;
1681 if (!secrets_init()) {
1682 libnet_join_set_error_string(mem_ctx, r,
1683 "Unable to open secrets database");
1684 return WERR_CAN_NOT_COMPLETE;
1687 return WERR_OK;
1690 /****************************************************************
1691 ****************************************************************/
1693 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1695 NTSTATUS status;
1697 /* Try adding dom admins to builtin\admins. Only log failures. */
1698 status = create_builtin_administrators(domain_sid);
1699 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1700 DEBUG(10,("Unable to auto-add domain administrators to "
1701 "BUILTIN\\Administrators during join because "
1702 "winbindd must be running.\n"));
1703 } else if (!NT_STATUS_IS_OK(status)) {
1704 DEBUG(5, ("Failed to auto-add domain administrators to "
1705 "BUILTIN\\Administrators during join: %s\n",
1706 nt_errstr(status)));
1709 /* Try adding dom users to builtin\users. Only log failures. */
1710 status = create_builtin_users(domain_sid);
1711 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1712 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1713 "during join because winbindd must be running.\n"));
1714 } else if (!NT_STATUS_IS_OK(status)) {
1715 DEBUG(5, ("Failed to auto-add domain administrators to "
1716 "BUILTIN\\Administrators during join: %s\n",
1717 nt_errstr(status)));
1721 /****************************************************************
1722 ****************************************************************/
1724 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1725 struct libnet_JoinCtx *r)
1727 WERROR werr;
1729 if (!W_ERROR_IS_OK(r->out.result)) {
1730 return r->out.result;
1733 werr = do_JoinConfig(r);
1734 if (!W_ERROR_IS_OK(werr)) {
1735 return werr;
1738 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1739 return WERR_OK;
1742 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1743 if (r->out.dns_domain_name) {
1744 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1747 #ifdef HAVE_ADS
1748 if (r->out.domain_is_ad &&
1749 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1750 ADS_STATUS ads_status;
1752 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1753 if (!ADS_ERR_OK(ads_status)) {
1754 return WERR_GENERAL_FAILURE;
1757 #endif /* HAVE_ADS */
1759 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1761 return WERR_OK;
1764 /****************************************************************
1765 ****************************************************************/
1767 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1769 if (r->in.ads) {
1770 ads_destroy(&r->in.ads);
1773 return 0;
1776 /****************************************************************
1777 ****************************************************************/
1779 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1781 if (r->in.ads) {
1782 ads_destroy(&r->in.ads);
1785 return 0;
1788 /****************************************************************
1789 ****************************************************************/
1791 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1792 struct libnet_JoinCtx **r)
1794 struct libnet_JoinCtx *ctx;
1796 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1797 if (!ctx) {
1798 return WERR_NOMEM;
1801 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1803 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1804 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1806 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1808 *r = ctx;
1810 return WERR_OK;
1813 /****************************************************************
1814 ****************************************************************/
1816 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1817 struct libnet_UnjoinCtx **r)
1819 struct libnet_UnjoinCtx *ctx;
1821 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1822 if (!ctx) {
1823 return WERR_NOMEM;
1826 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1828 ctx->in.machine_name = talloc_strdup(mem_ctx, lp_netbios_name());
1829 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1831 *r = ctx;
1833 return WERR_OK;
1836 /****************************************************************
1837 ****************************************************************/
1839 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1840 struct libnet_JoinCtx *r)
1842 bool valid_security = false;
1843 bool valid_workgroup = false;
1844 bool valid_realm = false;
1846 /* check if configuration is already set correctly */
1848 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1850 switch (r->out.domain_is_ad) {
1851 case false:
1852 valid_security = (lp_security() == SEC_DOMAIN);
1853 if (valid_workgroup && valid_security) {
1854 /* nothing to be done */
1855 return WERR_OK;
1857 break;
1858 case true:
1859 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1860 switch (lp_security()) {
1861 case SEC_DOMAIN:
1862 case SEC_ADS:
1863 valid_security = true;
1866 if (valid_workgroup && valid_realm && valid_security) {
1867 /* nothing to be done */
1868 return WERR_OK;
1870 break;
1873 /* check if we are supposed to manipulate configuration */
1875 if (!r->in.modify_config) {
1877 char *wrong_conf = talloc_strdup(mem_ctx, "");
1879 if (!valid_workgroup) {
1880 wrong_conf = talloc_asprintf_append(wrong_conf,
1881 "\"workgroup\" set to '%s', should be '%s'",
1882 lp_workgroup(), r->out.netbios_domain_name);
1883 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1886 if (!valid_realm) {
1887 wrong_conf = talloc_asprintf_append(wrong_conf,
1888 "\"realm\" set to '%s', should be '%s'",
1889 lp_realm(), r->out.dns_domain_name);
1890 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1893 if (!valid_security) {
1894 const char *sec = NULL;
1895 switch (lp_security()) {
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, smbXcli_conn_remote_sockaddr(cli->conn),
1982 smbXcli_conn_remote_name(cli->conn));
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;