2 * Unix SMB/CIFS implementation.
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/>.
22 #include "libnet/libnet.h"
24 /****************************************************************
25 ****************************************************************/
27 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
30 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
31 DEBUG(1,("libnet_Join:\n%s", str)); \
35 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
36 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
37 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
38 LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
40 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
43 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
44 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
48 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
49 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
50 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
51 LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
53 #define W_ERROR_NOT_OK_GOTO_DONE(x) do { \
54 if (!W_ERROR_IS_OK(x)) {\
59 /****************************************************************
60 ****************************************************************/
62 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
63 struct libnet_JoinCtx *r,
64 const char *format, ...)
68 if (r->out.error_string) {
72 va_start(args, format);
73 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
77 /****************************************************************
78 ****************************************************************/
80 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
81 struct libnet_UnjoinCtx *r,
82 const char *format, ...)
86 if (r->out.error_string) {
90 va_start(args, format);
91 r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
97 /****************************************************************
98 ****************************************************************/
100 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
101 const char *netbios_domain_name,
103 const char *user_name,
104 const char *password,
108 ADS_STRUCT *my_ads = NULL;
110 my_ads = ads_init(dns_domain_name,
114 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
118 SAFE_FREE(my_ads->auth.user_name);
119 my_ads->auth.user_name = SMB_STRDUP(user_name);
123 SAFE_FREE(my_ads->auth.password);
124 my_ads->auth.password = SMB_STRDUP(password);
127 status = ads_connect(my_ads);
128 if (!ADS_ERR_OK(status)) {
129 ads_destroy(&my_ads);
137 /****************************************************************
138 ****************************************************************/
140 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
141 struct libnet_JoinCtx *r)
145 status = libnet_connect_ads(r->in.domain_name,
149 r->in.admin_password,
151 if (!ADS_ERR_OK(status)) {
152 libnet_join_set_error_string(mem_ctx, r,
153 "failed to connect to AD: %s",
158 if (!r->out.netbios_domain_name) {
159 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
160 r->in.ads->server.workgroup);
161 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
164 if (!r->out.dns_domain_name) {
165 r->out.dns_domain_name = talloc_strdup(mem_ctx,
166 r->in.ads->config.realm);
167 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
170 r->out.domain_is_ad = true;
175 /****************************************************************
176 ****************************************************************/
178 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
179 struct libnet_UnjoinCtx *r)
183 status = libnet_connect_ads(r->in.domain_name,
187 r->in.admin_password,
189 if (!ADS_ERR_OK(status)) {
190 libnet_unjoin_set_error_string(mem_ctx, r,
191 "failed to connect to AD: %s",
198 /****************************************************************
199 join a domain using ADS (LDAP mods)
200 ****************************************************************/
202 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
203 struct libnet_JoinCtx *r)
206 LDAPMessage *res = NULL;
207 const char *attrs[] = { "dn", NULL };
210 status = ads_check_ou_dn(mem_ctx, r->in.ads, r->in.account_ou);
211 if (!ADS_ERR_OK(status)) {
215 status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
216 if (!ADS_ERR_OK(status)) {
220 if (ads_count_replies(r->in.ads, res) != 1) {
221 ads_msgfree(r->in.ads, res);
222 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
225 ads_msgfree(r->in.ads, res);
227 /* Attempt to create the machine account and bail if this fails.
228 Assume that the admin wants exactly what they requested */
230 status = ads_create_machine_acct(r->in.ads,
234 if (ADS_ERR_OK(status)) {
235 DEBUG(1,("machine account creation created\n"));
237 } else if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
238 (status.err.rc == LDAP_ALREADY_EXISTS)) {
239 status = ADS_SUCCESS;
242 if (!ADS_ERR_OK(status)) {
243 DEBUG(1,("machine account creation failed\n"));
247 status = ads_move_machine_acct(r->in.ads,
251 if (!ADS_ERR_OK(status)) {
252 DEBUG(1,("failure to locate/move pre-existing "
253 "machine account\n"));
257 DEBUG(1,("The machine account %s the specified OU.\n",
258 moved ? "was moved into" : "already exists in"));
263 /****************************************************************
264 ****************************************************************/
266 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
267 struct libnet_UnjoinCtx *r)
272 return libnet_unjoin_connect_ads(mem_ctx, r);
275 status = ads_leave_realm(r->in.ads, r->in.machine_name);
276 if (!ADS_ERR_OK(status)) {
277 libnet_unjoin_set_error_string(mem_ctx, r,
278 "failed to leave realm: %s",
286 /****************************************************************
287 ****************************************************************/
289 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
290 struct libnet_JoinCtx *r)
293 LDAPMessage *res = NULL;
296 if (!r->in.machine_name) {
297 return ADS_ERROR(LDAP_NO_MEMORY);
300 status = ads_find_machine_acct(r->in.ads,
303 if (!ADS_ERR_OK(status)) {
307 if (ads_count_replies(r->in.ads, res) != 1) {
308 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
312 dn = ads_get_dn(r->in.ads, res);
314 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
318 r->out.dn = talloc_strdup(mem_ctx, dn);
320 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
325 ads_msgfree(r->in.ads, res);
326 ads_memfree(r->in.ads, dn);
331 /****************************************************************
332 Set a machines dNSHostName and servicePrincipalName attributes
333 ****************************************************************/
335 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
336 struct libnet_JoinCtx *r)
341 const char *spn_array[3] = {NULL, NULL, NULL};
346 status = libnet_join_find_machine_acct(mem_ctx, r);
347 if (!ADS_ERR_OK(status)) {
351 /* Windows only creates HOST/shortname & HOST/fqdn. */
353 spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
355 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
360 if (name_to_fqdn(my_fqdn, r->in.machine_name) &&
361 !strequal(my_fqdn, r->in.machine_name)) {
364 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
366 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
371 mods = ads_init_mods(mem_ctx);
373 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
376 /* fields of primary importance */
378 status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
379 if (!ADS_ERR_OK(status)) {
380 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
383 status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
385 if (!ADS_ERR_OK(status)) {
386 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
389 return ads_gen_mod(r->in.ads, r->out.dn, mods);
392 /****************************************************************
393 ****************************************************************/
395 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
396 struct libnet_JoinCtx *r)
401 if (!r->in.create_upn) {
407 status = libnet_join_find_machine_acct(mem_ctx, r);
408 if (!ADS_ERR_OK(status)) {
413 r->in.upn = talloc_asprintf(mem_ctx,
416 r->out.dns_domain_name);
418 return ADS_ERROR(LDAP_NO_MEMORY);
422 /* now do the mods */
424 mods = ads_init_mods(mem_ctx);
426 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
429 /* fields of primary importance */
431 status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
432 if (!ADS_ERR_OK(status)) {
433 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
436 return ads_gen_mod(r->in.ads, r->out.dn, mods);
440 /****************************************************************
441 ****************************************************************/
443 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
444 struct libnet_JoinCtx *r)
450 if (!r->in.os_name || !r->in.os_version ) {
456 status = libnet_join_find_machine_acct(mem_ctx, r);
457 if (!ADS_ERR_OK(status)) {
461 /* now do the mods */
463 mods = ads_init_mods(mem_ctx);
465 return ADS_ERROR(LDAP_NO_MEMORY);
468 os_sp = talloc_asprintf(mem_ctx, "Samba %s", SAMBA_VERSION_STRING);
470 return ADS_ERROR(LDAP_NO_MEMORY);
473 /* fields of primary importance */
475 status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
477 if (!ADS_ERR_OK(status)) {
481 status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
483 if (!ADS_ERR_OK(status)) {
487 status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
489 if (!ADS_ERR_OK(status)) {
493 return ads_gen_mod(r->in.ads, r->out.dn, mods);
496 /****************************************************************
497 ****************************************************************/
499 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
500 struct libnet_JoinCtx *r)
502 if (!lp_use_kerberos_keytab()) {
506 if (!ads_keytab_create_default(r->in.ads)) {
513 /****************************************************************
514 ****************************************************************/
516 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
517 struct libnet_JoinCtx *r)
519 uint32_t domain_func;
521 const char *salt = NULL;
522 char *std_salt = NULL;
524 status = ads_domain_func_level(r->in.ads, &domain_func);
525 if (!ADS_ERR_OK(status)) {
526 libnet_join_set_error_string(mem_ctx, r,
527 "failed to determine domain functional level: %s",
532 /* go ahead and setup the default salt */
534 std_salt = kerberos_standard_des_salt();
536 libnet_join_set_error_string(mem_ctx, r,
537 "failed to obtain standard DES salt");
541 salt = talloc_strdup(mem_ctx, std_salt);
548 /* if it's a Windows functional domain, we have to look for the UPN */
550 if (domain_func == DS_DOMAIN_FUNCTION_2000) {
553 upn = ads_get_upn(r->in.ads, mem_ctx,
556 salt = talloc_strdup(mem_ctx, upn);
563 return kerberos_secrets_store_des_salt(salt);
566 /****************************************************************
567 ****************************************************************/
569 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
570 struct libnet_JoinCtx *r)
575 status = libnet_join_connect_ads(mem_ctx, r);
576 if (!ADS_ERR_OK(status)) {
581 status = libnet_join_set_machine_spn(mem_ctx, r);
582 if (!ADS_ERR_OK(status)) {
583 libnet_join_set_error_string(mem_ctx, r,
584 "failed to set machine spn: %s",
589 status = libnet_join_set_os_attributes(mem_ctx, r);
590 if (!ADS_ERR_OK(status)) {
591 libnet_join_set_error_string(mem_ctx, r,
592 "failed to set machine os attributes: %s",
597 status = libnet_join_set_machine_upn(mem_ctx, r);
598 if (!ADS_ERR_OK(status)) {
599 libnet_join_set_error_string(mem_ctx, r,
600 "failed to set machine upn: %s",
605 if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
606 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
609 if (!libnet_join_create_keytab(mem_ctx, r)) {
610 libnet_join_set_error_string(mem_ctx, r,
611 "failed to create kerberos keytab");
612 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
617 #endif /* WITH_ADS */
619 /****************************************************************
620 Store the machine password and domain SID
621 ****************************************************************/
623 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
624 struct libnet_JoinCtx *r)
626 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
629 DEBUG(1,("Failed to save domain sid\n"));
633 if (!secrets_store_machine_password(r->in.machine_password,
634 r->out.netbios_domain_name,
635 r->in.secure_channel_type))
637 DEBUG(1,("Failed to save machine password\n"));
644 /****************************************************************
646 ****************************************************************/
648 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
649 struct libnet_JoinCtx *r)
651 struct cli_state *cli = NULL;
652 struct rpc_pipe_client *pipe_hnd = NULL;
653 POLICY_HND sam_pol, domain_pol, user_pol, lsa_pol;
654 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
656 struct lsa_String lsa_acct_name;
658 uint32_t acct_flags = ACB_WSTRUST;
660 struct MD5Context md5ctx;
662 DATA_BLOB digested_session_key;
663 uchar md4_trust_password[16];
664 union lsa_PolicyInformation *info = NULL;
665 struct samr_Ids user_rids;
666 struct samr_Ids name_types;
667 union samr_UserInfo user_info;
669 if (!r->in.machine_password) {
670 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
671 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
674 status = cli_full_connection(&cli, NULL,
680 r->in.admin_password,
684 if (!NT_STATUS_IS_OK(status)) {
688 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
690 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
695 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
696 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
697 if (!NT_STATUS_IS_OK(status)) {
701 status = rpccli_lsa_QueryInfoPolicy2(pipe_hnd, mem_ctx,
705 if (NT_STATUS_IS_OK(status)) {
706 r->out.domain_is_ad = true;
707 r->out.netbios_domain_name = info->dns.name.string;
708 r->out.dns_domain_name = info->dns.dns_domain.string;
709 r->out.domain_sid = info->dns.sid;
712 if (!NT_STATUS_IS_OK(status)) {
713 status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
715 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
717 if (!NT_STATUS_IS_OK(status)) {
721 r->out.netbios_domain_name = info->account_domain.name.string;
722 r->out.domain_sid = info->account_domain.sid;
725 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
726 cli_rpc_pipe_close(pipe_hnd);
728 /* Open the domain */
730 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
732 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
737 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
738 pipe_hnd->cli->desthost,
739 SEC_RIGHTS_MAXIMUM_ALLOWED,
741 if (!NT_STATUS_IS_OK(status)) {
745 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
747 SEC_RIGHTS_MAXIMUM_ALLOWED,
750 if (!NT_STATUS_IS_OK(status)) {
754 /* Create domain user */
756 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
757 strlower_m(acct_name);
759 init_lsa_String(&lsa_acct_name, acct_name);
761 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
762 uint32_t access_desired =
763 SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
764 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
765 SAMR_USER_ACCESS_SET_PASSWORD |
766 SAMR_USER_ACCESS_GET_ATTRIBUTES |
767 SAMR_USER_ACCESS_SET_ATTRIBUTES;
768 uint32_t access_granted = 0;
770 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
772 DEBUG(10,("Creating account with desired access mask: %d\n",
775 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
783 if (!NT_STATUS_IS_OK(status) &&
784 !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
786 DEBUG(10,("Creation of workstation account failed: %s\n",
789 /* If NT_STATUS_ACCESS_DENIED then we have a valid
790 username/password combo but the user does not have
791 administrator access. */
793 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
794 libnet_join_set_error_string(mem_ctx, r,
795 "User specified does not have "
796 "administrator privileges");
802 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
803 if (!(r->in.join_flags &
804 WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
809 /* We *must* do this.... don't ask... */
811 if (NT_STATUS_IS_OK(status)) {
812 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
816 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
822 if (!NT_STATUS_IS_OK(status)) {
826 if (name_types.ids[0] != SID_NAME_USER) {
827 DEBUG(0,("%s is not a user account (type=%d)\n",
828 acct_name, name_types.ids[0]));
829 status = NT_STATUS_INVALID_WORKSTATION;
833 user_rid = user_rids.ids[0];
835 /* Open handle on user */
837 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
839 SEC_RIGHTS_MAXIMUM_ALLOWED,
842 if (!NT_STATUS_IS_OK(status)) {
846 /* Create a random machine account password and generate the hash */
848 E_md4hash(r->in.machine_password, md4_trust_password);
849 encode_pw_buffer(pwbuf, r->in.machine_password, STR_UNICODE);
851 generate_random_buffer((uint8_t*)md5buffer, sizeof(md5buffer));
852 digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
855 MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
856 MD5Update(&md5ctx, cli->user_session_key.data,
857 cli->user_session_key.length);
858 MD5Final(digested_session_key.data, &md5ctx);
860 SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
861 memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
863 /* Fill in the additional account flags now */
865 acct_flags |= ACB_PWNOEXP;
866 if (r->out.domain_is_ad) {
867 #if !defined(ENCTYPE_ARCFOUR_HMAC)
868 acct_flags |= ACB_USE_DES_KEY_ONLY;
873 /* Set password and account flags on machine account */
875 ZERO_STRUCT(user_info.info25);
877 user_info.info25.info.fields_present = ACCT_NT_PWD_SET |
879 SAMR_FIELD_ACCT_FLAGS;
881 user_info.info25.info.acct_flags = acct_flags;
882 memcpy(&user_info.info25.password.data, pwbuf, sizeof(pwbuf));
884 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
889 if (NT_STATUS_EQUAL(status, NT_STATUS(DCERPC_FAULT_INVALID_TAG))) {
893 encode_pw_buffer(pwbuf2, r->in.machine_password, STR_UNICODE);
895 /* retry with level 24 */
896 init_samr_user_info24(&user_info.info24, pwbuf2, 24);
898 SamOEMhashBlob(user_info.info24.password.data, 516,
899 &cli->user_session_key);
901 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
907 if (!NT_STATUS_IS_OK(status)) {
908 libnet_join_set_error_string(mem_ctx, r,
909 "Failed to set password for machine account (%s)\n",
914 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
915 cli_rpc_pipe_close(pipe_hnd);
917 status = NT_STATUS_OK;
926 /****************************************************************
927 ****************************************************************/
929 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
930 const char *machine_name,
933 uint32_t neg_flags = NETLOGON_NEG_SELECT_AUTH2_FLAGS |
934 NETLOGON_NEG_SCHANNEL;
935 struct cli_state *cli = NULL;
936 struct rpc_pipe_client *pipe_hnd = NULL;
937 struct rpc_pipe_client *netlogon_pipe = NULL;
939 char *machine_password = NULL;
940 char *machine_account = NULL;
943 return NT_STATUS_INVALID_PARAMETER;
946 if (!secrets_init()) {
947 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
950 machine_password = secrets_fetch_machine_password(netbios_domain_name,
952 if (!machine_password) {
953 return NT_STATUS_NO_TRUST_LSA_SECRET;
956 asprintf(&machine_account, "%s$", machine_name);
957 if (!machine_account) {
958 SAFE_FREE(machine_password);
959 return NT_STATUS_NO_MEMORY;
962 status = cli_full_connection(&cli, NULL,
971 free(machine_account);
972 free(machine_password);
974 if (!NT_STATUS_IS_OK(status)) {
975 status = cli_full_connection(&cli, NULL,
986 if (!NT_STATUS_IS_OK(status)) {
990 netlogon_pipe = get_schannel_session_key(cli,
992 &neg_flags, &status);
993 if (!netlogon_pipe) {
994 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
999 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1000 "key from server %s for domain %s. Error was %s\n",
1001 cli->desthost, netbios_domain_name, nt_errstr(status)));
1006 if (!lp_client_schannel()) {
1008 return NT_STATUS_OK;
1011 pipe_hnd = cli_rpc_pipe_open_schannel_with_key(cli, PI_NETLOGON,
1012 PIPE_AUTH_LEVEL_PRIVACY,
1013 netbios_domain_name,
1020 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1021 "on netlogon pipe to server %s for domain %s. "
1023 cli->desthost, netbios_domain_name, nt_errstr(status)));
1027 return NT_STATUS_OK;
1030 /****************************************************************
1031 ****************************************************************/
1033 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1034 struct libnet_JoinCtx *r)
1038 status = libnet_join_ok(r->out.netbios_domain_name,
1041 if (!NT_STATUS_IS_OK(status)) {
1042 libnet_join_set_error_string(mem_ctx, r,
1043 "failed to verify domain membership after joining: %s",
1044 get_friendly_nt_error_msg(status));
1045 return WERR_SETUP_NOT_JOINED;
1051 /****************************************************************
1052 ****************************************************************/
1054 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1055 struct libnet_UnjoinCtx *r)
1057 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1061 if (!secrets_delete_domain_sid(lp_workgroup())) {
1068 /****************************************************************
1069 ****************************************************************/
1071 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1072 struct libnet_UnjoinCtx *r)
1074 struct cli_state *cli = NULL;
1075 struct rpc_pipe_client *pipe_hnd = NULL;
1076 POLICY_HND sam_pol, domain_pol, user_pol;
1077 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1080 struct lsa_String lsa_acct_name;
1081 struct samr_Ids user_rids;
1082 struct samr_Ids name_types;
1083 union samr_UserInfo *info = NULL;
1085 status = cli_full_connection(&cli, NULL,
1089 r->in.admin_account,
1091 r->in.admin_password,
1092 0, Undefined, NULL);
1094 if (!NT_STATUS_IS_OK(status)) {
1098 /* Open the domain */
1100 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
1102 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1103 nt_errstr(status)));
1107 status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1108 pipe_hnd->cli->desthost,
1109 SEC_RIGHTS_MAXIMUM_ALLOWED,
1111 if (!NT_STATUS_IS_OK(status)) {
1115 status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1117 SEC_RIGHTS_MAXIMUM_ALLOWED,
1120 if (!NT_STATUS_IS_OK(status)) {
1124 /* Create domain user */
1126 acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1127 strlower_m(acct_name);
1129 init_lsa_String(&lsa_acct_name, acct_name);
1131 status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1138 if (!NT_STATUS_IS_OK(status)) {
1142 if (name_types.ids[0] != SID_NAME_USER) {
1143 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1144 name_types.ids[0]));
1145 status = NT_STATUS_INVALID_WORKSTATION;
1149 user_rid = user_rids.ids[0];
1151 /* Open handle on user */
1153 status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1155 SEC_RIGHTS_MAXIMUM_ALLOWED,
1158 if (!NT_STATUS_IS_OK(status)) {
1164 status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1168 if (!NT_STATUS_IS_OK(status)) {
1169 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1173 /* now disable and setuser info */
1175 info->info16.acct_flags |= ACB_DISABLED;
1177 status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1182 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1186 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1187 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1188 cli_rpc_pipe_close(pipe_hnd);
1198 /****************************************************************
1199 ****************************************************************/
1201 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1204 struct smbconf_ctx *ctx;
1206 werr = smbconf_init_reg(r, &ctx, NULL);
1207 if (!W_ERROR_IS_OK(werr)) {
1211 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1213 werr = smbconf_set_global_parameter(ctx, "security", "user");
1214 W_ERROR_NOT_OK_GOTO_DONE(werr);
1216 werr = smbconf_set_global_parameter(ctx, "workgroup",
1221 werr = smbconf_set_global_parameter(ctx, "security", "domain");
1222 W_ERROR_NOT_OK_GOTO_DONE(werr);
1224 werr = smbconf_set_global_parameter(ctx, "workgroup",
1225 r->out.netbios_domain_name);
1226 W_ERROR_NOT_OK_GOTO_DONE(werr);
1228 if (r->out.domain_is_ad) {
1229 werr = smbconf_set_global_parameter(ctx, "security", "ads");
1230 W_ERROR_NOT_OK_GOTO_DONE(werr);
1232 werr = smbconf_set_global_parameter(ctx, "realm",
1233 r->out.dns_domain_name);
1234 W_ERROR_NOT_OK_GOTO_DONE(werr);
1238 smbconf_shutdown(ctx);
1242 /****************************************************************
1243 ****************************************************************/
1245 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1247 WERROR werr = WERR_OK;
1248 struct smbconf_ctx *ctx;
1250 werr = smbconf_init_reg(r, &ctx, NULL);
1251 if (!W_ERROR_IS_OK(werr)) {
1255 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1257 werr = smbconf_set_global_parameter(ctx, "security", "user");
1258 W_ERROR_NOT_OK_GOTO_DONE(werr);
1259 smbconf_delete_global_parameter(ctx, "realm");
1263 smbconf_shutdown(ctx);
1267 /****************************************************************
1268 ****************************************************************/
1270 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1274 if (!W_ERROR_IS_OK(r->out.result)) {
1275 return r->out.result;
1278 if (!r->in.modify_config) {
1282 werr = do_join_modify_vals_config(r);
1283 if (!W_ERROR_IS_OK(werr)) {
1287 r->out.modified_config = true;
1288 r->out.result = werr;
1293 /****************************************************************
1294 ****************************************************************/
1296 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1300 if (!W_ERROR_IS_OK(r->out.result)) {
1301 return r->out.result;
1304 if (!r->in.modify_config) {
1308 werr = do_unjoin_modify_vals_config(r);
1309 if (!W_ERROR_IS_OK(werr)) {
1313 r->out.modified_config = true;
1314 r->out.result = werr;
1319 /****************************************************************
1320 ****************************************************************/
1322 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1323 const char *domain_str,
1324 const char **domain_p,
1327 char *domain = NULL;
1329 const char *p = NULL;
1331 if (!domain_str || !domain_p || !dc_p) {
1335 p = strchr_m(domain_str, '\\');
1338 domain = talloc_strndup(mem_ctx, domain_str,
1339 PTR_DIFF(p, domain_str));
1340 dc = talloc_strdup(mem_ctx, p+1);
1345 domain = talloc_strdup(mem_ctx, domain_str);
1361 /****************************************************************
1362 ****************************************************************/
1364 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1365 struct libnet_JoinCtx *r)
1367 if (!r->in.domain_name) {
1368 libnet_join_set_error_string(mem_ctx, r,
1369 "No domain name defined");
1370 return WERR_INVALID_PARAM;
1373 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1376 libnet_join_set_error_string(mem_ctx, r,
1377 "Failed to parse domain name");
1378 return WERR_INVALID_PARAM;
1381 if (r->in.modify_config && !lp_config_backend_is_registry()) {
1382 libnet_join_set_error_string(mem_ctx, r,
1383 "Configuration manipulation requested but not "
1384 "supported by backend");
1385 return WERR_NOT_SUPPORTED;
1389 return WERR_SETUP_DOMAIN_CONTROLLER;
1392 if (!secrets_init()) {
1393 libnet_join_set_error_string(mem_ctx, r,
1394 "Unable to open secrets database");
1395 return WERR_CAN_NOT_COMPLETE;
1401 /****************************************************************
1402 ****************************************************************/
1404 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1405 struct libnet_JoinCtx *r)
1409 if (!W_ERROR_IS_OK(r->out.result)) {
1410 return r->out.result;
1413 werr = do_JoinConfig(r);
1414 if (!W_ERROR_IS_OK(werr)) {
1418 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1419 saf_store(r->in.domain_name, r->in.dc_name);
1425 /****************************************************************
1426 ****************************************************************/
1428 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1431 ads_destroy(&r->in.ads);
1437 /****************************************************************
1438 ****************************************************************/
1440 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1443 ads_destroy(&r->in.ads);
1449 /****************************************************************
1450 ****************************************************************/
1452 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1453 struct libnet_JoinCtx **r)
1455 struct libnet_JoinCtx *ctx;
1457 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1462 talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1464 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1465 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1467 ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1474 /****************************************************************
1475 ****************************************************************/
1477 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1478 struct libnet_UnjoinCtx **r)
1480 struct libnet_UnjoinCtx *ctx;
1482 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1487 talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1489 ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1490 W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1497 /****************************************************************
1498 ****************************************************************/
1500 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1501 struct libnet_JoinCtx *r)
1505 ADS_STATUS ads_status;
1506 #endif /* WITH_ADS */
1508 if (!r->in.dc_name) {
1509 struct netr_DsRGetDCNameInfo *info;
1510 status = dsgetdcname(mem_ctx,
1514 DS_DIRECTORY_SERVICE_REQUIRED |
1515 DS_WRITABLE_REQUIRED |
1518 if (!NT_STATUS_IS_OK(status)) {
1519 libnet_join_set_error_string(mem_ctx, r,
1520 "failed to find DC for domain %s",
1522 get_friendly_nt_error_msg(status));
1523 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1526 r->in.dc_name = talloc_strdup(mem_ctx,
1528 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1532 if (r->in.account_ou) {
1534 ads_status = libnet_join_connect_ads(mem_ctx, r);
1535 if (!ADS_ERR_OK(ads_status)) {
1536 return WERR_DEFAULT_JOIN_REQUIRED;
1539 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1540 if (!ADS_ERR_OK(ads_status)) {
1541 libnet_join_set_error_string(mem_ctx, r,
1542 "failed to precreate account in ou %s: %s",
1544 ads_errstr(ads_status));
1545 return WERR_DEFAULT_JOIN_REQUIRED;
1548 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1550 #endif /* WITH_ADS */
1552 status = libnet_join_joindomain_rpc(mem_ctx, r);
1553 if (!NT_STATUS_IS_OK(status)) {
1554 libnet_join_set_error_string(mem_ctx, r,
1555 "failed to join domain over rpc: %s",
1556 get_friendly_nt_error_msg(status));
1557 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1558 return WERR_SETUP_ALREADY_JOINED;
1560 return ntstatus_to_werror(status);
1563 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1564 return WERR_SETUP_NOT_JOINED;
1568 if (r->out.domain_is_ad) {
1569 ads_status = libnet_join_post_processing_ads(mem_ctx, r);
1570 if (!ADS_ERR_OK(ads_status)) {
1571 return WERR_GENERAL_FAILURE;
1574 #endif /* WITH_ADS */
1579 /****************************************************************
1580 ****************************************************************/
1582 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1583 struct libnet_JoinCtx *r)
1588 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1591 werr = libnet_join_pre_processing(mem_ctx, r);
1592 if (!W_ERROR_IS_OK(werr)) {
1596 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1597 werr = libnet_DomainJoin(mem_ctx, r);
1598 if (!W_ERROR_IS_OK(werr)) {
1602 werr = libnet_join_post_verify(mem_ctx, r);
1603 if (!W_ERROR_IS_OK(werr)) {
1608 werr = libnet_join_post_processing(mem_ctx, r);
1609 if (!W_ERROR_IS_OK(werr)) {
1613 r->out.result = werr;
1616 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1621 /****************************************************************
1622 ****************************************************************/
1624 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1625 struct libnet_UnjoinCtx *r)
1629 if (!r->in.domain_sid) {
1631 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1632 libnet_unjoin_set_error_string(mem_ctx, r,
1633 "Unable to fetch domain sid: are we joined?");
1634 return WERR_SETUP_NOT_JOINED;
1636 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
1637 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
1640 if (!r->in.dc_name) {
1641 struct netr_DsRGetDCNameInfo *info;
1642 status = dsgetdcname(mem_ctx,
1646 DS_DIRECTORY_SERVICE_REQUIRED |
1647 DS_WRITABLE_REQUIRED |
1650 if (!NT_STATUS_IS_OK(status)) {
1651 libnet_unjoin_set_error_string(mem_ctx, r,
1652 "failed to find DC for domain %s",
1654 get_friendly_nt_error_msg(status));
1655 return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1658 r->in.dc_name = talloc_strdup(mem_ctx,
1660 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1663 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
1664 if (!NT_STATUS_IS_OK(status)) {
1665 libnet_unjoin_set_error_string(mem_ctx, r,
1666 "failed to disable machine account via rpc: %s",
1667 get_friendly_nt_error_msg(status));
1668 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1669 return WERR_SETUP_NOT_JOINED;
1671 return ntstatus_to_werror(status);
1674 r->out.disabled_machine_account = true;
1677 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
1678 ADS_STATUS ads_status;
1679 libnet_unjoin_connect_ads(mem_ctx, r);
1680 ads_status = libnet_unjoin_remove_machine_acct(mem_ctx, r);
1681 if (!ADS_ERR_OK(ads_status)) {
1682 libnet_unjoin_set_error_string(mem_ctx, r,
1683 "failed to remove machine account from AD: %s",
1684 ads_errstr(ads_status));
1686 r->out.deleted_machine_account = true;
1688 r->out.dns_domain_name = talloc_strdup(mem_ctx,
1689 r->in.ads->server.realm);
1690 W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
1693 #endif /* WITH_ADS */
1695 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
1700 /****************************************************************
1701 ****************************************************************/
1703 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
1704 struct libnet_UnjoinCtx *r)
1706 if (!r->in.domain_name) {
1707 libnet_unjoin_set_error_string(mem_ctx, r,
1708 "No domain name defined");
1709 return WERR_INVALID_PARAM;
1712 if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1715 libnet_unjoin_set_error_string(mem_ctx, r,
1716 "Failed to parse domain name");
1717 return WERR_INVALID_PARAM;
1720 if (r->in.modify_config && !lp_config_backend_is_registry()) {
1721 libnet_unjoin_set_error_string(mem_ctx, r,
1722 "Configuration manipulation requested but not "
1723 "supported by backend");
1724 return WERR_NOT_SUPPORTED;
1728 return WERR_SETUP_DOMAIN_CONTROLLER;
1731 if (!secrets_init()) {
1732 libnet_unjoin_set_error_string(mem_ctx, r,
1733 "Unable to open secrets database");
1734 return WERR_CAN_NOT_COMPLETE;
1740 /****************************************************************
1741 ****************************************************************/
1743 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
1744 struct libnet_UnjoinCtx *r)
1746 saf_delete(r->out.netbios_domain_name);
1747 saf_delete(r->out.dns_domain_name);
1749 return libnet_unjoin_config(r);
1752 /****************************************************************
1753 ****************************************************************/
1755 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
1756 struct libnet_UnjoinCtx *r)
1761 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
1764 werr = libnet_unjoin_pre_processing(mem_ctx, r);
1765 if (!W_ERROR_IS_OK(werr)) {
1769 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1770 werr = libnet_DomainUnjoin(mem_ctx, r);
1771 if (!W_ERROR_IS_OK(werr)) {
1772 libnet_unjoin_config(r);
1777 werr = libnet_unjoin_post_processing(mem_ctx, r);
1778 if (!W_ERROR_IS_OK(werr)) {
1783 r->out.result = werr;
1786 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);