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"
35 * find out Site specific stuff:
36 * 1.) setup an CLDAP socket
37 * 2.) lookup the Site name
38 * 3.) Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
39 * TODO: 4.) use DsAddEntry() to create CN=NTDS Settings,CN=<netbios name>,CN=Servers,CN=<site name>...
41 static NTSTATUS libnet_JoinSite(struct libnet_context *ctx,
42 struct dcerpc_pipe *drsuapi_pipe,
43 struct policy_handle drsuapi_bind_handle,
44 struct ldb_context *remote_ldb,
45 struct libnet_JoinDomain *libnet_r)
50 struct cldap_socket *cldap = NULL;
51 struct cldap_netlogon search;
53 struct ldb_dn *server_dn;
54 struct ldb_message *msg;
57 const char *site_name;
58 const char *server_dn_str;
59 const char *config_dn_str;
61 tmp_ctx = talloc_named(libnet_r, 0, "libnet_JoinSite temp context");
63 libnet_r->out.error_string = NULL;
64 return NT_STATUS_NO_MEMORY;
67 /* Resolve the site name. */
70 search.in.dest_address = libnet_r->out.samr_binding->host;
71 search.in.acct_control = -1;
72 search.in.version = 6;
74 cldap = cldap_socket_init(tmp_ctx, NULL);
75 status = cldap_netlogon(cldap, tmp_ctx, &search);
76 if (!NT_STATUS_IS_OK(status)) {
77 /* Default to using Default-First-Site-Name rather than returning status at this point. */
78 site_name = talloc_asprintf(tmp_ctx, "%s", "Default-First-Site-Name");
80 libnet_r->out.error_string = NULL;
82 return NT_STATUS_NO_MEMORY;
85 site_name = search.out.netlogon.logon5.site_name;
88 config_dn_str = talloc_asprintf(tmp_ctx, "CN=Configuration,%s", libnet_r->out.domain_dn_str);
90 libnet_r->out.error_string = NULL;
92 return NT_STATUS_NO_MEMORY;
95 server_dn_str = talloc_asprintf(tmp_ctx, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
96 libnet_r->in.netbios_name, site_name, config_dn_str);
98 libnet_r->out.error_string = NULL;
100 return NT_STATUS_NO_MEMORY;
104 Add entry CN=<netbios name>,CN=Servers,CN=<site name>,CN=Sites,CN=Configuration,<domain dn>.
106 msg = ldb_msg_new(tmp_ctx);
108 libnet_r->out.error_string = NULL;
109 talloc_free(tmp_ctx);
110 return NT_STATUS_NO_MEMORY;
113 rtn = ldb_msg_add_string(msg, "objectClass", "server");
115 libnet_r->out.error_string = NULL;
116 talloc_free(tmp_ctx);
117 return NT_STATUS_NO_MEMORY;
119 rtn = ldb_msg_add_string(msg, "systemFlags", "50000000");
121 libnet_r->out.error_string = NULL;
122 talloc_free(tmp_ctx);
123 return NT_STATUS_NO_MEMORY;
125 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
127 libnet_r->out.error_string = NULL;
128 talloc_free(tmp_ctx);
129 return NT_STATUS_NO_MEMORY;
132 server_dn = ldb_dn_explode(tmp_ctx, server_dn_str);
133 if (server_dn == NULL) {
134 libnet_r->out.error_string = talloc_asprintf(libnet_r,
135 "Invalid server dn: %s",
137 talloc_free(tmp_ctx);
138 return NT_STATUS_UNSUCCESSFUL;
143 rtn = ldb_add(remote_ldb, msg);
144 if (rtn == LDB_ERR_ENTRY_ALREADY_EXISTS) {
147 /* make a 'modify' msg, and only for serverReference */
148 msg = ldb_msg_new(tmp_ctx);
150 libnet_r->out.error_string = NULL;
151 talloc_free(tmp_ctx);
152 return NT_STATUS_NO_MEMORY;
156 rtn = ldb_msg_add_string(msg, "serverReference",libnet_r->out.account_dn_str);
158 libnet_r->out.error_string = NULL;
159 talloc_free(tmp_ctx);
160 return NT_STATUS_NO_MEMORY;
163 /* mark all the message elements (should be just one)
164 as LDB_FLAG_MOD_REPLACE */
165 for (i=0;i<msg->num_elements;i++) {
166 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
169 rtn = ldb_modify(remote_ldb, msg);
171 libnet_r->out.error_string
172 = talloc_asprintf(libnet_r,
173 "Failed to modify server entry %s: %s: %d",
175 ldb_errstring(remote_ldb), rtn);
176 talloc_free(tmp_ctx);
177 return NT_STATUS_INTERNAL_DB_CORRUPTION;
179 } else if (rtn != 0) {
180 libnet_r->out.error_string
181 = talloc_asprintf(libnet_r,
182 "Failed to add server entry %s: %s: %d",
184 ldb_errstring(remote_ldb), rtn);
185 talloc_free(tmp_ctx);
186 return NT_STATUS_INTERNAL_DB_CORRUPTION;
188 DEBUG(0, ("We still need to perform a DsAddEntry() so that we can create the CN=NTDS Settings container.\n"));
190 /* Store the server DN in libnet_r */
191 libnet_r->out.server_dn_str = server_dn_str;
192 talloc_steal(libnet_r, server_dn_str);
194 talloc_free(tmp_ctx);
199 * complete a domain join, when joining to a AD domain:
200 * 1.) connect and bind to the DRSUAPI pipe
201 * 2.) do a DsCrackNames() to find the machine account dn
202 * 3.) connect to LDAP
203 * 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
204 * 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
205 * 6.) do a DsCrackNames() to find the domain dn
206 * 7.) find out Site specific stuff, look at libnet_JoinSite() for details
208 static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_JoinDomain *r)
214 const char *realm = r->out.realm;
216 struct dcerpc_binding *samr_binding = r->out.samr_binding;
218 struct dcerpc_pipe *drsuapi_pipe;
219 struct dcerpc_binding *drsuapi_binding;
220 struct drsuapi_DsBind r_drsuapi_bind;
221 struct drsuapi_DsCrackNames r_crack_names;
222 struct drsuapi_DsNameString names[1];
223 struct policy_handle drsuapi_bind_handle;
224 struct GUID drsuapi_bind_guid;
226 struct ldb_context *remote_ldb;
227 const struct ldb_dn *account_dn;
228 const char *account_dn_str;
229 const char *remote_ldb_url;
230 struct ldb_result *res;
231 struct ldb_message *msg;
237 const char * const attrs[] = {
238 "msDS-KeyVersionNumber",
239 "servicePrincipalName",
244 r->out.error_string = NULL;
246 /* We need to convert between a samAccountName and domain to a
247 * DN in the directory. The correct way to do this is with
248 * DRSUAPI CrackNames */
250 /* Fiddle with the bindings, so get to DRSUAPI on
251 * NCACN_IP_TCP, sealed */
252 tmp_ctx = talloc_named(r, 0, "libnet_JoinADSDomain temp context");
254 r->out.error_string = NULL;
255 return NT_STATUS_NO_MEMORY;
258 drsuapi_binding = talloc(tmp_ctx, struct dcerpc_binding);
259 if (!drsuapi_binding) {
260 r->out.error_string = NULL;
261 talloc_free(tmp_ctx);
262 return NT_STATUS_NO_MEMORY;
265 *drsuapi_binding = *samr_binding;
267 /* DRSUAPI is only available on IP_TCP, and locally on NCALRPC */
268 if (drsuapi_binding->transport != NCALRPC) {
269 drsuapi_binding->transport = NCACN_IP_TCP;
271 drsuapi_binding->endpoint = NULL;
272 drsuapi_binding->flags |= DCERPC_SEAL;
274 status = dcerpc_pipe_connect_b(tmp_ctx,
278 DCERPC_DRSUAPI_VERSION,
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_name = DCERPC_LSARPC_NAME;
648 c->in.dcerpc_iface_uuid = DCERPC_LSARPC_UUID;
649 c->in.dcerpc_iface_version = DCERPC_LSARPC_VERSION;
651 /* connect to the LSA pipe of the PDC */
653 status = libnet_RpcConnect(ctx, c, c);
654 if (!NT_STATUS_IS_OK(status)) {
655 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
656 r->out.error_string = talloc_asprintf(mem_ctx,
657 "Connection to LSA pipe of PDC of domain '%s' failed: %s",
658 r->in.domain_name, nt_errstr(status));
660 r->out.error_string = talloc_asprintf(mem_ctx,
661 "Connection to LSA pipe with binding '%s' failed: %s",
662 r->in.binding, nt_errstr(status));
664 talloc_free(tmp_ctx);
667 lsa_pipe = c->out.dcerpc_pipe;
669 /* Get an LSA policy handle */
671 ZERO_STRUCT(lsa_p_handle);
673 qos.impersonation_level = 2;
674 qos.context_mode = 1;
675 qos.effective_only = 0;
678 attr.root_dir = NULL;
679 attr.object_name = NULL;
681 attr.sec_desc = NULL;
684 lsa_open_policy.in.attr = &attr;
686 lsa_open_policy.in.system_name = talloc_asprintf(tmp_ctx, "\\");
687 if (!lsa_open_policy.in.system_name) {
688 r->out.error_string = NULL;
689 talloc_free(tmp_ctx);
690 return NT_STATUS_NO_MEMORY;
693 lsa_open_policy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
694 lsa_open_policy.out.handle = &lsa_p_handle;
696 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, tmp_ctx, &lsa_open_policy);
698 /* This now fails on ncacn_ip_tcp against Win2k3 SP1 */
699 if (NT_STATUS_IS_OK(status)) {
700 /* Look to see if this is ADS (a fault indicates NT4 or Samba 3.0) */
702 lsa_query_info2.in.handle = &lsa_p_handle;
703 lsa_query_info2.in.level = LSA_POLICY_INFO_DNS;
705 status = dcerpc_lsa_QueryInfoPolicy2(lsa_pipe, tmp_ctx,
708 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
709 if (!NT_STATUS_IS_OK(status)) {
710 r->out.error_string = talloc_asprintf(mem_ctx,
711 "lsa_QueryInfoPolicy2 failed: %s",
713 talloc_free(tmp_ctx);
716 realm = lsa_query_info2.out.info->dns.dns_domain.string;
719 /* Grab the domain SID (regardless of the result of the previous call */
721 lsa_query_info.in.handle = &lsa_p_handle;
722 lsa_query_info.in.level = LSA_POLICY_INFO_DOMAIN;
724 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, tmp_ctx,
727 if (!NT_STATUS_IS_OK(status)) {
728 r->out.error_string = talloc_asprintf(mem_ctx,
729 "lsa_QueryInfoPolicy2 failed: %s",
731 talloc_free(tmp_ctx);
735 domain_sid = lsa_query_info.out.info->domain.sid;
736 domain_name = lsa_query_info.out.info->domain.name.string;
738 /* Cause the code further down to try this with just SAMR */
740 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
741 domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
743 /* Bugger, we just lost our way to automaticly find the domain name */
744 domain_name = talloc_strdup(tmp_ctx, lp_workgroup());
749 establish a SAMR connection, on the same CIFS transport
752 /* Find the original binding string */
753 status = dcerpc_parse_binding(tmp_ctx, lsa_pipe->conn->binding_string, &samr_binding);
754 if (!NT_STATUS_IS_OK(status)) {
755 r->out.error_string = talloc_asprintf(mem_ctx,
756 "Failed to parse lsa binding '%s'",
757 lsa_pipe->conn->binding_string);
758 talloc_free(tmp_ctx);
762 /* Make binding string for samr, not the other pipe */
763 status = dcerpc_epm_map_binding(tmp_ctx, samr_binding,
764 DCERPC_SAMR_UUID, DCERPC_SAMR_VERSION,
765 lsa_pipe->conn->event_ctx);
766 if (!NT_STATUS_IS_OK(status)) {
767 r->out.error_string = talloc_asprintf(mem_ctx,
768 "Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s",
769 DCERPC_NETLOGON_UUID,
771 talloc_free(tmp_ctx);
775 /* Setup a SAMR connection */
776 status = dcerpc_secondary_connection(lsa_pipe, &samr_pipe, samr_binding);
777 if (!NT_STATUS_IS_OK(status)) {
778 r->out.error_string = talloc_asprintf(mem_ctx,
779 "SAMR secondary connection failed: %s",
781 talloc_free(tmp_ctx);
785 status = dcerpc_pipe_auth(samr_pipe, samr_binding, DCERPC_SAMR_UUID,
786 DCERPC_SAMR_VERSION, ctx->cred);
787 if (!NT_STATUS_IS_OK(status)) {
788 r->out.error_string = talloc_asprintf(mem_ctx,
789 "SAMR bind failed: %s",
791 talloc_free(tmp_ctx);
795 /* prepare samr_Connect */
796 ZERO_STRUCT(p_handle);
797 sc.in.system_name = NULL;
798 sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
799 sc.out.connect_handle = &p_handle;
801 /* 2. do a samr_Connect to get a policy handle */
802 status = dcerpc_samr_Connect(samr_pipe, tmp_ctx, &sc);
803 if (!NT_STATUS_IS_OK(status)) {
804 r->out.error_string = talloc_asprintf(mem_ctx,
805 "samr_Connect failed: %s",
807 talloc_free(tmp_ctx);
811 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
813 struct lsa_String name;
814 struct samr_LookupDomain l;
815 name.string = domain_name;
816 l.in.connect_handle = &p_handle;
817 l.in.domain_name = &name;
819 status = dcerpc_samr_LookupDomain(samr_pipe, tmp_ctx, &l);
820 if (!NT_STATUS_IS_OK(status)) {
821 r->out.error_string = talloc_asprintf(mem_ctx,
822 "SAMR LookupDomain failed: %s",
824 talloc_free(tmp_ctx);
827 domain_sid = l.out.sid;
830 /* prepare samr_OpenDomain */
831 ZERO_STRUCT(d_handle);
832 od.in.connect_handle = &p_handle;
833 od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
834 od.in.sid = domain_sid;
835 od.out.domain_handle = &d_handle;
837 /* do a samr_OpenDomain to get a domain handle */
838 status = dcerpc_samr_OpenDomain(samr_pipe, tmp_ctx, &od);
839 if (!NT_STATUS_IS_OK(status)) {
840 r->out.error_string = talloc_asprintf(mem_ctx,
841 "samr_OpenDomain for [%s] failed: %s",
844 talloc_free(tmp_ctx);
848 /* prepare samr_CreateUser2 */
849 ZERO_STRUCTP(u_handle);
850 cu.in.domain_handle = &d_handle;
851 cu.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
852 samr_account_name.string = r->in.account_name;
853 cu.in.account_name = &samr_account_name;
854 cu.in.acct_flags = r->in.acct_type;
855 cu.out.user_handle = u_handle;
857 cu.out.access_granted = &access_granted;
859 /* do a samr_CreateUser2 to get an account handle, or an error */
860 cu_status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
862 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
863 /* prepare samr_LookupNames */
864 ln.in.domain_handle = &d_handle;
866 ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
868 r->out.error_string = NULL;
869 talloc_free(tmp_ctx);
870 return NT_STATUS_NO_MEMORY;
872 ln.in.names[0].string = r->in.account_name;
874 /* 5. do a samr_LookupNames to get the users rid */
875 status = dcerpc_samr_LookupNames(samr_pipe, tmp_ctx, &ln);
876 if (!NT_STATUS_IS_OK(status)) {
877 r->out.error_string = talloc_asprintf(mem_ctx,
878 "samr_LookupNames for [%s] failed: %s",
881 talloc_free(tmp_ctx);
885 /* check if we got one RID for the user */
886 if (ln.out.rids.count != 1) {
887 r->out.error_string = talloc_asprintf(mem_ctx,
888 "samr_LookupNames for [%s] returns %d RIDs\n",
889 r->in.account_name, ln.out.rids.count);
890 talloc_free(tmp_ctx);
891 return NT_STATUS_INVALID_PARAMETER;
894 /* prepare samr_OpenUser */
895 ZERO_STRUCTP(u_handle);
896 ou.in.domain_handle = &d_handle;
897 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
898 ou.in.rid = ln.out.rids.ids[0];
900 ou.out.user_handle = u_handle;
902 /* 6. do a samr_OpenUser to get a user handle */
903 status = dcerpc_samr_OpenUser(samr_pipe, tmp_ctx, &ou);
904 if (!NT_STATUS_IS_OK(status)) {
905 r->out.error_string = talloc_asprintf(mem_ctx,
906 "samr_OpenUser for [%s] failed: %s",
909 talloc_free(tmp_ctx);
913 if (r->in.recreate_account) {
914 struct samr_DeleteUser d;
915 d.in.user_handle = u_handle;
916 d.out.user_handle = u_handle;
917 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
918 if (!NT_STATUS_IS_OK(status)) {
919 r->out.error_string = talloc_asprintf(mem_ctx,
920 "samr_DeleteUser (for recreate) of [%s] failed: %s",
923 talloc_free(tmp_ctx);
927 /* We want to recreate, so delete and another samr_CreateUser2 */
929 /* &cu filled in above */
930 status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
931 if (!NT_STATUS_IS_OK(status)) {
932 r->out.error_string = talloc_asprintf(mem_ctx,
933 "samr_CreateUser2 (recreate) for [%s] failed: %s\n",
934 r->in.domain_name, nt_errstr(status));
935 talloc_free(tmp_ctx);
939 } else if (!NT_STATUS_IS_OK(status)) {
940 r->out.error_string = talloc_asprintf(mem_ctx,
941 "samr_CreateUser2 for [%s] failed: %s\n",
942 r->in.domain_name, nt_errstr(status));
943 talloc_free(tmp_ctx);
947 /* prepare samr_QueryUserInfo (get flags) */
948 qui.in.user_handle = u_handle;
951 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
952 if (!NT_STATUS_IS_OK(status)) {
953 r->out.error_string = talloc_asprintf(mem_ctx,
954 "samr_QueryUserInfo for [%s] failed: %s",
957 talloc_free(tmp_ctx);
962 status = NT_STATUS_INVALID_PARAMETER;
964 = talloc_asprintf(mem_ctx,
965 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s\n",
966 r->in.account_name, nt_errstr(status));
967 talloc_free(tmp_ctx);
971 old_acct_flags = (qui.out.info->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
972 /* Possibly bail if the account is of the wrong type */
974 != r->in.acct_type) {
975 const char *old_account_type, *new_account_type;
976 switch (old_acct_flags) {
978 old_account_type = "domain member (member)";
981 old_account_type = "domain controller (bdc)";
984 old_account_type = "trusted domain";
987 return NT_STATUS_INVALID_PARAMETER;
989 switch (r->in.acct_type) {
991 new_account_type = "domain member (member)";
994 new_account_type = "domain controller (bdc)";
997 new_account_type = "trusted domain";
1000 return NT_STATUS_INVALID_PARAMETER;
1003 if (!NT_STATUS_EQUAL(cu_status, NT_STATUS_USER_EXISTS)) {
1004 /* We created a new user, but they didn't come out the right type?!? */
1006 = talloc_asprintf(mem_ctx,
1007 "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",
1008 r->in.account_name, new_account_type, old_account_type);
1009 talloc_free(tmp_ctx);
1010 return NT_STATUS_INVALID_PARAMETER;
1012 /* The account is of the wrong type, so bail */
1014 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
1016 = talloc_asprintf(mem_ctx,
1017 "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",
1018 r->in.account_name, domain_name, old_account_type, new_account_type);
1019 talloc_free(tmp_ctx);
1020 return NT_STATUS_USER_EXISTS;
1023 acct_flags = qui.out.info->info16.acct_flags;
1026 acct_flags = (acct_flags & ~ACB_DISABLED);
1028 /* Find out what password policy this user has */
1029 pwp.in.user_handle = u_handle;
1031 status = dcerpc_samr_GetUserPwInfo(samr_pipe, tmp_ctx, &pwp);
1032 if (NT_STATUS_IS_OK(status)) {
1033 policy_min_pw_len = pwp.out.info.min_password_length;
1036 /* Grab a password of that minimum length */
1038 password_str = generate_random_str(tmp_ctx, MAX(8, policy_min_pw_len));
1040 r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
1041 r2.samr_handle.in.account_name = r->in.account_name;
1042 r2.samr_handle.in.newpassword = password_str;
1043 r2.samr_handle.in.user_handle = u_handle;
1044 r2.samr_handle.in.dcerpc_pipe = samr_pipe;
1046 status = libnet_SetPassword(ctx, tmp_ctx, &r2);
1047 if (!NT_STATUS_IS_OK(status)) {
1048 r->out.error_string = talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
1049 talloc_free(tmp_ctx);
1053 /* reset flags (if required) */
1054 if (acct_flags != qui.out.info->info16.acct_flags) {
1055 ZERO_STRUCT(u_info);
1056 u_info.info16.acct_flags = acct_flags;
1058 sui.in.user_handle = u_handle;
1059 sui.in.info = &u_info;
1062 dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
1063 if (!NT_STATUS_IS_OK(status)) {
1064 r->out.error_string = talloc_asprintf(mem_ctx,
1065 "samr_SetUserInfo for [%s] failed to remove ACB_DISABLED flag: %s",
1068 talloc_free(tmp_ctx);
1073 account_sid = dom_sid_add_rid(mem_ctx, domain_sid, rid);
1075 r->out.error_string = NULL;
1076 talloc_free(tmp_ctx);
1077 return NT_STATUS_NO_MEMORY;
1080 /* Finish out by pushing various bits of status data out for the caller to use */
1081 r->out.join_password = password_str;
1082 talloc_steal(mem_ctx, password_str);
1084 r->out.domain_sid = domain_sid;
1085 talloc_steal(mem_ctx, domain_sid);
1087 r->out.account_sid = account_sid;
1088 talloc_steal(mem_ctx, account_sid);
1090 r->out.domain_name = domain_name;
1091 talloc_steal(mem_ctx, domain_name);
1092 r->out.realm = realm;
1093 talloc_steal(mem_ctx, realm);
1094 r->out.lsa_pipe = lsa_pipe;
1095 talloc_steal(mem_ctx, lsa_pipe);
1096 r->out.samr_pipe = samr_pipe;
1097 talloc_steal(mem_ctx, samr_pipe);
1098 r->out.samr_binding = samr_binding;
1099 talloc_steal(mem_ctx, samr_binding);
1100 r->out.user_handle = u_handle;
1101 talloc_steal(mem_ctx, u_handle);
1102 r->out.error_string = r2.samr_handle.out.error_string;
1103 talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
1105 r->out.server_dn_str = NULL;
1106 talloc_free(tmp_ctx);
1108 /* Now, if it was AD, then we want to start looking changing a
1109 * few more things. Otherwise, we are done. */
1111 status = libnet_JoinADSDomain(ctx, r);
1118 static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
1119 TALLOC_CTX *mem_ctx,
1120 struct libnet_Join *r)
1123 TALLOC_CTX *tmp_mem;
1124 struct libnet_JoinDomain *r2;
1126 struct ldb_context *ldb;
1127 const struct ldb_dn *base_dn;
1128 struct ldb_message **msgs, *msg;
1130 const char * const attrs[] = {
1137 uint32_t acct_type = 0;
1138 const char *account_name;
1139 const char *netbios_name;
1142 r->out.error_string = NULL;
1144 tmp_mem = talloc_new(mem_ctx);
1146 return NT_STATUS_NO_MEMORY;
1149 r2 = talloc(tmp_mem, struct libnet_JoinDomain);
1151 r->out.error_string = NULL;
1152 talloc_free(tmp_mem);
1153 return NT_STATUS_NO_MEMORY;
1156 if (r->in.secure_channel_type == SEC_CHAN_BDC) {
1157 acct_type = ACB_SVRTRUST;
1158 } else if (r->in.secure_channel_type == SEC_CHAN_WKSTA) {
1159 acct_type = ACB_WSTRUST;
1161 r->out.error_string = NULL;
1162 talloc_free(tmp_mem);
1163 return NT_STATUS_INVALID_PARAMETER;
1166 if ((r->in.netbios_name != NULL) && (r->in.level != LIBNET_JOIN_AUTOMATIC)) {
1167 netbios_name = r->in.netbios_name;
1169 netbios_name = talloc_asprintf(tmp_mem, "%s", lp_netbios_name());
1170 if (!netbios_name) {
1171 r->out.error_string = NULL;
1172 talloc_free(tmp_mem);
1173 return NT_STATUS_NO_MEMORY;
1177 account_name = talloc_asprintf(tmp_mem, "%s$", netbios_name);
1178 if (!account_name) {
1179 r->out.error_string = NULL;
1180 talloc_free(tmp_mem);
1181 return NT_STATUS_NO_MEMORY;
1185 * Local secrets are stored in secrets.ldb
1186 * open it to make sure we can write the info into it after the join
1188 ldb = secrets_db_connect(tmp_mem);
1191 = talloc_asprintf(mem_ctx,
1192 "Could not open secrets database\n");
1193 talloc_free(tmp_mem);
1194 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1201 r2->in.domain_name = r->in.domain_name;
1202 r2->in.account_name = account_name;
1203 r2->in.netbios_name = netbios_name;
1204 r2->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
1205 r2->in.acct_type = acct_type;
1206 r2->in.recreate_account = False;
1207 status = libnet_JoinDomain(ctx, r2, r2);
1208 if (!NT_STATUS_IS_OK(status)) {
1209 r->out.error_string = talloc_steal(mem_ctx, r2->out.error_string);
1210 talloc_free(tmp_mem);
1215 * now prepare the record for secrets.ldb
1217 sct = talloc_asprintf(tmp_mem, "%d", r->in.secure_channel_type);
1219 r->out.error_string = NULL;
1220 talloc_free(tmp_mem);
1221 return NT_STATUS_NO_MEMORY;
1224 msg = ldb_msg_new(tmp_mem);
1226 r->out.error_string = NULL;
1227 talloc_free(tmp_mem);
1228 return NT_STATUS_NO_MEMORY;
1231 base_dn = ldb_dn_explode(tmp_mem, "cn=Primary Domains");
1233 r->out.error_string = NULL;
1234 talloc_free(tmp_mem);
1235 return NT_STATUS_NO_MEMORY;
1238 msg->dn = ldb_dn_build_child(tmp_mem, "flatname", r2->out.domain_name, base_dn);
1240 r->out.error_string = NULL;
1241 talloc_free(tmp_mem);
1242 return NT_STATUS_NO_MEMORY;
1245 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "flatname", r2->out.domain_name);
1247 r->out.error_string = NULL;
1248 talloc_free(tmp_mem);
1249 return NT_STATUS_NO_MEMORY;
1252 if (r2->out.realm) {
1253 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "realm", r2->out.realm);
1255 r->out.error_string = NULL;
1256 talloc_free(tmp_mem);
1257 return NT_STATUS_NO_MEMORY;
1260 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
1262 r->out.error_string = NULL;
1263 talloc_free(tmp_mem);
1264 return NT_STATUS_NO_MEMORY;
1268 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
1270 r->out.error_string = NULL;
1271 talloc_free(tmp_mem);
1272 return NT_STATUS_NO_MEMORY;
1275 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
1277 r->out.error_string = NULL;
1278 talloc_free(tmp_mem);
1279 return NT_STATUS_NO_MEMORY;
1282 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1284 r->out.error_string = NULL;
1285 talloc_free(tmp_mem);
1286 return NT_STATUS_NO_MEMORY;
1289 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1291 r->out.error_string = NULL;
1292 talloc_free(tmp_mem);
1293 return NT_STATUS_NO_MEMORY;
1297 rtn = samdb_msg_add_uint(ldb, tmp_mem, msg, "msDS-KeyVersionNumber",
1300 r->out.error_string = NULL;
1301 talloc_free(tmp_mem);
1302 return NT_STATUS_NO_MEMORY;
1306 if (r2->out.domain_sid) {
1307 rtn = samdb_msg_add_dom_sid(ldb, tmp_mem, msg, "objectSid",
1308 r2->out.domain_sid);
1310 r->out.error_string = NULL;
1311 talloc_free(tmp_mem);
1312 return NT_STATUS_NO_MEMORY;
1317 * search for the secret record
1318 * - remove the records we find
1319 * - and fetch the old secret and store it under priorSecret
1321 ret = gendb_search(ldb,
1324 "(|" SECRETS_PRIMARY_DOMAIN_FILTER "(realm=%s))",
1325 r2->out.domain_name, r2->out.realm);
1327 } else if (ret == -1) {
1329 = talloc_asprintf(mem_ctx,
1330 "Search for domain: %s and realm: %s failed: %s",
1331 r2->out.domain_name, r2->out.realm, ldb_errstring(ldb));
1332 talloc_free(tmp_mem);
1333 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1335 const struct ldb_val *prior_secret;
1336 const struct ldb_val *prior_modified_time;
1339 for (i = 0; i < ret; i++) {
1340 ldb_delete(ldb, msgs[i]->dn);
1343 prior_secret = ldb_msg_find_ldb_val(msgs[0], "secret");
1345 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorSecret", prior_secret);
1347 r->out.error_string = NULL;
1348 talloc_free(tmp_mem);
1349 return NT_STATUS_NO_MEMORY;
1352 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
1354 r->out.error_string = NULL;
1355 talloc_free(tmp_mem);
1356 return NT_STATUS_NO_MEMORY;
1359 prior_modified_time = ldb_msg_find_ldb_val(msgs[0],
1361 if (prior_modified_time) {
1362 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorWhenChanged",
1363 prior_modified_time);
1365 r->out.error_string = NULL;
1366 talloc_free(tmp_mem);
1367 return NT_STATUS_NO_MEMORY;
1371 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1373 r->out.error_string = NULL;
1374 talloc_free(tmp_mem);
1375 return NT_STATUS_NO_MEMORY;
1378 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1380 r->out.error_string = NULL;
1381 talloc_free(tmp_mem);
1382 return NT_STATUS_NO_MEMORY;
1386 /* create the secret */
1387 ret = samdb_add(ldb, tmp_mem, msg);
1389 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to create secret record %s\n",
1390 ldb_dn_linearize(ldb, msg->dn));
1391 talloc_free(tmp_mem);
1392 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1395 if (r2->out.realm) {
1396 struct cli_credentials *creds;
1397 /* Make a credentials structure from it */
1398 creds = cli_credentials_init(mem_ctx);
1400 r->out.error_string = NULL;
1401 talloc_free(tmp_mem);
1402 return NT_STATUS_NO_MEMORY;
1404 cli_credentials_set_conf(creds);
1405 filter = talloc_asprintf(mem_ctx, "dn=%s", ldb_dn_linearize(mem_ctx, msg->dn));
1406 status = cli_credentials_set_secrets(creds, NULL, filter);
1407 if (!NT_STATUS_IS_OK(status)) {
1408 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to read secrets for keytab update for %s\n",
1410 talloc_free(tmp_mem);
1413 ret = cli_credentials_update_keytab(creds);
1415 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to update keytab for %s\n",
1417 talloc_free(tmp_mem);
1418 return NT_STATUS_UNSUCCESSFUL;
1422 /* move all out parameter to the callers TALLOC_CTX */
1423 r->out.error_string = NULL;
1424 r->out.join_password = r2->out.join_password;
1425 talloc_steal(mem_ctx, r2->out.join_password);
1426 r->out.domain_sid = r2->out.domain_sid;
1427 talloc_steal(mem_ctx, r2->out.domain_sid);
1428 r->out.domain_name = r2->out.domain_name;
1429 talloc_steal(mem_ctx, r2->out.domain_name);
1430 talloc_free(tmp_mem);
1431 return NT_STATUS_OK;
1434 NTSTATUS libnet_Join(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_Join *r)
1436 switch (r->in.secure_channel_type) {
1437 case SEC_CHAN_WKSTA:
1438 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1440 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1441 case SEC_CHAN_DOMAIN:
1445 r->out.error_string = talloc_asprintf(mem_ctx,
1446 "Invalid secure channel type specified (%08X) attempting to join domain %s",
1447 r->in.secure_channel_type, r->in.domain_name);
1448 return NT_STATUS_INVALID_PARAMETER;