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_join.h"
23 #include "libnet/libnet_proto.h"
25 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
26 struct libnet_JoinCtx *r)
28 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
34 if (!secrets_store_machine_password(r->in.machine_password,
35 r->out.netbios_domain_name,
44 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
45 struct libnet_JoinCtx *r)
47 struct cli_state *cli = NULL;
48 struct rpc_pipe_client *pipe_hnd = NULL;
49 POLICY_HND sam_pol, domain_pol, user_pol, lsa_pol;
50 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
52 const char *const_acct_name;
54 uint32 num_rids, *name_types, *user_rids;
56 uint32 acb_info = ACB_WSTRUST;
57 uint32 fields_present;
61 const int infolevel = 25;
62 struct MD5Context md5ctx;
64 DATA_BLOB digested_session_key;
65 uchar md4_trust_password[16];
67 if (!r->in.machine_password) {
68 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
69 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
72 status = cli_full_connection(&cli, NULL,
82 if (!NT_STATUS_IS_OK(status)) {
86 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
91 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
92 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
93 if (!NT_STATUS_IS_OK(status)) {
97 status = rpccli_lsa_query_info_policy2(pipe_hnd, mem_ctx, &lsa_pol,
99 &r->out.netbios_domain_name,
100 &r->out.dns_domain_name,
105 if (!NT_STATUS_IS_OK(status)) {
106 status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &lsa_pol,
108 &r->out.netbios_domain_name,
110 if (!NT_STATUS_IS_OK(status)) {
115 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
116 cli_rpc_pipe_close(pipe_hnd);
118 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
123 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
124 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
125 if (!NT_STATUS_IS_OK(status)) {
129 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
130 SEC_RIGHTS_MAXIMUM_ALLOWED,
133 if (!NT_STATUS_IS_OK(status)) {
137 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
138 strlower_m(acct_name);
139 const_acct_name = acct_name;
141 status = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
142 acct_name, ACB_WSTRUST,
143 0xe005000b, &user_pol, &user_rid);
144 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
145 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
150 if (NT_STATUS_IS_OK(status)) {
151 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
154 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
155 &domain_pol, flags, 1,
157 &num_rids, &user_rids, &name_types);
158 if (!NT_STATUS_IS_OK(status)) {
162 if (name_types[0] != SID_NAME_USER) {
163 status = NT_STATUS_INVALID_WORKSTATION;
167 user_rid = user_rids[0];
169 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
170 SEC_RIGHTS_MAXIMUM_ALLOWED, user_rid,
172 if (!NT_STATUS_IS_OK(status)) {
176 E_md4hash(r->in.machine_password, md4_trust_password);
177 encode_pw_buffer(pwbuf, r->in.machine_password, STR_UNICODE);
179 generate_random_buffer((uint8*)md5buffer, sizeof(md5buffer));
180 digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
183 MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
184 MD5Update(&md5ctx, cli->user_session_key.data,
185 cli->user_session_key.length);
186 MD5Final(digested_session_key.data, &md5ctx);
188 SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
189 memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
191 acb_info |= ACB_PWNOEXP;
193 if ( dom_type == ND_TYPE_AD ) {
194 #if !defined(ENCTYPE_ARCFOUR_HMAC)
195 acb_info |= ACB_USE_DES_KEY_ONLY;
203 fields_present = ACCT_NT_PWD_SET | ACCT_LM_PWD_SET | ACCT_FLAGS;
204 init_sam_user_info25P(&p25, fields_present, acb_info, (char *)pwbuf);
206 ctr.switch_value = infolevel;
207 ctr.info.id25 = &p25;
209 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol,
210 infolevel, &cli->user_session_key,
212 if (!NT_STATUS_IS_OK(status)) {
216 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
217 cli_rpc_pipe_close(pipe_hnd);
219 status = NT_STATUS_OK;
228 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
229 struct libnet_UnjoinCtx *r)
231 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
235 if (!secrets_delete_domain_sid(lp_workgroup())) {
242 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
243 struct libnet_UnjoinCtx *r)
245 struct cli_state *cli = NULL;
246 struct rpc_pipe_client *pipe_hnd = NULL;
247 POLICY_HND sam_pol, domain_pol, user_pol;
248 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
250 uint32 flags = 0x3e8;
251 const char *const_acct_name;
253 uint32 num_rids, *name_types, *user_rids;
254 SAM_USERINFO_CTR ctr, *qctr = NULL;
255 SAM_USER_INFO_16 p16;
257 status = cli_full_connection(&cli, NULL,
263 r->in.admin_password,
266 if (!NT_STATUS_IS_OK(status)) {
270 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
275 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
276 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
277 if (!NT_STATUS_IS_OK(status)) {
281 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
282 SEC_RIGHTS_MAXIMUM_ALLOWED,
285 if (!NT_STATUS_IS_OK(status)) {
289 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
290 strlower_m(acct_name);
291 const_acct_name = acct_name;
293 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
294 &domain_pol, flags, 1,
296 &num_rids, &user_rids, &name_types);
297 if (!NT_STATUS_IS_OK(status)) {
301 if (name_types[0] != SID_NAME_USER) {
302 status = NT_STATUS_INVALID_WORKSTATION;
306 user_rid = user_rids[0];
308 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
309 SEC_RIGHTS_MAXIMUM_ALLOWED,
310 user_rid, &user_pol);
311 if (!NT_STATUS_IS_OK(status)) {
315 status = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx,
316 &user_pol, 16, &qctr);
317 if (!NT_STATUS_IS_OK(status)) {
318 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
323 ctr.switch_value = 16;
324 ctr.info.id16 = &p16;
326 p16.acb_info = qctr->info.id16->acb_info | ACB_DISABLED;
328 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol, 16,
329 &cli->user_session_key, &ctr);
331 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
335 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
336 rpccli_samr_close(pipe_hnd, mem_ctx, &sam_pol);
337 cli_rpc_pipe_close(pipe_hnd);
347 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
352 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
354 werr = libnet_conf_set_global_parameter("security", "user");
355 W_ERROR_NOT_OK_RETURN(werr);
357 werr = libnet_conf_set_global_parameter("workgroup",
362 if (r->out.dns_domain_name) {
366 werr = libnet_conf_set_global_parameter("security", "domain");
367 W_ERROR_NOT_OK_RETURN(werr);
369 werr = libnet_conf_set_global_parameter("workgroup",
370 r->out.netbios_domain_name);
371 W_ERROR_NOT_OK_RETURN(werr);
374 werr = libnet_conf_set_global_parameter("security", "ads");
375 W_ERROR_NOT_OK_RETURN(werr);
377 werr = libnet_conf_set_global_parameter("realm",
378 r->out.dns_domain_name);
379 W_ERROR_NOT_OK_RETURN(werr);
385 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
387 WERROR werr = WERR_OK;
389 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
391 werr = libnet_conf_set_global_parameter("security", "user");
392 W_ERROR_NOT_OK_RETURN(werr);
395 werr = libnet_conf_delete_parameter(GLOBAL_NAME, "realm");
401 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
405 if (!W_ERROR_IS_OK(r->out.result)) {
406 return r->out.result;
409 if (!r->in.modify_config) {
413 werr = do_join_modify_vals_config(r);
414 if (!W_ERROR_IS_OK(werr)) {
418 r->out.modified_config = true;
419 r->out.result = werr;
424 static WERROR do_UnjoinConfig(struct libnet_UnjoinCtx *r)
428 if (!W_ERROR_IS_OK(r->out.result)) {
429 return r->out.result;
432 if (!r->in.modify_config) {
436 werr = do_unjoin_modify_vals_config(r);
437 if (!W_ERROR_IS_OK(werr)) {
441 r->out.modified_config = true;
442 r->out.result = werr;
447 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
448 struct libnet_JoinCtx **r)
450 struct libnet_JoinCtx *ctx;
452 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
462 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
463 struct libnet_UnjoinCtx **r)
465 struct libnet_UnjoinCtx *ctx;
467 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
477 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
478 struct libnet_JoinCtx *r)
483 if (!r->in.domain_name) {
484 return WERR_INVALID_PARAM;
487 if (r->in.modify_config && !lp_include_registry_globals()) {
488 return WERR_NOT_SUPPORTED;
492 return WERR_SETUP_DOMAIN_CONTROLLER;
495 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
497 status = libnet_join_joindomain_rpc(mem_ctx, r);
498 if (!NT_STATUS_IS_OK(status)) {
499 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
500 return WERR_SETUP_ALREADY_JOINED;
502 return ntstatus_to_werror(status);
505 if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
506 return WERR_SETUP_NOT_JOINED;
510 werr = do_JoinConfig(r);
511 if (!W_ERROR_IS_OK(werr)) {
518 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
519 struct libnet_UnjoinCtx *r)
524 if (r->in.modify_config && !lp_include_registry_globals()) {
525 return WERR_NOT_SUPPORTED;
528 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
530 status = libnet_join_unjoindomain_rpc(mem_ctx, r);
531 if (!NT_STATUS_IS_OK(status)) {
532 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
533 return WERR_SETUP_NOT_JOINED;
535 return ntstatus_to_werror(status);
538 libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
541 werr = do_UnjoinConfig(r);
542 if (!W_ERROR_IS_OK(werr)) {