s3:libnet increase timeout for machine password change
[Samba.git] / source3 / libnet / libnet_join.c
blobe84682dae2b1f82bf2119383e094c8d178502df2
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 Undefined);
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;
853 unsigned int old_timeout = 0;
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 */
1066 * increase the timeout as password filter modules on the DC
1067 * might delay the operation for a significant amount of time
1069 old_timeout = rpccli_set_timeout(pipe_hnd, 600000);
1071 init_samr_CryptPasswordEx(r->in.machine_password,
1072 &cli->user_session_key,
1073 &crypt_pwd_ex);
1075 user_info.info26.password = crypt_pwd_ex;
1076 user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1078 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1079 &user_pol,
1081 &user_info,
1082 &result);
1084 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE)) {
1086 /* retry with level 24 */
1088 init_samr_CryptPassword(r->in.machine_password,
1089 &cli->user_session_key,
1090 &crypt_pwd);
1092 user_info.info24.password = crypt_pwd;
1093 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
1095 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
1096 &user_pol,
1098 &user_info,
1099 &result);
1102 old_timeout = rpccli_set_timeout(pipe_hnd, old_timeout);
1104 if (!NT_STATUS_IS_OK(status)) {
1106 dcerpc_samr_DeleteUser(b, mem_ctx,
1107 &user_pol,
1108 &result);
1110 libnet_join_set_error_string(mem_ctx, r,
1111 "Failed to set password for machine account (%s)\n",
1112 nt_errstr(status));
1113 goto done;
1115 if (!NT_STATUS_IS_OK(result)) {
1116 status = result;
1118 dcerpc_samr_DeleteUser(b, mem_ctx,
1119 &user_pol,
1120 &result);
1122 libnet_join_set_error_string(mem_ctx, r,
1123 "Failed to set password for machine account (%s)\n",
1124 nt_errstr(status));
1125 goto done;
1128 status = NT_STATUS_OK;
1130 done:
1131 if (!pipe_hnd) {
1132 return status;
1135 if (is_valid_policy_hnd(&sam_pol)) {
1136 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1138 if (is_valid_policy_hnd(&domain_pol)) {
1139 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1141 if (is_valid_policy_hnd(&user_pol)) {
1142 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1144 TALLOC_FREE(pipe_hnd);
1146 return status;
1149 /****************************************************************
1150 ****************************************************************/
1152 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1153 const char *machine_name,
1154 const char *dc_name)
1156 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1157 struct cli_state *cli = NULL;
1158 struct rpc_pipe_client *pipe_hnd = NULL;
1159 struct rpc_pipe_client *netlogon_pipe = NULL;
1160 NTSTATUS status;
1161 char *machine_password = NULL;
1162 char *machine_account = NULL;
1164 if (!dc_name) {
1165 return NT_STATUS_INVALID_PARAMETER;
1168 if (!secrets_init()) {
1169 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1172 machine_password = secrets_fetch_machine_password(netbios_domain_name,
1173 NULL, NULL);
1174 if (!machine_password) {
1175 return NT_STATUS_NO_TRUST_LSA_SECRET;
1178 if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1179 SAFE_FREE(machine_password);
1180 return NT_STATUS_NO_MEMORY;
1183 status = cli_full_connection(&cli, NULL,
1184 dc_name,
1185 NULL, 0,
1186 "IPC$", "IPC",
1187 machine_account,
1188 NULL,
1189 machine_password,
1191 Undefined);
1192 free(machine_account);
1193 free(machine_password);
1195 if (!NT_STATUS_IS_OK(status)) {
1196 status = cli_full_connection(&cli, NULL,
1197 dc_name,
1198 NULL, 0,
1199 "IPC$", "IPC",
1201 NULL,
1204 Undefined);
1207 if (!NT_STATUS_IS_OK(status)) {
1208 return status;
1211 status = get_schannel_session_key(cli, netbios_domain_name,
1212 &neg_flags, &netlogon_pipe);
1213 if (!NT_STATUS_IS_OK(status)) {
1214 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1215 cli_shutdown(cli);
1216 return NT_STATUS_OK;
1219 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1220 "key from server %s for domain %s. Error was %s\n",
1221 cli->desthost, netbios_domain_name, nt_errstr(status)));
1222 cli_shutdown(cli);
1223 return status;
1226 if (!lp_client_schannel()) {
1227 cli_shutdown(cli);
1228 return NT_STATUS_OK;
1231 status = cli_rpc_pipe_open_schannel_with_key(
1232 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
1233 DCERPC_AUTH_LEVEL_PRIVACY,
1234 netbios_domain_name, &netlogon_pipe->dc, &pipe_hnd);
1236 cli_shutdown(cli);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1240 "on netlogon pipe to server %s for domain %s. "
1241 "Error was %s\n",
1242 cli->desthost, netbios_domain_name, nt_errstr(status)));
1243 return status;
1246 return NT_STATUS_OK;
1249 /****************************************************************
1250 ****************************************************************/
1252 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1253 struct libnet_JoinCtx *r)
1255 NTSTATUS status;
1257 status = libnet_join_ok(r->out.netbios_domain_name,
1258 r->in.machine_name,
1259 r->in.dc_name);
1260 if (!NT_STATUS_IS_OK(status)) {
1261 libnet_join_set_error_string(mem_ctx, r,
1262 "failed to verify domain membership after joining: %s",
1263 get_friendly_nt_error_msg(status));
1264 return WERR_SETUP_NOT_JOINED;
1267 return WERR_OK;
1270 /****************************************************************
1271 ****************************************************************/
1273 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1274 struct libnet_UnjoinCtx *r)
1276 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1277 return false;
1280 if (!secrets_delete_domain_sid(lp_workgroup())) {
1281 return false;
1284 return true;
1287 /****************************************************************
1288 ****************************************************************/
1290 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1291 struct libnet_UnjoinCtx *r)
1293 struct cli_state *cli = NULL;
1294 struct rpc_pipe_client *pipe_hnd = NULL;
1295 struct policy_handle sam_pol, domain_pol, user_pol;
1296 NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
1297 char *acct_name;
1298 uint32_t user_rid;
1299 struct lsa_String lsa_acct_name;
1300 struct samr_Ids user_rids;
1301 struct samr_Ids name_types;
1302 union samr_UserInfo *info = NULL;
1303 struct dcerpc_binding_handle *b = NULL;
1305 ZERO_STRUCT(sam_pol);
1306 ZERO_STRUCT(domain_pol);
1307 ZERO_STRUCT(user_pol);
1309 status = libnet_join_connect_dc_ipc(r->in.dc_name,
1310 r->in.admin_account,
1311 r->in.admin_password,
1312 r->in.use_kerberos,
1313 &cli);
1314 if (!NT_STATUS_IS_OK(status)) {
1315 goto done;
1318 /* Open the domain */
1320 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1321 &pipe_hnd);
1322 if (!NT_STATUS_IS_OK(status)) {
1323 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1324 nt_errstr(status)));
1325 goto done;
1328 b = pipe_hnd->binding_handle;
1330 status = dcerpc_samr_Connect2(b, mem_ctx,
1331 pipe_hnd->desthost,
1332 SEC_FLAG_MAXIMUM_ALLOWED,
1333 &sam_pol,
1334 &result);
1335 if (!NT_STATUS_IS_OK(status)) {
1336 goto done;
1338 if (!NT_STATUS_IS_OK(result)) {
1339 status = result;
1340 goto done;
1343 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1344 &sam_pol,
1345 SEC_FLAG_MAXIMUM_ALLOWED,
1346 r->in.domain_sid,
1347 &domain_pol,
1348 &result);
1349 if (!NT_STATUS_IS_OK(status)) {
1350 goto done;
1352 if (!NT_STATUS_IS_OK(result)) {
1353 status = result;
1354 goto done;
1357 /* Create domain user */
1359 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1360 strlower_m(acct_name);
1362 init_lsa_String(&lsa_acct_name, acct_name);
1364 status = dcerpc_samr_LookupNames(b, mem_ctx,
1365 &domain_pol,
1367 &lsa_acct_name,
1368 &user_rids,
1369 &name_types,
1370 &result);
1372 if (!NT_STATUS_IS_OK(status)) {
1373 goto done;
1375 if (!NT_STATUS_IS_OK(result)) {
1376 status = result;
1377 goto done;
1380 if (name_types.ids[0] != SID_NAME_USER) {
1381 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1382 name_types.ids[0]));
1383 status = NT_STATUS_INVALID_WORKSTATION;
1384 goto done;
1387 user_rid = user_rids.ids[0];
1389 /* Open handle on user */
1391 status = dcerpc_samr_OpenUser(b, mem_ctx,
1392 &domain_pol,
1393 SEC_FLAG_MAXIMUM_ALLOWED,
1394 user_rid,
1395 &user_pol,
1396 &result);
1397 if (!NT_STATUS_IS_OK(status)) {
1398 goto done;
1400 if (!NT_STATUS_IS_OK(result)) {
1401 status = result;
1402 goto done;
1405 /* Get user info */
1407 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1408 &user_pol,
1410 &info,
1411 &result);
1412 if (!NT_STATUS_IS_OK(status)) {
1413 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1414 goto done;
1416 if (!NT_STATUS_IS_OK(result)) {
1417 status = result;
1418 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1419 goto done;
1422 /* now disable and setuser info */
1424 info->info16.acct_flags |= ACB_DISABLED;
1426 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1427 &user_pol,
1429 info,
1430 &result);
1431 if (!NT_STATUS_IS_OK(status)) {
1432 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1433 goto done;
1435 if (!NT_STATUS_IS_OK(result)) {
1436 status = result;
1437 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1438 goto done;
1440 status = result;
1441 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1443 done:
1444 if (pipe_hnd && b) {
1445 if (is_valid_policy_hnd(&domain_pol)) {
1446 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1448 if (is_valid_policy_hnd(&sam_pol)) {
1449 dcerpc_samr_Close(b, mem_ctx, &sam_pol, &result);
1451 TALLOC_FREE(pipe_hnd);
1454 if (cli) {
1455 cli_shutdown(cli);
1458 return status;
1461 /****************************************************************
1462 ****************************************************************/
1464 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1466 WERROR werr = WERR_OK;
1467 sbcErr err;
1468 struct smbconf_ctx *ctx;
1470 err = smbconf_init_reg(r, &ctx, NULL);
1471 if (!SBC_ERROR_IS_OK(err)) {
1472 werr = WERR_NO_SUCH_SERVICE;
1473 goto done;
1476 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1478 err = smbconf_set_global_parameter(ctx, "security", "user");
1479 if (!SBC_ERROR_IS_OK(err)) {
1480 werr = WERR_NO_SUCH_SERVICE;
1481 goto done;
1484 err = smbconf_set_global_parameter(ctx, "workgroup",
1485 r->in.domain_name);
1486 if (!SBC_ERROR_IS_OK(err)) {
1487 werr = WERR_NO_SUCH_SERVICE;
1488 goto done;
1491 smbconf_delete_global_parameter(ctx, "realm");
1492 goto done;
1495 err = smbconf_set_global_parameter(ctx, "security", "domain");
1496 if (!SBC_ERROR_IS_OK(err)) {
1497 werr = WERR_NO_SUCH_SERVICE;
1498 goto done;
1501 err = smbconf_set_global_parameter(ctx, "workgroup",
1502 r->out.netbios_domain_name);
1503 if (!SBC_ERROR_IS_OK(err)) {
1504 werr = WERR_NO_SUCH_SERVICE;
1505 goto done;
1508 if (r->out.domain_is_ad) {
1509 err = smbconf_set_global_parameter(ctx, "security", "ads");
1510 if (!SBC_ERROR_IS_OK(err)) {
1511 werr = WERR_NO_SUCH_SERVICE;
1512 goto done;
1515 err = smbconf_set_global_parameter(ctx, "realm",
1516 r->out.dns_domain_name);
1517 if (!SBC_ERROR_IS_OK(err)) {
1518 werr = WERR_NO_SUCH_SERVICE;
1519 goto done;
1523 done:
1524 smbconf_shutdown(ctx);
1525 return werr;
1528 /****************************************************************
1529 ****************************************************************/
1531 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1533 WERROR werr = WERR_OK;
1534 sbcErr err;
1535 struct smbconf_ctx *ctx;
1537 err = smbconf_init_reg(r, &ctx, NULL);
1538 if (!SBC_ERROR_IS_OK(err)) {
1539 werr = WERR_NO_SUCH_SERVICE;
1540 goto done;
1543 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1545 err = smbconf_set_global_parameter(ctx, "security", "user");
1546 if (!SBC_ERROR_IS_OK(err)) {
1547 werr = WERR_NO_SUCH_SERVICE;
1548 goto done;
1551 err = smbconf_delete_global_parameter(ctx, "workgroup");
1552 if (!SBC_ERROR_IS_OK(err)) {
1553 werr = WERR_NO_SUCH_SERVICE;
1554 goto done;
1557 smbconf_delete_global_parameter(ctx, "realm");
1560 done:
1561 smbconf_shutdown(ctx);
1562 return werr;
1565 /****************************************************************
1566 ****************************************************************/
1568 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1570 WERROR werr;
1572 if (!W_ERROR_IS_OK(r->out.result)) {
1573 return r->out.result;
1576 if (!r->in.modify_config) {
1577 return WERR_OK;
1580 werr = do_join_modify_vals_config(r);
1581 if (!W_ERROR_IS_OK(werr)) {
1582 return werr;
1585 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1587 r->out.modified_config = true;
1588 r->out.result = werr;
1590 return werr;
1593 /****************************************************************
1594 ****************************************************************/
1596 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1598 WERROR werr;
1600 if (!W_ERROR_IS_OK(r->out.result)) {
1601 return r->out.result;
1604 if (!r->in.modify_config) {
1605 return WERR_OK;
1608 werr = do_unjoin_modify_vals_config(r);
1609 if (!W_ERROR_IS_OK(werr)) {
1610 return werr;
1613 lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1615 r->out.modified_config = true;
1616 r->out.result = werr;
1618 return werr;
1621 /****************************************************************
1622 ****************************************************************/
1624 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1625 const char *domain_str,
1626 const char **domain_p,
1627 const char **dc_p)
1629 char *domain = NULL;
1630 char *dc = NULL;
1631 const char *p = NULL;
1633 if (!domain_str || !domain_p || !dc_p) {
1634 return false;
1637 p = strchr_m(domain_str, '\\');
1639 if (p != NULL) {
1640 domain = talloc_strndup(mem_ctx, domain_str,
1641 PTR_DIFF(p, domain_str));
1642 dc = talloc_strdup(mem_ctx, p+1);
1643 if (!dc) {
1644 return false;
1646 } else {
1647 domain = talloc_strdup(mem_ctx, domain_str);
1648 dc = NULL;
1650 if (!domain) {
1651 return false;
1654 *domain_p = domain;
1656 if (!*dc_p && dc) {
1657 *dc_p = dc;
1660 return true;
1663 /****************************************************************
1664 ****************************************************************/
1666 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1667 struct libnet_JoinCtx *r)
1669 if (!r->in.domain_name) {
1670 libnet_join_set_error_string(mem_ctx, r,
1671 "No domain name defined");
1672 return WERR_INVALID_PARAM;
1675 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1676 &r->in.domain_name,
1677 &r->in.dc_name)) {
1678 libnet_join_set_error_string(mem_ctx, r,
1679 "Failed to parse domain name");
1680 return WERR_INVALID_PARAM;
1683 if (IS_DC) {
1684 return WERR_SETUP_DOMAIN_CONTROLLER;
1687 if (!secrets_init()) {
1688 libnet_join_set_error_string(mem_ctx, r,
1689 "Unable to open secrets database");
1690 return WERR_CAN_NOT_COMPLETE;
1693 return WERR_OK;
1696 /****************************************************************
1697 ****************************************************************/
1699 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1701 NTSTATUS status;
1703 /* Try adding dom admins to builtin\admins. Only log failures. */
1704 status = create_builtin_administrators(domain_sid);
1705 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1706 DEBUG(10,("Unable to auto-add domain administrators to "
1707 "BUILTIN\\Administrators during join because "
1708 "winbindd must be running."));
1709 } else if (!NT_STATUS_IS_OK(status)) {
1710 DEBUG(5, ("Failed to auto-add domain administrators to "
1711 "BUILTIN\\Administrators during join: %s\n",
1712 nt_errstr(status)));
1715 /* Try adding dom users to builtin\users. Only log failures. */
1716 status = create_builtin_users(domain_sid);
1717 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1718 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1719 "during join because winbindd must be running."));
1720 } else if (!NT_STATUS_IS_OK(status)) {
1721 DEBUG(5, ("Failed to auto-add domain administrators to "
1722 "BUILTIN\\Administrators during join: %s\n",
1723 nt_errstr(status)));
1727 /****************************************************************
1728 ****************************************************************/
1730 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1731 struct libnet_JoinCtx *r)
1733 WERROR werr;
1735 if (!W_ERROR_IS_OK(r->out.result)) {
1736 return r->out.result;
1739 werr = do_JoinConfig(r);
1740 if (!W_ERROR_IS_OK(werr)) {
1741 return werr;
1744 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1745 return WERR_OK;
1748 saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1749 if (r->out.dns_domain_name) {
1750 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1753 #ifdef HAVE_ADS
1754 if (r->out.domain_is_ad &&
1755 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1756 ADS_STATUS ads_status;
1758 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1759 if (!ADS_ERR_OK(ads_status)) {
1760 return WERR_GENERAL_FAILURE;
1763 #endif /* HAVE_ADS */
1765 libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1767 return WERR_OK;
1770 /****************************************************************
1771 ****************************************************************/
1773 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1775 if (r->in.ads) {
1776 ads_destroy(&r->in.ads);
1779 return 0;
1782 /****************************************************************
1783 ****************************************************************/
1785 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1787 if (r->in.ads) {
1788 ads_destroy(&r->in.ads);
1791 return 0;
1794 /****************************************************************
1795 ****************************************************************/
1797 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1798 struct libnet_JoinCtx **r)
1800 struct libnet_JoinCtx *ctx;
1802 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1803 if (!ctx) {
1804 return WERR_NOMEM;
1807 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1809 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1810 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1812 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1814 *r = ctx;
1816 return WERR_OK;
1819 /****************************************************************
1820 ****************************************************************/
1822 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1823 struct libnet_UnjoinCtx **r)
1825 struct libnet_UnjoinCtx *ctx;
1827 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1828 if (!ctx) {
1829 return WERR_NOMEM;
1832 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1834 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1835 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1837 *r = ctx;
1839 return WERR_OK;
1842 /****************************************************************
1843 ****************************************************************/
1845 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1846 struct libnet_JoinCtx *r)
1848 bool valid_security = false;
1849 bool valid_workgroup = false;
1850 bool valid_realm = false;
1852 /* check if configuration is already set correctly */
1854 valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1856 switch (r->out.domain_is_ad) {
1857 case false:
1858 valid_security = (lp_security() == SEC_DOMAIN);
1859 if (valid_workgroup && valid_security) {
1860 /* nothing to be done */
1861 return WERR_OK;
1863 break;
1864 case true:
1865 valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1866 switch (lp_security()) {
1867 case SEC_DOMAIN:
1868 case SEC_ADS:
1869 valid_security = true;
1872 if (valid_workgroup && valid_realm && valid_security) {
1873 /* nothing to be done */
1874 return WERR_OK;
1876 break;
1879 /* check if we are supposed to manipulate configuration */
1881 if (!r->in.modify_config) {
1883 char *wrong_conf = talloc_strdup(mem_ctx, "");
1885 if (!valid_workgroup) {
1886 wrong_conf = talloc_asprintf_append(wrong_conf,
1887 "\"workgroup\" set to '%s', should be '%s'",
1888 lp_workgroup(), r->out.netbios_domain_name);
1889 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1892 if (!valid_realm) {
1893 wrong_conf = talloc_asprintf_append(wrong_conf,
1894 "\"realm\" set to '%s', should be '%s'",
1895 lp_realm(), r->out.dns_domain_name);
1896 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1899 if (!valid_security) {
1900 const char *sec = NULL;
1901 switch (lp_security()) {
1902 case SEC_SHARE: sec = "share"; break;
1903 case SEC_USER: sec = "user"; break;
1904 case SEC_DOMAIN: sec = "domain"; break;
1905 case SEC_ADS: sec = "ads"; break;
1907 wrong_conf = talloc_asprintf_append(wrong_conf,
1908 "\"security\" set to '%s', should be %s",
1909 sec, r->out.domain_is_ad ?
1910 "either 'domain' or 'ads'" : "'domain'");
1911 W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1914 libnet_join_set_error_string(mem_ctx, r,
1915 "Invalid configuration (%s) and configuration modification "
1916 "was not requested", wrong_conf);
1917 return WERR_CAN_NOT_COMPLETE;
1920 /* check if we are able to manipulate configuration */
1922 if (!lp_config_backend_is_registry()) {
1923 libnet_join_set_error_string(mem_ctx, r,
1924 "Configuration manipulation requested but not "
1925 "supported by backend");
1926 return WERR_NOT_SUPPORTED;
1929 return WERR_OK;
1932 /****************************************************************
1933 ****************************************************************/
1935 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1936 struct libnet_JoinCtx *r)
1938 NTSTATUS status;
1939 WERROR werr;
1940 struct cli_state *cli = NULL;
1941 #ifdef HAVE_ADS
1942 ADS_STATUS ads_status;
1943 #endif /* HAVE_ADS */
1945 if (!r->in.dc_name) {
1946 struct netr_DsRGetDCNameInfo *info;
1947 const char *dc;
1948 status = dsgetdcname(mem_ctx,
1949 r->in.msg_ctx,
1950 r->in.domain_name,
1951 NULL,
1952 NULL,
1953 DS_FORCE_REDISCOVERY |
1954 DS_DIRECTORY_SERVICE_REQUIRED |
1955 DS_WRITABLE_REQUIRED |
1956 DS_RETURN_DNS_NAME,
1957 &info);
1958 if (!NT_STATUS_IS_OK(status)) {
1959 libnet_join_set_error_string(mem_ctx, r,
1960 "failed to find DC for domain %s",
1961 r->in.domain_name,
1962 get_friendly_nt_error_msg(status));
1963 return WERR_DCNOTFOUND;
1966 dc = strip_hostname(info->dc_unc);
1967 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1968 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1971 status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1972 if (!NT_STATUS_IS_OK(status)) {
1973 libnet_join_set_error_string(mem_ctx, r,
1974 "failed to lookup DC info for domain '%s' over rpc: %s",
1975 r->in.domain_name, get_friendly_nt_error_msg(status));
1976 return ntstatus_to_werror(status);
1979 werr = libnet_join_check_config(mem_ctx, r);
1980 if (!W_ERROR_IS_OK(werr)) {
1981 goto done;
1984 #ifdef HAVE_ADS
1986 create_local_private_krb5_conf_for_domain(
1987 r->out.dns_domain_name, r->out.netbios_domain_name,
1988 NULL, &cli->dest_ss, cli->desthost);
1990 if (r->out.domain_is_ad && r->in.account_ou &&
1991 !(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
1993 ads_status = libnet_join_connect_ads(mem_ctx, r);
1994 if (!ADS_ERR_OK(ads_status)) {
1995 return WERR_DEFAULT_JOIN_REQUIRED;
1998 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1999 if (!ADS_ERR_OK(ads_status)) {
2000 libnet_join_set_error_string(mem_ctx, r,
2001 "failed to precreate account in ou %s: %s",
2002 r->in.account_ou,
2003 ads_errstr(ads_status));
2004 return WERR_DEFAULT_JOIN_REQUIRED;
2007 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
2009 #endif /* HAVE_ADS */
2011 if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE) &&
2012 (r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED)) {
2013 status = libnet_join_joindomain_rpc_unsecure(mem_ctx, r, cli);
2014 } else {
2015 status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
2017 if (!NT_STATUS_IS_OK(status)) {
2018 libnet_join_set_error_string(mem_ctx, r,
2019 "failed to join domain '%s' over rpc: %s",
2020 r->in.domain_name, get_friendly_nt_error_msg(status));
2021 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
2022 return WERR_SETUP_ALREADY_JOINED;
2024 werr = ntstatus_to_werror(status);
2025 goto done;
2028 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
2029 werr = WERR_SETUP_NOT_JOINED;
2030 goto done;
2033 werr = WERR_OK;
2035 done:
2036 if (cli) {
2037 cli_shutdown(cli);
2040 return werr;
2043 /****************************************************************
2044 ****************************************************************/
2046 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
2047 struct libnet_JoinCtx *r)
2049 WERROR werr;
2050 struct libnet_UnjoinCtx *u = NULL;
2052 werr = libnet_init_UnjoinCtx(mem_ctx, &u);
2053 if (!W_ERROR_IS_OK(werr)) {
2054 return werr;
2057 u->in.debug = r->in.debug;
2058 u->in.dc_name = r->in.dc_name;
2059 u->in.domain_name = r->in.domain_name;
2060 u->in.admin_account = r->in.admin_account;
2061 u->in.admin_password = r->in.admin_password;
2062 u->in.modify_config = r->in.modify_config;
2063 u->in.unjoin_flags = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
2064 WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
2066 werr = libnet_Unjoin(mem_ctx, u);
2067 TALLOC_FREE(u);
2069 return werr;
2072 /****************************************************************
2073 ****************************************************************/
2075 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
2076 struct libnet_JoinCtx *r)
2078 WERROR werr;
2080 if (r->in.debug) {
2081 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
2084 ZERO_STRUCT(r->out);
2086 werr = libnet_join_pre_processing(mem_ctx, r);
2087 if (!W_ERROR_IS_OK(werr)) {
2088 goto done;
2091 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2092 werr = libnet_DomainJoin(mem_ctx, r);
2093 if (!W_ERROR_IS_OK(werr)) {
2094 goto done;
2098 werr = libnet_join_post_processing(mem_ctx, r);
2099 if (!W_ERROR_IS_OK(werr)) {
2100 goto done;
2103 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2104 werr = libnet_join_post_verify(mem_ctx, r);
2105 if (!W_ERROR_IS_OK(werr)) {
2106 libnet_join_rollback(mem_ctx, r);
2110 done:
2111 r->out.result = werr;
2113 if (r->in.debug) {
2114 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
2116 return werr;
2119 /****************************************************************
2120 ****************************************************************/
2122 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
2123 struct libnet_UnjoinCtx *r)
2125 NTSTATUS status;
2127 if (!r->in.domain_sid) {
2128 struct dom_sid sid;
2129 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
2130 libnet_unjoin_set_error_string(mem_ctx, r,
2131 "Unable to fetch domain sid: are we joined?");
2132 return WERR_SETUP_NOT_JOINED;
2134 r->in.domain_sid = dom_sid_dup(mem_ctx, &sid);
2135 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
2138 if (!(r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) &&
2139 !r->in.delete_machine_account) {
2140 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2141 return WERR_OK;
2144 if (!r->in.dc_name) {
2145 struct netr_DsRGetDCNameInfo *info;
2146 const char *dc;
2147 status = dsgetdcname(mem_ctx,
2148 r->in.msg_ctx,
2149 r->in.domain_name,
2150 NULL,
2151 NULL,
2152 DS_DIRECTORY_SERVICE_REQUIRED |
2153 DS_WRITABLE_REQUIRED |
2154 DS_RETURN_DNS_NAME,
2155 &info);
2156 if (!NT_STATUS_IS_OK(status)) {
2157 libnet_unjoin_set_error_string(mem_ctx, r,
2158 "failed to find DC for domain %s",
2159 r->in.domain_name,
2160 get_friendly_nt_error_msg(status));
2161 return WERR_DCNOTFOUND;
2164 dc = strip_hostname(info->dc_unc);
2165 r->in.dc_name = talloc_strdup(mem_ctx, dc);
2166 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
2169 #ifdef HAVE_ADS
2170 /* for net ads leave, try to delete the account. If it works,
2171 no sense in disabling. If it fails, we can still try to
2172 disable it. jmcd */
2174 if (r->in.delete_machine_account) {
2175 ADS_STATUS ads_status;
2176 ads_status = libnet_unjoin_connect_ads(mem_ctx, r);
2177 if (ADS_ERR_OK(ads_status)) {
2178 /* dirty hack */
2179 r->out.dns_domain_name =
2180 talloc_strdup(mem_ctx,
2181 r->in.ads->server.realm);
2182 ads_status =
2183 libnet_unjoin_remove_machine_acct(mem_ctx, r);
2185 if (!ADS_ERR_OK(ads_status)) {
2186 libnet_unjoin_set_error_string(mem_ctx, r,
2187 "failed to remove machine account from AD: %s",
2188 ads_errstr(ads_status));
2189 } else {
2190 r->out.deleted_machine_account = true;
2191 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
2192 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2193 return WERR_OK;
2196 #endif /* HAVE_ADS */
2198 /* The WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE flag really means
2199 "disable". */
2200 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
2201 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
2202 if (!NT_STATUS_IS_OK(status)) {
2203 libnet_unjoin_set_error_string(mem_ctx, r,
2204 "failed to disable machine account via rpc: %s",
2205 get_friendly_nt_error_msg(status));
2206 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2207 return WERR_SETUP_NOT_JOINED;
2209 return ntstatus_to_werror(status);
2212 r->out.disabled_machine_account = true;
2215 /* If disable succeeded or was not requested at all, we
2216 should be getting rid of our end of things */
2218 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
2220 return WERR_OK;
2223 /****************************************************************
2224 ****************************************************************/
2226 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
2227 struct libnet_UnjoinCtx *r)
2229 if (!r->in.domain_name) {
2230 libnet_unjoin_set_error_string(mem_ctx, r,
2231 "No domain name defined");
2232 return WERR_INVALID_PARAM;
2235 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2236 &r->in.domain_name,
2237 &r->in.dc_name)) {
2238 libnet_unjoin_set_error_string(mem_ctx, r,
2239 "Failed to parse domain name");
2240 return WERR_INVALID_PARAM;
2243 if (IS_DC) {
2244 return WERR_SETUP_DOMAIN_CONTROLLER;
2247 if (!secrets_init()) {
2248 libnet_unjoin_set_error_string(mem_ctx, r,
2249 "Unable to open secrets database");
2250 return WERR_CAN_NOT_COMPLETE;
2253 return WERR_OK;
2256 /****************************************************************
2257 ****************************************************************/
2259 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2260 struct libnet_UnjoinCtx *r)
2262 saf_delete(r->out.netbios_domain_name);
2263 saf_delete(r->out.dns_domain_name);
2265 return libnet_unjoin_config(r);
2268 /****************************************************************
2269 ****************************************************************/
2271 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2272 struct libnet_UnjoinCtx *r)
2274 WERROR werr;
2276 if (r->in.debug) {
2277 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2280 werr = libnet_unjoin_pre_processing(mem_ctx, r);
2281 if (!W_ERROR_IS_OK(werr)) {
2282 goto done;
2285 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2286 werr = libnet_DomainUnjoin(mem_ctx, r);
2287 if (!W_ERROR_IS_OK(werr)) {
2288 libnet_unjoin_config(r);
2289 goto done;
2293 werr = libnet_unjoin_post_processing(mem_ctx, r);
2294 if (!W_ERROR_IS_OK(werr)) {
2295 goto done;
2298 done:
2299 r->out.result = werr;
2301 if (r->in.debug) {
2302 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2305 return werr;