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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_samr.h"
26 #include "librpc/gen_ndr/ndr_lsa.h"
27 #include "librpc/gen_ndr/ndr_drsuapi.h"
28 #include "lib/ldb/include/ldb.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "libcli/cldap/cldap.h"
31 #include "include/secrets.h"
32 #include "librpc/gen_ndr/drsuapi.h"
33 #include "dsdb/samdb/samdb.h"
36 * find out Site specific stuff:
37 * 1.) setup an CLDAP socket
38 * 2.) lookup the Site name
39 * 3.) Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
40 * TODO: 4.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>...
42 static NTSTATUS libnet_JoinSite(struct libnet_context *ctx,
43 struct dcerpc_pipe *drsuapi_pipe,
44 struct policy_handle drsuapi_bind_handle,
45 struct ldb_context *remote_ldb,
46 struct libnet_JoinDomain *libnet_r)
51 struct cldap_socket *cldap = NULL;
52 struct cldap_netlogon search;
54 struct ldb_dn *server_dn;
55 struct ldb_message *msg;
58 const char *site_name;
59 const char *server_dn_str;
60 const char *config_dn_str;
62 tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
64 libnet_r->out.error_string = NULL;
65 return NT_STATUS_NO_MEMORY;
68 /* Resolve the site name. */
71 search.in.dest_address = libnet_r->out.samr_binding->host;
72 search.in.acct_control = -1;
73 search.in.version = 6;
75 cldap = cldap_socket_init(tmp_ctx, NULL);
76 status = cldap_netlogon(cldap, tmp_ctx, &search);
77 if (!NT_STATUS_IS_OK(status)) {
78 /* Default to using Default-First-Site-Name rather than returning status at this point. */
79 site_name = talloc_asprintf(tmp_ctx, "%s", "Default-First-Site-Name");
81 libnet_r->out.error_string = NULL;
83 return NT_STATUS_NO_MEMORY;
86 site_name = search.out.netlogon.logon5.site_name;
89 config_dn_str = talloc_asprintf(tmp_ctx, "CN=Configuration,%s", libnet_r->out.domain_dn_str);
91 libnet_r->out.error_string = NULL;
93 return NT_STATUS_NO_MEMORY;
96 server_dn_str = talloc_asprintf(tmp_ctx, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
97 libnet_r->in.netbios_name, site_name, config_dn_str);
99 libnet_r->out.error_string = NULL;
100 talloc_free(tmp_ctx);
101 return NT_STATUS_NO_MEMORY;
105 Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
107 msg = ldb_msg_new(tmp_ctx);
109 libnet_r->out.error_string = NULL;
110 talloc_free(tmp_ctx);
111 return NT_STATUS_NO_MEMORY;
114 rtn = ldb_msg_add_string(msg, "objectClass", "server");
116 libnet_r->out.error_string = NULL;
117 talloc_free(tmp_ctx);
118 return NT_STATUS_NO_MEMORY;
120 rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
122 libnet_r->out.error_string = NULL;
123 talloc_free(tmp_ctx);
124 return NT_STATUS_NO_MEMORY;
126 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
128 libnet_r->out.error_string = NULL;
129 talloc_free(tmp_ctx);
130 return NT_STATUS_NO_MEMORY;
133 server_dn = ldb_dn_explode(tmp_ctx, server_dn_str);
134 if (server_dn == NULL) {
135 libnet_r->out.error_string = talloc_asprintf(libnet_r,
136 "Invalid server dn: %s",
138 talloc_free(tmp_ctx);
139 return NT_STATUS_UNSUCCESSFUL;
144 rtn = ldb_add(remote_ldb, msg);
145 if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
148 /* make a 'modify' msg, and only for serverReference */
149 msg = ldb_msg_new(tmp_ctx);
151 libnet_r->out.error_string = NULL;
152 talloc_free(tmp_ctx);
153 return NT_STATUS_NO_MEMORY;
157 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
159 libnet_r->out.error_string = NULL;
160 talloc_free(tmp_ctx);
161 return NT_STATUS_NO_MEMORY;
164 /* mark all the message elements (should be just one)
165 as LDB_FLAG_MOD_REPLACE */
166 for (i=0;i<msg->num_elements;i++) {
167 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
170 rtn = ldb_modify(remote_ldb, msg);
172 libnet_r->out.error_string
173 = talloc_asprintf(libnet_r,
174 "Failed to modify server entry %s: %s: %d",
176 ldb_errstring(remote_ldb), rtn);
177 talloc_free(tmp_ctx);
178 return NT_STATUS_INTERNAL_DB_CORRUPTION;
180 } else if (rtn != 0) {
181 libnet_r->out.error_string
182 = talloc_asprintf(libnet_r,
183 "Failed to add server entry %s: %s: %d",
185 ldb_errstring(remote_ldb), rtn);
186 talloc_free(tmp_ctx);
187 return NT_STATUS_INTERNAL_DB_CORRUPTION;
189 DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
191 /* Store the server DN in libnet_r */
192 libnet_r->out.server_dn_str = server_dn_str;
193 talloc_steal(libnet_r, server_dn_str);
195 talloc_free(tmp_ctx);
200 * complete a domain join, when joining to a AD domain:
201 * 1.) connect and bind to the DRSUAPI pipe
202 * 2.) do a DsCrackNames() to find the machine account dn
203 * 3.) connect to LDAP
204 * 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
205 * 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
206 * 6.) do a DsCrackNames() to find the domain dn
207 * 7.) find out Site specific stuff, look at libnet_JoinSite() for details
209 static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_JoinDomain *r)
215 const char *realm = r->out.realm;
217 struct dcerpc_binding *samr_binding = r->out.samr_binding;
219 struct dcerpc_pipe *drsuapi_pipe;
220 struct dcerpc_binding *drsuapi_binding;
221 struct drsuapi_DsBind r_drsuapi_bind;
222 struct drsuapi_DsCrackNames r_crack_names;
223 struct drsuapi_DsNameString names[1];
224 struct policy_handle drsuapi_bind_handle;
225 struct GUID drsuapi_bind_guid;
227 struct ldb_context *remote_ldb;
228 const struct ldb_dn *account_dn;
229 const char *account_dn_str;
230 const char *remote_ldb_url;
231 struct ldb_result *res;
232 struct ldb_message *msg;
238 const char * const attrs[] = {
239 "msDS-KeyVersionNumber",
240 "servicePrincipalName",
245 r->out.error_string = NULL;
247 /* We need to convert between a samAccountName and domain to a
248 * DN in the directory. The correct way to do this is with
249 * DRSUAPI CrackNames */
251 /* Fiddle with the bindings, so get to DRSUAPI on
252 * NCACN_IP_TCP, sealed */
253 tmp_ctx = talloc_named(r, 0, "libnet_JoinADSDomain temp context");
255 r->out.error_string = NULL;
256 return NT_STATUS_NO_MEMORY;
259 drsuapi_binding = talloc(tmp_ctx, struct dcerpc_binding);
260 if (!drsuapi_binding) {
261 r->out.error_string = NULL;
262 talloc_free(tmp_ctx);
263 return NT_STATUS_NO_MEMORY;
266 *drsuapi_binding = *samr_binding;
268 /* DRSUAPI is only available on IP_TCP, and locally on NCALRPC */
269 if (drsuapi_binding->transport != NCALRPC) {
270 drsuapi_binding->transport = NCACN_IP_TCP;
272 drsuapi_binding->endpoint = NULL;
273 drsuapi_binding->flags |= DCERPC_SEAL;
275 status = dcerpc_pipe_connect_b(tmp_ctx,
278 &dcerpc_table_drsuapi,
281 if (!NT_STATUS_IS_OK(status)) {
282 r->out.error_string = talloc_asprintf(r,
283 "Connection to DRSUAPI pipe of PDC of domain '%s' failed: %s",
286 talloc_free(tmp_ctx);
290 /* get a DRSUAPI pipe handle */
291 GUID_from_string(DRSUAPI_DS_BIND_GUID, &drsuapi_bind_guid);
293 r_drsuapi_bind.in.bind_guid = &drsuapi_bind_guid;
294 r_drsuapi_bind.in.bind_info = NULL;
295 r_drsuapi_bind.out.bind_handle = &drsuapi_bind_handle;
297 status = dcerpc_drsuapi_DsBind(drsuapi_pipe, tmp_ctx, &r_drsuapi_bind);
298 if (!NT_STATUS_IS_OK(status)) {
299 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
302 "dcerpc_drsuapi_DsBind for [%s\\%s] failed - %s\n",
303 r->in.domain_name, r->in.account_name,
304 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
305 talloc_free(tmp_ctx);
310 "dcerpc_drsuapi_DsBind for [%s\\%s] failed - %s\n",
311 r->in.domain_name, r->in.account_name,
313 talloc_free(tmp_ctx);
316 } else if (!W_ERROR_IS_OK(r_drsuapi_bind.out.result)) {
319 "DsBind failed - %s\n",
320 win_errstr(r_drsuapi_bind.out.result));
321 talloc_free(tmp_ctx);
322 return NT_STATUS_UNSUCCESSFUL;
325 /* Actually 'crack' the names */
326 ZERO_STRUCT(r_crack_names);
327 r_crack_names.in.bind_handle = &drsuapi_bind_handle;
328 r_crack_names.in.level = 1;
329 r_crack_names.in.req.req1.unknown1 = 0x000004e4;
330 r_crack_names.in.req.req1.unknown2 = 0x00000407;
331 r_crack_names.in.req.req1.count = 1;
332 r_crack_names.in.req.req1.names = names;
333 r_crack_names.in.req.req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
334 r_crack_names.in.req.req1.format_offered= DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY;
335 r_crack_names.in.req.req1.format_desired= DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
336 names[0].str = dom_sid_string(tmp_ctx, r->out.account_sid);
338 r->out.error_string = NULL;
339 talloc_free(tmp_ctx);
340 return NT_STATUS_NO_MEMORY;
343 status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
344 if (!NT_STATUS_IS_OK(status)) {
345 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
348 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
350 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
351 talloc_free(tmp_ctx);
356 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
359 talloc_free(tmp_ctx);
362 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
365 "DsCrackNames failed - %s\n", win_errstr(r_crack_names.out.result));
366 talloc_free(tmp_ctx);
367 return NT_STATUS_UNSUCCESSFUL;
368 } else if (r_crack_names.out.level != 1
369 || !r_crack_names.out.ctr.ctr1
370 || r_crack_names.out.ctr.ctr1->count != 1
371 || !r_crack_names.out.ctr.ctr1->array[0].result_name
372 || r_crack_names.out.ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
373 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed\n");
374 talloc_free(tmp_ctx);
375 return NT_STATUS_UNSUCCESSFUL;
378 /* Store the DN of our machine account. */
379 account_dn_str = r_crack_names.out.ctr.ctr1->array[0].result_name;
381 account_dn = ldb_dn_explode(tmp_ctx, account_dn_str);
383 r->out.error_string = talloc_asprintf(r, "Invalid account dn: %s",
385 talloc_free(tmp_ctx);
386 return NT_STATUS_UNSUCCESSFUL;
389 /* Now we know the user's DN, open with LDAP, read and modify a few things */
391 remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s",
392 drsuapi_binding->host);
393 if (!remote_ldb_url) {
394 r->out.error_string = NULL;
395 talloc_free(tmp_ctx);
396 return NT_STATUS_NO_MEMORY;
399 remote_ldb = ldb_wrap_connect(tmp_ctx, remote_ldb_url,
400 NULL, ctx->cred, 0, NULL);
402 r->out.error_string = NULL;
403 talloc_free(tmp_ctx);
404 return NT_STATUS_UNSUCCESSFUL;
407 /* search for the user's record */
408 ret = ldb_search(remote_ldb, account_dn, LDB_SCOPE_BASE,
410 if (ret != LDB_SUCCESS || res->count != 1) {
411 r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - %s\n",
412 account_dn_str, ldb_errstring(remote_ldb));
413 talloc_free(tmp_ctx);
414 return NT_STATUS_UNSUCCESSFUL;
417 /* If we have a kvno recorded in AD, we need it locally as well */
418 kvno = ldb_msg_find_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
420 /* Prepare a new message, for the modify */
421 msg = ldb_msg_new(tmp_ctx);
423 r->out.error_string = NULL;
424 talloc_free(tmp_ctx);
425 return NT_STATUS_NO_MEMORY;
427 msg->dn = res->msgs[0]->dn;
431 const char *service_principal_name[6];
432 const char *dns_host_name = strlower_talloc(tmp_ctx,
433 talloc_asprintf(tmp_ctx,
438 if (!dns_host_name) {
439 r->out.error_string = NULL;
440 talloc_free(tmp_ctx);
441 return NT_STATUS_NO_MEMORY;
444 service_principal_name[0] = talloc_asprintf(tmp_ctx, "host/%s", dns_host_name);
445 service_principal_name[1] = talloc_asprintf(tmp_ctx, "host/%s", strlower_talloc(tmp_ctx, r->in.netbios_name));
446 service_principal_name[2] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, realm);
447 service_principal_name[3] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), realm);
448 service_principal_name[4] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, r->out.domain_name);
449 service_principal_name[5] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), r->out.domain_name);
451 for (i=0; i < ARRAY_SIZE(service_principal_name); i++) {
452 if (!service_principal_name[i]) {
453 r->out.error_string = NULL;
454 talloc_free(tmp_ctx);
455 return NT_STATUS_NO_MEMORY;
457 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "servicePrincipalName", service_principal_name[i]);
459 r->out.error_string = NULL;
460 talloc_free(tmp_ctx);
461 return NT_STATUS_NO_MEMORY;
465 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "dNSHostName", dns_host_name);
467 r->out.error_string = NULL;
468 talloc_free(tmp_ctx);
469 return NT_STATUS_NO_MEMORY;
472 rtn = samdb_replace(remote_ldb, tmp_ctx, msg);
476 "Failed to replace entries on %s\n",
477 ldb_dn_linearize(tmp_ctx, msg->dn));
478 talloc_free(tmp_ctx);
479 return NT_STATUS_INTERNAL_DB_CORRUPTION;
483 /* DsCrackNames to find out the DN of the domain. */
484 r_crack_names.in.req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
485 r_crack_names.in.req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
486 names[0].str = talloc_asprintf(tmp_ctx, "%s\\", r->out.domain_name);
488 r->out.error_string = NULL;
489 talloc_free(tmp_ctx);
490 return NT_STATUS_NO_MEMORY;
493 status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
494 if (!NT_STATUS_IS_OK(status)) {
495 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
498 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
500 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
501 talloc_free(tmp_ctx);
506 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
509 talloc_free(tmp_ctx);
512 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
515 "DsCrackNames failed - %s\n", win_errstr(r_crack_names.out.result));
516 talloc_free(tmp_ctx);
517 return NT_STATUS_UNSUCCESSFUL;
518 } else if (r_crack_names.out.level != 1
519 || !r_crack_names.out.ctr.ctr1
520 || r_crack_names.out.ctr.ctr1->count != 1
521 || !r_crack_names.out.ctr.ctr1->array[0].result_name
522 || r_crack_names.out.ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
523 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed\n");
524 talloc_free(tmp_ctx);
525 return NT_STATUS_UNSUCCESSFUL;
528 /* Store the account DN. */
529 r->out.account_dn_str = account_dn_str;
530 talloc_steal(r, account_dn_str);
532 /* Store the domain DN. */
533 r->out.domain_dn_str = r_crack_names.out.ctr.ctr1->array[0].result_name;
534 talloc_steal(r, r_crack_names.out.ctr.ctr1->array[0].result_name);
538 if (r->in.acct_type == ACB_SVRTRUST) {
539 status = libnet_JoinSite(ctx,
540 drsuapi_pipe, drsuapi_bind_handle,
543 talloc_free(tmp_ctx);
549 * do a domain join using DCERPC/SAMR calls
550 * 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation)
551 * is it correct to contact the the pdc of the domain of the user who's password should be set?
552 * 2. do a samr_Connect to get a policy handle
553 * 3. do a samr_LookupDomain to get the domain sid
554 * 4. do a samr_OpenDomain to get a domain handle
555 * 5. do a samr_CreateAccount to try and get a new account
558 * 5.1. do a samr_LookupNames to get the users rid
559 * 5.2. do a samr_OpenUser to get a user handle
561 * 6. call libnet_SetPassword_samr_handle to set the password
563 * 7. do a samrSetUserInfo to set the account flags
564 * 8. do some ADS specific things when we join as Domain Controller,
565 * look at libnet_joinADSDomain() for the details
567 NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *r)
571 NTSTATUS status, cu_status;
572 struct libnet_RpcConnect *c;
573 struct lsa_ObjectAttribute attr;
574 struct lsa_QosInfo qos;
575 struct lsa_OpenPolicy2 lsa_open_policy;
576 struct policy_handle lsa_p_handle;
577 struct lsa_QueryInfoPolicy2 lsa_query_info2;
578 struct lsa_QueryInfoPolicy lsa_query_info;
580 struct dcerpc_binding *samr_binding;
581 struct dcerpc_pipe *samr_pipe;
582 struct dcerpc_pipe *lsa_pipe;
583 struct samr_Connect sc;
584 struct policy_handle p_handle;
585 struct samr_OpenDomain od;
586 struct policy_handle d_handle;
587 struct samr_LookupNames ln;
588 struct samr_OpenUser ou;
589 struct samr_CreateUser2 cu;
590 struct policy_handle *u_handle = NULL;
591 struct samr_QueryUserInfo qui;
592 struct samr_SetUserInfo sui;
593 union samr_UserInfo u_info;
594 union libnet_SetPassword r2;
595 struct samr_GetUserPwInfo pwp;
596 struct lsa_String samr_account_name;
598 uint32_t acct_flags, old_acct_flags;
599 uint32_t rid, access_granted;
600 int policy_min_pw_len = 0;
602 struct dom_sid *domain_sid = NULL;
603 struct dom_sid *account_sid = NULL;
604 const char *domain_name = NULL;
605 const char *password_str = NULL;
606 const char *realm = NULL; /* Also flag for remote being AD */
609 r->out.error_string = NULL;
610 r2.samr_handle.out.error_string = NULL;
612 tmp_ctx = talloc_named(mem_ctx, 0, "libnet_Join temp context");
614 r->out.error_string = NULL;
615 return NT_STATUS_NO_MEMORY;
618 u_handle = talloc(tmp_ctx, struct policy_handle);
620 r->out.error_string = NULL;
621 talloc_free(tmp_ctx);
622 return NT_STATUS_NO_MEMORY;
625 samr_pipe = talloc(tmp_ctx, struct dcerpc_pipe);
627 r->out.error_string = NULL;
628 talloc_free(tmp_ctx);
629 return NT_STATUS_NO_MEMORY;
632 c = talloc(tmp_ctx, struct libnet_RpcConnect);
634 r->out.error_string = NULL;
635 talloc_free(tmp_ctx);
636 return NT_STATUS_NO_MEMORY;
639 /* prepare connect to the LSA pipe of PDC */
640 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
641 c->level = LIBNET_RPC_CONNECT_PDC;
642 c->in.domain_name = r->in.domain_name;
644 c->level = LIBNET_RPC_CONNECT_BINDING;
645 c->in.binding = r->in.binding;
647 c->in.dcerpc_iface = &dcerpc_table_lsarpc;
649 /* connect to the LSA pipe of the PDC */
651 status = libnet_RpcConnect(ctx, c, c);
652 if (!NT_STATUS_IS_OK(status)) {
653 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
654 r->out.error_string = talloc_asprintf(mem_ctx,
655 "Connection to LSA pipe of PDC of domain '%s' failed: %s",
656 r->in.domain_name, nt_errstr(status));
658 r->out.error_string = talloc_asprintf(mem_ctx,
659 "Connection to LSA pipe with binding '%s' failed: %s",
660 r->in.binding, nt_errstr(status));
662 talloc_free(tmp_ctx);
665 lsa_pipe = c->out.dcerpc_pipe;
667 /* Get an LSA policy handle */
669 ZERO_STRUCT(lsa_p_handle);
671 qos.impersonation_level = 2;
672 qos.context_mode = 1;
673 qos.effective_only = 0;
676 attr.root_dir = NULL;
677 attr.object_name = NULL;
679 attr.sec_desc = NULL;
682 lsa_open_policy.in.attr = &attr;
684 lsa_open_policy.in.system_name = talloc_asprintf(tmp_ctx, "\\");
685 if (!lsa_open_policy.in.system_name) {
686 r->out.error_string = NULL;
687 talloc_free(tmp_ctx);
688 return NT_STATUS_NO_MEMORY;
691 lsa_open_policy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
692 lsa_open_policy.out.handle = &lsa_p_handle;
694 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, tmp_ctx, &lsa_open_policy);
696 /* This now fails on ncacn_ip_tcp against Win2k3 SP1 */
697 if (NT_STATUS_IS_OK(status)) {
698 /* Look to see if this is ADS (a fault indicates NT4 or Samba 3.0) */
700 lsa_query_info2.in.handle = &lsa_p_handle;
701 lsa_query_info2.in.level = LSA_POLICY_INFO_DNS;
703 status = dcerpc_lsa_QueryInfoPolicy2(lsa_pipe, tmp_ctx,
706 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
707 if (!NT_STATUS_IS_OK(status)) {
708 r->out.error_string = talloc_asprintf(mem_ctx,
709 "lsa_QueryInfoPolicy2 failed: %s",
711 talloc_free(tmp_ctx);
714 realm = lsa_query_info2.out.info->dns.dns_domain.string;
717 /* Grab the domain SID (regardless of the result of the previous call */
719 lsa_query_info.in.handle = &lsa_p_handle;
720 lsa_query_info.in.level = LSA_POLICY_INFO_DOMAIN;
722 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, tmp_ctx,
725 if (!NT_STATUS_IS_OK(status)) {
726 r->out.error_string = talloc_asprintf(mem_ctx,
727 "lsa_QueryInfoPolicy2 failed: %s",
729 talloc_free(tmp_ctx);
733 domain_sid = lsa_query_info.out.info->domain.sid;
734 domain_name = lsa_query_info.out.info->domain.name.string;
736 /* Cause the code further down to try this with just SAMR */
738 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
739 domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
741 /* Bugger, we just lost our way to automaticly find the domain name */
742 domain_name = talloc_strdup(tmp_ctx, lp_workgroup());
747 establish a SAMR connection, on the same CIFS transport
750 /* Find the original binding string */
751 status = dcerpc_parse_binding(tmp_ctx, lsa_pipe->conn->binding_string, &samr_binding);
752 if (!NT_STATUS_IS_OK(status)) {
753 r->out.error_string = talloc_asprintf(mem_ctx,
754 "Failed to parse lsa binding '%s'",
755 lsa_pipe->conn->binding_string);
756 talloc_free(tmp_ctx);
760 /* Make binding string for samr, not the other pipe */
761 status = dcerpc_epm_map_binding(tmp_ctx, samr_binding,
763 lsa_pipe->conn->event_ctx);
764 if (!NT_STATUS_IS_OK(status)) {
765 r->out.error_string = talloc_asprintf(mem_ctx,
766 "Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s",
767 DCERPC_NETLOGON_UUID,
769 talloc_free(tmp_ctx);
773 /* Setup a SAMR connection */
774 status = dcerpc_secondary_connection(lsa_pipe, &samr_pipe, samr_binding);
775 if (!NT_STATUS_IS_OK(status)) {
776 r->out.error_string = talloc_asprintf(mem_ctx,
777 "SAMR secondary connection failed: %s",
779 talloc_free(tmp_ctx);
783 status = dcerpc_pipe_auth(samr_pipe, samr_binding, &dcerpc_table_samr, ctx->cred);
784 if (!NT_STATUS_IS_OK(status)) {
785 r->out.error_string = talloc_asprintf(mem_ctx,
786 "SAMR bind failed: %s",
788 talloc_free(tmp_ctx);
792 /* prepare samr_Connect */
793 ZERO_STRUCT(p_handle);
794 sc.in.system_name = NULL;
795 sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
796 sc.out.connect_handle = &p_handle;
798 /* 2. do a samr_Connect to get a policy handle */
799 status = dcerpc_samr_Connect(samr_pipe, tmp_ctx, &sc);
800 if (!NT_STATUS_IS_OK(status)) {
801 r->out.error_string = talloc_asprintf(mem_ctx,
802 "samr_Connect failed: %s",
804 talloc_free(tmp_ctx);
808 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
810 struct lsa_String name;
811 struct samr_LookupDomain l;
812 name.string = domain_name;
813 l.in.connect_handle = &p_handle;
814 l.in.domain_name = &name;
816 status = dcerpc_samr_LookupDomain(samr_pipe, tmp_ctx, &l);
817 if (!NT_STATUS_IS_OK(status)) {
818 r->out.error_string = talloc_asprintf(mem_ctx,
819 "SAMR LookupDomain failed: %s",
821 talloc_free(tmp_ctx);
824 domain_sid = l.out.sid;
827 /* prepare samr_OpenDomain */
828 ZERO_STRUCT(d_handle);
829 od.in.connect_handle = &p_handle;
830 od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
831 od.in.sid = domain_sid;
832 od.out.domain_handle = &d_handle;
834 /* do a samr_OpenDomain to get a domain handle */
835 status = dcerpc_samr_OpenDomain(samr_pipe, tmp_ctx, &od);
836 if (!NT_STATUS_IS_OK(status)) {
837 r->out.error_string = talloc_asprintf(mem_ctx,
838 "samr_OpenDomain for [%s] failed: %s",
841 talloc_free(tmp_ctx);
845 /* prepare samr_CreateUser2 */
846 ZERO_STRUCTP(u_handle);
847 cu.in.domain_handle = &d_handle;
848 cu.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
849 samr_account_name.string = r->in.account_name;
850 cu.in.account_name = &samr_account_name;
851 cu.in.acct_flags = r->in.acct_type;
852 cu.out.user_handle = u_handle;
854 cu.out.access_granted = &access_granted;
856 /* do a samr_CreateUser2 to get an account handle, or an error */
857 cu_status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
859 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
860 /* prepare samr_LookupNames */
861 ln.in.domain_handle = &d_handle;
863 ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
865 r->out.error_string = NULL;
866 talloc_free(tmp_ctx);
867 return NT_STATUS_NO_MEMORY;
869 ln.in.names[0].string = r->in.account_name;
871 /* 5. do a samr_LookupNames to get the users rid */
872 status = dcerpc_samr_LookupNames(samr_pipe, tmp_ctx, &ln);
873 if (!NT_STATUS_IS_OK(status)) {
874 r->out.error_string = talloc_asprintf(mem_ctx,
875 "samr_LookupNames for [%s] failed: %s",
878 talloc_free(tmp_ctx);
882 /* check if we got one RID for the user */
883 if (ln.out.rids.count != 1) {
884 r->out.error_string = talloc_asprintf(mem_ctx,
885 "samr_LookupNames for [%s] returns %d RIDs\n",
886 r->in.account_name, ln.out.rids.count);
887 talloc_free(tmp_ctx);
888 return NT_STATUS_INVALID_PARAMETER;
891 /* prepare samr_OpenUser */
892 ZERO_STRUCTP(u_handle);
893 ou.in.domain_handle = &d_handle;
894 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
895 ou.in.rid = ln.out.rids.ids[0];
897 ou.out.user_handle = u_handle;
899 /* 6. do a samr_OpenUser to get a user handle */
900 status = dcerpc_samr_OpenUser(samr_pipe, tmp_ctx, &ou);
901 if (!NT_STATUS_IS_OK(status)) {
902 r->out.error_string = talloc_asprintf(mem_ctx,
903 "samr_OpenUser for [%s] failed: %s",
906 talloc_free(tmp_ctx);
910 if (r->in.recreate_account) {
911 struct samr_DeleteUser d;
912 d.in.user_handle = u_handle;
913 d.out.user_handle = u_handle;
914 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
915 if (!NT_STATUS_IS_OK(status)) {
916 r->out.error_string = talloc_asprintf(mem_ctx,
917 "samr_DeleteUser (for recreate) of [%s] failed: %s",
920 talloc_free(tmp_ctx);
924 /* We want to recreate, so delete and another samr_CreateUser2 */
926 /* &cu filled in above */
927 status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
928 if (!NT_STATUS_IS_OK(status)) {
929 r->out.error_string = talloc_asprintf(mem_ctx,
930 "samr_CreateUser2 (recreate) for [%s] failed: %s\n",
931 r->in.domain_name, nt_errstr(status));
932 talloc_free(tmp_ctx);
936 } else if (!NT_STATUS_IS_OK(status)) {
937 r->out.error_string = talloc_asprintf(mem_ctx,
938 "samr_CreateUser2 for [%s] failed: %s\n",
939 r->in.domain_name, nt_errstr(status));
940 talloc_free(tmp_ctx);
944 /* prepare samr_QueryUserInfo (get flags) */
945 qui.in.user_handle = u_handle;
948 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
949 if (!NT_STATUS_IS_OK(status)) {
950 r->out.error_string = talloc_asprintf(mem_ctx,
951 "samr_QueryUserInfo for [%s] failed: %s",
954 talloc_free(tmp_ctx);
959 status = NT_STATUS_INVALID_PARAMETER;
961 = talloc_asprintf(mem_ctx,
962 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s\n",
963 r->in.account_name, nt_errstr(status));
964 talloc_free(tmp_ctx);
968 old_acct_flags = (qui.out.info->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
969 /* Possibly bail if the account is of the wrong type */
971 != r->in.acct_type) {
972 const char *old_account_type, *new_account_type;
973 switch (old_acct_flags) {
975 old_account_type = "domain member (member)";
978 old_account_type = "domain controller (bdc)";
981 old_account_type = "trusted domain";
984 return NT_STATUS_INVALID_PARAMETER;
986 switch (r->in.acct_type) {
988 new_account_type = "domain member (member)";
991 new_account_type = "domain controller (bdc)";
994 new_account_type = "trusted domain";
997 return NT_STATUS_INVALID_PARAMETER;
1000 if (!NT_STATUS_EQUAL(cu_status, NT_STATUS_USER_EXISTS)) {
1001 /* We created a new user, but they didn't come out the right type?!? */
1003 = talloc_asprintf(mem_ctx,
1004 "We asked to create a new machine account (%s) of type %s, but we got an account of type %s. This is unexpected. Perhaps delete the account and try again.\n",
1005 r->in.account_name, new_account_type, old_account_type);
1006 talloc_free(tmp_ctx);
1007 return NT_STATUS_INVALID_PARAMETER;
1009 /* The account is of the wrong type, so bail */
1011 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
1013 = talloc_asprintf(mem_ctx,
1014 "The machine account (%s) already exists in the domain %s, but is a %s. You asked to join as a %s. Please delete the account and try again.\n",
1015 r->in.account_name, domain_name, old_account_type, new_account_type);
1016 talloc_free(tmp_ctx);
1017 return NT_STATUS_USER_EXISTS;
1020 acct_flags = qui.out.info->info16.acct_flags;
1023 acct_flags = (acct_flags & ~ACB_DISABLED);
1025 /* Find out what password policy this user has */
1026 pwp.in.user_handle = u_handle;
1028 status = dcerpc_samr_GetUserPwInfo(samr_pipe, tmp_ctx, &pwp);
1029 if (NT_STATUS_IS_OK(status)) {
1030 policy_min_pw_len = pwp.out.info.min_password_length;
1033 /* Grab a password of that minimum length */
1035 password_str = generate_random_str(tmp_ctx, MAX(8, policy_min_pw_len));
1037 r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
1038 r2.samr_handle.in.account_name = r->in.account_name;
1039 r2.samr_handle.in.newpassword = password_str;
1040 r2.samr_handle.in.user_handle = u_handle;
1041 r2.samr_handle.in.dcerpc_pipe = samr_pipe;
1043 status = libnet_SetPassword(ctx, tmp_ctx, &r2);
1044 if (!NT_STATUS_IS_OK(status)) {
1045 r->out.error_string = talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
1046 talloc_free(tmp_ctx);
1050 /* reset flags (if required) */
1051 if (acct_flags != qui.out.info->info16.acct_flags) {
1052 ZERO_STRUCT(u_info);
1053 u_info.info16.acct_flags = acct_flags;
1055 sui.in.user_handle = u_handle;
1056 sui.in.info = &u_info;
1059 dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
1060 if (!NT_STATUS_IS_OK(status)) {
1061 r->out.error_string = talloc_asprintf(mem_ctx,
1062 "samr_SetUserInfo for [%s] failed to remove ACB_DISABLED flag: %s",
1065 talloc_free(tmp_ctx);
1070 account_sid = dom_sid_add_rid(mem_ctx, domain_sid, rid);
1072 r->out.error_string = NULL;
1073 talloc_free(tmp_ctx);
1074 return NT_STATUS_NO_MEMORY;
1077 /* Finish out by pushing various bits of status data out for the caller to use */
1078 r->out.join_password = password_str;
1079 talloc_steal(mem_ctx, password_str);
1081 r->out.domain_sid = domain_sid;
1082 talloc_steal(mem_ctx, domain_sid);
1084 r->out.account_sid = account_sid;
1085 talloc_steal(mem_ctx, account_sid);
1087 r->out.domain_name = domain_name;
1088 talloc_steal(mem_ctx, domain_name);
1089 r->out.realm = realm;
1090 talloc_steal(mem_ctx, realm);
1091 r->out.lsa_pipe = lsa_pipe;
1092 talloc_steal(mem_ctx, lsa_pipe);
1093 r->out.samr_pipe = samr_pipe;
1094 talloc_steal(mem_ctx, samr_pipe);
1095 r->out.samr_binding = samr_binding;
1096 talloc_steal(mem_ctx, samr_binding);
1097 r->out.user_handle = u_handle;
1098 talloc_steal(mem_ctx, u_handle);
1099 r->out.error_string = r2.samr_handle.out.error_string;
1100 talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
1102 r->out.server_dn_str = NULL;
1103 talloc_free(tmp_ctx);
1105 /* Now, if it was AD, then we want to start looking changing a
1106 * few more things. Otherwise, we are done. */
1108 status = libnet_JoinADSDomain(ctx, r);
1115 static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
1116 TALLOC_CTX *mem_ctx,
1117 struct libnet_Join *r)
1120 TALLOC_CTX *tmp_mem;
1121 struct libnet_JoinDomain *r2;
1123 struct ldb_context *ldb;
1124 const struct ldb_dn *base_dn;
1125 struct ldb_message **msgs, *msg;
1127 const char * const attrs[] = {
1134 uint32_t acct_type = 0;
1135 const char *account_name;
1136 const char *netbios_name;
1139 r->out.error_string = NULL;
1141 tmp_mem = talloc_new(mem_ctx);
1143 return NT_STATUS_NO_MEMORY;
1146 r2 = talloc(tmp_mem, struct libnet_JoinDomain);
1148 r->out.error_string = NULL;
1149 talloc_free(tmp_mem);
1150 return NT_STATUS_NO_MEMORY;
1153 if (r->in.secure_channel_type == SEC_CHAN_BDC) {
1154 acct_type = ACB_SVRTRUST;
1155 } else if (r->in.secure_channel_type == SEC_CHAN_WKSTA) {
1156 acct_type = ACB_WSTRUST;
1158 r->out.error_string = NULL;
1159 talloc_free(tmp_mem);
1160 return NT_STATUS_INVALID_PARAMETER;
1163 if ((r->in.netbios_name != NULL) && (r->in.level != LIBNET_JOIN_AUTOMATIC)) {
1164 netbios_name = r->in.netbios_name;
1166 netbios_name = talloc_asprintf(tmp_mem, "%s", lp_netbios_name());
1167 if (!netbios_name) {
1168 r->out.error_string = NULL;
1169 talloc_free(tmp_mem);
1170 return NT_STATUS_NO_MEMORY;
1174 account_name = talloc_asprintf(tmp_mem, "%s$", netbios_name);
1175 if (!account_name) {
1176 r->out.error_string = NULL;
1177 talloc_free(tmp_mem);
1178 return NT_STATUS_NO_MEMORY;
1182 * Local secrets are stored in secrets.ldb
1183 * open it to make sure we can write the info into it after the join
1185 ldb = secrets_db_connect(tmp_mem);
1188 = talloc_asprintf(mem_ctx,
1189 "Could not open secrets database\n");
1190 talloc_free(tmp_mem);
1191 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1198 r2->in.domain_name = r->in.domain_name;
1199 r2->in.account_name = account_name;
1200 r2->in.netbios_name = netbios_name;
1201 r2->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
1202 r2->in.acct_type = acct_type;
1203 r2->in.recreate_account = False;
1204 status = libnet_JoinDomain(ctx, r2, r2);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 r->out.error_string = talloc_steal(mem_ctx, r2->out.error_string);
1207 talloc_free(tmp_mem);
1212 * now prepare the record for secrets.ldb
1214 sct = talloc_asprintf(tmp_mem, "%d", r->in.secure_channel_type);
1216 r->out.error_string = NULL;
1217 talloc_free(tmp_mem);
1218 return NT_STATUS_NO_MEMORY;
1221 msg = ldb_msg_new(tmp_mem);
1223 r->out.error_string = NULL;
1224 talloc_free(tmp_mem);
1225 return NT_STATUS_NO_MEMORY;
1228 base_dn = ldb_dn_explode(tmp_mem, "cn=Primary Domains");
1230 r->out.error_string = NULL;
1231 talloc_free(tmp_mem);
1232 return NT_STATUS_NO_MEMORY;
1235 msg->dn = ldb_dn_build_child(tmp_mem, "flatname", r2->out.domain_name, base_dn);
1237 r->out.error_string = NULL;
1238 talloc_free(tmp_mem);
1239 return NT_STATUS_NO_MEMORY;
1242 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "flatname", r2->out.domain_name);
1244 r->out.error_string = NULL;
1245 talloc_free(tmp_mem);
1246 return NT_STATUS_NO_MEMORY;
1249 if (r2->out.realm) {
1250 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "realm", r2->out.realm);
1252 r->out.error_string = NULL;
1253 talloc_free(tmp_mem);
1254 return NT_STATUS_NO_MEMORY;
1257 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
1259 r->out.error_string = NULL;
1260 talloc_free(tmp_mem);
1261 return NT_STATUS_NO_MEMORY;
1265 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
1267 r->out.error_string = NULL;
1268 talloc_free(tmp_mem);
1269 return NT_STATUS_NO_MEMORY;
1272 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
1274 r->out.error_string = NULL;
1275 talloc_free(tmp_mem);
1276 return NT_STATUS_NO_MEMORY;
1279 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1281 r->out.error_string = NULL;
1282 talloc_free(tmp_mem);
1283 return NT_STATUS_NO_MEMORY;
1286 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1288 r->out.error_string = NULL;
1289 talloc_free(tmp_mem);
1290 return NT_STATUS_NO_MEMORY;
1294 rtn = samdb_msg_add_uint(ldb, tmp_mem, msg, "msDS-KeyVersionNumber",
1297 r->out.error_string = NULL;
1298 talloc_free(tmp_mem);
1299 return NT_STATUS_NO_MEMORY;
1303 if (r2->out.domain_sid) {
1304 rtn = samdb_msg_add_dom_sid(ldb, tmp_mem, msg, "objectSid",
1305 r2->out.domain_sid);
1307 r->out.error_string = NULL;
1308 talloc_free(tmp_mem);
1309 return NT_STATUS_NO_MEMORY;
1314 * search for the secret record
1315 * - remove the records we find
1316 * - and fetch the old secret and store it under priorSecret
1318 ret = gendb_search(ldb,
1321 "(|" SECRETS_PRIMARY_DOMAIN_FILTER "(realm=%s))",
1322 r2->out.domain_name, r2->out.realm);
1324 } else if (ret == -1) {
1326 = talloc_asprintf(mem_ctx,
1327 "Search for domain: %s and realm: %s failed: %s",
1328 r2->out.domain_name, r2->out.realm, ldb_errstring(ldb));
1329 talloc_free(tmp_mem);
1330 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1332 const struct ldb_val *prior_secret;
1333 const struct ldb_val *prior_modified_time;
1336 for (i = 0; i < ret; i++) {
1337 ldb_delete(ldb, msgs[i]->dn);
1340 prior_secret = ldb_msg_find_ldb_val(msgs[0], "secret");
1342 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorSecret", prior_secret);
1344 r->out.error_string = NULL;
1345 talloc_free(tmp_mem);
1346 return NT_STATUS_NO_MEMORY;
1349 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
1351 r->out.error_string = NULL;
1352 talloc_free(tmp_mem);
1353 return NT_STATUS_NO_MEMORY;
1356 prior_modified_time = ldb_msg_find_ldb_val(msgs[0],
1358 if (prior_modified_time) {
1359 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorWhenChanged",
1360 prior_modified_time);
1362 r->out.error_string = NULL;
1363 talloc_free(tmp_mem);
1364 return NT_STATUS_NO_MEMORY;
1368 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1370 r->out.error_string = NULL;
1371 talloc_free(tmp_mem);
1372 return NT_STATUS_NO_MEMORY;
1375 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1377 r->out.error_string = NULL;
1378 talloc_free(tmp_mem);
1379 return NT_STATUS_NO_MEMORY;
1383 /* create the secret */
1384 ret = samdb_add(ldb, tmp_mem, msg);
1386 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to create secret record %s\n",
1387 ldb_dn_linearize(ldb, msg->dn));
1388 talloc_free(tmp_mem);
1389 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1392 if (r2->out.realm) {
1393 struct cli_credentials *creds;
1394 /* Make a credentials structure from it */
1395 creds = cli_credentials_init(mem_ctx);
1397 r->out.error_string = NULL;
1398 talloc_free(tmp_mem);
1399 return NT_STATUS_NO_MEMORY;
1401 cli_credentials_set_conf(creds);
1402 filter = talloc_asprintf(mem_ctx, "dn=%s", ldb_dn_linearize(mem_ctx, msg->dn));
1403 status = cli_credentials_set_secrets(creds, NULL, filter);
1404 if (!NT_STATUS_IS_OK(status)) {
1405 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to read secrets for keytab update for %s\n",
1407 talloc_free(tmp_mem);
1410 ret = cli_credentials_update_keytab(creds);
1412 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to update keytab for %s\n",
1414 talloc_free(tmp_mem);
1415 return NT_STATUS_UNSUCCESSFUL;
1419 /* move all out parameter to the callers TALLOC_CTX */
1420 r->out.error_string = NULL;
1421 r->out.join_password = r2->out.join_password;
1422 talloc_steal(mem_ctx, r2->out.join_password);
1423 r->out.domain_sid = r2->out.domain_sid;
1424 talloc_steal(mem_ctx, r2->out.domain_sid);
1425 r->out.domain_name = r2->out.domain_name;
1426 talloc_steal(mem_ctx, r2->out.domain_name);
1427 talloc_free(tmp_mem);
1428 return NT_STATUS_OK;
1431 NTSTATUS libnet_Join(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_Join *r)
1433 switch (r->in.secure_channel_type) {
1434 case SEC_CHAN_WKSTA:
1435 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1437 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1438 case SEC_CHAN_DOMAIN:
1442 r->out.error_string = talloc_asprintf(mem_ctx,
1443 "Invalid secure channel type specified (%08X) attempting to join domain %s",
1444 r->in.secure_channel_type, r->in.domain_name);
1445 return NT_STATUS_INVALID_PARAMETER;