s3-docs: Fix make manpages3.
[Samba/vl.git] / source4 / libnet / libnet_unbecome_dc.c
blob6e1ecf072d26aeabdfe390635c2cda492af0d06f
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 "lib/ldb/include/ldb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "lib/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"
32 /*****************************************************************************
33 * Windows 2003 (w2k3) does the following steps when changing the server role
34 * from domain controller back to domain member
36 * We mostly do the same.
37 *****************************************************************************/
40 * lookup DC:
41 * - using nbt name<1C> request and a samlogon mailslot request
42 * or
43 * - using a DNS SRV _ldap._tcp.dc._msdcs. request and a CLDAP netlogon request
45 * see: unbecomeDC_send_cldap() and unbecomeDC_recv_cldap()
49 * Open 1st LDAP connection to the DC using admin credentials
51 * see: unbecomeDC_ldap_connect()
55 * LDAP search 1st LDAP connection:
57 * see: unbecomeDC_ldap_rootdse()
59 * Request:
60 * basedn: ""
61 * scope: base
62 * filter: (objectClass=*)
63 * attrs: defaultNamingContext
64 * configurationNamingContext
65 * Result:
66 * ""
67 * defaultNamingContext: <domain_partition>
68 * configurationNamingContext:CN=Configuration,<domain_partition>
72 * LDAP search 1st LDAP connection:
74 * see: unbecomeDC_ldap_computer_object()
76 * Request:
77 * basedn: <domain_partition>
78 * scope: sub
79 * filter: (&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<new_dc_account_name>))
80 * attrs: distinguishedName
81 * userAccountControl
82 * Result:
83 * CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition>
84 * distinguishedName: CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition>
85 * userAccoountControl: 532480 <0x82000>
89 * LDAP search 1st LDAP connection:
91 * see: unbecomeDC_ldap_modify_computer()
93 * Request:
94 * basedn: CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
95 * scope: base
96 * filter: (objectClass=*)
97 * attrs: userAccountControl
98 * Result:
99 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
100 * userAccoountControl: 532480 <0x82000>
104 * LDAP modify 1st LDAP connection:
106 * see: unbecomeDC_ldap_modify_computer()
108 * Request (replace):
109 * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
110 * userAccoountControl: 4096 <0x1000>
111 * Result:
112 * <success>
116 * LDAP search 1st LDAP connection:
118 * see: unbecomeDC_ldap_move_computer()
120 * Request:
121 * basedn: <WKGUID=aa312825768811d1aded00c04fd8d5cd,<domain_partition>>
122 * scope: base
123 * filter: (objectClass=*)
124 * attrs: 1.1
125 * Result:
126 * CN=Computers,<domain_partition>
130 * LDAP search 1st LDAP connection:
132 * not implemented because it doesn't give any new information
134 * Request:
135 * basedn: CN=Computers,<domain_partition>
136 * scope: base
137 * filter: (objectClass=*)
138 * attrs: distinguishedName
139 * Result:
140 * CN=Computers,<domain_partition>
141 * distinguishedName: CN=Computers,<domain_partition>
145 * LDAP modifyRDN 1st LDAP connection:
147 * see: unbecomeDC_ldap_move_computer()
149 * Request:
150 * entry: CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition>
151 * newrdn: CN=<new_dc_netbios_name>
152 * deleteoldrdn: TRUE
153 * newparent: CN=Computers,<domain_partition>
154 * Result:
155 * <success>
159 * LDAP unbind on the 1st LDAP connection
161 * not implemented, because it's not needed...
165 * Open 1st DRSUAPI connection to the DC using admin credentials
166 * DsBind with DRSUAPI_DS_BIND_GUID ("e24d201a-4fd6-11d1-a3da-0000f875ae0d")
168 * see: unbecomeDC_drsuapi_connect_send(), unbecomeDC_drsuapi_connect_recv(),
169 * unbecomeDC_drsuapi_bind_send() and unbecomeDC_drsuapi_bind_recv()
173 * DsRemoveDsServer to remove the
174 * CN=<machine_name>,CN=Servers,CN=<site_name>,CN=Configuration,<domain_partition>
175 * and CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=<site_name>,CN=Configuration,<domain_partition>
176 * on the 1st DRSUAPI connection
178 * see: unbecomeDC_drsuapi_remove_ds_server_send() and unbecomeDC_drsuapi_remove_ds_server_recv()
182 * DsUnbind on the 1st DRSUAPI connection
184 * not implemented, because it's not needed...
188 struct libnet_UnbecomeDC_state {
189 struct composite_context *creq;
191 struct libnet_context *libnet;
193 struct {
194 struct cldap_socket *sock;
195 struct cldap_netlogon io;
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 drsuapi_DsBind bind_r;
207 struct GUID bind_guid;
208 struct drsuapi_DsBindInfoCtr bind_info_ctr;
209 struct drsuapi_DsBindInfo28 local_info28;
210 struct drsuapi_DsBindInfo28 remote_info28;
211 struct policy_handle bind_handle;
212 struct drsuapi_DsRemoveDSServer rm_ds_srv_r;
213 } drsuapi;
215 struct {
216 /* input */
217 const char *dns_name;
218 const char *netbios_name;
220 /* constructed */
221 struct GUID guid;
222 const char *dn_str;
223 } domain;
225 struct {
226 /* constructed */
227 const char *config_dn_str;
228 } forest;
230 struct {
231 /* input */
232 const char *address;
234 /* constructed */
235 const char *dns_name;
236 const char *netbios_name;
237 const char *site_name;
238 } source_dsa;
240 struct {
241 /* input */
242 const char *netbios_name;
244 /* constructed */
245 const char *dns_name;
246 const char *site_name;
247 const char *computer_dn_str;
248 const char *server_dn_str;
249 uint32_t user_account_control;
250 } dest_dsa;
253 static void unbecomeDC_recv_cldap(struct tevent_req *req);
255 static void unbecomeDC_send_cldap(struct libnet_UnbecomeDC_state *s)
257 struct composite_context *c = s->creq;
258 struct tevent_req *req;
260 s->cldap.io.in.dest_address = s->source_dsa.address;
261 s->cldap.io.in.dest_port = lp_cldap_port(s->libnet->lp_ctx);
262 s->cldap.io.in.realm = s->domain.dns_name;
263 s->cldap.io.in.host = s->dest_dsa.netbios_name;
264 s->cldap.io.in.user = NULL;
265 s->cldap.io.in.domain_guid = NULL;
266 s->cldap.io.in.domain_sid = NULL;
267 s->cldap.io.in.acct_control = -1;
268 s->cldap.io.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
269 s->cldap.io.in.map_response = true;
271 c->status = cldap_socket_init(s, s->libnet->event_ctx,
272 NULL, NULL, &s->cldap.sock);//TODO
273 if (!composite_is_ok(c)) return;
275 req = cldap_netlogon_send(s, s->cldap.sock, &s->cldap.io);
276 if (composite_nomem(req, c)) return;
277 tevent_req_set_callback(req, unbecomeDC_recv_cldap, s);
280 static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s);
282 static void unbecomeDC_recv_cldap(struct tevent_req *req)
284 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(req,
285 struct libnet_UnbecomeDC_state);
286 struct composite_context *c = s->creq;
288 c->status = cldap_netlogon_recv(req,
289 lp_iconv_convenience(s->libnet->lp_ctx),
290 s, &s->cldap.io);
291 talloc_free(req);
292 if (!composite_is_ok(c)) return;
294 s->cldap.netlogon = s->cldap.io.out.netlogon.data.nt5_ex;
296 s->domain.dns_name = s->cldap.netlogon.dns_domain;
297 s->domain.netbios_name = s->cldap.netlogon.domain;
298 s->domain.guid = s->cldap.netlogon.domain_uuid;
300 s->source_dsa.dns_name = s->cldap.netlogon.pdc_dns_name;
301 s->source_dsa.netbios_name = s->cldap.netlogon.pdc_name;
302 s->source_dsa.site_name = s->cldap.netlogon.server_site;
304 s->dest_dsa.site_name = s->cldap.netlogon.client_site;
306 unbecomeDC_connect_ldap(s);
309 static NTSTATUS unbecomeDC_ldap_connect(struct libnet_UnbecomeDC_state *s)
311 char *url;
313 url = talloc_asprintf(s, "ldap://%s/", s->source_dsa.dns_name);
314 NT_STATUS_HAVE_NO_MEMORY(url);
316 s->ldap.ldb = ldb_wrap_connect(s, s->libnet->event_ctx, s->libnet->lp_ctx, url,
317 NULL,
318 s->libnet->cred,
320 talloc_free(url);
321 if (s->ldap.ldb == NULL) {
322 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
325 return NT_STATUS_OK;
328 static NTSTATUS unbecomeDC_ldap_rootdse(struct libnet_UnbecomeDC_state *s)
330 int ret;
331 struct ldb_result *r;
332 struct ldb_dn *basedn;
333 static const char *attrs[] = {
334 "defaultNamingContext",
335 "configurationNamingContext",
336 NULL
339 basedn = ldb_dn_new(s, s->ldap.ldb, NULL);
340 NT_STATUS_HAVE_NO_MEMORY(basedn);
342 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_BASE, attrs,
343 "(objectClass=*)");
344 talloc_free(basedn);
345 if (ret != LDB_SUCCESS) {
346 return NT_STATUS_LDAP(ret);
347 } else if (r->count != 1) {
348 talloc_free(r);
349 return NT_STATUS_INVALID_NETWORK_RESPONSE;
352 s->domain.dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "defaultNamingContext", NULL);
353 if (!s->domain.dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
354 talloc_steal(s, s->domain.dn_str);
356 s->forest.config_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "configurationNamingContext", NULL);
357 if (!s->forest.config_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
358 talloc_steal(s, s->forest.config_dn_str);
360 s->dest_dsa.server_dn_str = talloc_asprintf(s, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
361 s->dest_dsa.netbios_name,
362 s->dest_dsa.site_name,
363 s->forest.config_dn_str);
364 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.server_dn_str);
366 talloc_free(r);
367 return NT_STATUS_OK;
370 static NTSTATUS unbecomeDC_ldap_computer_object(struct libnet_UnbecomeDC_state *s)
372 int ret;
373 struct ldb_result *r;
374 struct ldb_dn *basedn;
375 static const char *attrs[] = {
376 "distinguishedName",
377 "userAccountControl",
378 NULL
381 basedn = ldb_dn_new(s, s->ldap.ldb, s->domain.dn_str);
382 NT_STATUS_HAVE_NO_MEMORY(basedn);
384 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_SUBTREE, attrs,
385 "(&(|(objectClass=user)(objectClass=computer))(sAMAccountName=%s$))",
386 s->dest_dsa.netbios_name);
387 talloc_free(basedn);
388 if (ret != LDB_SUCCESS) {
389 return NT_STATUS_LDAP(ret);
390 } else if (r->count != 1) {
391 talloc_free(r);
392 return NT_STATUS_INVALID_NETWORK_RESPONSE;
395 s->dest_dsa.computer_dn_str = samdb_result_string(r->msgs[0], "distinguishedName", NULL);
396 if (!s->dest_dsa.computer_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
397 talloc_steal(s, s->dest_dsa.computer_dn_str);
399 s->dest_dsa.user_account_control = samdb_result_uint(r->msgs[0], "userAccountControl", 0);
401 talloc_free(r);
402 return NT_STATUS_OK;
405 static NTSTATUS unbecomeDC_ldap_modify_computer(struct libnet_UnbecomeDC_state *s)
407 int ret;
408 struct ldb_message *msg;
409 uint32_t user_account_control = UF_WORKSTATION_TRUST_ACCOUNT;
410 uint32_t i;
412 /* as the value is already as we want it to be, we're done */
413 if (s->dest_dsa.user_account_control == user_account_control) {
414 return NT_STATUS_OK;
417 /* make a 'modify' msg, and only for serverReference */
418 msg = ldb_msg_new(s);
419 NT_STATUS_HAVE_NO_MEMORY(msg);
420 msg->dn = ldb_dn_new(msg, s->ldap.ldb, s->dest_dsa.computer_dn_str);
421 NT_STATUS_HAVE_NO_MEMORY(msg->dn);
423 ret = ldb_msg_add_fmt(msg, "userAccountControl", "%u", user_account_control);
424 if (ret != 0) {
425 talloc_free(msg);
426 return NT_STATUS_NO_MEMORY;
429 /* mark all the message elements (should be just one)
430 as LDB_FLAG_MOD_REPLACE */
431 for (i=0;i<msg->num_elements;i++) {
432 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
435 ret = ldb_modify(s->ldap.ldb, msg);
436 talloc_free(msg);
437 if (ret != LDB_SUCCESS) {
438 return NT_STATUS_LDAP(ret);
441 s->dest_dsa.user_account_control = user_account_control;
443 return NT_STATUS_OK;
446 static NTSTATUS unbecomeDC_ldap_move_computer(struct libnet_UnbecomeDC_state *s)
448 int ret;
449 struct ldb_result *r;
450 struct ldb_dn *basedn;
451 struct ldb_dn *old_dn;
452 struct ldb_dn *new_dn;
453 static const char *_1_1_attrs[] = {
454 "1.1",
455 NULL
458 basedn = ldb_dn_new_fmt(s, s->ldap.ldb, "<WKGUID=aa312825768811d1aded00c04fd8d5cd,%s>",
459 s->domain.dn_str);
460 NT_STATUS_HAVE_NO_MEMORY(basedn);
462 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_BASE,
463 _1_1_attrs, "(objectClass=*)");
464 talloc_free(basedn);
465 if (ret != LDB_SUCCESS) {
466 return NT_STATUS_LDAP(ret);
467 } else if (r->count != 1) {
468 talloc_free(r);
469 return NT_STATUS_INVALID_NETWORK_RESPONSE;
472 old_dn = ldb_dn_new(r, s->ldap.ldb, s->dest_dsa.computer_dn_str);
473 NT_STATUS_HAVE_NO_MEMORY(old_dn);
475 new_dn = r->msgs[0]->dn;
477 if (!ldb_dn_add_child_fmt(new_dn, "CN=%s", s->dest_dsa.netbios_name)) {
478 talloc_free(r);
479 return NT_STATUS_NO_MEMORY;
482 if (ldb_dn_compare(old_dn, new_dn) == 0) {
483 /* we don't need to rename if the old and new dn match */
484 talloc_free(r);
485 return NT_STATUS_OK;
488 ret = ldb_rename(s->ldap.ldb, old_dn, new_dn);
489 if (ret != LDB_SUCCESS) {
490 talloc_free(r);
491 return NT_STATUS_LDAP(ret);
494 s->dest_dsa.computer_dn_str = ldb_dn_alloc_linearized(s, new_dn);
495 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.computer_dn_str);
497 talloc_free(r);
499 return NT_STATUS_OK;
502 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s);
504 static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s)
506 struct composite_context *c = s->creq;
508 c->status = unbecomeDC_ldap_connect(s);
509 if (!composite_is_ok(c)) return;
511 c->status = unbecomeDC_ldap_rootdse(s);
512 if (!composite_is_ok(c)) return;
514 c->status = unbecomeDC_ldap_computer_object(s);
515 if (!composite_is_ok(c)) return;
517 c->status = unbecomeDC_ldap_modify_computer(s);
518 if (!composite_is_ok(c)) return;
520 c->status = unbecomeDC_ldap_move_computer(s);
521 if (!composite_is_ok(c)) return;
523 unbecomeDC_drsuapi_connect_send(s);
526 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *creq);
528 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s)
530 struct composite_context *c = s->creq;
531 struct composite_context *creq;
532 char *binding_str;
534 binding_str = talloc_asprintf(s, "ncacn_ip_tcp:%s[seal]", s->source_dsa.dns_name);
535 if (composite_nomem(binding_str, c)) return;
537 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi.binding);
538 talloc_free(binding_str);
539 if (!composite_is_ok(c)) return;
541 creq = dcerpc_pipe_connect_b_send(s, s->drsuapi.binding, &ndr_table_drsuapi,
542 s->libnet->cred, s->libnet->event_ctx,
543 s->libnet->lp_ctx);
544 composite_continue(c, creq, unbecomeDC_drsuapi_connect_recv, s);
547 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s);
549 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *req)
551 struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
552 struct libnet_UnbecomeDC_state);
553 struct composite_context *c = s->creq;
555 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi.pipe);
556 if (!composite_is_ok(c)) return;
558 unbecomeDC_drsuapi_bind_send(s);
561 static void unbecomeDC_drsuapi_bind_recv(struct rpc_request *req);
563 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s)
565 struct composite_context *c = s->creq;
566 struct rpc_request *req;
567 struct drsuapi_DsBindInfo28 *bind_info28;
569 GUID_from_string(DRSUAPI_DS_BIND_GUID, &s->drsuapi.bind_guid);
571 bind_info28 = &s->drsuapi.local_info28;
572 bind_info28->supported_extensions = 0;
573 bind_info28->site_guid = GUID_zero();
574 bind_info28->pid = 0;
575 bind_info28->repl_epoch = 0;
577 s->drsuapi.bind_info_ctr.length = 28;
578 s->drsuapi.bind_info_ctr.info.info28 = *bind_info28;
580 s->drsuapi.bind_r.in.bind_guid = &s->drsuapi.bind_guid;
581 s->drsuapi.bind_r.in.bind_info = &s->drsuapi.bind_info_ctr;
582 s->drsuapi.bind_r.out.bind_handle = &s->drsuapi.bind_handle;
584 req = dcerpc_drsuapi_DsBind_send(s->drsuapi.pipe, s, &s->drsuapi.bind_r);
585 composite_continue_rpc(c, req, unbecomeDC_drsuapi_bind_recv, s);
588 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s);
590 static void unbecomeDC_drsuapi_bind_recv(struct rpc_request *req)
592 struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
593 struct libnet_UnbecomeDC_state);
594 struct composite_context *c = s->creq;
596 c->status = dcerpc_ndr_request_recv(req);
597 if (!composite_is_ok(c)) return;
599 if (!W_ERROR_IS_OK(s->drsuapi.bind_r.out.result)) {
600 composite_error(c, werror_to_ntstatus(s->drsuapi.bind_r.out.result));
601 return;
604 ZERO_STRUCT(s->drsuapi.remote_info28);
605 if (s->drsuapi.bind_r.out.bind_info) {
606 switch (s->drsuapi.bind_r.out.bind_info->length) {
607 case 24: {
608 struct drsuapi_DsBindInfo24 *info24;
609 info24 = &s->drsuapi.bind_r.out.bind_info->info.info24;
610 s->drsuapi.remote_info28.supported_extensions = info24->supported_extensions;
611 s->drsuapi.remote_info28.site_guid = info24->site_guid;
612 s->drsuapi.remote_info28.pid = info24->pid;
613 s->drsuapi.remote_info28.repl_epoch = 0;
614 break;
616 case 48: {
617 struct drsuapi_DsBindInfo48 *info48;
618 info48 = &s->drsuapi.bind_r.out.bind_info->info.info48;
619 s->drsuapi.remote_info28.supported_extensions = info48->supported_extensions;
620 s->drsuapi.remote_info28.site_guid = info48->site_guid;
621 s->drsuapi.remote_info28.pid = info48->pid;
622 s->drsuapi.remote_info28.repl_epoch = info48->repl_epoch;
623 break;
625 case 28:
626 s->drsuapi.remote_info28 = s->drsuapi.bind_r.out.bind_info->info.info28;
627 break;
631 unbecomeDC_drsuapi_remove_ds_server_send(s);
634 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct rpc_request *req);
636 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s)
638 struct composite_context *c = s->creq;
639 struct rpc_request *req;
640 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
642 r->in.bind_handle = &s->drsuapi.bind_handle;
643 r->in.level = 1;
644 r->in.req = talloc(s, union drsuapi_DsRemoveDSServerRequest);
645 r->in.req->req1.server_dn = s->dest_dsa.server_dn_str;
646 r->in.req->req1.domain_dn = s->domain.dn_str;
647 r->in.req->req1.commit = true;
649 r->out.level_out = talloc(s, int32_t);
650 r->out.res = talloc(s, union drsuapi_DsRemoveDSServerResult);
652 req = dcerpc_drsuapi_DsRemoveDSServer_send(s->drsuapi.pipe, s, r);
653 composite_continue_rpc(c, req, unbecomeDC_drsuapi_remove_ds_server_recv, s);
656 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct rpc_request *req)
658 struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
659 struct libnet_UnbecomeDC_state);
660 struct composite_context *c = s->creq;
661 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
663 c->status = dcerpc_ndr_request_recv(req);
664 if (!composite_is_ok(c)) return;
666 if (!W_ERROR_IS_OK(r->out.result)) {
667 composite_error(c, werror_to_ntstatus(r->out.result));
668 return;
671 if (*r->out.level_out != 1) {
672 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
673 return;
676 composite_done(c);
679 struct composite_context *libnet_UnbecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
681 struct composite_context *c;
682 struct libnet_UnbecomeDC_state *s;
683 char *tmp_name;
685 c = composite_create(mem_ctx, ctx->event_ctx);
686 if (c == NULL) return NULL;
688 s = talloc_zero(c, struct libnet_UnbecomeDC_state);
689 if (composite_nomem(s, c)) return c;
690 c->private_data = s;
691 s->creq = c;
692 s->libnet = ctx;
694 /* Domain input */
695 s->domain.dns_name = talloc_strdup(s, r->in.domain_dns_name);
696 if (composite_nomem(s->domain.dns_name, c)) return c;
697 s->domain.netbios_name = talloc_strdup(s, r->in.domain_netbios_name);
698 if (composite_nomem(s->domain.netbios_name, c)) return c;
700 /* Source DSA input */
701 s->source_dsa.address = talloc_strdup(s, r->in.source_dsa_address);
702 if (composite_nomem(s->source_dsa.address, c)) return c;
704 /* Destination DSA input */
705 s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
706 if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
708 /* Destination DSA dns_name construction */
709 tmp_name = strlower_talloc(s, s->dest_dsa.netbios_name);
710 if (composite_nomem(tmp_name, c)) return c;
711 s->dest_dsa.dns_name = talloc_asprintf_append_buffer(tmp_name, ".%s",
712 s->domain.dns_name);
713 if (composite_nomem(s->dest_dsa.dns_name, c)) return c;
715 unbecomeDC_send_cldap(s);
716 return c;
719 NTSTATUS libnet_UnbecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
721 NTSTATUS status;
723 status = composite_wait(c);
725 ZERO_STRUCT(r->out);
727 talloc_free(c);
728 return status;
731 NTSTATUS libnet_UnbecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
733 NTSTATUS status;
734 struct composite_context *c;
735 c = libnet_UnbecomeDC_send(ctx, mem_ctx, r);
736 status = libnet_UnbecomeDC_recv(c, mem_ctx, r);
737 return status;