s3-waf: Build libwbclient as dynamic library
[Samba/gebeck_regimport.git] / source4 / libnet / libnet_unbecome_dc.c
blobcf0142da8625caa7b80d3f6e4694a7fef0a3f12a
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 "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(errno);
281 if (!composite_is_ok(c)) return;
284 c->status = cldap_socket_init(s, s->libnet->event_ctx,
285 NULL, dest_address, &s->cldap.sock);
286 if (!composite_is_ok(c)) return;
288 req = cldap_netlogon_send(s, 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 = samdb_result_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 = samdb_result_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 = ldb_msg_add_fmt(msg, "userAccountControl", "%u", user_account_control);
435 if (ret != 0) {
436 talloc_free(msg);
437 return NT_STATUS_NO_MEMORY;
440 /* mark all the message elements (should be just one)
441 as LDB_FLAG_MOD_REPLACE */
442 for (i=0;i<msg->num_elements;i++) {
443 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
446 ret = ldb_modify(s->ldap.ldb, msg);
447 talloc_free(msg);
448 if (ret != LDB_SUCCESS) {
449 return NT_STATUS_LDAP(ret);
452 s->dest_dsa.user_account_control = user_account_control;
454 return NT_STATUS_OK;
457 static NTSTATUS unbecomeDC_ldap_move_computer(struct libnet_UnbecomeDC_state *s)
459 int ret;
460 struct ldb_result *r;
461 struct ldb_dn *basedn;
462 struct ldb_dn *old_dn;
463 struct ldb_dn *new_dn;
464 static const char *_1_1_attrs[] = {
465 "1.1",
466 NULL
469 basedn = ldb_dn_new_fmt(s, s->ldap.ldb, "<WKGUID=aa312825768811d1aded00c04fd8d5cd,%s>",
470 s->domain.dn_str);
471 NT_STATUS_HAVE_NO_MEMORY(basedn);
473 ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_BASE,
474 _1_1_attrs, "(objectClass=*)");
475 talloc_free(basedn);
476 if (ret != LDB_SUCCESS) {
477 return NT_STATUS_LDAP(ret);
478 } else if (r->count != 1) {
479 talloc_free(r);
480 return NT_STATUS_INVALID_NETWORK_RESPONSE;
483 old_dn = ldb_dn_new(r, s->ldap.ldb, s->dest_dsa.computer_dn_str);
484 NT_STATUS_HAVE_NO_MEMORY(old_dn);
486 new_dn = r->msgs[0]->dn;
488 if (!ldb_dn_add_child_fmt(new_dn, "CN=%s", s->dest_dsa.netbios_name)) {
489 talloc_free(r);
490 return NT_STATUS_NO_MEMORY;
493 if (ldb_dn_compare(old_dn, new_dn) == 0) {
494 /* we don't need to rename if the old and new dn match */
495 talloc_free(r);
496 return NT_STATUS_OK;
499 ret = ldb_rename(s->ldap.ldb, old_dn, new_dn);
500 if (ret != LDB_SUCCESS) {
501 talloc_free(r);
502 return NT_STATUS_LDAP(ret);
505 s->dest_dsa.computer_dn_str = ldb_dn_alloc_linearized(s, new_dn);
506 NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.computer_dn_str);
508 talloc_free(r);
510 return NT_STATUS_OK;
513 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s);
515 static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s)
517 struct composite_context *c = s->creq;
519 c->status = unbecomeDC_ldap_connect(s);
520 if (!composite_is_ok(c)) return;
522 c->status = unbecomeDC_ldap_rootdse(s);
523 if (!composite_is_ok(c)) return;
525 c->status = unbecomeDC_ldap_computer_object(s);
526 if (!composite_is_ok(c)) return;
528 c->status = unbecomeDC_ldap_modify_computer(s);
529 if (!composite_is_ok(c)) return;
531 c->status = unbecomeDC_ldap_move_computer(s);
532 if (!composite_is_ok(c)) return;
534 unbecomeDC_drsuapi_connect_send(s);
537 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *creq);
539 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s)
541 struct composite_context *c = s->creq;
542 struct composite_context *creq;
543 char *binding_str;
545 binding_str = talloc_asprintf(s, "ncacn_ip_tcp:%s[seal]", s->source_dsa.dns_name);
546 if (composite_nomem(binding_str, c)) return;
548 c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi.binding);
549 talloc_free(binding_str);
550 if (!composite_is_ok(c)) return;
552 creq = dcerpc_pipe_connect_b_send(s, s->drsuapi.binding, &ndr_table_drsuapi,
553 s->libnet->cred, s->libnet->event_ctx,
554 s->libnet->lp_ctx);
555 composite_continue(c, creq, unbecomeDC_drsuapi_connect_recv, s);
558 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s);
560 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *req)
562 struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
563 struct libnet_UnbecomeDC_state);
564 struct composite_context *c = s->creq;
566 c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi.pipe);
567 if (!composite_is_ok(c)) return;
569 s->drsuapi.drsuapi_handle = s->drsuapi.pipe->binding_handle;
571 unbecomeDC_drsuapi_bind_send(s);
574 static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq);
576 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s)
578 struct composite_context *c = s->creq;
579 struct drsuapi_DsBindInfo28 *bind_info28;
580 struct tevent_req *subreq;
582 GUID_from_string(DRSUAPI_DS_BIND_GUID, &s->drsuapi.bind_guid);
584 bind_info28 = &s->drsuapi.local_info28;
585 bind_info28->supported_extensions = 0;
586 bind_info28->site_guid = GUID_zero();
587 bind_info28->pid = 0;
588 bind_info28->repl_epoch = 0;
590 s->drsuapi.bind_info_ctr.length = 28;
591 s->drsuapi.bind_info_ctr.info.info28 = *bind_info28;
593 s->drsuapi.bind_r.in.bind_guid = &s->drsuapi.bind_guid;
594 s->drsuapi.bind_r.in.bind_info = &s->drsuapi.bind_info_ctr;
595 s->drsuapi.bind_r.out.bind_handle = &s->drsuapi.bind_handle;
597 subreq = dcerpc_drsuapi_DsBind_r_send(s, c->event_ctx,
598 s->drsuapi.drsuapi_handle,
599 &s->drsuapi.bind_r);
600 if (composite_nomem(subreq, c)) return;
601 tevent_req_set_callback(subreq, unbecomeDC_drsuapi_bind_recv, s);
604 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s);
606 static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq)
608 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
609 struct libnet_UnbecomeDC_state);
610 struct composite_context *c = s->creq;
612 c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
613 TALLOC_FREE(subreq);
614 if (!composite_is_ok(c)) return;
616 if (!W_ERROR_IS_OK(s->drsuapi.bind_r.out.result)) {
617 composite_error(c, werror_to_ntstatus(s->drsuapi.bind_r.out.result));
618 return;
621 ZERO_STRUCT(s->drsuapi.remote_info28);
622 if (s->drsuapi.bind_r.out.bind_info) {
623 switch (s->drsuapi.bind_r.out.bind_info->length) {
624 case 24: {
625 struct drsuapi_DsBindInfo24 *info24;
626 info24 = &s->drsuapi.bind_r.out.bind_info->info.info24;
627 s->drsuapi.remote_info28.supported_extensions = info24->supported_extensions;
628 s->drsuapi.remote_info28.site_guid = info24->site_guid;
629 s->drsuapi.remote_info28.pid = info24->pid;
630 s->drsuapi.remote_info28.repl_epoch = 0;
631 break;
633 case 48: {
634 struct drsuapi_DsBindInfo48 *info48;
635 info48 = &s->drsuapi.bind_r.out.bind_info->info.info48;
636 s->drsuapi.remote_info28.supported_extensions = info48->supported_extensions;
637 s->drsuapi.remote_info28.site_guid = info48->site_guid;
638 s->drsuapi.remote_info28.pid = info48->pid;
639 s->drsuapi.remote_info28.repl_epoch = info48->repl_epoch;
640 break;
642 case 28:
643 s->drsuapi.remote_info28 = s->drsuapi.bind_r.out.bind_info->info.info28;
644 break;
648 unbecomeDC_drsuapi_remove_ds_server_send(s);
651 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq);
653 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s)
655 struct composite_context *c = s->creq;
656 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
657 struct tevent_req *subreq;
659 r->in.bind_handle = &s->drsuapi.bind_handle;
660 r->in.level = 1;
661 r->in.req = talloc(s, union drsuapi_DsRemoveDSServerRequest);
662 r->in.req->req1.server_dn = s->dest_dsa.server_dn_str;
663 r->in.req->req1.domain_dn = s->domain.dn_str;
664 r->in.req->req1.commit = true;
666 r->out.level_out = talloc(s, uint32_t);
667 r->out.res = talloc(s, union drsuapi_DsRemoveDSServerResult);
669 subreq = dcerpc_drsuapi_DsRemoveDSServer_r_send(s, c->event_ctx,
670 s->drsuapi.drsuapi_handle,
672 if (composite_nomem(subreq, c)) return;
673 tevent_req_set_callback(subreq, unbecomeDC_drsuapi_remove_ds_server_recv, s);
676 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq)
678 struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
679 struct libnet_UnbecomeDC_state);
680 struct composite_context *c = s->creq;
681 struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
683 c->status = dcerpc_drsuapi_DsRemoveDSServer_r_recv(subreq, s);
684 TALLOC_FREE(subreq);
685 if (!composite_is_ok(c)) return;
687 if (!W_ERROR_IS_OK(r->out.result)) {
688 composite_error(c, werror_to_ntstatus(r->out.result));
689 return;
692 if (*r->out.level_out != 1) {
693 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
694 return;
697 composite_done(c);
700 struct composite_context *libnet_UnbecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
702 struct composite_context *c;
703 struct libnet_UnbecomeDC_state *s;
704 char *tmp_name;
706 c = composite_create(mem_ctx, ctx->event_ctx);
707 if (c == NULL) return NULL;
709 s = talloc_zero(c, struct libnet_UnbecomeDC_state);
710 if (composite_nomem(s, c)) return c;
711 c->private_data = s;
712 s->creq = c;
713 s->libnet = ctx;
715 /* Domain input */
716 s->domain.dns_name = talloc_strdup(s, r->in.domain_dns_name);
717 if (composite_nomem(s->domain.dns_name, c)) return c;
718 s->domain.netbios_name = talloc_strdup(s, r->in.domain_netbios_name);
719 if (composite_nomem(s->domain.netbios_name, c)) return c;
721 /* Source DSA input */
722 s->source_dsa.address = talloc_strdup(s, r->in.source_dsa_address);
723 if (composite_nomem(s->source_dsa.address, c)) return c;
725 /* Destination DSA input */
726 s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
727 if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
729 /* Destination DSA dns_name construction */
730 tmp_name = strlower_talloc(s, s->dest_dsa.netbios_name);
731 if (composite_nomem(tmp_name, c)) return c;
732 s->dest_dsa.dns_name = talloc_asprintf_append_buffer(tmp_name, ".%s",
733 s->domain.dns_name);
734 if (composite_nomem(s->dest_dsa.dns_name, c)) return c;
736 unbecomeDC_send_cldap(s);
737 return c;
740 NTSTATUS libnet_UnbecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
742 NTSTATUS status;
744 status = composite_wait(c);
746 ZERO_STRUCT(r->out);
748 talloc_free(c);
749 return status;
752 NTSTATUS libnet_UnbecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
754 NTSTATUS status;
755 struct composite_context *c;
756 c = libnet_UnbecomeDC_send(ctx, mem_ctx, r);
757 status = libnet_UnbecomeDC_recv(c, mem_ctx, r);
758 return status;