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 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 POLICY_HND sam_pol, domain_pol, user_pol, lsa_pol;
31 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
33 const char *const_acct_name;
35 uint32 num_rids, *name_types, *user_rids;
37 uint32 acb_info = ACB_WSTRUST;
38 uint32 fields_present;
42 const int infolevel = 25;
43 struct MD5Context md5ctx;
45 DATA_BLOB digested_session_key;
46 uchar md4_trust_password[16];
48 if (!r->in.machine_password) {
49 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
50 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
53 status = cli_full_connection(&cli, NULL, r->in.server_name,
62 if (!NT_STATUS_IS_OK(status)) {
66 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
71 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
72 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
73 if (!NT_STATUS_IS_OK(status)) {
77 status = rpccli_lsa_query_info_policy2(pipe_hnd, mem_ctx, &lsa_pol,
79 &r->out.netbios_domain_name,
80 &r->out.dns_domain_name,
85 if (!NT_STATUS_IS_OK(status)) {
86 status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &lsa_pol,
88 &r->out.netbios_domain_name,
90 if (!NT_STATUS_IS_OK(status)) {
95 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
96 cli_rpc_pipe_close(pipe_hnd);
98 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
103 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
104 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
105 if (!NT_STATUS_IS_OK(status)) {
109 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
110 SEC_RIGHTS_MAXIMUM_ALLOWED,
113 if (!NT_STATUS_IS_OK(status)) {
117 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
118 strlower_m(acct_name);
119 const_acct_name = acct_name;
121 status = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
122 acct_name, ACB_WSTRUST,
123 0xe005000b, &user_pol, &user_rid);
124 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
125 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
130 if (NT_STATUS_IS_OK(status)) {
131 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
134 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
135 &domain_pol, flags, 1,
137 &num_rids, &user_rids, &name_types);
138 if (!NT_STATUS_IS_OK(status)) {
142 if (name_types[0] != SID_NAME_USER) {
143 status = NT_STATUS_INVALID_WORKSTATION;
147 user_rid = user_rids[0];
149 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
150 SEC_RIGHTS_MAXIMUM_ALLOWED, user_rid,
152 if (!NT_STATUS_IS_OK(status)) {
156 E_md4hash(r->in.machine_password, md4_trust_password);
157 encode_pw_buffer(pwbuf, r->in.machine_password, STR_UNICODE);
159 generate_random_buffer((uint8*)md5buffer, sizeof(md5buffer));
160 digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
163 MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
164 MD5Update(&md5ctx, cli->user_session_key.data,
165 cli->user_session_key.length);
166 MD5Final(digested_session_key.data, &md5ctx);
168 SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
169 memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
171 acb_info |= ACB_PWNOEXP;
173 if ( dom_type == ND_TYPE_AD ) {
174 #if !defined(ENCTYPE_ARCFOUR_HMAC)
175 acb_info |= ACB_USE_DES_KEY_ONLY;
183 fields_present = ACCT_NT_PWD_SET | ACCT_LM_PWD_SET | ACCT_FLAGS;
184 init_sam_user_info25P(&p25, fields_present, acb_info, (char *)pwbuf);
186 ctr.switch_value = infolevel;
187 ctr.info.id25 = &p25;
189 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol,
190 infolevel, &cli->user_session_key,
192 if (!NT_STATUS_IS_OK(status)) {
196 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
197 cli_rpc_pipe_close(pipe_hnd);
199 if (!secrets_store_domain_sid(r->out.netbios_domain_name,
202 status = NT_STATUS_INTERNAL_DB_ERROR;
206 if (!secrets_store_machine_password(password,
207 r->out.netbios_domain_name,
210 status = NT_STATUS_INTERNAL_DB_ERROR;
214 status = NT_STATUS_OK;
223 static NTSTATUS do_DomainUnjoin(TALLOC_CTX *mem_ctx,
224 struct libnet_UnjoinCtx *r)
226 struct cli_state *cli = NULL;
227 struct rpc_pipe_client *pipe_hnd = NULL;
228 POLICY_HND sam_pol, domain_pol, user_pol;
229 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
231 uint32 flags = 0x3e8;
232 const char *const_acct_name;
234 uint32 num_rids, *name_types, *user_rids;
235 SAM_USERINFO_CTR ctr, *qctr = NULL;
236 SAM_USER_INFO_16 p16;
238 status = cli_full_connection(&cli, NULL, r->in.server_name,
243 r->in.admin_password,
246 if (!NT_STATUS_IS_OK(status)) {
250 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
255 status = rpccli_samr_connect(pipe_hnd, mem_ctx,
256 SEC_RIGHTS_MAXIMUM_ALLOWED, &sam_pol);
257 if (!NT_STATUS_IS_OK(status)) {
261 status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &sam_pol,
262 SEC_RIGHTS_MAXIMUM_ALLOWED,
265 if (!NT_STATUS_IS_OK(status)) {
269 acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
270 strlower_m(acct_name);
271 const_acct_name = acct_name;
273 status = rpccli_samr_lookup_names(pipe_hnd, mem_ctx,
274 &domain_pol, flags, 1,
276 &num_rids, &user_rids, &name_types);
277 if (!NT_STATUS_IS_OK(status)) {
281 if (name_types[0] != SID_NAME_USER) {
282 status = NT_STATUS_INVALID_WORKSTATION;
286 user_rid = user_rids[0];
288 status = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
289 SEC_RIGHTS_MAXIMUM_ALLOWED,
290 user_rid, &user_pol);
291 if (!NT_STATUS_IS_OK(status)) {
295 status = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx,
296 &user_pol, 16, &qctr);
297 if (!NT_STATUS_IS_OK(status)) {
298 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
303 ctr.switch_value = 16;
304 ctr.info.id16 = &p16;
306 p16.acb_info = qctr->info.id16->acb_info | ACB_DISABLED;
308 status = rpccli_samr_set_userinfo2(pipe_hnd, mem_ctx, &user_pol, 16,
309 &cli->user_session_key, &ctr);
311 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
313 if (!secrets_delete_machine_password_ex(lp_workgroup())) {
314 status = NT_STATUS_INTERNAL_DB_ERROR;
318 if (!secrets_delete_domain_sid(lp_workgroup())) {
319 status = NT_STATUS_INTERNAL_DB_ERROR;
325 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
326 rpccli_samr_close(pipe_hnd, mem_ctx, &sam_pol);
327 cli_rpc_pipe_close(pipe_hnd);
337 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
342 if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
344 werr = libnet_conf_set_global_parameter("security", "user");
345 W_ERROR_NOT_OK_RETURN(werr);
347 werr = libnet_conf_set_global_parameter("workgroup",
352 if (r->out.dns_domain_name) {
356 werr = libnet_conf_set_global_parameter("security", "domain");
357 W_ERROR_NOT_OK_RETURN(werr);
359 werr = libnet_conf_set_global_parameter("workgroup",
360 r->out.netbios_domain_name);
361 W_ERROR_NOT_OK_RETURN(werr);
364 werr = libnet_conf_set_global_parameter("security", "ads");
365 W_ERROR_NOT_OK_RETURN(werr);
367 werr = libnet_conf_set_global_parameter("realm",
368 r->out.dns_domain_name);
369 W_ERROR_NOT_OK_RETURN(werr);
375 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
377 WERROR werr = WERR_OK;
379 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
381 werr = libnet_conf_set_global_parameter("security", "user");
382 W_ERROR_NOT_OK_RETURN(werr);
385 werr = libnet_conf_delete_parameter(GLOBAL_NAME, "realm");
391 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
395 if (!W_ERROR_IS_OK(r->out.result)) {
396 return r->out.result;
399 if (!r->in.modify_config) {
403 werr = do_join_modify_vals_config(r);
404 if (!W_ERROR_IS_OK(werr)) {
408 r->out.modified_config = true;
409 r->out.result = werr;
414 static WERROR do_UnjoinConfig(struct libnet_UnjoinCtx *r)
418 if (!W_ERROR_IS_OK(r->out.result)) {
419 return r->out.result;
422 if (!r->in.modify_config) {
426 werr = do_unjoin_modify_vals_config(r);
427 if (!W_ERROR_IS_OK(werr)) {
431 r->out.modified_config = true;
432 r->out.result = werr;
437 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
438 struct libnet_JoinCtx **r)
440 struct libnet_JoinCtx *ctx;
442 ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
452 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
453 struct libnet_UnjoinCtx **r)
455 struct libnet_UnjoinCtx *ctx;
457 ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
467 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
468 struct libnet_JoinCtx *r)
473 if (!r->in.domain_name) {
474 return WERR_INVALID_PARAM;
477 if (r->in.modify_config && !lp_include_registry_globals()) {
478 return WERR_NOT_SUPPORTED;
482 return WERR_SETUP_DOMAIN_CONTROLLER;
485 if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
487 status = do_DomainJoin(mem_ctx, r);
488 if (!NT_STATUS_IS_OK(status)) {
489 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
490 return WERR_SETUP_ALREADY_JOINED;
492 return ntstatus_to_werror(status);
496 werr = do_JoinConfig(r);
497 if (!W_ERROR_IS_OK(werr)) {
504 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
505 struct libnet_UnjoinCtx *r)
510 if (r->in.modify_config && !lp_include_registry_globals()) {
511 return WERR_NOT_SUPPORTED;
514 if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
516 status = do_DomainUnjoin(mem_ctx, r);
517 if (!NT_STATUS_IS_OK(status)) {
518 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
519 return WERR_SETUP_NOT_JOINED;
521 return ntstatus_to_werror(status);
525 werr = do_UnjoinConfig(r);
526 if (!W_ERROR_IS_OK(werr)) {