libcli/smb: fix smb2cli_ioctl*() against Windows 2008.
[Samba.git] / source4 / libnet / libnet_unbecome_dc.c
bloba46d1433bc6f69a33563b0919b151ebd773485e1
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]", s->source_dsa.dns_name);
547 if (composite_nomem(binding_str, c)) return;
549 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi.binding);
550 talloc_free(binding_str);
551 if (!composite_is_ok(c)) return;
553 if (DEBUGLEVEL >= 10) {
554 s->drsuapi.binding->flags |= DCERPC_DEBUG_PRINT_BOTH;
557 creq = dcerpc_pipe_connect_b_send(s, s->drsuapi.binding, &ndr_table_drsuapi,
558 s->libnet->cred, s->libnet->event_ctx,
559 s->libnet->lp_ctx);
560 composite_continue(c, creq, unbecomeDC_drsuapi_connect_recv, s);
563 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s);
565 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *req)
567 struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
568 struct libnet_UnbecomeDC_state);
569 struct composite_context *c = s->creq;
571 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi.pipe);
572 if (!composite_is_ok(c)) return;
574 s->drsuapi.drsuapi_handle = s->drsuapi.pipe->binding_handle;
576 unbecomeDC_drsuapi_bind_send(s);
579 static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq);
581 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s)
583 struct composite_context *c = s->creq;
584 struct drsuapi_DsBindInfo28 *bind_info28;
585 struct tevent_req *subreq;
587 GUID_from_string(DRSUAPI_DS_BIND_GUID, &s->drsuapi.bind_guid);
589 bind_info28 = &s->drsuapi.local_info28;
590 bind_info28->supported_extensions = 0;
591 bind_info28->site_guid = GUID_zero();
592 bind_info28->pid = 0;
593 bind_info28->repl_epoch = 0;
595 s->drsuapi.bind_info_ctr.length = 28;
596 s->drsuapi.bind_info_ctr.info.info28 = *bind_info28;
598 s->drsuapi.bind_r.in.bind_guid = &s->drsuapi.bind_guid;
599 s->drsuapi.bind_r.in.bind_info = &s->drsuapi.bind_info_ctr;
600 s->drsuapi.bind_r.out.bind_handle = &s->drsuapi.bind_handle;
602 subreq = dcerpc_drsuapi_DsBind_r_send(s, c->event_ctx,
603 s->drsuapi.drsuapi_handle,
604 &s->drsuapi.bind_r);
605 if (composite_nomem(subreq, c)) return;
606 tevent_req_set_callback(subreq, unbecomeDC_drsuapi_bind_recv, s);
609 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s);
611 static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq)
613 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
614 struct libnet_UnbecomeDC_state);
615 struct composite_context *c = s->creq;
617 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
618 TALLOC_FREE(subreq);
619 if (!composite_is_ok(c)) return;
621 if (!W_ERROR_IS_OK(s->drsuapi.bind_r.out.result)) {
622 composite_error(c, werror_to_ntstatus(s->drsuapi.bind_r.out.result));
623 return;
626 ZERO_STRUCT(s->drsuapi.remote_info28);
627 if (s->drsuapi.bind_r.out.bind_info) {
628 switch (s->drsuapi.bind_r.out.bind_info->length) {
629 case 24: {
630 struct drsuapi_DsBindInfo24 *info24;
631 info24 = &s->drsuapi.bind_r.out.bind_info->info.info24;
632 s->drsuapi.remote_info28.supported_extensions = info24->supported_extensions;
633 s->drsuapi.remote_info28.site_guid = info24->site_guid;
634 s->drsuapi.remote_info28.pid = info24->pid;
635 s->drsuapi.remote_info28.repl_epoch = 0;
636 break;
638 case 48: {
639 struct drsuapi_DsBindInfo48 *info48;
640 info48 = &s->drsuapi.bind_r.out.bind_info->info.info48;
641 s->drsuapi.remote_info28.supported_extensions = info48->supported_extensions;
642 s->drsuapi.remote_info28.site_guid = info48->site_guid;
643 s->drsuapi.remote_info28.pid = info48->pid;
644 s->drsuapi.remote_info28.repl_epoch = info48->repl_epoch;
645 break;
647 case 28:
648 s->drsuapi.remote_info28 = s->drsuapi.bind_r.out.bind_info->info.info28;
649 break;
653 unbecomeDC_drsuapi_remove_ds_server_send(s);
656 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq);
658 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s)
660 struct composite_context *c = s->creq;
661 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
662 struct tevent_req *subreq;
664 r->in.bind_handle = &s->drsuapi.bind_handle;
665 r->in.level = 1;
666 r->in.req = talloc(s, union drsuapi_DsRemoveDSServerRequest);
667 r->in.req->req1.server_dn = s->dest_dsa.server_dn_str;
668 r->in.req->req1.domain_dn = s->domain.dn_str;
669 r->in.req->req1.commit = true;
671 r->out.level_out = talloc(s, uint32_t);
672 r->out.res = talloc(s, union drsuapi_DsRemoveDSServerResult);
674 subreq = dcerpc_drsuapi_DsRemoveDSServer_r_send(s, c->event_ctx,
675 s->drsuapi.drsuapi_handle,
677 if (composite_nomem(subreq, c)) return;
678 tevent_req_set_callback(subreq, unbecomeDC_drsuapi_remove_ds_server_recv, s);
681 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq)
683 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
684 struct libnet_UnbecomeDC_state);
685 struct composite_context *c = s->creq;
686 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
688 c->status = dcerpc_drsuapi_DsRemoveDSServer_r_recv(subreq, s);
689 TALLOC_FREE(subreq);
690 if (!composite_is_ok(c)) return;
692 if (!W_ERROR_IS_OK(r->out.result)) {
693 composite_error(c, werror_to_ntstatus(r->out.result));
694 return;
697 if (*r->out.level_out != 1) {
698 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
699 return;
702 composite_done(c);
705 struct composite_context *libnet_UnbecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
707 struct composite_context *c;
708 struct libnet_UnbecomeDC_state *s;
709 char *tmp_name;
711 c = composite_create(mem_ctx, ctx->event_ctx);
712 if (c == NULL) return NULL;
714 s = talloc_zero(c, struct libnet_UnbecomeDC_state);
715 if (composite_nomem(s, c)) return c;
716 c->private_data = s;
717 s->creq = c;
718 s->libnet = ctx;
720 /* Domain input */
721 s->domain.dns_name = talloc_strdup(s, r->in.domain_dns_name);
722 if (composite_nomem(s->domain.dns_name, c)) return c;
723 s->domain.netbios_name = talloc_strdup(s, r->in.domain_netbios_name);
724 if (composite_nomem(s->domain.netbios_name, c)) return c;
726 /* Source DSA input */
727 s->source_dsa.address = talloc_strdup(s, r->in.source_dsa_address);
728 if (composite_nomem(s->source_dsa.address, c)) return c;
730 /* Destination DSA input */
731 s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
732 if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
734 /* Destination DSA dns_name construction */
735 tmp_name = strlower_talloc(s, s->dest_dsa.netbios_name);
736 if (composite_nomem(tmp_name, c)) return c;
737 s->dest_dsa.dns_name = talloc_asprintf_append_buffer(tmp_name, ".%s",
738 s->domain.dns_name);
739 if (composite_nomem(s->dest_dsa.dns_name, c)) return c;
741 unbecomeDC_send_cldap(s);
742 return c;
745 NTSTATUS libnet_UnbecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
747 NTSTATUS status;
749 status = composite_wait(c);
751 ZERO_STRUCT(r->out);
753 talloc_free(c);
754 return status;
757 NTSTATUS libnet_UnbecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
759 NTSTATUS status;
760 struct composite_context *c;
761 c = libnet_UnbecomeDC_send(ctx, mem_ctx, r);
762 status = libnet_UnbecomeDC_recv(c, mem_ctx, r);
763 return status;