r25446: Merge some changes I made on the way home from SFO:
[jelmer/samba4-debian.git] / source / libnet / libnet_unbecome_dc.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
5
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.
10
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.
15
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/>.
18 */
19
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/db_wrap.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "dsdb/common/flags.h"
29 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
30 #include "param/param.h"
31
32 /*****************************************************************************
33  * Windows 2003 (w2k3) does the following steps when changing the server role
34  * from domain controller back to domain member
35  *
36  * We mostly do the same.
37  *****************************************************************************/
38
39 /*
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
44  *
45  * see: unbecomeDC_send_cldap() and unbecomeDC_recv_cldap()
46  */
47
48 /*
49  * Open 1st LDAP connection to the DC using admin credentials
50  *
51  * see: unbecomeDC_ldap_connect()
52  */
53
54 /*
55  * LDAP search 1st LDAP connection:
56  *
57  * see: unbecomeDC_ldap_rootdse()
58  *
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>
69  */
70
71 /*
72  * LDAP search 1st LDAP connection:
73  * 
74  * see: unbecomeDC_ldap_computer_object()
75  *
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>
86  */
87
88 /*
89  * LDAP search 1st LDAP connection:
90  * 
91  * see: unbecomeDC_ldap_modify_computer()
92  *
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>
101  */
102
103 /*
104  * LDAP modify 1st LDAP connection:
105  *
106  * see: unbecomeDC_ldap_modify_computer()
107  * 
108  * Request (replace):
109  *      CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
110  *      userAccoountControl:    4096 <0x1000>
111  * Result:
112  *      <success>
113  */
114
115 /*
116  * LDAP search 1st LDAP connection:
117  * 
118  * see: unbecomeDC_ldap_move_computer()
119  *
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>
127  */
128
129 /*
130  * LDAP search 1st LDAP connection:
131  *
132  * not implemented because it doesn't give any new information
133  *
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>
142  */
143
144 /*
145  * LDAP modifyRDN 1st LDAP connection:
146  * 
147  * see: unbecomeDC_ldap_move_computer()
148  *
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>
156  */
157
158 /*
159  * LDAP unbind on the 1st LDAP connection
160  *
161  * not implemented, because it's not needed...
162  */
163
164 /*
165  * Open 1st DRSUAPI connection to the DC using admin credentials
166  * DsBind with DRSUAPI_DS_BIND_GUID ("e24d201a-4fd6-11d1-a3da-0000f875ae0d")
167  *
168  * see: unbecomeDC_drsuapi_connect_send(), unbecomeDC_drsuapi_connect_recv(),
169  *      unbecomeDC_drsuapi_bind_send() and unbecomeDC_drsuapi_bind_recv()
170  */
171
172 /*
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
177  *
178  * see: unbecomeDC_drsuapi_remove_ds_server_send() and unbecomeDC_drsuapi_remove_ds_server_recv()
179  */
180
181 /*
182  * DsUnbind on the 1st DRSUAPI connection
183  *
184  * not implemented, because it's not needed...
185  */
186
187
188 struct libnet_UnbecomeDC_state {
189         struct composite_context *creq;
190
191         struct libnet_context *libnet;
192
193         struct {
194                 struct cldap_socket *sock;
195                 struct cldap_netlogon io;
196                 struct nbt_cldap_netlogon_5 netlogon5;
197         } cldap;
198
199         struct {
200                 struct ldb_context *ldb;
201         } ldap;
202
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;
214
215         struct {
216                 /* input */
217                 const char *dns_name;
218                 const char *netbios_name;
219
220                 /* constructed */
221                 struct GUID guid;
222                 const char *dn_str;
223         } domain;
224
225         struct {
226                 /* constructed */
227                 const char *config_dn_str;
228         } forest;
229
230         struct {
231                 /* input */
232                 const char *address;
233
234                 /* constructed */
235                 const char *dns_name;
236                 const char *netbios_name;
237                 const char *site_name;
238         } source_dsa;
239
240         struct {
241                 /* input */
242                 const char *netbios_name;
243
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;
251 };
252
253 static void unbecomeDC_recv_cldap(struct cldap_request *req);
254
255 static void unbecomeDC_send_cldap(struct libnet_UnbecomeDC_state *s)
256 {
257         struct composite_context *c = s->creq;
258         struct cldap_request *req;
259
260         s->cldap.io.in.dest_address     = s->source_dsa.address;
261         s->cldap.io.in.realm            = s->domain.dns_name;
262         s->cldap.io.in.host             = s->dest_dsa.netbios_name;
263         s->cldap.io.in.user             = NULL;
264         s->cldap.io.in.domain_guid      = NULL;
265         s->cldap.io.in.domain_sid       = NULL;
266         s->cldap.io.in.acct_control     = -1;
267         s->cldap.io.in.version          = 6;
268
269         s->cldap.sock = cldap_socket_init(s, s->libnet->event_ctx);
270         if (composite_nomem(s->cldap.sock, c)) return;
271
272         req = cldap_netlogon_send(s->cldap.sock, &s->cldap.io);
273         if (composite_nomem(req, c)) return;
274         req->async.fn           = unbecomeDC_recv_cldap;
275         req->async.private      = s;
276 }
277
278 static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s);
279
280 static void unbecomeDC_recv_cldap(struct cldap_request *req)
281 {
282         struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private,
283                                             struct libnet_UnbecomeDC_state);
284         struct composite_context *c = s->creq;
285
286         c->status = cldap_netlogon_recv(req, s, &s->cldap.io);
287         if (!composite_is_ok(c)) return;
288
289         s->cldap.netlogon5 = s->cldap.io.out.netlogon.logon5;
290
291         s->domain.dns_name              = s->cldap.netlogon5.dns_domain;
292         s->domain.netbios_name          = s->cldap.netlogon5.domain;
293         s->domain.guid                  = s->cldap.netlogon5.domain_uuid;
294
295         s->source_dsa.dns_name          = s->cldap.netlogon5.pdc_dns_name;
296         s->source_dsa.netbios_name      = s->cldap.netlogon5.pdc_name;
297         s->source_dsa.site_name         = s->cldap.netlogon5.server_site;
298
299         s->dest_dsa.site_name           = s->cldap.netlogon5.client_site;
300
301         unbecomeDC_connect_ldap(s);
302 }
303
304 static NTSTATUS unbecomeDC_ldap_connect(struct libnet_UnbecomeDC_state *s)
305 {
306         char *url;
307
308         url = talloc_asprintf(s, "ldap://%s/", s->source_dsa.dns_name);
309         NT_STATUS_HAVE_NO_MEMORY(url);
310
311         s->ldap.ldb = ldb_wrap_connect(s, global_loadparm, url,
312                                        NULL,
313                                        s->libnet->cred,
314                                        0, NULL);
315         talloc_free(url);
316         if (s->ldap.ldb == NULL) {
317                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
318         }
319
320         return NT_STATUS_OK;
321 }
322
323 static NTSTATUS unbecomeDC_ldap_rootdse(struct libnet_UnbecomeDC_state *s)
324 {
325         int ret;
326         struct ldb_result *r;
327         struct ldb_dn *basedn;
328         static const char *attrs[] = {
329                 "defaultNamingContext",
330                 "configurationNamingContext",
331                 NULL
332         };
333
334         basedn = ldb_dn_new(s, s->ldap.ldb, NULL);
335         NT_STATUS_HAVE_NO_MEMORY(basedn);
336
337         ret = ldb_search(s->ldap.ldb, basedn, LDB_SCOPE_BASE, 
338                          "(objectClass=*)", attrs, &r);
339         talloc_free(basedn);
340         if (ret != LDB_SUCCESS) {
341                 return NT_STATUS_LDAP(ret);
342         } else if (r->count != 1) {
343                 talloc_free(r);
344                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
345         }
346         talloc_steal(s, r);
347
348         s->domain.dn_str        = ldb_msg_find_attr_as_string(r->msgs[0], "defaultNamingContext", NULL);
349         if (!s->domain.dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
350         talloc_steal(s, s->domain.dn_str);
351
352         s->forest.config_dn_str = ldb_msg_find_attr_as_string(r->msgs[0], "configurationNamingContext", NULL);
353         if (!s->forest.config_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;
354         talloc_steal(s, s->forest.config_dn_str);
355
356         s->dest_dsa.server_dn_str = talloc_asprintf(s, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",
357                                                     s->dest_dsa.netbios_name,
358                                                     s->dest_dsa.site_name,
359                                                     s->forest.config_dn_str);
360         NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.server_dn_str);
361
362         talloc_free(r);
363         return NT_STATUS_OK;
364 }
365
366 static NTSTATUS unbecomeDC_ldap_computer_object(struct libnet_UnbecomeDC_state *s)
367 {
368         int ret;
369         struct ldb_result *r;
370         struct ldb_dn *basedn;
371         char *filter;
372         static const char *attrs[] = {
373                 "distinguishedName",
374                 "userAccountControl",
375                 NULL
376         };
377
378         basedn = ldb_dn_new(s, s->ldap.ldb, s->domain.dn_str);
379         NT_STATUS_HAVE_NO_MEMORY(basedn);
380
381         filter = talloc_asprintf(basedn, "(&(|(objectClass=user)(objectClass=computer))(sAMAccountName=%s$))",
382                                  s->dest_dsa.netbios_name);
383         NT_STATUS_HAVE_NO_MEMORY(filter);
384
385         ret = ldb_search(s->ldap.ldb, basedn, LDB_SCOPE_SUBTREE, 
386                          filter, attrs, &r);
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;
393         }
394
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);
398
399         s->dest_dsa.user_account_control = samdb_result_uint(r->msgs[0], "userAccountControl", 0);
400
401         talloc_free(r);
402         return NT_STATUS_OK;
403 }
404
405 static NTSTATUS unbecomeDC_ldap_modify_computer(struct libnet_UnbecomeDC_state *s)
406 {
407         int ret;
408         struct ldb_message *msg;
409         uint32_t user_account_control = UF_WORKSTATION_TRUST_ACCOUNT;
410         uint32_t i;
411
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;
415         }
416
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);
422
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;
427         }
428
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;
433         }
434
435         ret = ldb_modify(s->ldap.ldb, msg);
436         talloc_free(msg);
437         if (ret != LDB_SUCCESS) {
438                 return NT_STATUS_LDAP(ret);
439         }
440
441         s->dest_dsa.user_account_control = user_account_control;
442
443         return NT_STATUS_OK;
444 }
445
446 static NTSTATUS unbecomeDC_ldap_move_computer(struct libnet_UnbecomeDC_state *s)
447 {
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
456         };
457
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);
461
462         ret = ldb_search(s->ldap.ldb, basedn, LDB_SCOPE_BASE,
463                          "(objectClass=*)", _1_1_attrs, &r);
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;
470         }
471
472         old_dn = ldb_dn_new(r, s->ldap.ldb, s->dest_dsa.computer_dn_str);
473         NT_STATUS_HAVE_NO_MEMORY(old_dn);
474
475         new_dn = r->msgs[0]->dn;
476
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;
480         }
481
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;
486         }
487
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);
492         }
493
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);
496
497         talloc_free(r);
498
499         return NT_STATUS_OK;
500 }
501
502 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s);
503
504 static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s)
505 {
506         struct composite_context *c = s->creq;
507
508         c->status = unbecomeDC_ldap_connect(s);
509         if (!composite_is_ok(c)) return;
510
511         c->status = unbecomeDC_ldap_rootdse(s);
512         if (!composite_is_ok(c)) return;
513
514         c->status = unbecomeDC_ldap_computer_object(s);
515         if (!composite_is_ok(c)) return;
516
517         c->status = unbecomeDC_ldap_modify_computer(s);
518         if (!composite_is_ok(c)) return;
519
520         c->status = unbecomeDC_ldap_move_computer(s);
521         if (!composite_is_ok(c)) return;
522
523         unbecomeDC_drsuapi_connect_send(s);
524 }
525
526 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *creq);
527
528 static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s)
529 {
530         struct composite_context *c = s->creq;
531         struct composite_context *creq;
532         char *binding_str;
533
534         binding_str = talloc_asprintf(s, "ncacn_ip_tcp:%s[seal]", s->source_dsa.dns_name);
535         if (composite_nomem(binding_str, c)) return;
536
537         c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi.binding);
538         talloc_free(binding_str);
539         if (!composite_is_ok(c)) return;
540
541         creq = dcerpc_pipe_connect_b_send(s, s->drsuapi.binding, &ndr_table_drsuapi,
542                                           s->libnet->cred, s->libnet->event_ctx);
543         composite_continue(c, creq, unbecomeDC_drsuapi_connect_recv, s);
544 }
545
546 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s);
547
548 static void unbecomeDC_drsuapi_connect_recv(struct composite_context *req)
549 {
550         struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
551                                             struct libnet_UnbecomeDC_state);
552         struct composite_context *c = s->creq;
553
554         c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi.pipe);
555         if (!composite_is_ok(c)) return;
556
557         unbecomeDC_drsuapi_bind_send(s);
558 }
559
560 static void unbecomeDC_drsuapi_bind_recv(struct rpc_request *req);
561
562 static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s)
563 {
564         struct composite_context *c = s->creq;
565         struct rpc_request *req;
566         struct drsuapi_DsBindInfo28 *bind_info28;
567
568         GUID_from_string(DRSUAPI_DS_BIND_GUID, &s->drsuapi.bind_guid);
569
570         bind_info28                             = &s->drsuapi.local_info28;
571         bind_info28->supported_extensions       = 0;
572         bind_info28->site_guid                  = GUID_zero();
573         bind_info28->u1                         = 508;
574         bind_info28->repl_epoch                 = 0;
575
576         s->drsuapi.bind_info_ctr.length         = 28;
577         s->drsuapi.bind_info_ctr.info.info28    = *bind_info28;
578
579         s->drsuapi.bind_r.in.bind_guid = &s->drsuapi.bind_guid;
580         s->drsuapi.bind_r.in.bind_info = &s->drsuapi.bind_info_ctr;
581         s->drsuapi.bind_r.out.bind_handle = &s->drsuapi.bind_handle;
582
583         req = dcerpc_drsuapi_DsBind_send(s->drsuapi.pipe, s, &s->drsuapi.bind_r);
584         composite_continue_rpc(c, req, unbecomeDC_drsuapi_bind_recv, s);
585 }
586
587 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s);
588
589 static void unbecomeDC_drsuapi_bind_recv(struct rpc_request *req)
590 {
591         struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
592                                             struct libnet_UnbecomeDC_state);
593         struct composite_context *c = s->creq;
594
595         c->status = dcerpc_ndr_request_recv(req);
596         if (!composite_is_ok(c)) return;
597
598         if (!W_ERROR_IS_OK(s->drsuapi.bind_r.out.result)) {
599                 composite_error(c, werror_to_ntstatus(s->drsuapi.bind_r.out.result));
600                 return;
601         }
602
603         ZERO_STRUCT(s->drsuapi.remote_info28);
604         if (s->drsuapi.bind_r.out.bind_info) {
605                 switch (s->drsuapi.bind_r.out.bind_info->length) {
606                 case 24: {
607                         struct drsuapi_DsBindInfo24 *info24;
608                         info24 = &s->drsuapi.bind_r.out.bind_info->info.info24;
609                         s->drsuapi.remote_info28.supported_extensions   = info24->supported_extensions;
610                         s->drsuapi.remote_info28.site_guid              = info24->site_guid;
611                         s->drsuapi.remote_info28.u1                     = info24->u1;
612                         s->drsuapi.remote_info28.repl_epoch             = 0;
613                         break;
614                 }
615                 case 28:
616                         s->drsuapi.remote_info28 = s->drsuapi.bind_r.out.bind_info->info.info28;
617                         break;
618                 }
619         }
620
621         unbecomeDC_drsuapi_remove_ds_server_send(s);
622 }
623
624 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct rpc_request *req);
625
626 static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s)
627 {
628         struct composite_context *c = s->creq;
629         struct rpc_request *req;
630         struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
631
632         r->in.bind_handle       = &s->drsuapi.bind_handle;
633         r->in.level             = 1;
634         r->in.req.req1.server_dn= s->dest_dsa.server_dn_str;
635         r->in.req.req1.domain_dn= s->domain.dn_str;
636         r->in.req.req1.unknown  = 0x00000001;
637
638         req = dcerpc_drsuapi_DsRemoveDSServer_send(s->drsuapi.pipe, s, r);
639         composite_continue_rpc(c, req, unbecomeDC_drsuapi_remove_ds_server_recv, s);
640 }
641
642 static void unbecomeDC_drsuapi_remove_ds_server_recv(struct rpc_request *req)
643 {
644         struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
645                                             struct libnet_UnbecomeDC_state);
646         struct composite_context *c = s->creq;
647         struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
648
649         c->status = dcerpc_ndr_request_recv(req);
650         if (!composite_is_ok(c)) return;
651
652         if (!W_ERROR_IS_OK(r->out.result)) {
653                 composite_error(c, werror_to_ntstatus(r->out.result));
654                 return;
655         }
656
657         if (r->out.level != 1) {
658                 composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
659                 return;
660         }
661                 
662         if (!W_ERROR_IS_OK(r->out.res.res1.status)) {
663                 composite_error(c, werror_to_ntstatus(r->out.res.res1.status));
664                 return;
665         }
666
667         composite_done(c);
668 }
669
670 struct composite_context *libnet_UnbecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
671 {
672         struct composite_context *c;
673         struct libnet_UnbecomeDC_state *s;
674         char *tmp_name;
675
676         c = composite_create(mem_ctx, ctx->event_ctx);
677         if (c == NULL) return NULL;
678
679         s = talloc_zero(c, struct libnet_UnbecomeDC_state);
680         if (composite_nomem(s, c)) return c;
681         c->private_data = s;
682         s->creq         = c;
683         s->libnet       = ctx;
684
685         /* Domain input */
686         s->domain.dns_name      = talloc_strdup(s, r->in.domain_dns_name);
687         if (composite_nomem(s->domain.dns_name, c)) return c;
688         s->domain.netbios_name  = talloc_strdup(s, r->in.domain_netbios_name);
689         if (composite_nomem(s->domain.netbios_name, c)) return c;
690
691         /* Source DSA input */
692         s->source_dsa.address   = talloc_strdup(s, r->in.source_dsa_address);
693         if (composite_nomem(s->source_dsa.address, c)) return c;
694
695         /* Destination DSA input */
696         s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
697         if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
698
699         /* Destination DSA dns_name construction */
700         tmp_name                = strlower_talloc(s, s->dest_dsa.netbios_name);
701         if (composite_nomem(tmp_name, c)) return c;
702         s->dest_dsa.dns_name    = talloc_asprintf_append_buffer(tmp_name, ".%s",
703                                                          s->domain.dns_name);
704         if (composite_nomem(s->dest_dsa.dns_name, c)) return c;
705
706         unbecomeDC_send_cldap(s);
707         return c;
708 }
709
710 NTSTATUS libnet_UnbecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
711 {
712         NTSTATUS status;
713
714         status = composite_wait(c);
715
716         ZERO_STRUCT(r->out);
717
718         talloc_free(c);
719         return status;
720 }
721
722 NTSTATUS libnet_UnbecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
723 {
724         NTSTATUS status;
725         struct composite_context *c;
726         c = libnet_UnbecomeDC_send(ctx, mem_ctx, r);
727         status = libnet_UnbecomeDC_recv(c, mem_ctx, r);
728         return status;
729 }