2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Gerald (Jerry) Carter 2006
5 * Copyright (C) Guenther Deschner 2007
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 NTSTATUS do_DomainJoin(TALLOC_CTX *mem_ctx,
26 struct libnet_JoinCtx *r)
28 struct cli_state *cli = NULL;
29 struct rpc_pipe_client *pipe_hnd = NULL;
30 const char *password = NULL;
31 POLICY_HND sam_pol, domain_pol, user_pol, lsa_pol;
32 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
34 const char *const_acct_name;
36 uint32 num_rids, *name_types, *user_rids;
38 uint32 acb_info = ACB_WSTRUST;
39 uint32 fields_present;
43 const int infolevel = 25;
44 struct MD5Context md5ctx;
46 DATA_BLOB digested_session_key;
47 uchar md4_trust_password[16];
49 password = talloc_strdup(mem_ctx,
50 generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
51 NT_STATUS_HAVE_NO_MEMORY(password);
53 status = cli_full_connection(&cli, NULL, r->in.server_name,
57 NULL, //r->in.domain_name,
61 if (!NT_STATUS_IS_OK(status)) {
65 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
70 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
71 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
72 if (!NT_STATUS_IS_OK(status)) {
76 status = rpccli_lsa_query_info_policy2(pipe_hnd, mem_ctx, &lsa_pol,
78 &r->out.netbios_domain_name,
79 &r->out.dns_domain_name,
84 if (!NT_STATUS_IS_OK(status)) {
85 status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &lsa_pol,
87 &r->out.netbios_domain_name,
89 if (!NT_STATUS_IS_OK(status)) {
94 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
95 cli_rpc_pipe_close(pipe_hnd);
97 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
102 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
103 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
104 if (!NT_STATUS_IS_OK(status)) {
108 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
109 SEC_RIGHTS_MAXIMUM_ALLOWED,
112 if (!NT_STATUS_IS_OK(status)) {
116 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
117 strlower_m(acct_name);
118 const_acct_name = acct_name;
120 status = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
121 acct_name, ACB_WSTRUST,
122 0xe005000b, &user_pol, &user_rid);
123 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
124 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
129 if (NT_STATUS_IS_OK(status)) {
130 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
133 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
134 &domain_pol, flags, 1,
136 &num_rids, &user_rids, &name_types);
137 if (!NT_STATUS_IS_OK(status)) {
141 if (name_types[0] != SID_NAME_USER) {
142 status = NT_STATUS_INVALID_WORKSTATION;
146 user_rid = user_rids[0];
148 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
149 SEC_RIGHTS_MAXIMUM_ALLOWED, user_rid,
151 if (!NT_STATUS_IS_OK(status)) {
155 E_md4hash(r->in.password, md4_trust_password);
156 encode_pw_buffer(pwbuf, r->in.password, STR_UNICODE);
158 generate_random_buffer((uint8*)md5buffer, sizeof(md5buffer));
159 digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
162 MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
163 MD5Update(&md5ctx, cli->user_session_key.data, cli->user_session_key.length);
164 MD5Final(digested_session_key.data, &md5ctx);
166 SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
167 memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
169 acb_info |= ACB_PWNOEXP;
171 if ( dom_type == ND_TYPE_AD ) {
172 #if !defined(ENCTYPE_ARCFOUR_HMAC)
173 acb_info |= ACB_USE_DES_KEY_ONLY;
181 fields_present = ACCT_NT_PWD_SET | ACCT_LM_PWD_SET | ACCT_FLAGS;
182 init_sam_user_info25P(&p25, fields_present, acb_info, (char *)pwbuf);
184 ctr.switch_value = infolevel;
185 ctr.info.id25 = &p25;
187 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol,
188 infolevel, &cli->user_session_key,
190 if (!NT_STATUS_IS_OK(status)) {
194 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
195 cli_rpc_pipe_close(pipe_hnd);
197 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
200 status = NT_STATUS_INTERNAL_DB_ERROR;
204 if (!secrets_store_machine_password(password,
205 r->out.netbios_domain_name,
208 status = NT_STATUS_INTERNAL_DB_ERROR;
212 status = NT_STATUS_OK;
221 static NTSTATUS do_DomainUnjoin(TALLOC_CTX *mem_ctx,
222 struct libnet_UnjoinCtx *r)
224 struct cli_state *cli = NULL;
225 struct rpc_pipe_client *pipe_hnd = NULL;
226 POLICY_HND sam_pol, domain_pol, user_pol;
227 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
229 uint32 flags = 0x3e8;
230 const char *const_acct_name;
232 uint32 num_rids, *name_types, *user_rids;
233 SAM_USERINFO_CTR ctr, *qctr = NULL;
234 SAM_USER_INFO_16 p16;
236 status = cli_full_connection(&cli, NULL, r->in.server_name,
240 NULL, //r->in.domain_name,
244 if (!NT_STATUS_IS_OK(status)) {
248 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
253 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
254 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
255 if (!NT_STATUS_IS_OK(status)) {
259 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
260 SEC_RIGHTS_MAXIMUM_ALLOWED,
263 if (!NT_STATUS_IS_OK(status)) {
267 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
268 strlower_m(acct_name);
269 const_acct_name = acct_name;
271 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
272 &domain_pol, flags, 1,
274 &num_rids, &user_rids, &name_types);
275 if (!NT_STATUS_IS_OK(status)) {
279 if (name_types[0] != SID_NAME_USER) {
280 status = NT_STATUS_INVALID_WORKSTATION;
284 user_rid = user_rids[0];
286 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
287 SEC_RIGHTS_MAXIMUM_ALLOWED,
288 user_rid, &user_pol);
289 if (!NT_STATUS_IS_OK(status)) {
293 status = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx,
294 &user_pol, 16, &qctr);
295 if (!NT_STATUS_IS_OK(status)) {
296 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
301 ctr.switch_value = 16;
302 ctr.info.id16 = &p16;
304 p16.acb_info = qctr->info.id16->acb_info | ACB_DISABLED;
306 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol, 16,
307 &cli->user_session_key, &ctr);
309 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
311 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
312 status = NT_STATUS_INTERNAL_DB_ERROR;
316 if (!secrets_delete_domain_sid(lp_workgroup())) {
317 status = NT_STATUS_INTERNAL_DB_ERROR;
322 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
323 rpccli_samr_close(pipe_hnd, mem_ctx, &sam_pol);
325 cli_rpc_pipe_close(pipe_hnd);
334 static WERROR do_modify_val_config(struct registry_key *key,
335 const char *val_name,
336 const char *val_data)
338 struct registry_value val;
343 val.v.sz.str = CONST_DISCARD(char *, val_data);
344 val.v.sz.len = strlen(val_data) + 1;
346 return reg_setvalue(key, val_name, &val);
349 static WERROR do_join_modify_vals_config(TALLOC_CTX *mem_ctx,
350 struct libnet_JoinCtx *r,
351 struct registry_key *key)
356 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
358 werr = do_modify_val_config(key, "security", "user");
359 W_ERROR_NOT_OK_RETURN(werr);
361 werr = do_modify_val_config(key, "workgroup",
366 if (r->out.dns_domain_name) {
370 werr = do_modify_val_config(key, "security", "domain");
371 W_ERROR_NOT_OK_RETURN(werr);
373 werr = do_modify_val_config(key, "workgroup",
374 r->out.netbios_domain_name);
375 W_ERROR_NOT_OK_RETURN(werr);
378 werr = do_modify_val_config(key, "security", "ads");
379 W_ERROR_NOT_OK_RETURN(werr);
381 werr = do_modify_val_config(key, "realm",
382 r->out.dns_domain_name);
383 W_ERROR_NOT_OK_RETURN(werr);
389 static WERROR do_unjoin_modify_vals_config(TALLOC_CTX *mem_ctx,
390 struct libnet_UnjoinCtx *r,
391 struct registry_key *key)
393 WERROR werr = WERR_OK;
395 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
397 werr = do_modify_val_config(key, "security", "user");
398 W_ERROR_NOT_OK_RETURN(werr);
401 werr = libnet_smbconf_delparm(mem_ctx, "GLOBAL", "realm");
407 static WERROR do_JoinConfig(TALLOC_CTX *mem_ctx,
408 struct libnet_JoinCtx *r)
411 struct registry_key *key = NULL;
413 if (!W_ERROR_IS_OK(r->out.result)) {
414 return r->out.result;
417 if (!r->in.modify_config) {
421 if (!registry_init_regdb()) {
422 return WERR_REG_IO_FAILURE;
425 if (!libnet_smbconf_key_exists(mem_ctx, GLOBAL_NAME)) {
426 werr = libnet_reg_createkey_internal(mem_ctx,
429 werr = libnet_smbconf_open_path(mem_ctx,
431 REG_KEY_WRITE, &key);
433 if (!W_ERROR_IS_OK(werr)) {
437 werr = do_join_modify_vals_config(mem_ctx, r, key);
438 if (!W_ERROR_IS_OK(werr)) {
442 r->out.modified_config = true;
443 r->out.result = werr;
448 static WERROR do_UnjoinConfig(TALLOC_CTX *mem_ctx,
449 struct libnet_UnjoinCtx *r)
452 struct registry_key *key = NULL;
454 if (!W_ERROR_IS_OK(r->out.result)) {
455 return r->out.result;
458 if (!r->in.modify_config) {
462 if (!registry_init_regdb()) {
463 return WERR_REG_IO_FAILURE;
466 if (!libnet_smbconf_key_exists(mem_ctx, GLOBAL_NAME)) {
467 werr = libnet_reg_createkey_internal(mem_ctx,
470 werr = libnet_smbconf_open_path(mem_ctx,
472 REG_KEY_WRITE, &key);
474 if (!W_ERROR_IS_OK(werr)) {
478 werr = do_unjoin_modify_vals_config(mem_ctx, r, key);
479 if (!W_ERROR_IS_OK(werr)) {
483 r->out.modified_config = true;
484 r->out.result = werr;
489 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
490 struct libnet_JoinCtx **r)
492 struct libnet_JoinCtx *ctx;
494 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
504 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
505 struct libnet_UnjoinCtx **r)
507 struct libnet_UnjoinCtx *ctx;
509 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
519 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
520 struct libnet_JoinCtx *r)
525 if (!r->in.domain_name) {
526 return WERR_INVALID_PARAM;
529 if (r->in.modify_config && !lp_include_registry_globals()) {
530 return WERR_NOT_SUPPORTED;
534 return WERR_SETUP_DOMAIN_CONTROLLER;
537 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
539 status = do_DomainJoin(mem_ctx, r);
540 if (!NT_STATUS_IS_OK(status)) {
541 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
542 return WERR_SETUP_ALREADY_JOINED;
544 return ntstatus_to_werror(status);
548 werr = do_JoinConfig(mem_ctx, r);
549 if (!W_ERROR_IS_OK(werr)) {
556 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
557 struct libnet_UnjoinCtx *r)
562 if (r->in.modify_config && !lp_include_registry_globals()) {
563 return WERR_NOT_SUPPORTED;
566 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
568 status = do_DomainUnjoin(mem_ctx, r);
569 if (!NT_STATUS_IS_OK(status)) {
570 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
571 return WERR_SETUP_NOT_JOINED;
573 return ntstatus_to_werror(status);
577 werr = do_UnjoinConfig(mem_ctx, r);
578 if (!W_ERROR_IS_OK(werr)) {