2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6 Copyright (C) Brad Henry 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libnet/libnet.h"
24 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "param/secrets.h"
28 #include "dsdb/samdb/samdb.h"
30 #include "libcli/security/security.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/credentials/credentials_krb5.h"
33 #include "librpc/gen_ndr/ndr_samr_c.h"
36 * complete a domain join, when joining to a AD domain:
37 * 1.) connect and bind to the DRSUAPI pipe
38 * 2.) do a DsCrackNames() to find the machine account dn
40 * 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
41 * 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
42 * 6.) do a DsCrackNames() to find the domain dn
43 * 7.) find out Site specific stuff, look at libnet_JoinSite() for details
45 static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_JoinDomain *r)
51 const char *realm = r->out.realm;
53 struct dcerpc_binding *samr_binding = r->out.samr_binding;
55 struct dcerpc_pipe *drsuapi_pipe;
56 struct dcerpc_binding *drsuapi_binding;
57 struct drsuapi_DsBind r_drsuapi_bind;
58 struct drsuapi_DsCrackNames r_crack_names;
59 struct drsuapi_DsNameString names[1];
60 struct policy_handle drsuapi_bind_handle;
61 struct GUID drsuapi_bind_guid;
63 struct ldb_context *remote_ldb;
64 struct ldb_dn *account_dn;
65 const char *account_dn_str;
66 const char *remote_ldb_url;
67 struct ldb_result *res;
68 struct ldb_message *msg;
72 const char * const attrs[] = {
73 "msDS-KeyVersionNumber",
74 "servicePrincipalName",
80 r->out.error_string = NULL;
82 /* We need to convert between a samAccountName and domain to a
83 * DN in the directory. The correct way to do this is with
84 * DRSUAPI CrackNames */
86 /* Fiddle with the bindings, so get to DRSUAPI on
87 * NCACN_IP_TCP, sealed */
88 tmp_ctx = talloc_named(r, 0, "libnet_JoinADSDomain temp context");
90 r->out.error_string = NULL;
91 return NT_STATUS_NO_MEMORY;
94 drsuapi_binding = talloc(tmp_ctx, struct dcerpc_binding);
95 if (!drsuapi_binding) {
96 r->out.error_string = NULL;
98 return NT_STATUS_NO_MEMORY;
101 *drsuapi_binding = *samr_binding;
103 /* DRSUAPI is only available on IP_TCP, and locally on NCALRPC */
104 if (drsuapi_binding->transport != NCALRPC) {
105 drsuapi_binding->transport = NCACN_IP_TCP;
107 drsuapi_binding->endpoint = NULL;
108 drsuapi_binding->flags |= DCERPC_SEAL;
110 status = dcerpc_pipe_connect_b(tmp_ctx,
113 &dcerpc_table_drsuapi,
116 if (!NT_STATUS_IS_OK(status)) {
117 r->out.error_string = talloc_asprintf(r,
118 "Connection to DRSUAPI pipe of PDC of domain '%s' failed: %s",
121 talloc_free(tmp_ctx);
125 /* get a DRSUAPI pipe handle */
126 GUID_from_string(DRSUAPI_DS_BIND_GUID, &drsuapi_bind_guid);
128 r_drsuapi_bind.in.bind_guid = &drsuapi_bind_guid;
129 r_drsuapi_bind.in.bind_info = NULL;
130 r_drsuapi_bind.out.bind_handle = &drsuapi_bind_handle;
132 status = dcerpc_drsuapi_DsBind(drsuapi_pipe, tmp_ctx, &r_drsuapi_bind);
133 if (!NT_STATUS_IS_OK(status)) {
134 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
137 "dcerpc_drsuapi_DsBind failed - %s",
138 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
139 talloc_free(tmp_ctx);
144 "dcerpc_drsuapi_DsBind failed - %s",
146 talloc_free(tmp_ctx);
149 } else if (!W_ERROR_IS_OK(r_drsuapi_bind.out.result)) {
152 "DsBind failed - %s",
153 win_errstr(r_drsuapi_bind.out.result));
154 talloc_free(tmp_ctx);
155 return NT_STATUS_UNSUCCESSFUL;
158 /* Actually 'crack' the names */
159 ZERO_STRUCT(r_crack_names);
160 r_crack_names.in.bind_handle = &drsuapi_bind_handle;
161 r_crack_names.in.level = 1;
162 r_crack_names.in.req.req1.codepage = 1252; /* western european */
163 r_crack_names.in.req.req1.language = 0x00000407; /* german */
164 r_crack_names.in.req.req1.count = 1;
165 r_crack_names.in.req.req1.names = names;
166 r_crack_names.in.req.req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
167 r_crack_names.in.req.req1.format_offered= DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY;
168 r_crack_names.in.req.req1.format_desired= DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
169 names[0].str = dom_sid_string(tmp_ctx, r->out.account_sid);
171 r->out.error_string = NULL;
172 talloc_free(tmp_ctx);
173 return NT_STATUS_NO_MEMORY;
176 status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
177 if (!NT_STATUS_IS_OK(status)) {
178 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
181 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
183 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
184 talloc_free(tmp_ctx);
189 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
192 talloc_free(tmp_ctx);
195 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
198 "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
199 talloc_free(tmp_ctx);
200 return NT_STATUS_UNSUCCESSFUL;
201 } else if (r_crack_names.out.level != 1
202 || !r_crack_names.out.ctr.ctr1
203 || r_crack_names.out.ctr.ctr1->count != 1) {
204 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
205 talloc_free(tmp_ctx);
206 return NT_STATUS_INVALID_PARAMETER;
207 } else if (r_crack_names.out.ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
208 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: %d", r_crack_names.out.ctr.ctr1->array[0].status);
209 talloc_free(tmp_ctx);
210 return NT_STATUS_UNSUCCESSFUL;
211 } else if (r_crack_names.out.ctr.ctr1->array[0].result_name == NULL) {
212 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: no result name");
213 talloc_free(tmp_ctx);
214 return NT_STATUS_INVALID_PARAMETER;
217 /* Store the DN of our machine account. */
218 account_dn_str = r_crack_names.out.ctr.ctr1->array[0].result_name;
220 /* Now we know the user's DN, open with LDAP, read and modify a few things */
222 remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s",
223 drsuapi_binding->target_hostname);
224 if (!remote_ldb_url) {
225 r->out.error_string = NULL;
226 talloc_free(tmp_ctx);
227 return NT_STATUS_NO_MEMORY;
230 remote_ldb = ldb_wrap_connect(tmp_ctx, remote_ldb_url,
231 NULL, ctx->cred, 0, NULL);
233 r->out.error_string = NULL;
234 talloc_free(tmp_ctx);
235 return NT_STATUS_UNSUCCESSFUL;
238 account_dn = ldb_dn_new(tmp_ctx, remote_ldb, account_dn_str);
239 if (! ldb_dn_validate(account_dn)) {
240 r->out.error_string = talloc_asprintf(r, "Invalid account dn: %s",
242 talloc_free(tmp_ctx);
243 return NT_STATUS_UNSUCCESSFUL;
246 /* search for the user's record */
247 ret = ldb_search(remote_ldb, account_dn, LDB_SCOPE_BASE,
249 if (ret != LDB_SUCCESS) {
250 r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - %s",
251 account_dn_str, ldb_errstring(remote_ldb));
252 talloc_free(tmp_ctx);
253 return NT_STATUS_UNSUCCESSFUL;
256 talloc_steal(tmp_ctx, res);
258 if (res->count != 1) {
259 r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - found %d entries",
260 account_dn_str, res->count);
261 talloc_free(tmp_ctx);
262 return NT_STATUS_UNSUCCESSFUL;
265 /* Prepare a new message, for the modify */
266 msg = ldb_msg_new(tmp_ctx);
268 r->out.error_string = NULL;
269 talloc_free(tmp_ctx);
270 return NT_STATUS_NO_MEMORY;
272 msg->dn = res->msgs[0]->dn;
276 const char *service_principal_name[6];
277 const char *dns_host_name = strlower_talloc(tmp_ctx,
278 talloc_asprintf(tmp_ctx,
283 if (!dns_host_name) {
284 r->out.error_string = NULL;
285 talloc_free(tmp_ctx);
286 return NT_STATUS_NO_MEMORY;
289 service_principal_name[0] = talloc_asprintf(tmp_ctx, "host/%s", dns_host_name);
290 service_principal_name[1] = talloc_asprintf(tmp_ctx, "host/%s", strlower_talloc(tmp_ctx, r->in.netbios_name));
291 service_principal_name[2] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, realm);
292 service_principal_name[3] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), realm);
293 service_principal_name[4] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, r->out.domain_name);
294 service_principal_name[5] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), r->out.domain_name);
296 for (i=0; i < ARRAY_SIZE(service_principal_name); i++) {
297 if (!service_principal_name[i]) {
298 r->out.error_string = NULL;
299 talloc_free(tmp_ctx);
300 return NT_STATUS_NO_MEMORY;
302 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "servicePrincipalName", service_principal_name[i]);
304 r->out.error_string = NULL;
305 talloc_free(tmp_ctx);
306 return NT_STATUS_NO_MEMORY;
310 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "dNSHostName", dns_host_name);
312 r->out.error_string = NULL;
313 talloc_free(tmp_ctx);
314 return NT_STATUS_NO_MEMORY;
317 rtn = samdb_replace(remote_ldb, tmp_ctx, msg);
321 "Failed to replace entries on %s",
322 ldb_dn_get_linearized(msg->dn));
323 talloc_free(tmp_ctx);
324 return NT_STATUS_INTERNAL_DB_CORRUPTION;
328 /* DsCrackNames to find out the DN of the domain. */
329 r_crack_names.in.req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
330 r_crack_names.in.req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
331 names[0].str = talloc_asprintf(tmp_ctx, "%s\\", r->out.domain_name);
333 r->out.error_string = NULL;
334 talloc_free(tmp_ctx);
335 return NT_STATUS_NO_MEMORY;
338 status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
339 if (!NT_STATUS_IS_OK(status)) {
340 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
343 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
345 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
346 talloc_free(tmp_ctx);
351 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
354 talloc_free(tmp_ctx);
357 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
360 "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
361 talloc_free(tmp_ctx);
362 return NT_STATUS_UNSUCCESSFUL;
363 } else if (r_crack_names.out.level != 1
364 || !r_crack_names.out.ctr.ctr1
365 || r_crack_names.out.ctr.ctr1->count != 1
366 || !r_crack_names.out.ctr.ctr1->array[0].result_name
367 || r_crack_names.out.ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
368 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
369 talloc_free(tmp_ctx);
370 return NT_STATUS_UNSUCCESSFUL;
373 /* Store the account DN. */
374 r->out.account_dn_str = account_dn_str;
375 talloc_steal(r, account_dn_str);
377 /* Store the domain DN. */
378 r->out.domain_dn_str = r_crack_names.out.ctr.ctr1->array[0].result_name;
379 talloc_steal(r, r_crack_names.out.ctr.ctr1->array[0].result_name);
381 /* Store the KVNO of the account, critical for some kerberos
383 r->out.kvno = ldb_msg_find_attr_as_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
385 /* Store the account GUID. */
386 r->out.account_guid = samdb_result_guid(res->msgs[0], "objectGUID");
388 if (r->in.acct_type == ACB_SVRTRUST) {
389 status = libnet_JoinSite(remote_ldb, r);
391 talloc_free(tmp_ctx);
397 * do a domain join using DCERPC/SAMR calls
398 * - connect to the LSA pipe, to try and find out information about the domain
399 * - create a secondary connection to SAMR pipe
400 * - do a samr_Connect to get a policy handle
401 * - do a samr_LookupDomain to get the domain sid
402 * - do a samr_OpenDomain to get a domain handle
403 * - do a samr_CreateAccount to try and get a new account
406 * - do a samr_LookupNames to get the users rid
407 * - do a samr_OpenUser to get a user handle
408 * - potentially delete and recreate the user
409 * - assert the account is of the right type with samrQueryUserInfo
411 * - call libnet_SetPassword_samr_handle to set the password,
412 * and pass a samr_UserInfo21 struct to set full_name and the account flags
414 * - do some ADS specific things when we join as Domain Controller,
415 * look at libnet_joinADSDomain() for the details
417 NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *r)
421 NTSTATUS status, cu_status;
423 struct libnet_RpcConnect *connect_with_info;
424 struct dcerpc_pipe *samr_pipe;
426 struct samr_Connect sc;
427 struct policy_handle p_handle;
428 struct samr_OpenDomain od;
429 struct policy_handle d_handle;
430 struct samr_LookupNames ln;
431 struct samr_OpenUser ou;
432 struct samr_CreateUser2 cu;
433 struct policy_handle *u_handle = NULL;
434 struct samr_QueryUserInfo qui;
435 struct samr_UserInfo21 u_info21;
436 union libnet_SetPassword r2;
437 struct samr_GetUserPwInfo pwp;
438 struct lsa_String samr_account_name;
440 uint32_t acct_flags, old_acct_flags;
441 uint32_t rid, access_granted;
442 int policy_min_pw_len = 0;
444 struct dom_sid *account_sid = NULL;
445 const char *password_str = NULL;
447 r->out.error_string = NULL;
448 r2.samr_handle.out.error_string = NULL;
450 tmp_ctx = talloc_named(mem_ctx, 0, "libnet_Join temp context");
452 r->out.error_string = NULL;
453 return NT_STATUS_NO_MEMORY;
456 u_handle = talloc(tmp_ctx, struct policy_handle);
458 r->out.error_string = NULL;
459 talloc_free(tmp_ctx);
460 return NT_STATUS_NO_MEMORY;
463 connect_with_info = talloc(tmp_ctx, struct libnet_RpcConnect);
464 if (!connect_with_info) {
465 r->out.error_string = NULL;
466 talloc_free(tmp_ctx);
467 return NT_STATUS_NO_MEMORY;
470 /* prepare connect to the SAMR pipe of PDC */
471 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
472 connect_with_info->in.binding = NULL;
473 connect_with_info->in.name = r->in.domain_name;
475 connect_with_info->in.binding = r->in.binding;
476 connect_with_info->in.name = NULL;
479 /* This level makes a connection to the LSA pipe on the way,
480 * to get some useful bits of information about the domain */
481 connect_with_info->level = LIBNET_RPC_CONNECT_DC_INFO;
482 connect_with_info->in.dcerpc_iface = &dcerpc_table_samr;
485 establish the SAMR connection
487 status = libnet_RpcConnect(ctx, tmp_ctx, connect_with_info);
488 if (!NT_STATUS_IS_OK(status)) {
490 r->out.error_string = talloc_asprintf(mem_ctx,
491 "Connection to SAMR pipe of DC %s failed: %s",
492 r->in.binding, connect_with_info->out.error_string);
494 r->out.error_string = talloc_asprintf(mem_ctx,
495 "Connection to SAMR pipe of PDC for %s failed: %s",
496 r->in.domain_name, connect_with_info->out.error_string);
498 talloc_free(tmp_ctx);
502 samr_pipe = connect_with_info->out.dcerpc_pipe;
504 status = dcerpc_pipe_auth(tmp_ctx, &samr_pipe,
505 connect_with_info->out.dcerpc_pipe->binding,
506 &dcerpc_table_samr, ctx->cred);
507 if (!NT_STATUS_IS_OK(status)) {
508 r->out.error_string = talloc_asprintf(mem_ctx,
509 "SAMR bind failed: %s",
511 talloc_free(tmp_ctx);
515 /* prepare samr_Connect */
516 ZERO_STRUCT(p_handle);
517 sc.in.system_name = NULL;
518 sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
519 sc.out.connect_handle = &p_handle;
521 /* 2. do a samr_Connect to get a policy handle */
522 status = dcerpc_samr_Connect(samr_pipe, tmp_ctx, &sc);
523 if (!NT_STATUS_IS_OK(status)) {
524 r->out.error_string = talloc_asprintf(mem_ctx,
525 "samr_Connect failed: %s",
527 talloc_free(tmp_ctx);
531 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
532 if (!connect_with_info->out.domain_name) {
533 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
534 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
536 /* Bugger, we just lost our way to automaticly find the domain name */
537 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, lp_workgroup());
538 connect_with_info->out.realm = talloc_strdup(tmp_ctx, lp_realm());
542 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
543 if (!connect_with_info->out.domain_sid) {
544 struct lsa_String name;
545 struct samr_LookupDomain l;
546 name.string = connect_with_info->out.domain_name;
547 l.in.connect_handle = &p_handle;
548 l.in.domain_name = &name;
550 status = dcerpc_samr_LookupDomain(samr_pipe, tmp_ctx, &l);
551 if (!NT_STATUS_IS_OK(status)) {
552 r->out.error_string = talloc_asprintf(mem_ctx,
553 "SAMR LookupDomain failed: %s",
555 talloc_free(tmp_ctx);
558 connect_with_info->out.domain_sid = l.out.sid;
561 /* prepare samr_OpenDomain */
562 ZERO_STRUCT(d_handle);
563 od.in.connect_handle = &p_handle;
564 od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
565 od.in.sid = connect_with_info->out.domain_sid;
566 od.out.domain_handle = &d_handle;
568 /* do a samr_OpenDomain to get a domain handle */
569 status = dcerpc_samr_OpenDomain(samr_pipe, tmp_ctx, &od);
570 if (!NT_STATUS_IS_OK(status)) {
571 r->out.error_string = talloc_asprintf(mem_ctx,
572 "samr_OpenDomain for [%s] failed: %s",
573 dom_sid_string(tmp_ctx, connect_with_info->out.domain_sid),
575 talloc_free(tmp_ctx);
579 /* prepare samr_CreateUser2 */
580 ZERO_STRUCTP(u_handle);
581 cu.in.domain_handle = &d_handle;
582 cu.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
583 samr_account_name.string = r->in.account_name;
584 cu.in.account_name = &samr_account_name;
585 cu.in.acct_flags = r->in.acct_type;
586 cu.out.user_handle = u_handle;
588 cu.out.access_granted = &access_granted;
590 /* do a samr_CreateUser2 to get an account handle, or an error */
591 cu_status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
593 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
594 /* prepare samr_LookupNames */
595 ln.in.domain_handle = &d_handle;
597 ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
599 r->out.error_string = NULL;
600 talloc_free(tmp_ctx);
601 return NT_STATUS_NO_MEMORY;
603 ln.in.names[0].string = r->in.account_name;
605 /* 5. do a samr_LookupNames to get the users rid */
606 status = dcerpc_samr_LookupNames(samr_pipe, tmp_ctx, &ln);
607 if (!NT_STATUS_IS_OK(status)) {
608 r->out.error_string = talloc_asprintf(mem_ctx,
609 "samr_LookupNames for [%s] failed: %s",
612 talloc_free(tmp_ctx);
616 /* check if we got one RID for the user */
617 if (ln.out.rids.count != 1) {
618 r->out.error_string = talloc_asprintf(mem_ctx,
619 "samr_LookupNames for [%s] returns %d RIDs",
620 r->in.account_name, ln.out.rids.count);
621 talloc_free(tmp_ctx);
622 return NT_STATUS_INVALID_PARAMETER;
625 /* prepare samr_OpenUser */
626 ZERO_STRUCTP(u_handle);
627 ou.in.domain_handle = &d_handle;
628 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
629 ou.in.rid = ln.out.rids.ids[0];
631 ou.out.user_handle = u_handle;
633 /* 6. do a samr_OpenUser to get a user handle */
634 status = dcerpc_samr_OpenUser(samr_pipe, tmp_ctx, &ou);
635 if (!NT_STATUS_IS_OK(status)) {
636 r->out.error_string = talloc_asprintf(mem_ctx,
637 "samr_OpenUser for [%s] failed: %s",
640 talloc_free(tmp_ctx);
644 if (r->in.recreate_account) {
645 struct samr_DeleteUser d;
646 d.in.user_handle = u_handle;
647 d.out.user_handle = u_handle;
648 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
649 if (!NT_STATUS_IS_OK(status)) {
650 r->out.error_string = talloc_asprintf(mem_ctx,
651 "samr_DeleteUser (for recreate) of [%s] failed: %s",
654 talloc_free(tmp_ctx);
658 /* We want to recreate, so delete and another samr_CreateUser2 */
660 /* &cu filled in above */
661 status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
662 if (!NT_STATUS_IS_OK(status)) {
663 r->out.error_string = talloc_asprintf(mem_ctx,
664 "samr_CreateUser2 (recreate) for [%s] failed: %s",
665 r->in.account_name, nt_errstr(status));
666 talloc_free(tmp_ctx);
670 } else if (!NT_STATUS_IS_OK(status)) {
671 r->out.error_string = talloc_asprintf(mem_ctx,
672 "samr_CreateUser2 for [%s] failed: %s",
673 r->in.account_name, nt_errstr(status));
674 talloc_free(tmp_ctx);
678 /* prepare samr_QueryUserInfo (get flags) */
679 qui.in.user_handle = u_handle;
682 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
683 if (!NT_STATUS_IS_OK(status)) {
684 r->out.error_string = talloc_asprintf(mem_ctx,
685 "samr_QueryUserInfo for [%s] failed: %s",
688 talloc_free(tmp_ctx);
693 status = NT_STATUS_INVALID_PARAMETER;
695 = talloc_asprintf(mem_ctx,
696 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s",
697 r->in.account_name, nt_errstr(status));
698 talloc_free(tmp_ctx);
702 old_acct_flags = (qui.out.info->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
703 /* Possibly bail if the account is of the wrong type */
705 != r->in.acct_type) {
706 const char *old_account_type, *new_account_type;
707 switch (old_acct_flags) {
709 old_account_type = "domain member (member)";
712 old_account_type = "domain controller (bdc)";
715 old_account_type = "trusted domain";
718 return NT_STATUS_INVALID_PARAMETER;
720 switch (r->in.acct_type) {
722 new_account_type = "domain member (member)";
725 new_account_type = "domain controller (bdc)";
728 new_account_type = "trusted domain";
731 return NT_STATUS_INVALID_PARAMETER;
734 if (!NT_STATUS_EQUAL(cu_status, NT_STATUS_USER_EXISTS)) {
735 /* We created a new user, but they didn't come out the right type?!? */
737 = talloc_asprintf(mem_ctx,
738 "We asked to create a new machine account (%s) of type %s, "
739 "but we got an account of type %s. This is unexpected. "
740 "Perhaps delete the account and try again.",
741 r->in.account_name, new_account_type, old_account_type);
742 talloc_free(tmp_ctx);
743 return NT_STATUS_INVALID_PARAMETER;
745 /* The account is of the wrong type, so bail */
747 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
749 = talloc_asprintf(mem_ctx,
750 "The machine account (%s) already exists in the domain %s, "
751 "but is a %s. You asked to join as a %s. Please delete "
752 "the account and try again.",
753 r->in.account_name, connect_with_info->out.domain_name, old_account_type, new_account_type);
754 talloc_free(tmp_ctx);
755 return NT_STATUS_USER_EXISTS;
758 acct_flags = qui.out.info->info16.acct_flags;
761 acct_flags = (acct_flags & ~(ACB_DISABLED|ACB_PWNOTREQ));
763 /* Find out what password policy this user has */
764 pwp.in.user_handle = u_handle;
766 status = dcerpc_samr_GetUserPwInfo(samr_pipe, tmp_ctx, &pwp);
767 if (NT_STATUS_IS_OK(status)) {
768 policy_min_pw_len = pwp.out.info.min_password_length;
771 /* Grab a password of that minimum length */
773 password_str = generate_random_str(tmp_ctx, MAX(8, policy_min_pw_len));
775 /* set full_name and reset flags */
776 ZERO_STRUCT(u_info21);
777 u_info21.full_name.string = r->in.account_name;
778 u_info21.acct_flags = acct_flags;
779 u_info21.fields_present = SAMR_FIELD_FULL_NAME | SAMR_FIELD_ACCT_FLAGS;
781 r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
782 r2.samr_handle.in.account_name = r->in.account_name;
783 r2.samr_handle.in.newpassword = password_str;
784 r2.samr_handle.in.user_handle = u_handle;
785 r2.samr_handle.in.dcerpc_pipe = samr_pipe;
786 r2.samr_handle.in.info21 = &u_info21;
788 status = libnet_SetPassword(ctx, tmp_ctx, &r2);
789 if (!NT_STATUS_IS_OK(status)) {
790 r->out.error_string = talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
791 talloc_free(tmp_ctx);
795 account_sid = dom_sid_add_rid(mem_ctx, connect_with_info->out.domain_sid, rid);
797 r->out.error_string = NULL;
798 talloc_free(tmp_ctx);
799 return NT_STATUS_NO_MEMORY;
802 /* Finish out by pushing various bits of status data out for the caller to use */
803 r->out.join_password = password_str;
804 talloc_steal(mem_ctx, r->out.join_password);
806 r->out.domain_sid = connect_with_info->out.domain_sid;
807 talloc_steal(mem_ctx, r->out.domain_sid);
809 r->out.account_sid = account_sid;
810 talloc_steal(mem_ctx, r->out.account_sid);
812 r->out.domain_name = connect_with_info->out.domain_name;
813 talloc_steal(mem_ctx, r->out.domain_name);
814 r->out.realm = connect_with_info->out.realm;
815 talloc_steal(mem_ctx, r->out.realm);
816 r->out.samr_pipe = samr_pipe;
817 talloc_steal(mem_ctx, samr_pipe);
818 r->out.samr_binding = samr_pipe->binding;
819 talloc_steal(mem_ctx, r->out.samr_binding);
820 r->out.user_handle = u_handle;
821 talloc_steal(mem_ctx, u_handle);
822 r->out.error_string = r2.samr_handle.out.error_string;
823 talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
825 r->out.server_dn_str = NULL;
826 talloc_free(tmp_ctx);
828 /* Now, if it was AD, then we want to start looking changing a
829 * few more things. Otherwise, we are done. */
831 status = libnet_JoinADSDomain(ctx, r);
838 static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
840 struct libnet_Join *r)
844 struct libnet_JoinDomain *r2;
846 struct ldb_context *ldb;
847 struct ldb_dn *base_dn;
848 struct ldb_message **msgs, *msg;
850 const char * const attrs[] = {
859 uint32_t acct_type = 0;
860 const char *account_name;
861 const char *netbios_name;
863 r->out.error_string = NULL;
865 tmp_mem = talloc_new(mem_ctx);
867 return NT_STATUS_NO_MEMORY;
870 r2 = talloc(tmp_mem, struct libnet_JoinDomain);
872 r->out.error_string = NULL;
873 talloc_free(tmp_mem);
874 return NT_STATUS_NO_MEMORY;
877 if (r->in.join_type == SEC_CHAN_BDC) {
878 acct_type = ACB_SVRTRUST;
879 } else if (r->in.join_type == SEC_CHAN_WKSTA) {
880 acct_type = ACB_WSTRUST;
882 r->out.error_string = NULL;
883 talloc_free(tmp_mem);
884 return NT_STATUS_INVALID_PARAMETER;
887 if (r->in.netbios_name != NULL) {
888 netbios_name = r->in.netbios_name;
890 netbios_name = talloc_reference(tmp_mem, lp_netbios_name());
892 r->out.error_string = NULL;
893 talloc_free(tmp_mem);
894 return NT_STATUS_NO_MEMORY;
898 account_name = talloc_asprintf(tmp_mem, "%s$", netbios_name);
900 r->out.error_string = NULL;
901 talloc_free(tmp_mem);
902 return NT_STATUS_NO_MEMORY;
906 * Local secrets are stored in secrets.ldb
907 * open it to make sure we can write the info into it after the join
909 ldb = secrets_db_connect(tmp_mem);
912 = talloc_asprintf(mem_ctx,
913 "Could not open secrets database");
914 talloc_free(tmp_mem);
915 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
922 r2->in.domain_name = r->in.domain_name;
923 r2->in.account_name = account_name;
924 r2->in.netbios_name = netbios_name;
925 r2->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
926 r2->in.acct_type = acct_type;
927 r2->in.recreate_account = False;
928 status = libnet_JoinDomain(ctx, r2, r2);
929 if (!NT_STATUS_IS_OK(status)) {
930 r->out.error_string = talloc_steal(mem_ctx, r2->out.error_string);
931 talloc_free(tmp_mem);
936 * now prepare the record for secrets.ldb
938 sct = talloc_asprintf(tmp_mem, "%d", r->in.join_type);
940 r->out.error_string = NULL;
941 talloc_free(tmp_mem);
942 return NT_STATUS_NO_MEMORY;
945 msg = ldb_msg_new(tmp_mem);
947 r->out.error_string = NULL;
948 talloc_free(tmp_mem);
949 return NT_STATUS_NO_MEMORY;
952 base_dn = ldb_dn_new(tmp_mem, ldb, "cn=Primary Domains");
954 r->out.error_string = NULL;
955 talloc_free(tmp_mem);
956 return NT_STATUS_NO_MEMORY;
959 msg->dn = ldb_dn_copy(tmp_mem, base_dn);
960 if ( ! ldb_dn_add_child_fmt(msg->dn, "flatname=%s", r2->out.domain_name)) {
961 r->out.error_string = NULL;
962 talloc_free(tmp_mem);
963 return NT_STATUS_NO_MEMORY;
966 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "flatname", r2->out.domain_name);
968 r->out.error_string = NULL;
969 talloc_free(tmp_mem);
970 return NT_STATUS_NO_MEMORY;
974 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "realm", r2->out.realm);
976 r->out.error_string = NULL;
977 talloc_free(tmp_mem);
978 return NT_STATUS_NO_MEMORY;
981 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
983 r->out.error_string = NULL;
984 talloc_free(tmp_mem);
985 return NT_STATUS_NO_MEMORY;
989 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
991 r->out.error_string = NULL;
992 talloc_free(tmp_mem);
993 return NT_STATUS_NO_MEMORY;
996 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
998 r->out.error_string = NULL;
999 talloc_free(tmp_mem);
1000 return NT_STATUS_NO_MEMORY;
1003 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1005 r->out.error_string = NULL;
1006 talloc_free(tmp_mem);
1007 return NT_STATUS_NO_MEMORY;
1010 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1012 r->out.error_string = NULL;
1013 talloc_free(tmp_mem);
1014 return NT_STATUS_NO_MEMORY;
1018 rtn = samdb_msg_add_uint(ldb, tmp_mem, msg, "msDS-KeyVersionNumber",
1021 r->out.error_string = NULL;
1022 talloc_free(tmp_mem);
1023 return NT_STATUS_NO_MEMORY;
1027 if (r2->out.domain_sid) {
1028 rtn = samdb_msg_add_dom_sid(ldb, tmp_mem, msg, "objectSid",
1029 r2->out.domain_sid);
1031 r->out.error_string = NULL;
1032 talloc_free(tmp_mem);
1033 return NT_STATUS_NO_MEMORY;
1038 * search for the secret record
1039 * - remove the records we find
1040 * - and fetch the old secret and store it under priorSecret
1042 ret = gendb_search(ldb,
1045 "(|" SECRETS_PRIMARY_DOMAIN_FILTER "(realm=%s))",
1046 r2->out.domain_name, r2->out.realm);
1048 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secretsKeytab", "secrets.keytab");
1050 r->out.error_string = NULL;
1051 talloc_free(tmp_mem);
1052 return NT_STATUS_NO_MEMORY;
1054 } else if (ret == -1) {
1056 = talloc_asprintf(mem_ctx,
1057 "Search for domain: %s and realm: %s failed: %s",
1058 r2->out.domain_name, r2->out.realm, ldb_errstring(ldb));
1059 talloc_free(tmp_mem);
1060 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1062 const struct ldb_val *private_keytab;
1063 const struct ldb_val *krb5_keytab;
1064 const struct ldb_val *prior_secret;
1065 const struct ldb_val *prior_modified_time;
1068 for (i = 0; i < ret; i++) {
1069 ldb_delete(ldb, msgs[i]->dn);
1072 prior_secret = ldb_msg_find_ldb_val(msgs[0], "secret");
1074 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorSecret", prior_secret);
1076 r->out.error_string = NULL;
1077 talloc_free(tmp_mem);
1078 return NT_STATUS_NO_MEMORY;
1081 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
1083 r->out.error_string = NULL;
1084 talloc_free(tmp_mem);
1085 return NT_STATUS_NO_MEMORY;
1088 prior_modified_time = ldb_msg_find_ldb_val(msgs[0],
1090 if (prior_modified_time) {
1091 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorWhenChanged",
1092 prior_modified_time);
1094 r->out.error_string = NULL;
1095 talloc_free(tmp_mem);
1096 return NT_STATUS_NO_MEMORY;
1100 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1102 r->out.error_string = NULL;
1103 talloc_free(tmp_mem);
1104 return NT_STATUS_NO_MEMORY;
1107 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1109 r->out.error_string = NULL;
1110 talloc_free(tmp_mem);
1111 return NT_STATUS_NO_MEMORY;
1114 /* We will want to keep the keytab names */
1115 private_keytab = ldb_msg_find_ldb_val(msgs[0], "privateKeytab");
1116 if (private_keytab) {
1117 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "privateKeytab", private_keytab);
1119 r->out.error_string = NULL;
1120 talloc_free(tmp_mem);
1121 return NT_STATUS_NO_MEMORY;
1124 krb5_keytab = ldb_msg_find_ldb_val(msgs[0], "krb5Keytab");
1126 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "krb5Keytab", krb5_keytab);
1128 r->out.error_string = NULL;
1129 talloc_free(tmp_mem);
1130 return NT_STATUS_NO_MEMORY;
1135 /* create the secret */
1136 ret = samdb_add(ldb, tmp_mem, msg);
1138 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to create secret record %s",
1139 ldb_dn_get_linearized(msg->dn));
1140 talloc_free(tmp_mem);
1141 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1144 /* move all out parameter to the callers TALLOC_CTX */
1145 r->out.error_string = NULL;
1146 r->out.join_password = r2->out.join_password;
1147 talloc_steal(mem_ctx, r2->out.join_password);
1148 r->out.domain_sid = r2->out.domain_sid;
1149 talloc_steal(mem_ctx, r2->out.domain_sid);
1150 r->out.domain_name = r2->out.domain_name;
1151 talloc_steal(mem_ctx, r2->out.domain_name);
1152 talloc_free(tmp_mem);
1153 return NT_STATUS_OK;
1156 NTSTATUS libnet_Join(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_Join *r)
1158 switch (r->in.join_type) {
1159 case SEC_CHAN_WKSTA:
1160 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1162 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1163 case SEC_CHAN_DOMAIN:
1167 r->out.error_string = talloc_asprintf(mem_ctx,
1168 "Invalid join type specified (%08X) attempting to join domain %s",
1169 r->in.join_type, r->in.domain_name);
1170 return NT_STATUS_INVALID_PARAMETER;