CVE-2020-25719 mit_samba: Create the talloc context earlier
[Samba.git] / source4 / libnet / libnet_unbecome_dc.c
blob38d6a94b23f1bd973d7c84af796ace2467c116b6
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "libnet/libnet.h"
22 #include "libcli/composite/composite.h"
23 #include "libcli/cldap/cldap.h"
24 #include <ldb.h>
25 #include <ldb_errors.h>
26 #include "ldb_wrap.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "../libds/common/flags.h"
29 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
30 #include "param/param.h"
31 #include "lib/tsocket/tsocket.h"
33 /*****************************************************************************
34 * Windows 2003 (w2k3) does the following steps when changing the server role
35 * from domain controller back to domain member
37 * We mostly do the same.
38 *****************************************************************************/
41 * lookup DC:
42 * - using nbt name<1C> request and a samlogon mailslot request
43 * or
44 * - using a DNS SRV _ldap._tcp.dc._msdcs. request and a CLDAP netlogon request
46 * see: unbecomeDC_send_cldap() and unbecomeDC_recv_cldap()
50 * Open 1st LDAP connection to the DC using admin credentials
52 * see: unbecomeDC_ldap_connect()
56 * LDAP search 1st LDAP connection:
58 * see: unbecomeDC_ldap_rootdse()
60 * Request:
61 * basedn: ""
62 * scope: base
63 * filter: (objectClass=*)
64 * attrs: defaultNamingContext
65 * configurationNamingContext
66 * Result:
67 * ""
68 * defaultNamingContext: <domain_partition>
69 * configurationNamingContext:CN=Configuration,<domain_partition>
73 * LDAP search 1st LDAP connection:
75 * see: unbecomeDC_ldap_computer_object()
77 * Request:
78 * basedn: <domain_partition>
79 * scope: sub
80 * filter: (&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<new_dc_account_name>))
81 * attrs: distinguishedName
82 * userAccountControl
83 * Result:
84 * CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition>
85 * distinguishedName: CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition>
86 * userAccoountControl: 532480 <0x82000>
90 * LDAP search 1st LDAP connection:
92 * see: unbecomeDC_ldap_modify_computer()
94 * Request:
95 * basedn: CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
96 * scope: base
97 * filter: (objectClass=*)
98 * attrs: userAccountControl
99 * Result:
100 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
101 * userAccoountControl: 532480 <0x82000>
105 * LDAP modify 1st LDAP connection:
107 * see: unbecomeDC_ldap_modify_computer()
109 * Request (replace):
110 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
111 * userAccoountControl: 4096 <0x1000>
112 * Result:
113 * <success>
117 * LDAP search 1st LDAP connection:
119 * see: unbecomeDC_ldap_move_computer()
121 * Request:
122 * basedn: <WKGUID=aa312825768811d1aded00c04fd8d5cd,<domain_partition>>
123 * scope: base
124 * filter: (objectClass=*)
125 * attrs: 1.1
126 * Result:
127 * CN=Computers,<domain_partition>
131 * LDAP search 1st LDAP connection:
133 * not implemented because it doesn't give any new information
135 * Request:
136 * basedn: CN=Computers,<domain_partition>
137 * scope: base
138 * filter: (objectClass=*)
139 * attrs: distinguishedName
140 * Result:
141 * CN=Computers,<domain_partition>
142 * distinguishedName: CN=Computers,<domain_partition>
146 * LDAP modifyRDN 1st LDAP connection:
148 * see: unbecomeDC_ldap_move_computer()
150 * Request:
151 * entry: CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition>
152 * newrdn: CN=<new_dc_netbios_name>
153 * deleteoldrdn: TRUE
154 * newparent: CN=Computers,<domain_partition>
155 * Result:
156 * <success>
160 * LDAP unbind on the 1st LDAP connection
162 * not implemented, because it's not needed...
166 * Open 1st DRSUAPI connection to the DC using admin credentials
167 * DsBind with DRSUAPI_DS_BIND_GUID ("e24d201a-4fd6-11d1-a3da-0000f875ae0d")
169 * see: unbecomeDC_drsuapi_connect_send(), unbecomeDC_drsuapi_connect_recv(),
170 * unbecomeDC_drsuapi_bind_send() and unbecomeDC_drsuapi_bind_recv()
174 * DsRemoveDsServer to remove the
175 * CN=<machine_name>,CN=Servers,CN=<site_name>,CN=Configuration,<domain_partition>
176 * and CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=<site_name>,CN=Configuration,<domain_partition>
177 * on the 1st DRSUAPI connection
179 * see: unbecomeDC_drsuapi_remove_ds_server_send() and unbecomeDC_drsuapi_remove_ds_server_recv()
183 * DsUnbind on the 1st DRSUAPI connection
185 * not implemented, because it's not needed...
189 struct libnet_UnbecomeDC_state {
190 struct composite_context *creq;
192 struct libnet_context *libnet;
194 struct {
195 struct cldap_socket *sock;
196 struct cldap_netlogon io;
197 struct NETLOGON_SAM_LOGON_RESPONSE_EX netlogon;
198 } cldap;
200 struct {
201 struct ldb_context *ldb;
202 } ldap;
204 struct {
205 struct dcerpc_binding *binding;
206 struct dcerpc_pipe *pipe;
207 struct dcerpc_binding_handle *drsuapi_handle;
208 struct drsuapi_DsBind bind_r;
209 struct GUID bind_guid;
210 struct drsuapi_DsBindInfoCtr bind_info_ctr;
211 struct drsuapi_DsBindInfo28 local_info28;
212 struct drsuapi_DsBindInfo28 remote_info28;
213 struct policy_handle bind_handle;
214 struct drsuapi_DsRemoveDSServer rm_ds_srv_r;
215 } drsuapi;
217 struct {
218 /* input */
219 const char *dns_name;
220 const char *netbios_name;
222 /* constructed */
223 struct GUID guid;
224 const char *dn_str;
225 } domain;
227 struct {
228 /* constructed */
229 const char *config_dn_str;
230 } forest;
232 struct {
233 /* input */
234 const char *address;
236 /* constructed */
237 const char *dns_name;
238 const char *netbios_name;
239 const char *site_name;
240 } source_dsa;
242 struct {
243 /* input */
244 const char *netbios_name;
246 /* constructed */
247 const char *dns_name;
248 const char *site_name;
249 const char *computer_dn_str;
250 const char *server_dn_str;
251 uint32_t user_account_control;
252 } dest_dsa;
255 static void unbecomeDC_recv_cldap(struct tevent_req *req);
257 static void unbecomeDC_send_cldap(struct libnet_UnbecomeDC_state *s)
259 struct composite_context *c = s->creq;
260 struct tevent_req *req;
261 struct tsocket_address *dest_address;
262 int ret;
264 s->cldap.io.in.dest_address = NULL;
265 s->cldap.io.in.dest_port = 0;
266 s->cldap.io.in.realm = s->domain.dns_name;
267 s->cldap.io.in.host = s->dest_dsa.netbios_name;
268 s->cldap.io.in.user = NULL;
269 s->cldap.io.in.domain_guid = NULL;
270 s->cldap.io.in.domain_sid = NULL;
271 s->cldap.io.in.acct_control = -1;
272 s->cldap.io.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
273 s->cldap.io.in.map_response = true;
275 ret = tsocket_address_inet_from_strings(s, "ip",
276 s->source_dsa.address,
277 lpcfg_cldap_port(s->libnet->lp_ctx),
278 &dest_address);
279 if (ret != 0) {
280 c->status = map_nt_error_from_unix_common(errno);
281 if (!composite_is_ok(c)) return;
284 c->status = cldap_socket_init(s, NULL, dest_address, &s->cldap.sock);
285 if (!composite_is_ok(c)) return;
287 req = cldap_netlogon_send(s, s->libnet->event_ctx,
288 s->cldap.sock, &s->cldap.io);
289 if (composite_nomem(req, c)) return;
290 tevent_req_set_callback(req, unbecomeDC_recv_cldap, s);
293 static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s);
295 static void unbecomeDC_recv_cldap(struct tevent_req *req)
297 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(req,
298 struct libnet_UnbecomeDC_state);
299 struct composite_context *c = s->creq;
301 c->status = cldap_netlogon_recv(req, s, &s->cldap.io);
302 talloc_free(req);
303 if (!composite_is_ok(c)) return;
305 s->cldap.netlogon = s->cldap.io.out.netlogon.data.nt5_ex;
307 s->domain.dns_name = s->cldap.netlogon.dns_domain;
308 s->domain.netbios_name = s->cldap.netlogon.domain_name;
309 s->domain.guid = s->cldap.netlogon.domain_uuid;
311 s->source_dsa.dns_name = s->cldap.netlogon.pdc_dns_name;
312 s->source_dsa.netbios_name = s->cldap.netlogon.pdc_name;
313 s->source_dsa.site_name = s->cldap.netlogon.server_site;
315 s->dest_dsa.site_name = s->cldap.netlogon.client_site;
317 unbecomeDC_connect_ldap(s);
320 static NTSTATUS unbecomeDC_ldap_connect(struct libnet_UnbecomeDC_state *s)
322 char *url;
324 url = talloc_asprintf(s, "ldap://%s/", s->source_dsa.dns_name);
325 NT_STATUS_HAVE_NO_MEMORY(url);
327 s->ldap.ldb = ldb_wrap_connect(s, s->libnet->event_ctx, s->libnet->lp_ctx, url,
328 NULL,
329 s->libnet->cred,
331 talloc_free(url);
332 if (s->ldap.ldb == NULL) {
333 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
336 return NT_STATUS_OK;
339 static NTSTATUS unbecomeDC_ldap_rootdse(struct libnet_UnbecomeDC_state *s)
341 int ret;
342 struct ldb_result *r;
343 struct ldb_dn *basedn;
344 static const char *attrs[] = {
345 "defaultNamingContext",
346 "configurationNamingContext",
347 NULL
350 basedn = ldb_dn_new(s, s->ldap.ldb, NULL);
351 NT_STATUS_HAVE_NO_MEMORY(basedn);
353 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
354 "(objectClass=*)");
355 talloc_free(basedn);
356 if (ret != LDB_SUCCESS) {
357 return NT_STATUS_LDAP(ret);
358 } else if (r->count != 1) {
359 talloc_free(r);
360 return NT_STATUS_INVALID_NETWORK_RESPONSE;
363 s->domain.dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "defaultNamingContext", NULL);
364 if (!s->domain.dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
365 talloc_steal(s, s->domain.dn_str);
367 s->forest.config_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "configurationNamingContext", NULL);
368 if (!s->forest.config_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
369 talloc_steal(s, s->forest.config_dn_str);
371 s->dest_dsa.server_dn_str = talloc_asprintf(s, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
372 s->dest_dsa.netbios_name,
373 s->dest_dsa.site_name,
374 s->forest.config_dn_str);
375 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.server_dn_str);
377 talloc_free(r);
378 return NT_STATUS_OK;
381 static NTSTATUS unbecomeDC_ldap_computer_object(struct libnet_UnbecomeDC_state *s)
383 int ret;
384 struct ldb_result *r;
385 struct ldb_dn *basedn;
386 static const char *attrs[] = {
387 "distinguishedName",
388 "userAccountControl",
389 NULL
392 basedn = ldb_dn_new(s, s->ldap.ldb, s->domain.dn_str);
393 NT_STATUS_HAVE_NO_MEMORY(basedn);
395 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_SUBTREE, attrs,
396 "(&(|(objectClass=user)(objectClass=computer))(sAMAccountName=%s$))",
397 s->dest_dsa.netbios_name);
398 talloc_free(basedn);
399 if (ret != LDB_SUCCESS) {
400 return NT_STATUS_LDAP(ret);
401 } else if (r->count != 1) {
402 talloc_free(r);
403 return NT_STATUS_INVALID_NETWORK_RESPONSE;
406 s->dest_dsa.computer_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "distinguishedName", NULL);
407 if (!s->dest_dsa.computer_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
408 talloc_steal(s, s->dest_dsa.computer_dn_str);
410 s->dest_dsa.user_account_control = ldb_msg_find_attr_as_uint(r->msgs[0], "userAccountControl", 0);
412 talloc_free(r);
413 return NT_STATUS_OK;
416 static NTSTATUS unbecomeDC_ldap_modify_computer(struct libnet_UnbecomeDC_state *s)
418 int ret;
419 struct ldb_message *msg;
420 uint32_t user_account_control = UF_WORKSTATION_TRUST_ACCOUNT;
421 unsigned int i;
423 /* as the value is already as we want it to be, we're done */
424 if (s->dest_dsa.user_account_control == user_account_control) {
425 return NT_STATUS_OK;
428 /* make a 'modify' msg, and only for serverReference */
429 msg = ldb_msg_new(s);
430 NT_STATUS_HAVE_NO_MEMORY(msg);
431 msg->dn = ldb_dn_new(msg, s->ldap.ldb, s->dest_dsa.computer_dn_str);
432 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
434 ret = samdb_msg_add_uint(s->ldap.ldb, msg, msg, "userAccountControl",
435 user_account_control);
436 if (ret != LDB_SUCCESS) {
437 talloc_free(msg);
438 return NT_STATUS_NO_MEMORY;
441 /* mark all the message elements (should be just one)
442 as LDB_FLAG_MOD_REPLACE */
443 for (i=0;i<msg->num_elements;i++) {
444 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
447 ret = ldb_modify(s->ldap.ldb, msg);
448 talloc_free(msg);
449 if (ret != LDB_SUCCESS) {
450 return NT_STATUS_LDAP(ret);
453 s->dest_dsa.user_account_control = user_account_control;
455 return NT_STATUS_OK;
458 static NTSTATUS unbecomeDC_ldap_move_computer(struct libnet_UnbecomeDC_state *s)
460 int ret;
461 struct ldb_result *r;
462 struct ldb_dn *basedn;
463 struct ldb_dn *old_dn;
464 struct ldb_dn *new_dn;
465 static const char *_1_1_attrs[] = {
466 "1.1",
467 NULL
470 basedn = ldb_dn_new_fmt(s, s->ldap.ldb, "<WKGUID=aa312825768811d1aded00c04fd8d5cd,%s>",
471 s->domain.dn_str);
472 NT_STATUS_HAVE_NO_MEMORY(basedn);
474 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_BASE,
475 _1_1_attrs, "(objectClass=*)");
476 talloc_free(basedn);
477 if (ret != LDB_SUCCESS) {
478 return NT_STATUS_LDAP(ret);
479 } else if (r->count != 1) {
480 talloc_free(r);
481 return NT_STATUS_INVALID_NETWORK_RESPONSE;
484 old_dn = ldb_dn_new(r, s->ldap.ldb, s->dest_dsa.computer_dn_str);
485 NT_STATUS_HAVE_NO_MEMORY(old_dn);
487 new_dn = r->msgs[0]->dn;
489 if (!ldb_dn_add_child_fmt(new_dn, "CN=%s", s->dest_dsa.netbios_name)) {
490 talloc_free(r);
491 return NT_STATUS_NO_MEMORY;
494 if (ldb_dn_compare(old_dn, new_dn) == 0) {
495 /* we don't need to rename if the old and new dn match */
496 talloc_free(r);
497 return NT_STATUS_OK;
500 ret = ldb_rename(s->ldap.ldb, old_dn, new_dn);
501 if (ret != LDB_SUCCESS) {
502 talloc_free(r);
503 return NT_STATUS_LDAP(ret);
506 s->dest_dsa.computer_dn_str = ldb_dn_alloc_linearized(s, new_dn);
507 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.computer_dn_str);
509 talloc_free(r);
511 return NT_STATUS_OK;
514 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s);
516 static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s)
518 struct composite_context *c = s->creq;
520 c->status = unbecomeDC_ldap_connect(s);
521 if (!composite_is_ok(c)) return;
523 c->status = unbecomeDC_ldap_rootdse(s);
524 if (!composite_is_ok(c)) return;
526 c->status = unbecomeDC_ldap_computer_object(s);
527 if (!composite_is_ok(c)) return;
529 c->status = unbecomeDC_ldap_modify_computer(s);
530 if (!composite_is_ok(c)) return;
532 c->status = unbecomeDC_ldap_move_computer(s);
533 if (!composite_is_ok(c)) return;
535 unbecomeDC_drsuapi_connect_send(s);
538 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *creq);
540 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s)
542 struct composite_context *c = s->creq;
543 struct composite_context *creq;
544 char *binding_str;
546 binding_str = talloc_asprintf(s, "ncacn_ip_tcp:%s[seal,target_hostname=%s]",
547 s->source_dsa.address,
548 s->source_dsa.dns_name);
549 if (composite_nomem(binding_str, c)) return;
551 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi.binding);
552 talloc_free(binding_str);
553 if (!composite_is_ok(c)) return;
555 if (DEBUGLEVEL >= 10) {
556 c->status = dcerpc_binding_set_flags(s->drsuapi.binding,
557 DCERPC_DEBUG_PRINT_BOTH,
559 if (!composite_is_ok(c)) return;
562 creq = dcerpc_pipe_connect_b_send(s, s->drsuapi.binding, &ndr_table_drsuapi,
563 s->libnet->cred, s->libnet->event_ctx,
564 s->libnet->lp_ctx);
565 composite_continue(c, creq, unbecomeDC_drsuapi_connect_recv, s);
568 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s);
570 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *req)
572 struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
573 struct libnet_UnbecomeDC_state);
574 struct composite_context *c = s->creq;
576 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi.pipe);
577 if (!composite_is_ok(c)) return;
579 s->drsuapi.drsuapi_handle = s->drsuapi.pipe->binding_handle;
581 unbecomeDC_drsuapi_bind_send(s);
584 static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq);
586 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s)
588 struct composite_context *c = s->creq;
589 struct drsuapi_DsBindInfo28 *bind_info28;
590 struct tevent_req *subreq;
592 GUID_from_string(DRSUAPI_DS_BIND_GUID, &s->drsuapi.bind_guid);
594 bind_info28 = &s->drsuapi.local_info28;
595 bind_info28->supported_extensions = 0;
596 bind_info28->site_guid = GUID_zero();
597 bind_info28->pid = 0;
598 bind_info28->repl_epoch = 0;
600 s->drsuapi.bind_info_ctr.length = 28;
601 s->drsuapi.bind_info_ctr.info.info28 = *bind_info28;
603 s->drsuapi.bind_r.in.bind_guid = &s->drsuapi.bind_guid;
604 s->drsuapi.bind_r.in.bind_info = &s->drsuapi.bind_info_ctr;
605 s->drsuapi.bind_r.out.bind_handle = &s->drsuapi.bind_handle;
607 subreq = dcerpc_drsuapi_DsBind_r_send(s, c->event_ctx,
608 s->drsuapi.drsuapi_handle,
609 &s->drsuapi.bind_r);
610 if (composite_nomem(subreq, c)) return;
611 tevent_req_set_callback(subreq, unbecomeDC_drsuapi_bind_recv, s);
614 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s);
616 static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq)
618 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
619 struct libnet_UnbecomeDC_state);
620 struct composite_context *c = s->creq;
622 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
623 TALLOC_FREE(subreq);
624 if (!composite_is_ok(c)) return;
626 if (!W_ERROR_IS_OK(s->drsuapi.bind_r.out.result)) {
627 composite_error(c, werror_to_ntstatus(s->drsuapi.bind_r.out.result));
628 return;
631 ZERO_STRUCT(s->drsuapi.remote_info28);
632 if (s->drsuapi.bind_r.out.bind_info) {
633 switch (s->drsuapi.bind_r.out.bind_info->length) {
634 case 24: {
635 struct drsuapi_DsBindInfo24 *info24;
636 info24 = &s->drsuapi.bind_r.out.bind_info->info.info24;
637 s->drsuapi.remote_info28.supported_extensions = info24->supported_extensions;
638 s->drsuapi.remote_info28.site_guid = info24->site_guid;
639 s->drsuapi.remote_info28.pid = info24->pid;
640 s->drsuapi.remote_info28.repl_epoch = 0;
641 break;
643 case 28: {
644 s->drsuapi.remote_info28 = s->drsuapi.bind_r.out.bind_info->info.info28;
645 break;
647 case 32: {
648 struct drsuapi_DsBindInfo32 *info32;
649 info32 = &s->drsuapi.bind_r.out.bind_info->info.info32;
650 s->drsuapi.remote_info28.supported_extensions = info32->supported_extensions;
651 s->drsuapi.remote_info28.site_guid = info32->site_guid;
652 s->drsuapi.remote_info28.pid = info32->pid;
653 s->drsuapi.remote_info28.repl_epoch = info32->repl_epoch;
654 break;
656 case 48: {
657 struct drsuapi_DsBindInfo48 *info48;
658 info48 = &s->drsuapi.bind_r.out.bind_info->info.info48;
659 s->drsuapi.remote_info28.supported_extensions = info48->supported_extensions;
660 s->drsuapi.remote_info28.site_guid = info48->site_guid;
661 s->drsuapi.remote_info28.pid = info48->pid;
662 s->drsuapi.remote_info28.repl_epoch = info48->repl_epoch;
663 break;
665 case 52: {
666 struct drsuapi_DsBindInfo52 *info52;
667 info52 = &s->drsuapi.bind_r.out.bind_info->info.info52;
668 s->drsuapi.remote_info28.supported_extensions = info52->supported_extensions;
669 s->drsuapi.remote_info28.site_guid = info52->site_guid;
670 s->drsuapi.remote_info28.pid = info52->pid;
671 s->drsuapi.remote_info28.repl_epoch = info52->repl_epoch;
672 break;
674 default:
675 DEBUG(1, ("Warning: invalid info length in bind info: %d\n",
676 s->drsuapi.bind_r.out.bind_info->length));
677 break;
681 unbecomeDC_drsuapi_remove_ds_server_send(s);
684 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq);
686 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s)
688 struct composite_context *c = s->creq;
689 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
690 struct tevent_req *subreq;
692 r->in.bind_handle = &s->drsuapi.bind_handle;
693 r->in.level = 1;
694 r->in.req = talloc(s, union drsuapi_DsRemoveDSServerRequest);
695 r->in.req->req1.server_dn = s->dest_dsa.server_dn_str;
696 r->in.req->req1.domain_dn = s->domain.dn_str;
697 r->in.req->req1.commit = true;
699 r->out.level_out = talloc(s, uint32_t);
700 r->out.res = talloc(s, union drsuapi_DsRemoveDSServerResult);
702 subreq = dcerpc_drsuapi_DsRemoveDSServer_r_send(s, c->event_ctx,
703 s->drsuapi.drsuapi_handle,
705 if (composite_nomem(subreq, c)) return;
706 tevent_req_set_callback(subreq, unbecomeDC_drsuapi_remove_ds_server_recv, s);
709 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq)
711 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
712 struct libnet_UnbecomeDC_state);
713 struct composite_context *c = s->creq;
714 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
716 c->status = dcerpc_drsuapi_DsRemoveDSServer_r_recv(subreq, s);
717 TALLOC_FREE(subreq);
718 if (!composite_is_ok(c)) return;
720 if (!W_ERROR_IS_OK(r->out.result)) {
721 composite_error(c, werror_to_ntstatus(r->out.result));
722 return;
725 if (*r->out.level_out != 1) {
726 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
727 return;
730 composite_done(c);
733 struct composite_context *libnet_UnbecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
735 struct composite_context *c;
736 struct libnet_UnbecomeDC_state *s;
737 char *tmp_name;
739 c = composite_create(mem_ctx, ctx->event_ctx);
740 if (c == NULL) return NULL;
742 s = talloc_zero(c, struct libnet_UnbecomeDC_state);
743 if (composite_nomem(s, c)) return c;
744 c->private_data = s;
745 s->creq = c;
746 s->libnet = ctx;
748 /* Domain input */
749 s->domain.dns_name = talloc_strdup(s, r->in.domain_dns_name);
750 if (composite_nomem(s->domain.dns_name, c)) return c;
751 s->domain.netbios_name = talloc_strdup(s, r->in.domain_netbios_name);
752 if (composite_nomem(s->domain.netbios_name, c)) return c;
754 /* Source DSA input */
755 s->source_dsa.address = talloc_strdup(s, r->in.source_dsa_address);
756 if (composite_nomem(s->source_dsa.address, c)) return c;
758 /* Destination DSA input */
759 s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
760 if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
762 /* Destination DSA dns_name construction */
763 tmp_name = strlower_talloc(s, s->dest_dsa.netbios_name);
764 if (composite_nomem(tmp_name, c)) return c;
765 s->dest_dsa.dns_name = talloc_asprintf_append_buffer(tmp_name, ".%s",
766 s->domain.dns_name);
767 if (composite_nomem(s->dest_dsa.dns_name, c)) return c;
769 unbecomeDC_send_cldap(s);
770 return c;
773 NTSTATUS libnet_UnbecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
775 NTSTATUS status;
777 status = composite_wait(c);
779 ZERO_STRUCT(r->out);
781 talloc_free(c);
782 return status;
785 NTSTATUS libnet_UnbecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
787 NTSTATUS status;
788 struct composite_context *c;
789 c = libnet_UnbecomeDC_send(ctx, mem_ctx, r);
790 status = libnet_UnbecomeDC_recv(c, mem_ctx, r);
791 return status;