libnet_unbecome_dc.c

来自「samba最新软件」· C语言 代码 · 共 734 行 · 第 1/2 页

C
734
字号
/*   Unix SMB/CIFS implementation.   Copyright (C) Stefan Metzmacher <metze@samba.org> 2006   This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 3 of the License, or   (at your option) any later version.   This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with this program.  If not, see <http://www.gnu.org/licenses/>.*/#include "includes.h"#include "libnet/libnet.h"#include "libcli/composite/composite.h"#include "libcli/cldap/cldap.h"#include "lib/ldb/include/ldb.h"#include "lib/ldb/include/ldb_errors.h"#include "lib/ldb_wrap.h"#include "dsdb/samdb/samdb.h"#include "dsdb/common/flags.h"#include "librpc/gen_ndr/ndr_drsuapi_c.h"#include "param/param.h"/***************************************************************************** * Windows 2003 (w2k3) does the following steps when changing the server role * from domain controller back to domain member * * We mostly do the same. *****************************************************************************//* * lookup DC: * - using nbt name<1C> request and a samlogon mailslot request * or * - using a DNS SRV _ldap._tcp.dc._msdcs. request and a CLDAP netlogon request * * see: unbecomeDC_send_cldap() and unbecomeDC_recv_cldap() *//* * Open 1st LDAP connection to the DC using admin credentials * * see: unbecomeDC_ldap_connect() *//* * LDAP search 1st LDAP connection: * * see: unbecomeDC_ldap_rootdse() * * Request: *	basedn:	"" *	scope:	base *	filter:	(objectClass=*) *	attrs:	defaultNamingContext *		configurationNamingContext * Result: *      "" *		defaultNamingContext:	<domain_partition> *		configurationNamingContext:CN=Configuration,<domain_partition> *//* * LDAP search 1st LDAP connection: *  * see: unbecomeDC_ldap_computer_object() * * Request: *	basedn:	<domain_partition> *	scope:	sub *	filter:	(&(|(objectClass=user)(objectClass=computer))(sAMAccountName=<new_dc_account_name>)) *	attrs:	distinguishedName *		userAccountControl * Result: *      CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition> *		distinguishedName:	CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition> *		userAccoountControl:	532480 <0x82000> *//* * LDAP search 1st LDAP connection: *  * see: unbecomeDC_ldap_modify_computer() * * Request: *	basedn:	CN=<new_dc_netbios_name>,CN=Computers,<domain_partition> *	scope:	base *	filter:	(objectClass=*) *	attrs:	userAccountControl * Result: *      CN=<new_dc_netbios_name>,CN=Computers,<domain_partition> *		userAccoountControl:	532480 <0x82000> *//* * LDAP modify 1st LDAP connection: * * see: unbecomeDC_ldap_modify_computer() *  * Request (replace): *	CN=<new_dc_netbios_name>,CN=Computers,<domain_partition> *	userAccoountControl:	4096 <0x1000> * Result: *	<success> *//* * LDAP search 1st LDAP connection: *  * see: unbecomeDC_ldap_move_computer() * * Request: *	basedn:	<WKGUID=aa312825768811d1aded00c04fd8d5cd,<domain_partition>> *	scope:	base *	filter:	(objectClass=*) *	attrs:	1.1 * Result: *	CN=Computers,<domain_partition> *//* * LDAP search 1st LDAP connection: * * not implemented because it doesn't give any new information * * Request: *	basedn:	CN=Computers,<domain_partition> *	scope:	base *	filter:	(objectClass=*) *	attrs:	distinguishedName * Result: *	CN=Computers,<domain_partition> *		distinguishedName:	CN=Computers,<domain_partition> *//* * LDAP modifyRDN 1st LDAP connection: *  * see: unbecomeDC_ldap_move_computer() * * Request: *      entry:		CN=<new_dc_netbios_name>,CN=Domain Controllers,<domain_partition> *	newrdn:		CN=<new_dc_netbios_name> *	deleteoldrdn:	TRUE *	newparent:	CN=Computers,<domain_partition> * Result: *	<success> *//* * LDAP unbind on the 1st LDAP connection * * not implemented, because it's not needed... *//* * Open 1st DRSUAPI connection to the DC using admin credentials * DsBind with DRSUAPI_DS_BIND_GUID ("e24d201a-4fd6-11d1-a3da-0000f875ae0d") * * see:	unbecomeDC_drsuapi_connect_send(), unbecomeDC_drsuapi_connect_recv(), *	unbecomeDC_drsuapi_bind_send() and unbecomeDC_drsuapi_bind_recv() *//* * DsRemoveDsServer to remove the  * CN=<machine_name>,CN=Servers,CN=<site_name>,CN=Configuration,<domain_partition> * and CN=NTDS Settings,CN=<machine_name>,CN=Servers,CN=<site_name>,CN=Configuration,<domain_partition> * on the 1st DRSUAPI connection * * see: unbecomeDC_drsuapi_remove_ds_server_send() and unbecomeDC_drsuapi_remove_ds_server_recv() *//* * DsUnbind on the 1st DRSUAPI connection * * not implemented, because it's not needed... */struct libnet_UnbecomeDC_state {	struct composite_context *creq;	struct libnet_context *libnet;	struct {		struct cldap_socket *sock;		struct cldap_netlogon io;		struct NETLOGON_SAM_LOGON_RESPONSE_EX netlogon;	} cldap;	struct {		struct ldb_context *ldb;	} ldap;	struct {		struct dcerpc_binding *binding;		struct dcerpc_pipe *pipe;		struct drsuapi_DsBind bind_r;		struct GUID bind_guid;		struct drsuapi_DsBindInfoCtr bind_info_ctr;		struct drsuapi_DsBindInfo28 local_info28;		struct drsuapi_DsBindInfo28 remote_info28;		struct policy_handle bind_handle;		struct drsuapi_DsRemoveDSServer rm_ds_srv_r;	} drsuapi;	struct {		/* input */		const char *dns_name;		const char *netbios_name;		/* constructed */		struct GUID guid;		const char *dn_str;	} domain;	struct {		/* constructed */		const char *config_dn_str;	} forest;	struct {		/* input */		const char *address;		/* constructed */		const char *dns_name;		const char *netbios_name;		const char *site_name;	} source_dsa;	struct {		/* input */		const char *netbios_name;		/* constructed */		const char *dns_name;		const char *site_name;		const char *computer_dn_str;		const char *server_dn_str;		uint32_t user_account_control;	} dest_dsa;};static void unbecomeDC_recv_cldap(struct cldap_request *req);static void unbecomeDC_send_cldap(struct libnet_UnbecomeDC_state *s){	struct composite_context *c = s->creq;	struct cldap_request *req;	s->cldap.io.in.dest_address	= s->source_dsa.address;	s->cldap.io.in.dest_port	= lp_cldap_port(s->libnet->lp_ctx);	s->cldap.io.in.realm		= s->domain.dns_name;	s->cldap.io.in.host		= s->dest_dsa.netbios_name;	s->cldap.io.in.user		= NULL;	s->cldap.io.in.domain_guid	= NULL;	s->cldap.io.in.domain_sid	= NULL;	s->cldap.io.in.acct_control	= -1;	s->cldap.io.in.version		= NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;	s->cldap.io.in.map_response	= true;	s->cldap.sock = cldap_socket_init(s, s->libnet->event_ctx,					  lp_iconv_convenience(s->libnet->lp_ctx));	if (composite_nomem(s->cldap.sock, c)) return;	req = cldap_netlogon_send(s->cldap.sock, &s->cldap.io);	if (composite_nomem(req, c)) return;	req->async.fn		= unbecomeDC_recv_cldap;	req->async.private	= s;}static void unbecomeDC_connect_ldap(struct libnet_UnbecomeDC_state *s);static void unbecomeDC_recv_cldap(struct cldap_request *req){	struct libnet_UnbecomeDC_state *s = talloc_get_type(req->async.private,					    struct libnet_UnbecomeDC_state);	struct composite_context *c = s->creq;	c->status = cldap_netlogon_recv(req, s, &s->cldap.io);	if (!composite_is_ok(c)) return;	s->cldap.netlogon = s->cldap.io.out.netlogon.nt5_ex;	s->domain.dns_name		= s->cldap.netlogon.dns_domain;	s->domain.netbios_name		= s->cldap.netlogon.domain;	s->domain.guid			= s->cldap.netlogon.domain_uuid;	s->source_dsa.dns_name		= s->cldap.netlogon.pdc_dns_name;	s->source_dsa.netbios_name	= s->cldap.netlogon.pdc_name;	s->source_dsa.site_name		= s->cldap.netlogon.server_site;	s->dest_dsa.site_name		= s->cldap.netlogon.client_site;	unbecomeDC_connect_ldap(s);}static NTSTATUS unbecomeDC_ldap_connect(struct libnet_UnbecomeDC_state *s){	char *url;	url = talloc_asprintf(s, "ldap://%s/", s->source_dsa.dns_name);	NT_STATUS_HAVE_NO_MEMORY(url);	s->ldap.ldb = ldb_wrap_connect(s, s->libnet->event_ctx, s->libnet->lp_ctx, url,				       NULL,				       s->libnet->cred,				       0, NULL);	talloc_free(url);	if (s->ldap.ldb == NULL) {		return NT_STATUS_UNEXPECTED_NETWORK_ERROR;	}	return NT_STATUS_OK;}static NTSTATUS unbecomeDC_ldap_rootdse(struct libnet_UnbecomeDC_state *s){	int ret;	struct ldb_result *r;	struct ldb_dn *basedn;	static const char *attrs[] = {		"defaultNamingContext",		"configurationNamingContext",		NULL	};	basedn = ldb_dn_new(s, s->ldap.ldb, NULL);	NT_STATUS_HAVE_NO_MEMORY(basedn);	ret = ldb_search(s->ldap.ldb, basedn, LDB_SCOPE_BASE, 			 "(objectClass=*)", attrs, &r);	talloc_free(basedn);	if (ret != LDB_SUCCESS) {		return NT_STATUS_LDAP(ret);	} else if (r->count != 1) {		talloc_free(r);		return NT_STATUS_INVALID_NETWORK_RESPONSE;	}	talloc_steal(s, r);	s->domain.dn_str	= ldb_msg_find_attr_as_string(r->msgs[0], "defaultNamingContext", NULL);	if (!s->domain.dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;	talloc_steal(s, s->domain.dn_str);	s->forest.config_dn_str	= ldb_msg_find_attr_as_string(r->msgs[0], "configurationNamingContext", NULL);	if (!s->forest.config_dn_str) return NT_STATUS_INVALID_NETWORK_RESPONSE;	talloc_steal(s, s->forest.config_dn_str);	s->dest_dsa.server_dn_str = talloc_asprintf(s, "CN=%s,CN=Servers,CN=%s,CN=Sites,%s",						    s->dest_dsa.netbios_name,						    s->dest_dsa.site_name,				                    s->forest.config_dn_str);	NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.server_dn_str);	talloc_free(r);	return NT_STATUS_OK;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?