libnet4: Use netlogon_pings() in unbecome_dc
[samba4-gss.git] / source4 / libnet / libnet_unbecome_dc.c
blob4ee9e20c558df956ce0d1dc19198fe2ef36c9af5
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 "source3/libads/netlogon_ping.h"
25 #include <ldb.h>
26 #include <ldb_errors.h>
27 #include "ldb_wrap.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "../libds/common/flags.h"
30 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
31 #include "param/param.h"
32 #include "lib/tsocket/tsocket.h"
34 /*****************************************************************************
35 * Windows 2003 (w2k3) does the following steps when changing the server role
36 * from domain controller back to domain member
38 * We mostly do the same.
39 *****************************************************************************/
42 * lookup DC:
43 * - using nbt name<1C> request and a samlogon mailslot request
44 * or
45 * - using a DNS SRV _ldap._tcp.dc._msdcs. request and a CLDAP netlogon request
47 * see: unbecomeDC_send_cldap() and unbecomeDC_recv_cldap()
51 * Open 1st LDAP connection to the DC using admin credentials
53 * see: unbecomeDC_ldap_connect()
57 * LDAP search 1st LDAP connection:
59 * see: unbecomeDC_ldap_rootdse()
61 * Request:
62 * basedn: ""
63 * scope: base
64 * filter: (objectClass=*)
65 * attrs: defaultNamingContext
66 * configurationNamingContext
67 * Result:
68 * ""
69 * defaultNamingContext: <domain_partition>
70 * configurationNamingContext:CN=Configuration,<domain_partition>
74 * LDAP search 1st LDAP connection:
76 * see: unbecomeDC_ldap_computer_object()
78 * Request:
79 * basedn: <domain_partition>
80 * scope: sub
81 * filter: (&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<new_dc_account_name>))
82 * attrs: distinguishedName
83 * userAccountControl
84 * Result:
85 * CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition>
86 * distinguishedName: CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition>
87 * userAccoountControl: 532480 <0x82000>
91 * LDAP search 1st LDAP connection:
93 * see: unbecomeDC_ldap_modify_computer()
95 * Request:
96 * basedn: CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
97 * scope: base
98 * filter: (objectClass=*)
99 * attrs: userAccountControl
100 * Result:
101 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
102 * userAccoountControl: 532480 <0x82000>
106 * LDAP modify 1st LDAP connection:
108 * see: unbecomeDC_ldap_modify_computer()
110 * Request (replace):
111 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
112 * userAccoountControl: 4096 <0x1000>
113 * Result:
114 * <success>
118 * LDAP search 1st LDAP connection:
120 * see: unbecomeDC_ldap_move_computer()
122 * Request:
123 * basedn: <WKGUID=aa312825768811d1aded00c04fd8d5cd,<domain_partition>>
124 * scope: base
125 * filter: (objectClass=*)
126 * attrs: 1.1
127 * Result:
128 * CN=Computers,<domain_partition>
132 * LDAP search 1st LDAP connection:
134 * not implemented because it doesn't give any new information
136 * Request:
137 * basedn: CN=Computers,<domain_partition>
138 * scope: base
139 * filter: (objectClass=*)
140 * attrs: distinguishedName
141 * Result:
142 * CN=Computers,<domain_partition>
143 * distinguishedName: CN=Computers,<domain_partition>
147 * LDAP modifyRDN 1st LDAP connection:
149 * see: unbecomeDC_ldap_move_computer()
151 * Request:
152 * entry: CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition>
153 * newrdn: CN=<new_dc_netbios_name>
154 * deleteoldrdn: TRUE
155 * newparent: CN=Computers,<domain_partition>
156 * Result:
157 * <success>
161 * LDAP unbind on the 1st LDAP connection
163 * not implemented, because it's not needed...
167 * Open 1st DRSUAPI connection to the DC using admin credentials
168 * DsBind with DRSUAPI_DS_BIND_GUID ("e24d201a-4fd6-11d1-a3da-0000f875ae0d")
170 * see: unbecomeDC_drsuapi_connect_send(), unbecomeDC_drsuapi_connect_recv(),
171 * unbecomeDC_drsuapi_bind_send() and unbecomeDC_drsuapi_bind_recv()
175 * DsRemoveDsServer to remove the
176 * CN=<machine_name>,CN=Servers,CN=<site_name>,CN=Configuration,<domain_partition>
177 * and CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=<site_name>,CN=Configuration,<domain_partition>
178 * on the 1st DRSUAPI connection
180 * see: unbecomeDC_drsuapi_remove_ds_server_send() and unbecomeDC_drsuapi_remove_ds_server_recv()
184 * DsUnbind on the 1st DRSUAPI connection
186 * not implemented, because it's not needed...
190 struct libnet_UnbecomeDC_state {
191 struct composite_context *creq;
193 struct libnet_context *libnet;
195 struct {
196 struct NETLOGON_SAM_LOGON_RESPONSE_EX netlogon;
197 } cldap;
199 struct {
200 struct ldb_context *ldb;
201 } ldap;
203 struct {
204 struct dcerpc_binding *binding;
205 struct dcerpc_pipe *pipe;
206 struct dcerpc_binding_handle *drsuapi_handle;
207 struct drsuapi_DsBind bind_r;
208 struct GUID bind_guid;
209 struct drsuapi_DsBindInfoCtr bind_info_ctr;
210 struct drsuapi_DsBindInfo28 local_info28;
211 struct drsuapi_DsBindInfo28 remote_info28;
212 struct policy_handle bind_handle;
213 struct drsuapi_DsRemoveDSServer rm_ds_srv_r;
214 } drsuapi;
216 struct {
217 /* input */
218 const char *dns_name;
219 const char *netbios_name;
221 /* constructed */
222 struct GUID guid;
223 const char *dn_str;
224 } domain;
226 struct {
227 /* constructed */
228 const char *config_dn_str;
229 } forest;
231 struct {
232 /* input */
233 const char *address;
235 /* constructed */
236 const char *dns_name;
237 const char *netbios_name;
238 const char *site_name;
239 } source_dsa;
241 struct {
242 /* input */
243 const char *netbios_name;
245 /* constructed */
246 const char *dns_name;
247 const char *site_name;
248 const char *computer_dn_str;
249 const char *server_dn_str;
250 uint32_t user_account_control;
251 } dest_dsa;
254 static void unbecomeDC_recv_cldap(struct tevent_req *req);
256 static void unbecomeDC_send_cldap(struct libnet_UnbecomeDC_state *s)
258 struct composite_context *c = s->creq;
259 struct libnet_context *libnet = s->libnet;
260 struct tevent_req *req;
261 struct tsocket_address *dest_address;
262 int ret;
264 ret = tsocket_address_inet_from_strings(
265 s, "ip", s->source_dsa.address, 389, &dest_address);
266 if (ret != 0) {
267 c->status = map_nt_error_from_unix_common(errno);
268 if (!composite_is_ok(c)) return;
271 req = netlogon_pings_send(s, /* mem_ctx */
272 libnet->event_ctx, /* ev */
273 lpcfg_client_netlogon_ping_protocol(
274 libnet->lp_ctx), /* proto */
275 &dest_address, /* servers*/
276 1, /* num_servers */
277 (struct netlogon_ping_filter){
278 .ntversion = NETLOGON_NT_VERSION_5 |
279 NETLOGON_NT_VERSION_5EX,
280 .acct_ctrl = -1,
281 .domain = s->domain.dns_name,
282 .hostname = s->dest_dsa.netbios_name,
284 1, /* min_servers */
285 tevent_timeval_current_ofs(2,
286 0)); /* timeout */
288 if (composite_nomem(req, c)) return;
289 tevent_req_set_callback(req, unbecomeDC_recv_cldap, s);
292 static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s);
294 static void unbecomeDC_recv_cldap(struct tevent_req *req)
296 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(req,
297 struct libnet_UnbecomeDC_state);
298 struct composite_context *c = s->creq;
299 struct netlogon_samlogon_response **responses = NULL;
300 struct netlogon_samlogon_response *resp = NULL;
302 c->status = netlogon_pings_recv(req, s, &responses);
303 talloc_free(req);
304 if (!composite_is_ok(c)) {
305 DBG_ERR("Failed to send, receive or parse CLDAP reply "
306 "for our host %s: %s\n",
307 s->dest_dsa.netbios_name,
308 nt_errstr(c->status));
309 return;
311 resp = responses[0];
313 map_netlogon_samlogon_response(resp);
314 s->cldap.netlogon = resp->data.nt5_ex;
316 s->domain.dns_name = s->cldap.netlogon.dns_domain;
317 s->domain.netbios_name = s->cldap.netlogon.domain_name;
318 s->domain.guid = s->cldap.netlogon.domain_uuid;
320 s->source_dsa.dns_name = s->cldap.netlogon.pdc_dns_name;
321 s->source_dsa.netbios_name = s->cldap.netlogon.pdc_name;
322 s->source_dsa.site_name = s->cldap.netlogon.server_site;
324 s->dest_dsa.site_name = s->cldap.netlogon.client_site;
326 unbecomeDC_connect_ldap(s);
329 static NTSTATUS unbecomeDC_ldap_connect(struct libnet_UnbecomeDC_state *s)
331 char *url;
333 url = talloc_asprintf(s, "ldap://%s/", s->source_dsa.dns_name);
334 NT_STATUS_HAVE_NO_MEMORY(url);
336 s->ldap.ldb = ldb_wrap_connect(s, s->libnet->event_ctx, s->libnet->lp_ctx, url,
337 NULL,
338 s->libnet->cred,
340 talloc_free(url);
341 if (s->ldap.ldb == NULL) {
342 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
345 return NT_STATUS_OK;
348 static NTSTATUS unbecomeDC_ldap_rootdse(struct libnet_UnbecomeDC_state *s)
350 int ret;
351 struct ldb_result *r;
352 struct ldb_dn *basedn;
353 static const char *attrs[] = {
354 "defaultNamingContext",
355 "configurationNamingContext",
356 NULL
359 basedn = ldb_dn_new(s, s->ldap.ldb, NULL);
360 NT_STATUS_HAVE_NO_MEMORY(basedn);
362 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
363 "(objectClass=*)");
364 talloc_free(basedn);
365 if (ret != LDB_SUCCESS) {
366 return NT_STATUS_LDAP(ret);
367 } else if (r->count != 1) {
368 talloc_free(r);
369 return NT_STATUS_INVALID_NETWORK_RESPONSE;
372 s->domain.dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "defaultNamingContext", NULL);
373 if (!s->domain.dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
374 talloc_steal(s, s->domain.dn_str);
376 s->forest.config_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "configurationNamingContext", NULL);
377 if (!s->forest.config_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
378 talloc_steal(s, s->forest.config_dn_str);
380 s->dest_dsa.server_dn_str = talloc_asprintf(s, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
381 s->dest_dsa.netbios_name,
382 s->dest_dsa.site_name,
383 s->forest.config_dn_str);
384 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.server_dn_str);
386 talloc_free(r);
387 return NT_STATUS_OK;
390 static NTSTATUS unbecomeDC_ldap_computer_object(struct libnet_UnbecomeDC_state *s)
392 int ret;
393 struct ldb_result *r;
394 struct ldb_dn *basedn;
395 static const char *attrs[] = {
396 "distinguishedName",
397 "userAccountControl",
398 NULL
401 basedn = ldb_dn_new(s, s->ldap.ldb, s->domain.dn_str);
402 NT_STATUS_HAVE_NO_MEMORY(basedn);
404 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_SUBTREE, attrs,
405 "(&(|(objectClass=user)(objectClass=computer))(sAMAccountName=%s$))",
406 s->dest_dsa.netbios_name);
407 talloc_free(basedn);
408 if (ret != LDB_SUCCESS) {
409 return NT_STATUS_LDAP(ret);
410 } else if (r->count != 1) {
411 talloc_free(r);
412 return NT_STATUS_INVALID_NETWORK_RESPONSE;
415 s->dest_dsa.computer_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "distinguishedName", NULL);
416 if (!s->dest_dsa.computer_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
417 talloc_steal(s, s->dest_dsa.computer_dn_str);
419 s->dest_dsa.user_account_control = ldb_msg_find_attr_as_uint(r->msgs[0], "userAccountControl", 0);
421 talloc_free(r);
422 return NT_STATUS_OK;
425 static NTSTATUS unbecomeDC_ldap_modify_computer(struct libnet_UnbecomeDC_state *s)
427 int ret;
428 struct ldb_message *msg;
429 uint32_t user_account_control = UF_WORKSTATION_TRUST_ACCOUNT;
430 unsigned int i;
432 /* as the value is already as we want it to be, we're done */
433 if (s->dest_dsa.user_account_control == user_account_control) {
434 return NT_STATUS_OK;
437 /* make a 'modify' msg, and only for serverReference */
438 msg = ldb_msg_new(s);
439 NT_STATUS_HAVE_NO_MEMORY(msg);
440 msg->dn = ldb_dn_new(msg, s->ldap.ldb, s->dest_dsa.computer_dn_str);
441 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
443 ret = samdb_msg_add_uint(s->ldap.ldb, msg, msg, "userAccountControl",
444 user_account_control);
445 if (ret != LDB_SUCCESS) {
446 talloc_free(msg);
447 return NT_STATUS_NO_MEMORY;
450 /* mark all the message elements (should be just one)
451 as LDB_FLAG_MOD_REPLACE */
452 for (i=0;i<msg->num_elements;i++) {
453 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
456 ret = ldb_modify(s->ldap.ldb, msg);
457 talloc_free(msg);
458 if (ret != LDB_SUCCESS) {
459 return NT_STATUS_LDAP(ret);
462 s->dest_dsa.user_account_control = user_account_control;
464 return NT_STATUS_OK;
467 static NTSTATUS unbecomeDC_ldap_move_computer(struct libnet_UnbecomeDC_state *s)
469 int ret;
470 struct ldb_result *r;
471 struct ldb_dn *basedn;
472 struct ldb_dn *old_dn;
473 struct ldb_dn *new_dn;
474 static const char *_1_1_attrs[] = {
475 "1.1",
476 NULL
479 basedn = ldb_dn_new_fmt(s, s->ldap.ldb, "<WKGUID=aa312825768811d1aded00c04fd8d5cd,%s>",
480 s->domain.dn_str);
481 NT_STATUS_HAVE_NO_MEMORY(basedn);
483 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_BASE,
484 _1_1_attrs, "(objectClass=*)");
485 talloc_free(basedn);
486 if (ret != LDB_SUCCESS) {
487 return NT_STATUS_LDAP(ret);
488 } else if (r->count != 1) {
489 talloc_free(r);
490 return NT_STATUS_INVALID_NETWORK_RESPONSE;
493 old_dn = ldb_dn_new(r, s->ldap.ldb, s->dest_dsa.computer_dn_str);
494 NT_STATUS_HAVE_NO_MEMORY(old_dn);
496 new_dn = r->msgs[0]->dn;
498 if (!ldb_dn_add_child_fmt(new_dn, "CN=%s", s->dest_dsa.netbios_name)) {
499 talloc_free(r);
500 return NT_STATUS_NO_MEMORY;
503 if (ldb_dn_compare(old_dn, new_dn) == 0) {
504 /* we don't need to rename if the old and new dn match */
505 talloc_free(r);
506 return NT_STATUS_OK;
509 ret = ldb_rename(s->ldap.ldb, old_dn, new_dn);
510 if (ret != LDB_SUCCESS) {
511 talloc_free(r);
512 return NT_STATUS_LDAP(ret);
515 s->dest_dsa.computer_dn_str = ldb_dn_alloc_linearized(s, new_dn);
516 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.computer_dn_str);
518 talloc_free(r);
520 return NT_STATUS_OK;
523 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s);
525 static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s)
527 struct composite_context *c = s->creq;
529 c->status = unbecomeDC_ldap_connect(s);
530 if (!composite_is_ok(c)) return;
532 c->status = unbecomeDC_ldap_rootdse(s);
533 if (!composite_is_ok(c)) return;
535 c->status = unbecomeDC_ldap_computer_object(s);
536 if (!composite_is_ok(c)) return;
538 c->status = unbecomeDC_ldap_modify_computer(s);
539 if (!composite_is_ok(c)) return;
541 c->status = unbecomeDC_ldap_move_computer(s);
542 if (!composite_is_ok(c)) return;
544 unbecomeDC_drsuapi_connect_send(s);
547 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *creq);
549 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s)
551 struct composite_context *c = s->creq;
552 struct composite_context *creq;
553 char *binding_str;
555 binding_str = talloc_asprintf(s, "ncacn_ip_tcp:%s[seal,target_hostname=%s]",
556 s->source_dsa.address,
557 s->source_dsa.dns_name);
558 if (composite_nomem(binding_str, c)) return;
560 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi.binding);
561 talloc_free(binding_str);
562 if (!composite_is_ok(c)) return;
564 if (DEBUGLEVEL >= 10) {
565 c->status = dcerpc_binding_set_flags(s->drsuapi.binding,
566 DCERPC_DEBUG_PRINT_BOTH,
568 if (!composite_is_ok(c)) return;
571 creq = dcerpc_pipe_connect_b_send(s, s->drsuapi.binding, &ndr_table_drsuapi,
572 s->libnet->cred, s->libnet->event_ctx,
573 s->libnet->lp_ctx);
574 composite_continue(c, creq, unbecomeDC_drsuapi_connect_recv, s);
577 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s);
579 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *req)
581 struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
582 struct libnet_UnbecomeDC_state);
583 struct composite_context *c = s->creq;
585 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi.pipe);
586 if (!composite_is_ok(c)) return;
588 s->drsuapi.drsuapi_handle = s->drsuapi.pipe->binding_handle;
590 unbecomeDC_drsuapi_bind_send(s);
593 static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq);
595 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s)
597 struct composite_context *c = s->creq;
598 struct drsuapi_DsBindInfo28 *bind_info28;
599 struct tevent_req *subreq;
601 GUID_from_string(DRSUAPI_DS_BIND_GUID, &s->drsuapi.bind_guid);
603 bind_info28 = &s->drsuapi.local_info28;
604 bind_info28->supported_extensions = 0;
605 bind_info28->site_guid = GUID_zero();
606 bind_info28->pid = 0;
607 bind_info28->repl_epoch = 0;
609 s->drsuapi.bind_info_ctr.length = 28;
610 s->drsuapi.bind_info_ctr.info.info28 = *bind_info28;
612 s->drsuapi.bind_r.in.bind_guid = &s->drsuapi.bind_guid;
613 s->drsuapi.bind_r.in.bind_info = &s->drsuapi.bind_info_ctr;
614 s->drsuapi.bind_r.out.bind_handle = &s->drsuapi.bind_handle;
616 subreq = dcerpc_drsuapi_DsBind_r_send(s, c->event_ctx,
617 s->drsuapi.drsuapi_handle,
618 &s->drsuapi.bind_r);
619 if (composite_nomem(subreq, c)) return;
620 tevent_req_set_callback(subreq, unbecomeDC_drsuapi_bind_recv, s);
623 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s);
625 static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq)
627 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
628 struct libnet_UnbecomeDC_state);
629 struct composite_context *c = s->creq;
631 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
632 TALLOC_FREE(subreq);
633 if (!composite_is_ok(c)) return;
635 if (!W_ERROR_IS_OK(s->drsuapi.bind_r.out.result)) {
636 composite_error(c, werror_to_ntstatus(s->drsuapi.bind_r.out.result));
637 return;
640 ZERO_STRUCT(s->drsuapi.remote_info28);
641 if (s->drsuapi.bind_r.out.bind_info) {
642 switch (s->drsuapi.bind_r.out.bind_info->length) {
643 case 24: {
644 struct drsuapi_DsBindInfo24 *info24;
645 info24 = &s->drsuapi.bind_r.out.bind_info->info.info24;
646 s->drsuapi.remote_info28.supported_extensions = info24->supported_extensions;
647 s->drsuapi.remote_info28.site_guid = info24->site_guid;
648 s->drsuapi.remote_info28.pid = info24->pid;
649 s->drsuapi.remote_info28.repl_epoch = 0;
650 break;
652 case 28: {
653 s->drsuapi.remote_info28 = s->drsuapi.bind_r.out.bind_info->info.info28;
654 break;
656 case 32: {
657 struct drsuapi_DsBindInfo32 *info32;
658 info32 = &s->drsuapi.bind_r.out.bind_info->info.info32;
659 s->drsuapi.remote_info28.supported_extensions = info32->supported_extensions;
660 s->drsuapi.remote_info28.site_guid = info32->site_guid;
661 s->drsuapi.remote_info28.pid = info32->pid;
662 s->drsuapi.remote_info28.repl_epoch = info32->repl_epoch;
663 break;
665 case 48: {
666 struct drsuapi_DsBindInfo48 *info48;
667 info48 = &s->drsuapi.bind_r.out.bind_info->info.info48;
668 s->drsuapi.remote_info28.supported_extensions = info48->supported_extensions;
669 s->drsuapi.remote_info28.site_guid = info48->site_guid;
670 s->drsuapi.remote_info28.pid = info48->pid;
671 s->drsuapi.remote_info28.repl_epoch = info48->repl_epoch;
672 break;
674 case 52: {
675 struct drsuapi_DsBindInfo52 *info52;
676 info52 = &s->drsuapi.bind_r.out.bind_info->info.info52;
677 s->drsuapi.remote_info28.supported_extensions = info52->supported_extensions;
678 s->drsuapi.remote_info28.site_guid = info52->site_guid;
679 s->drsuapi.remote_info28.pid = info52->pid;
680 s->drsuapi.remote_info28.repl_epoch = info52->repl_epoch;
681 break;
683 default:
684 DEBUG(1, ("Warning: invalid info length in bind info: %d\n",
685 s->drsuapi.bind_r.out.bind_info->length));
686 break;
690 unbecomeDC_drsuapi_remove_ds_server_send(s);
693 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq);
695 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s)
697 struct composite_context *c = s->creq;
698 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
699 struct tevent_req *subreq;
701 r->in.bind_handle = &s->drsuapi.bind_handle;
702 r->in.level = 1;
703 r->in.req = talloc(s, union drsuapi_DsRemoveDSServerRequest);
704 r->in.req->req1.server_dn = s->dest_dsa.server_dn_str;
705 r->in.req->req1.domain_dn = s->domain.dn_str;
706 r->in.req->req1.commit = true;
708 r->out.level_out = talloc(s, uint32_t);
709 r->out.res = talloc(s, union drsuapi_DsRemoveDSServerResult);
711 subreq = dcerpc_drsuapi_DsRemoveDSServer_r_send(s, c->event_ctx,
712 s->drsuapi.drsuapi_handle,
714 if (composite_nomem(subreq, c)) return;
715 tevent_req_set_callback(subreq, unbecomeDC_drsuapi_remove_ds_server_recv, s);
718 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq)
720 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
721 struct libnet_UnbecomeDC_state);
722 struct composite_context *c = s->creq;
723 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
725 c->status = dcerpc_drsuapi_DsRemoveDSServer_r_recv(subreq, s);
726 TALLOC_FREE(subreq);
727 if (!composite_is_ok(c)) return;
729 if (!W_ERROR_IS_OK(r->out.result)) {
730 composite_error(c, werror_to_ntstatus(r->out.result));
731 return;
734 if (*r->out.level_out != 1) {
735 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
736 return;
739 composite_done(c);
742 struct composite_context *libnet_UnbecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
744 struct composite_context *c;
745 struct libnet_UnbecomeDC_state *s;
746 char *tmp_name;
748 c = composite_create(mem_ctx, ctx->event_ctx);
749 if (c == NULL) return NULL;
751 s = talloc_zero(c, struct libnet_UnbecomeDC_state);
752 if (composite_nomem(s, c)) return c;
753 c->private_data = s;
754 s->creq = c;
755 s->libnet = ctx;
757 /* Domain input */
758 s->domain.dns_name = talloc_strdup(s, r->in.domain_dns_name);
759 if (composite_nomem(s->domain.dns_name, c)) return c;
760 s->domain.netbios_name = talloc_strdup(s, r->in.domain_netbios_name);
761 if (composite_nomem(s->domain.netbios_name, c)) return c;
763 /* Source DSA input */
764 s->source_dsa.address = talloc_strdup(s, r->in.source_dsa_address);
765 if (composite_nomem(s->source_dsa.address, c)) return c;
767 /* Destination DSA input */
768 s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
769 if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
771 /* Destination DSA dns_name construction */
772 tmp_name = strlower_talloc(s, s->dest_dsa.netbios_name);
773 if (composite_nomem(tmp_name, c)) return c;
774 s->dest_dsa.dns_name = talloc_asprintf_append_buffer(tmp_name, ".%s",
775 s->domain.dns_name);
776 if (composite_nomem(s->dest_dsa.dns_name, c)) return c;
778 unbecomeDC_send_cldap(s);
779 return c;
782 NTSTATUS libnet_UnbecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
784 NTSTATUS status;
786 status = composite_wait(c);
788 ZERO_STRUCT(r->out);
790 talloc_free(c);
791 return status;
794 NTSTATUS libnet_UnbecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
796 NTSTATUS status;
797 struct composite_context *c;
798 c = libnet_UnbecomeDC_send(ctx, mem_ctx, r);
799 status = libnet_UnbecomeDC_recv(c, mem_ctx, r);
800 return status;