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 <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"
|
---|
32 |
|
---|
33 | /*****************************************************************************
|
---|
34 | * Windows 2003 (w2k3) does the following steps when changing the server role
|
---|
35 | * from domain controller back to domain member
|
---|
36 | *
|
---|
37 | * We mostly do the same.
|
---|
38 | *****************************************************************************/
|
---|
39 |
|
---|
40 | /*
|
---|
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
|
---|
45 | *
|
---|
46 | * see: unbecomeDC_send_cldap() and unbecomeDC_recv_cldap()
|
---|
47 | */
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Open 1st LDAP connection to the DC using admin credentials
|
---|
51 | *
|
---|
52 | * see: unbecomeDC_ldap_connect()
|
---|
53 | */
|
---|
54 |
|
---|
55 | /*
|
---|
56 | * LDAP search 1st LDAP connection:
|
---|
57 | *
|
---|
58 | * see: unbecomeDC_ldap_rootdse()
|
---|
59 | *
|
---|
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>
|
---|
70 | */
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * LDAP search 1st LDAP connection:
|
---|
74 | *
|
---|
75 | * see: unbecomeDC_ldap_computer_object()
|
---|
76 | *
|
---|
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>
|
---|
87 | */
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * LDAP search 1st LDAP connection:
|
---|
91 | *
|
---|
92 | * see: unbecomeDC_ldap_modify_computer()
|
---|
93 | *
|
---|
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>
|
---|
102 | */
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * LDAP modify 1st LDAP connection:
|
---|
106 | *
|
---|
107 | * see: unbecomeDC_ldap_modify_computer()
|
---|
108 | *
|
---|
109 | * Request (replace):
|
---|
110 | * CN=<new_dc_netbios_name>,CN=Computers,<domain_partition>
|
---|
111 | * userAccoountControl: 4096 <0x1000>
|
---|
112 | * Result:
|
---|
113 | * <success>
|
---|
114 | */
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * LDAP search 1st LDAP connection:
|
---|
118 | *
|
---|
119 | * see: unbecomeDC_ldap_move_computer()
|
---|
120 | *
|
---|
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>
|
---|
128 | */
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * LDAP search 1st LDAP connection:
|
---|
132 | *
|
---|
133 | * not implemented because it doesn't give any new information
|
---|
134 | *
|
---|
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>
|
---|
143 | */
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * LDAP modifyRDN 1st LDAP connection:
|
---|
147 | *
|
---|
148 | * see: unbecomeDC_ldap_move_computer()
|
---|
149 | *
|
---|
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>
|
---|
157 | */
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * LDAP unbind on the 1st LDAP connection
|
---|
161 | *
|
---|
162 | * not implemented, because it's not needed...
|
---|
163 | */
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Open 1st DRSUAPI connection to the DC using admin credentials
|
---|
167 | * DsBind with DRSUAPI_DS_BIND_GUID ("e24d201a-4fd6-11d1-a3da-0000f875ae0d")
|
---|
168 | *
|
---|
169 | * see: unbecomeDC_drsuapi_connect_send(), unbecomeDC_drsuapi_connect_recv(),
|
---|
170 | * unbecomeDC_drsuapi_bind_send() and unbecomeDC_drsuapi_bind_recv()
|
---|
171 | */
|
---|
172 |
|
---|
173 | /*
|
---|
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
|
---|
178 | *
|
---|
179 | * see: unbecomeDC_drsuapi_remove_ds_server_send() and unbecomeDC_drsuapi_remove_ds_server_recv()
|
---|
180 | */
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * DsUnbind on the 1st DRSUAPI connection
|
---|
184 | *
|
---|
185 | * not implemented, because it's not needed...
|
---|
186 | */
|
---|
187 |
|
---|
188 |
|
---|
189 | struct libnet_UnbecomeDC_state {
|
---|
190 | struct composite_context *creq;
|
---|
191 |
|
---|
192 | struct libnet_context *libnet;
|
---|
193 |
|
---|
194 | struct {
|
---|
195 | struct cldap_socket *sock;
|
---|
196 | struct cldap_netlogon io;
|
---|
197 | struct NETLOGON_SAM_LOGON_RESPONSE_EX netlogon;
|
---|
198 | } cldap;
|
---|
199 |
|
---|
200 | struct {
|
---|
201 | struct ldb_context *ldb;
|
---|
202 | } ldap;
|
---|
203 |
|
---|
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;
|
---|
216 |
|
---|
217 | struct {
|
---|
218 | /* input */
|
---|
219 | const char *dns_name;
|
---|
220 | const char *netbios_name;
|
---|
221 |
|
---|
222 | /* constructed */
|
---|
223 | struct GUID guid;
|
---|
224 | const char *dn_str;
|
---|
225 | } domain;
|
---|
226 |
|
---|
227 | struct {
|
---|
228 | /* constructed */
|
---|
229 | const char *config_dn_str;
|
---|
230 | } forest;
|
---|
231 |
|
---|
232 | struct {
|
---|
233 | /* input */
|
---|
234 | const char *address;
|
---|
235 |
|
---|
236 | /* constructed */
|
---|
237 | const char *dns_name;
|
---|
238 | const char *netbios_name;
|
---|
239 | const char *site_name;
|
---|
240 | } source_dsa;
|
---|
241 |
|
---|
242 | struct {
|
---|
243 | /* input */
|
---|
244 | const char *netbios_name;
|
---|
245 |
|
---|
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;
|
---|
253 | };
|
---|
254 |
|
---|
255 | static void unbecomeDC_recv_cldap(struct tevent_req *req);
|
---|
256 |
|
---|
257 | static void unbecomeDC_send_cldap(struct libnet_UnbecomeDC_state *s)
|
---|
258 | {
|
---|
259 | struct composite_context *c = s->creq;
|
---|
260 | struct tevent_req *req;
|
---|
261 | struct tsocket_address *dest_address;
|
---|
262 | int ret;
|
---|
263 |
|
---|
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;
|
---|
274 |
|
---|
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;
|
---|
282 | }
|
---|
283 |
|
---|
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;
|
---|
287 |
|
---|
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);
|
---|
291 | }
|
---|
292 |
|
---|
293 | static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s);
|
---|
294 |
|
---|
295 | static void unbecomeDC_recv_cldap(struct tevent_req *req)
|
---|
296 | {
|
---|
297 | struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(req,
|
---|
298 | struct libnet_UnbecomeDC_state);
|
---|
299 | struct composite_context *c = s->creq;
|
---|
300 |
|
---|
301 | c->status = cldap_netlogon_recv(req, s, &s->cldap.io);
|
---|
302 | talloc_free(req);
|
---|
303 | if (!composite_is_ok(c)) return;
|
---|
304 |
|
---|
305 | s->cldap.netlogon = s->cldap.io.out.netlogon.data.nt5_ex;
|
---|
306 |
|
---|
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;
|
---|
310 |
|
---|
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;
|
---|
314 |
|
---|
315 | s->dest_dsa.site_name = s->cldap.netlogon.client_site;
|
---|
316 |
|
---|
317 | unbecomeDC_connect_ldap(s);
|
---|
318 | }
|
---|
319 |
|
---|
320 | static NTSTATUS unbecomeDC_ldap_connect(struct libnet_UnbecomeDC_state *s)
|
---|
321 | {
|
---|
322 | char *url;
|
---|
323 |
|
---|
324 | url = talloc_asprintf(s, "ldap://%s/", s->source_dsa.dns_name);
|
---|
325 | NT_STATUS_HAVE_NO_MEMORY(url);
|
---|
326 |
|
---|
327 | s->ldap.ldb = ldb_wrap_connect(s, s->libnet->event_ctx, s->libnet->lp_ctx, url,
|
---|
328 | NULL,
|
---|
329 | s->libnet->cred,
|
---|
330 | 0);
|
---|
331 | talloc_free(url);
|
---|
332 | if (s->ldap.ldb == NULL) {
|
---|
333 | return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
|
---|
334 | }
|
---|
335 |
|
---|
336 | return NT_STATUS_OK;
|
---|
337 | }
|
---|
338 |
|
---|
339 | static NTSTATUS unbecomeDC_ldap_rootdse(struct libnet_UnbecomeDC_state *s)
|
---|
340 | {
|
---|
341 | int ret;
|
---|
342 | struct ldb_result *r;
|
---|
343 | struct ldb_dn *basedn;
|
---|
344 | static const char *attrs[] = {
|
---|
345 | "defaultNamingContext",
|
---|
346 | "configurationNamingContext",
|
---|
347 | NULL
|
---|
348 | };
|
---|
349 |
|
---|
350 | basedn = ldb_dn_new(s, s->ldap.ldb, NULL);
|
---|
351 | NT_STATUS_HAVE_NO_MEMORY(basedn);
|
---|
352 |
|
---|
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;
|
---|
361 | }
|
---|
362 |
|
---|
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);
|
---|
366 |
|
---|
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);
|
---|
370 |
|
---|
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);
|
---|
376 |
|
---|
377 | talloc_free(r);
|
---|
378 | return NT_STATUS_OK;
|
---|
379 | }
|
---|
380 |
|
---|
381 | static NTSTATUS unbecomeDC_ldap_computer_object(struct libnet_UnbecomeDC_state *s)
|
---|
382 | {
|
---|
383 | int ret;
|
---|
384 | struct ldb_result *r;
|
---|
385 | struct ldb_dn *basedn;
|
---|
386 | static const char *attrs[] = {
|
---|
387 | "distinguishedName",
|
---|
388 | "userAccountControl",
|
---|
389 | NULL
|
---|
390 | };
|
---|
391 |
|
---|
392 | basedn = ldb_dn_new(s, s->ldap.ldb, s->domain.dn_str);
|
---|
393 | NT_STATUS_HAVE_NO_MEMORY(basedn);
|
---|
394 |
|
---|
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;
|
---|
404 | }
|
---|
405 |
|
---|
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);
|
---|
409 |
|
---|
410 | s->dest_dsa.user_account_control = ldb_msg_find_attr_as_uint(r->msgs[0], "userAccountControl", 0);
|
---|
411 |
|
---|
412 | talloc_free(r);
|
---|
413 | return NT_STATUS_OK;
|
---|
414 | }
|
---|
415 |
|
---|
416 | static NTSTATUS unbecomeDC_ldap_modify_computer(struct libnet_UnbecomeDC_state *s)
|
---|
417 | {
|
---|
418 | int ret;
|
---|
419 | struct ldb_message *msg;
|
---|
420 | uint32_t user_account_control = UF_WORKSTATION_TRUST_ACCOUNT;
|
---|
421 | unsigned int i;
|
---|
422 |
|
---|
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;
|
---|
426 | }
|
---|
427 |
|
---|
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);
|
---|
433 |
|
---|
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;
|
---|
439 | }
|
---|
440 |
|
---|
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;
|
---|
445 | }
|
---|
446 |
|
---|
447 | ret = ldb_modify(s->ldap.ldb, msg);
|
---|
448 | talloc_free(msg);
|
---|
449 | if (ret != LDB_SUCCESS) {
|
---|
450 | return NT_STATUS_LDAP(ret);
|
---|
451 | }
|
---|
452 |
|
---|
453 | s->dest_dsa.user_account_control = user_account_control;
|
---|
454 |
|
---|
455 | return NT_STATUS_OK;
|
---|
456 | }
|
---|
457 |
|
---|
458 | static NTSTATUS unbecomeDC_ldap_move_computer(struct libnet_UnbecomeDC_state *s)
|
---|
459 | {
|
---|
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
|
---|
468 | };
|
---|
469 |
|
---|
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);
|
---|
473 |
|
---|
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;
|
---|
482 | }
|
---|
483 |
|
---|
484 | old_dn = ldb_dn_new(r, s->ldap.ldb, s->dest_dsa.computer_dn_str);
|
---|
485 | NT_STATUS_HAVE_NO_MEMORY(old_dn);
|
---|
486 |
|
---|
487 | new_dn = r->msgs[0]->dn;
|
---|
488 |
|
---|
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;
|
---|
492 | }
|
---|
493 |
|
---|
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;
|
---|
498 | }
|
---|
499 |
|
---|
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);
|
---|
504 | }
|
---|
505 |
|
---|
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);
|
---|
508 |
|
---|
509 | talloc_free(r);
|
---|
510 |
|
---|
511 | return NT_STATUS_OK;
|
---|
512 | }
|
---|
513 |
|
---|
514 | static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s);
|
---|
515 |
|
---|
516 | static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s)
|
---|
517 | {
|
---|
518 | struct composite_context *c = s->creq;
|
---|
519 |
|
---|
520 | c->status = unbecomeDC_ldap_connect(s);
|
---|
521 | if (!composite_is_ok(c)) return;
|
---|
522 |
|
---|
523 | c->status = unbecomeDC_ldap_rootdse(s);
|
---|
524 | if (!composite_is_ok(c)) return;
|
---|
525 |
|
---|
526 | c->status = unbecomeDC_ldap_computer_object(s);
|
---|
527 | if (!composite_is_ok(c)) return;
|
---|
528 |
|
---|
529 | c->status = unbecomeDC_ldap_modify_computer(s);
|
---|
530 | if (!composite_is_ok(c)) return;
|
---|
531 |
|
---|
532 | c->status = unbecomeDC_ldap_move_computer(s);
|
---|
533 | if (!composite_is_ok(c)) return;
|
---|
534 |
|
---|
535 | unbecomeDC_drsuapi_connect_send(s);
|
---|
536 | }
|
---|
537 |
|
---|
538 | static void unbecomeDC_drsuapi_connect_recv(struct composite_context *creq);
|
---|
539 |
|
---|
540 | static void unbecomeDC_drsuapi_connect_send(struct libnet_UnbecomeDC_state *s)
|
---|
541 | {
|
---|
542 | struct composite_context *c = s->creq;
|
---|
543 | struct composite_context *creq;
|
---|
544 | char *binding_str;
|
---|
545 |
|
---|
546 | binding_str = talloc_asprintf(s, "ncacn_ip_tcp:%s[seal]", s->source_dsa.dns_name);
|
---|
547 | if (composite_nomem(binding_str, c)) return;
|
---|
548 |
|
---|
549 | c->status = dcerpc_parse_binding(s, binding_str, &s->drsuapi.binding);
|
---|
550 | talloc_free(binding_str);
|
---|
551 | if (!composite_is_ok(c)) return;
|
---|
552 |
|
---|
553 | creq = dcerpc_pipe_connect_b_send(s, s->drsuapi.binding, &ndr_table_drsuapi,
|
---|
554 | s->libnet->cred, s->libnet->event_ctx,
|
---|
555 | s->libnet->lp_ctx);
|
---|
556 | composite_continue(c, creq, unbecomeDC_drsuapi_connect_recv, s);
|
---|
557 | }
|
---|
558 |
|
---|
559 | static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s);
|
---|
560 |
|
---|
561 | static void unbecomeDC_drsuapi_connect_recv(struct composite_context *req)
|
---|
562 | {
|
---|
563 | struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private_data,
|
---|
564 | struct libnet_UnbecomeDC_state);
|
---|
565 | struct composite_context *c = s->creq;
|
---|
566 |
|
---|
567 | c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi.pipe);
|
---|
568 | if (!composite_is_ok(c)) return;
|
---|
569 |
|
---|
570 | s->drsuapi.drsuapi_handle = s->drsuapi.pipe->binding_handle;
|
---|
571 |
|
---|
572 | unbecomeDC_drsuapi_bind_send(s);
|
---|
573 | }
|
---|
574 |
|
---|
575 | static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq);
|
---|
576 |
|
---|
577 | static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s)
|
---|
578 | {
|
---|
579 | struct composite_context *c = s->creq;
|
---|
580 | struct drsuapi_DsBindInfo28 *bind_info28;
|
---|
581 | struct tevent_req *subreq;
|
---|
582 |
|
---|
583 | GUID_from_string(DRSUAPI_DS_BIND_GUID, &s->drsuapi.bind_guid);
|
---|
584 |
|
---|
585 | bind_info28 = &s->drsuapi.local_info28;
|
---|
586 | bind_info28->supported_extensions = 0;
|
---|
587 | bind_info28->site_guid = GUID_zero();
|
---|
588 | bind_info28->pid = 0;
|
---|
589 | bind_info28->repl_epoch = 0;
|
---|
590 |
|
---|
591 | s->drsuapi.bind_info_ctr.length = 28;
|
---|
592 | s->drsuapi.bind_info_ctr.info.info28 = *bind_info28;
|
---|
593 |
|
---|
594 | s->drsuapi.bind_r.in.bind_guid = &s->drsuapi.bind_guid;
|
---|
595 | s->drsuapi.bind_r.in.bind_info = &s->drsuapi.bind_info_ctr;
|
---|
596 | s->drsuapi.bind_r.out.bind_handle = &s->drsuapi.bind_handle;
|
---|
597 |
|
---|
598 | subreq = dcerpc_drsuapi_DsBind_r_send(s, c->event_ctx,
|
---|
599 | s->drsuapi.drsuapi_handle,
|
---|
600 | &s->drsuapi.bind_r);
|
---|
601 | if (composite_nomem(subreq, c)) return;
|
---|
602 | tevent_req_set_callback(subreq, unbecomeDC_drsuapi_bind_recv, s);
|
---|
603 | }
|
---|
604 |
|
---|
605 | static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s);
|
---|
606 |
|
---|
607 | static void unbecomeDC_drsuapi_bind_recv(struct tevent_req *subreq)
|
---|
608 | {
|
---|
609 | struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
|
---|
610 | struct libnet_UnbecomeDC_state);
|
---|
611 | struct composite_context *c = s->creq;
|
---|
612 |
|
---|
613 | c->status = dcerpc_drsuapi_DsBind_r_recv(subreq, s);
|
---|
614 | TALLOC_FREE(subreq);
|
---|
615 | if (!composite_is_ok(c)) return;
|
---|
616 |
|
---|
617 | if (!W_ERROR_IS_OK(s->drsuapi.bind_r.out.result)) {
|
---|
618 | composite_error(c, werror_to_ntstatus(s->drsuapi.bind_r.out.result));
|
---|
619 | return;
|
---|
620 | }
|
---|
621 |
|
---|
622 | ZERO_STRUCT(s->drsuapi.remote_info28);
|
---|
623 | if (s->drsuapi.bind_r.out.bind_info) {
|
---|
624 | switch (s->drsuapi.bind_r.out.bind_info->length) {
|
---|
625 | case 24: {
|
---|
626 | struct drsuapi_DsBindInfo24 *info24;
|
---|
627 | info24 = &s->drsuapi.bind_r.out.bind_info->info.info24;
|
---|
628 | s->drsuapi.remote_info28.supported_extensions = info24->supported_extensions;
|
---|
629 | s->drsuapi.remote_info28.site_guid = info24->site_guid;
|
---|
630 | s->drsuapi.remote_info28.pid = info24->pid;
|
---|
631 | s->drsuapi.remote_info28.repl_epoch = 0;
|
---|
632 | break;
|
---|
633 | }
|
---|
634 | case 48: {
|
---|
635 | struct drsuapi_DsBindInfo48 *info48;
|
---|
636 | info48 = &s->drsuapi.bind_r.out.bind_info->info.info48;
|
---|
637 | s->drsuapi.remote_info28.supported_extensions = info48->supported_extensions;
|
---|
638 | s->drsuapi.remote_info28.site_guid = info48->site_guid;
|
---|
639 | s->drsuapi.remote_info28.pid = info48->pid;
|
---|
640 | s->drsuapi.remote_info28.repl_epoch = info48->repl_epoch;
|
---|
641 | break;
|
---|
642 | }
|
---|
643 | case 28:
|
---|
644 | s->drsuapi.remote_info28 = s->drsuapi.bind_r.out.bind_info->info.info28;
|
---|
645 | break;
|
---|
646 | }
|
---|
647 | }
|
---|
648 |
|
---|
649 | unbecomeDC_drsuapi_remove_ds_server_send(s);
|
---|
650 | }
|
---|
651 |
|
---|
652 | static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq);
|
---|
653 |
|
---|
654 | static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s)
|
---|
655 | {
|
---|
656 | struct composite_context *c = s->creq;
|
---|
657 | struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
|
---|
658 | struct tevent_req *subreq;
|
---|
659 |
|
---|
660 | r->in.bind_handle = &s->drsuapi.bind_handle;
|
---|
661 | r->in.level = 1;
|
---|
662 | r->in.req = talloc(s, union drsuapi_DsRemoveDSServerRequest);
|
---|
663 | r->in.req->req1.server_dn = s->dest_dsa.server_dn_str;
|
---|
664 | r->in.req->req1.domain_dn = s->domain.dn_str;
|
---|
665 | r->in.req->req1.commit = true;
|
---|
666 |
|
---|
667 | r->out.level_out = talloc(s, uint32_t);
|
---|
668 | r->out.res = talloc(s, union drsuapi_DsRemoveDSServerResult);
|
---|
669 |
|
---|
670 | subreq = dcerpc_drsuapi_DsRemoveDSServer_r_send(s, c->event_ctx,
|
---|
671 | s->drsuapi.drsuapi_handle,
|
---|
672 | r);
|
---|
673 | if (composite_nomem(subreq, c)) return;
|
---|
674 | tevent_req_set_callback(subreq, unbecomeDC_drsuapi_remove_ds_server_recv, s);
|
---|
675 | }
|
---|
676 |
|
---|
677 | static void unbecomeDC_drsuapi_remove_ds_server_recv(struct tevent_req *subreq)
|
---|
678 | {
|
---|
679 | struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(subreq,
|
---|
680 | struct libnet_UnbecomeDC_state);
|
---|
681 | struct composite_context *c = s->creq;
|
---|
682 | struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
|
---|
683 |
|
---|
684 | c->status = dcerpc_drsuapi_DsRemoveDSServer_r_recv(subreq, s);
|
---|
685 | TALLOC_FREE(subreq);
|
---|
686 | if (!composite_is_ok(c)) return;
|
---|
687 |
|
---|
688 | if (!W_ERROR_IS_OK(r->out.result)) {
|
---|
689 | composite_error(c, werror_to_ntstatus(r->out.result));
|
---|
690 | return;
|
---|
691 | }
|
---|
692 |
|
---|
693 | if (*r->out.level_out != 1) {
|
---|
694 | composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
|
---|
695 | return;
|
---|
696 | }
|
---|
697 |
|
---|
698 | composite_done(c);
|
---|
699 | }
|
---|
700 |
|
---|
701 | struct composite_context *libnet_UnbecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
|
---|
702 | {
|
---|
703 | struct composite_context *c;
|
---|
704 | struct libnet_UnbecomeDC_state *s;
|
---|
705 | char *tmp_name;
|
---|
706 |
|
---|
707 | c = composite_create(mem_ctx, ctx->event_ctx);
|
---|
708 | if (c == NULL) return NULL;
|
---|
709 |
|
---|
710 | s = talloc_zero(c, struct libnet_UnbecomeDC_state);
|
---|
711 | if (composite_nomem(s, c)) return c;
|
---|
712 | c->private_data = s;
|
---|
713 | s->creq = c;
|
---|
714 | s->libnet = ctx;
|
---|
715 |
|
---|
716 | /* Domain input */
|
---|
717 | s->domain.dns_name = talloc_strdup(s, r->in.domain_dns_name);
|
---|
718 | if (composite_nomem(s->domain.dns_name, c)) return c;
|
---|
719 | s->domain.netbios_name = talloc_strdup(s, r->in.domain_netbios_name);
|
---|
720 | if (composite_nomem(s->domain.netbios_name, c)) return c;
|
---|
721 |
|
---|
722 | /* Source DSA input */
|
---|
723 | s->source_dsa.address = talloc_strdup(s, r->in.source_dsa_address);
|
---|
724 | if (composite_nomem(s->source_dsa.address, c)) return c;
|
---|
725 |
|
---|
726 | /* Destination DSA input */
|
---|
727 | s->dest_dsa.netbios_name= talloc_strdup(s, r->in.dest_dsa_netbios_name);
|
---|
728 | if (composite_nomem(s->dest_dsa.netbios_name, c)) return c;
|
---|
729 |
|
---|
730 | /* Destination DSA dns_name construction */
|
---|
731 | tmp_name = strlower_talloc(s, s->dest_dsa.netbios_name);
|
---|
732 | if (composite_nomem(tmp_name, c)) return c;
|
---|
733 | s->dest_dsa.dns_name = talloc_asprintf_append_buffer(tmp_name, ".%s",
|
---|
734 | s->domain.dns_name);
|
---|
735 | if (composite_nomem(s->dest_dsa.dns_name, c)) return c;
|
---|
736 |
|
---|
737 | unbecomeDC_send_cldap(s);
|
---|
738 | return c;
|
---|
739 | }
|
---|
740 |
|
---|
741 | NTSTATUS libnet_UnbecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
|
---|
742 | {
|
---|
743 | NTSTATUS status;
|
---|
744 |
|
---|
745 | status = composite_wait(c);
|
---|
746 |
|
---|
747 | ZERO_STRUCT(r->out);
|
---|
748 |
|
---|
749 | talloc_free(c);
|
---|
750 | return status;
|
---|
751 | }
|
---|
752 |
|
---|
753 | NTSTATUS libnet_UnbecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
|
---|
754 | {
|
---|
755 | NTSTATUS status;
|
---|
756 | struct composite_context *c;
|
---|
757 | c = libnet_UnbecomeDC_send(ctx, mem_ctx, r);
|
---|
758 | status = libnet_UnbecomeDC_recv(c, mem_ctx, r);
|
---|
759 | return status;
|
---|
760 | }
|
---|