| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 |
|
|---|
| 4 | Copyright (C) Stefan Metzmacher <[email protected]> 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/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 |
|
|---|
| 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 NETLOGON_SAM_LOGON_RESPONSE_EX netlogon;
|
|---|
| 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 tevent_req *req);
|
|---|
| 254 |
|
|---|
| 255 | static void unbecomeDC_send_cldap(struct libnet_UnbecomeDC_state *s)
|
|---|
| 256 | {
|
|---|
| 257 | struct composite_context *c = s->creq;
|
|---|
| 258 | struct tevent_req *req;
|
|---|
| 259 |
|
|---|
| 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;
|
|---|
| 270 |
|
|---|
| 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;
|
|---|
| 274 |
|
|---|
| 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);
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s);
|
|---|
| 281 |
|
|---|
| 282 | static void unbecomeDC_recv_cldap(struct tevent_req *req)
|
|---|
| 283 | {
|
|---|
| 284 | struct libnet_UnbecomeDC_state *s = tevent_req_callback_data(req,
|
|---|
| 285 | struct libnet_UnbecomeDC_state);
|
|---|
| 286 | struct composite_context *c = s->creq;
|
|---|
| 287 |
|
|---|
| 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;
|
|---|
| 293 |
|
|---|
| 294 | s->cldap.netlogon = s->cldap.io.out.netlogon.data.nt5_ex;
|
|---|
| 295 |
|
|---|
| 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;
|
|---|
| 299 |
|
|---|
| 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;
|
|---|
| 303 |
|
|---|
| 304 | s->dest_dsa.site_name = s->cldap.netlogon.client_site;
|
|---|
| 305 |
|
|---|
| 306 | unbecomeDC_connect_ldap(s);
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | static NTSTATUS unbecomeDC_ldap_connect(struct libnet_UnbecomeDC_state *s)
|
|---|
| 310 | {
|
|---|
| 311 | char *url;
|
|---|
| 312 |
|
|---|
| 313 | url = talloc_asprintf(s, "ldap://%s/", s->source_dsa.dns_name);
|
|---|
| 314 | NT_STATUS_HAVE_NO_MEMORY(url);
|
|---|
| 315 |
|
|---|
| 316 | s->ldap.ldb = ldb_wrap_connect(s, s->libnet->event_ctx, s->libnet->lp_ctx, url,
|
|---|
| 317 | NULL,
|
|---|
| 318 | s->libnet->cred,
|
|---|
| 319 | 0, NULL);
|
|---|
| 320 | talloc_free(url);
|
|---|
| 321 | if (s->ldap.ldb == NULL) {
|
|---|
| 322 | return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | return NT_STATUS_OK;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | static NTSTATUS unbecomeDC_ldap_rootdse(struct libnet_UnbecomeDC_state *s)
|
|---|
| 329 | {
|
|---|
| 330 | int ret;
|
|---|
| 331 | struct ldb_result *r;
|
|---|
| 332 | struct ldb_dn *basedn;
|
|---|
| 333 | static const char *attrs[] = {
|
|---|
| 334 | "defaultNamingContext",
|
|---|
| 335 | "configurationNamingContext",
|
|---|
| 336 | NULL
|
|---|
| 337 | };
|
|---|
| 338 |
|
|---|
| 339 | basedn = ldb_dn_new(s, s->ldap.ldb, NULL);
|
|---|
| 340 | NT_STATUS_HAVE_NO_MEMORY(basedn);
|
|---|
| 341 |
|
|---|
| 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;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 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);
|
|---|
| 355 |
|
|---|
| 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);
|
|---|
| 359 |
|
|---|
| 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);
|
|---|
| 365 |
|
|---|
| 366 | talloc_free(r);
|
|---|
| 367 | return NT_STATUS_OK;
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | static NTSTATUS unbecomeDC_ldap_computer_object(struct libnet_UnbecomeDC_state *s)
|
|---|
| 371 | {
|
|---|
| 372 | int ret;
|
|---|
| 373 | struct ldb_result *r;
|
|---|
| 374 | struct ldb_dn *basedn;
|
|---|
| 375 | static const char *attrs[] = {
|
|---|
| 376 | "distinguishedName",
|
|---|
| 377 | "userAccountControl",
|
|---|
| 378 | NULL
|
|---|
| 379 | };
|
|---|
| 380 |
|
|---|
| 381 | basedn = ldb_dn_new(s, s->ldap.ldb, s->domain.dn_str);
|
|---|
| 382 | NT_STATUS_HAVE_NO_MEMORY(basedn);
|
|---|
| 383 |
|
|---|
| 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;
|
|---|
| 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, 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;
|
|---|
| 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 | s->libnet->lp_ctx);
|
|---|
| 544 | composite_continue(c, creq, unbecomeDC_drsuapi_connect_recv, s);
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s);
|
|---|
| 548 |
|
|---|
| 549 | static void unbecomeDC_drsuapi_connect_recv(struct composite_context *req)
|
|---|
| 550 | {
|
|---|
| 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;
|
|---|
| 554 |
|
|---|
| 555 | c->status = dcerpc_pipe_connect_b_recv(req, s, &s->drsuapi.pipe);
|
|---|
| 556 | if (!composite_is_ok(c)) return;
|
|---|
| 557 |
|
|---|
| 558 | unbecomeDC_drsuapi_bind_send(s);
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | static void unbecomeDC_drsuapi_bind_recv(struct rpc_request *req);
|
|---|
| 562 |
|
|---|
| 563 | static void unbecomeDC_drsuapi_bind_send(struct libnet_UnbecomeDC_state *s)
|
|---|
| 564 | {
|
|---|
| 565 | struct composite_context *c = s->creq;
|
|---|
| 566 | struct rpc_request *req;
|
|---|
| 567 | struct drsuapi_DsBindInfo28 *bind_info28;
|
|---|
| 568 |
|
|---|
| 569 | GUID_from_string(DRSUAPI_DS_BIND_GUID, &s->drsuapi.bind_guid);
|
|---|
| 570 |
|
|---|
| 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;
|
|---|
| 576 |
|
|---|
| 577 | s->drsuapi.bind_info_ctr.length = 28;
|
|---|
| 578 | s->drsuapi.bind_info_ctr.info.info28 = *bind_info28;
|
|---|
| 579 |
|
|---|
| 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;
|
|---|
| 583 |
|
|---|
| 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);
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s);
|
|---|
| 589 |
|
|---|
| 590 | static void unbecomeDC_drsuapi_bind_recv(struct rpc_request *req)
|
|---|
| 591 | {
|
|---|
| 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;
|
|---|
| 595 |
|
|---|
| 596 | c->status = dcerpc_ndr_request_recv(req);
|
|---|
| 597 | if (!composite_is_ok(c)) return;
|
|---|
| 598 |
|
|---|
| 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;
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 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;
|
|---|
| 615 | }
|
|---|
| 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;
|
|---|
| 624 | }
|
|---|
| 625 | case 28:
|
|---|
| 626 | s->drsuapi.remote_info28 = s->drsuapi.bind_r.out.bind_info->info.info28;
|
|---|
| 627 | break;
|
|---|
| 628 | }
|
|---|
| 629 | }
|
|---|
| 630 |
|
|---|
| 631 | unbecomeDC_drsuapi_remove_ds_server_send(s);
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | static void unbecomeDC_drsuapi_remove_ds_server_recv(struct rpc_request *req);
|
|---|
| 635 |
|
|---|
| 636 | static void unbecomeDC_drsuapi_remove_ds_server_send(struct libnet_UnbecomeDC_state *s)
|
|---|
| 637 | {
|
|---|
| 638 | struct composite_context *c = s->creq;
|
|---|
| 639 | struct rpc_request *req;
|
|---|
| 640 | struct drsuapi_DsRemoveDSServer *r = &s->drsuapi.rm_ds_srv_r;
|
|---|
| 641 |
|
|---|
| 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;
|
|---|
| 648 |
|
|---|
| 649 | r->out.level_out = talloc(s, int32_t);
|
|---|
| 650 | r->out.res = talloc(s, union drsuapi_DsRemoveDSServerResult);
|
|---|
| 651 |
|
|---|
| 652 | req = dcerpc_drsuapi_DsRemoveDSServer_send(s->drsuapi.pipe, s, r);
|
|---|
| 653 | composite_continue_rpc(c, req, unbecomeDC_drsuapi_remove_ds_server_recv, s);
|
|---|
| 654 | }
|
|---|
| 655 |
|
|---|
| 656 | static void unbecomeDC_drsuapi_remove_ds_server_recv(struct rpc_request *req)
|
|---|
| 657 | {
|
|---|
| 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;
|
|---|
| 662 |
|
|---|
| 663 | c->status = dcerpc_ndr_request_recv(req);
|
|---|
| 664 | if (!composite_is_ok(c)) return;
|
|---|
| 665 |
|
|---|
| 666 | if (!W_ERROR_IS_OK(r->out.result)) {
|
|---|
| 667 | composite_error(c, werror_to_ntstatus(r->out.result));
|
|---|
| 668 | return;
|
|---|
| 669 | }
|
|---|
| 670 |
|
|---|
| 671 | if (*r->out.level_out != 1) {
|
|---|
| 672 | composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
|
|---|
| 673 | return;
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | composite_done(c);
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | struct composite_context *libnet_UnbecomeDC_send(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
|
|---|
| 680 | {
|
|---|
| 681 | struct composite_context *c;
|
|---|
| 682 | struct libnet_UnbecomeDC_state *s;
|
|---|
| 683 | char *tmp_name;
|
|---|
| 684 |
|
|---|
| 685 | c = composite_create(mem_ctx, ctx->event_ctx);
|
|---|
| 686 | if (c == NULL) return NULL;
|
|---|
| 687 |
|
|---|
| 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;
|
|---|
| 693 |
|
|---|
| 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;
|
|---|
| 699 |
|
|---|
| 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;
|
|---|
| 703 |
|
|---|
| 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;
|
|---|
| 707 |
|
|---|
| 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;
|
|---|
| 714 |
|
|---|
| 715 | unbecomeDC_send_cldap(s);
|
|---|
| 716 | return c;
|
|---|
| 717 | }
|
|---|
| 718 |
|
|---|
| 719 | NTSTATUS libnet_UnbecomeDC_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
|
|---|
| 720 | {
|
|---|
| 721 | NTSTATUS status;
|
|---|
| 722 |
|
|---|
| 723 | status = composite_wait(c);
|
|---|
| 724 |
|
|---|
| 725 | ZERO_STRUCT(r->out);
|
|---|
| 726 |
|
|---|
| 727 | talloc_free(c);
|
|---|
| 728 | return status;
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | NTSTATUS libnet_UnbecomeDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_UnbecomeDC *r)
|
|---|
| 732 | {
|
|---|
| 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;
|
|---|
| 738 | }
|
|---|