⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectclass.c

📁 samba最新软件
💻 C
📖 第 1 页 / 共 3 页
字号:
/*    ldb database library   Copyright (C) Simo Sorce  2006   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2007   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/>.*//* *  Name: ldb * *  Component: objectClass sorting module * *  Description:  *  - sort the objectClass attribute into the class *    hierarchy,  *  - fix DNs and attributes into 'standard' case *  - Add objectCategory and ntSecurityDescriptor defaults * *  Author: Andrew Bartlett */#include "includes.h"#include "ldb/include/ldb.h"#include "ldb/include/ldb_errors.h"#include "ldb/include/ldb_private.h"#include "dsdb/samdb/samdb.h"#include "lib/util/dlinklist.h"#include "librpc/ndr/libndr.h"#include "librpc/gen_ndr/ndr_security.h"#include "libcli/security/security.h"#include "auth/auth.h"#include "param/param.h"struct oc_context {	enum oc_step {OC_DO_REQ, OC_SEARCH_SELF, OC_DO_MOD, 		      OC_SEARCH_ADD_PARENT, OC_DO_ADD, 		      OC_SEARCH_RENAME_PARENT, OC_DO_RENAME} step;	struct ldb_module *module;	struct ldb_request *orig_req;	struct ldb_request *down_req;	struct ldb_request *search_req;	struct ldb_reply *search_res;	struct ldb_request *add_req;	struct ldb_request *mod_req;	struct ldb_request *rename_req;};struct class_list {	struct class_list *prev, *next;	const struct dsdb_class *objectclass;};static int objectclass_do_add(struct ldb_handle *h);static struct ldb_handle *oc_init_handle(struct ldb_request *req, struct ldb_module *module){	struct oc_context *ac;	struct ldb_handle *h;	h = talloc_zero(req, struct ldb_handle);	if (h == NULL) {		ldb_set_errstring(module->ldb, "Out of Memory");		return NULL;	}	h->module = module;	ac = talloc_zero(h, struct oc_context);	if (ac == NULL) {		ldb_set_errstring(module->ldb, "Out of Memory");		talloc_free(h);		return NULL;	}	h->private_data = (void *)ac;	h->state = LDB_ASYNC_INIT;	h->status = LDB_SUCCESS;	ac->module = module;	ac->orig_req = req;	return h;}/* Sort objectClasses into correct order, and validate that all * objectClasses specified actually exist in the schema */static int objectclass_sort(struct ldb_module *module,			    const struct dsdb_schema *schema,			    struct ldb_message *msg, /* so that when we create new elements, we put it on the right parent */			    TALLOC_CTX *mem_ctx,			    struct ldb_message_element *objectclass_element,			    struct class_list **sorted_out) {	int i;	int layer;	struct class_list *sorted = NULL, *parent_class = NULL,		*subclass = NULL, *unsorted = NULL, *current, *poss_subclass, *poss_parent, *new_parent;	/* DESIGN:	 *	 * We work on 4 different 'bins' (implemented here as linked lists):	 *	 * * sorted:       the eventual list, in the order we wish to push	 *                 into the database.  This is the only ordered list.	 *	 * * parent_class: The current parent class 'bin' we are	 *                 trying to find subclasses for	 *	 * * subclass:     The subclasses we have found so far	 *	 * * unsorted:     The remaining objectClasses	 *	 * The process is a matter of filtering objectClasses up from	 * unsorted into sorted.  Order is irrelevent in the later 3 'bins'.	 * 	 * We start with 'top' (found and promoted to parent_class	 * initially).  Then we find (in unsorted) all the direct	 * subclasses of 'top'.  parent_classes is concatenated onto	 * the end of 'sorted', and subclass becomes the list in	 * parent_class.	 *	 * We then repeat, until we find no more subclasses.  Any left	 * over classes are added to the end.	 *	 */	/* Firstly, dump all the objectClass elements into the	 * unsorted bin, except for 'top', which is special */	for (i=0; i < objectclass_element->num_values; i++) {		current = talloc(mem_ctx, struct class_list);		if (!current) {			ldb_set_errstring(module->ldb, "objectclass: out of memory allocating objectclass list");			talloc_free(mem_ctx);			return LDB_ERR_OPERATIONS_ERROR;		}		current->objectclass = dsdb_class_by_lDAPDisplayName(schema, (const char *)objectclass_element->values[i].data);		if (!current->objectclass) {			ldb_asprintf_errstring(module->ldb, "objectclass %s is not a valid objectClass in schema", (const char *)objectclass_element->values[i].data);			return LDB_ERR_OBJECT_CLASS_VIOLATION;		}		/* this is the root of the tree.  We will start		 * looking for subclasses from here */		if (ldb_attr_cmp("top", current->objectclass->lDAPDisplayName) == 0) {			DLIST_ADD_END(parent_class, current, struct class_list *);		} else {			DLIST_ADD_END(unsorted, current, struct class_list *);		}	}	if (parent_class == NULL) {		current = talloc(mem_ctx, struct class_list);		current->objectclass = dsdb_class_by_lDAPDisplayName(schema, "top");		DLIST_ADD_END(parent_class, current, struct class_list *);	}	/* For each object:  find parent chain */	for (current = unsorted; schema && current; current = current->next) {		for (poss_parent = unsorted; poss_parent; poss_parent = poss_parent->next) {			if (ldb_attr_cmp(poss_parent->objectclass->lDAPDisplayName, current->objectclass->subClassOf) == 0) {				break;			}		}		/* If we didn't get to the end of the list, we need to add this parent */		if (poss_parent || (ldb_attr_cmp("top", current->objectclass->subClassOf) == 0)) {			continue;		}		new_parent = talloc(mem_ctx, struct class_list);		new_parent->objectclass = dsdb_class_by_lDAPDisplayName(schema, current->objectclass->subClassOf);		DLIST_ADD_END(unsorted, new_parent, struct class_list *);	}	/* DEBUGGING aid:  how many layers are we down now? */	layer = 0;	do {		layer++;		/* Find all the subclasses of classes in the		 * parent_classes.  Push them onto the subclass list */		/* Ensure we don't bother if there are no unsorted entries left */		for (current = parent_class; schema && unsorted && current; current = current->next) {			/* Walk the list of possible subclasses in unsorted */			for (poss_subclass = unsorted; poss_subclass; ) {				struct class_list *next;								/* Save the next pointer, as the DLIST_ macros will change poss_subclass->next */				next = poss_subclass->next;				if (ldb_attr_cmp(poss_subclass->objectclass->subClassOf, current->objectclass->lDAPDisplayName) == 0) {					DLIST_REMOVE(unsorted, poss_subclass);					DLIST_ADD(subclass, poss_subclass);										break;				}				poss_subclass = next;			}		}		/* Now push the parent_classes as sorted, we are done with		these.  Add to the END of the list by concatenation */		DLIST_CONCATENATE(sorted, parent_class, struct class_list *);		/* and now find subclasses of these */		parent_class = subclass;		subclass = NULL;		/* If we didn't find any subclasses we will fall out		 * the bottom here */	} while (parent_class);	if (!unsorted) {		*sorted_out = sorted;		return LDB_SUCCESS;	}	if (!schema) {		/* If we don't have schema yet, then just merge the lists again */		DLIST_CONCATENATE(sorted, unsorted, struct class_list *);		*sorted_out = sorted;		return LDB_SUCCESS;	}	/* This shouldn't happen, and would break MMC, perhaps there	 * was no 'top', a conflict in the objectClasses or some other	 * schema error?	 */	ldb_asprintf_errstring(module->ldb, "objectclass %s is not a valid objectClass in objectClass chain", unsorted->objectclass->lDAPDisplayName);	return LDB_ERR_OBJECT_CLASS_VIOLATION;}static DATA_BLOB *get_sd(struct ldb_module *module, TALLOC_CTX *mem_ctx, 			 const struct dsdb_class *objectclass) {	enum ndr_err_code ndr_err;	DATA_BLOB *linear_sd;	struct auth_session_info *session_info		= ldb_get_opaque(module->ldb, "sessionInfo");	struct security_descriptor *sd;	struct dom_sid *domain_sid = samdb_domain_sid(module->ldb);	if (!objectclass->defaultSecurityDescriptor || !domain_sid) {		return NULL;	}		sd = sddl_decode(mem_ctx, 			 objectclass->defaultSecurityDescriptor,			 domain_sid);	if (!sd || !session_info || !session_info->security_token) {		return NULL;	}		sd->owner_sid = session_info->security_token->user_sid;	sd->group_sid = session_info->security_token->group_sid;		linear_sd = talloc(mem_ctx, DATA_BLOB);	if (!linear_sd) {		return NULL;	}	ndr_err = ndr_push_struct_blob(linear_sd, mem_ctx, 					lp_iconv_convenience(ldb_get_opaque(module->ldb, "loadparm")),				       sd,				       (ndr_push_flags_fn_t)ndr_push_security_descriptor);	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {		return NULL;	}		return linear_sd;}static int get_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares){	struct oc_context *ac;	ac = talloc_get_type(context, struct oc_context);	/* we are interested only in the single reply (base search) we receive here */	if (ares->type == LDB_REPLY_ENTRY) {		if (ac->search_res != NULL) {			ldb_set_errstring(ldb, "Too many results");			talloc_free(ares);			return LDB_ERR_OPERATIONS_ERROR;		}		ac->search_res = talloc_move(ac, &ares);	} else {		talloc_free(ares);	}	return LDB_SUCCESS;}/* Fix up the DN to be in the standard form, taking particular care to match the parent DN   This should mean that if the parent is:    CN=Users,DC=samba,DC=example,DC=com   and a proposed child is    cn=Admins ,cn=USERS,dc=Samba,dc=example,dc=COM   The resulting DN should be:    CN=Admins,CN=Users,DC=samba,DC=example,DC=com    */static int fix_dn(TALLOC_CTX *mem_ctx, 		  struct ldb_dn *newdn, struct ldb_dn *parent_dn, 		  struct ldb_dn **fixed_dn) {	char *upper_rdn_attr;	/* Fix up the DN to be in the standard form, taking particular care to match the parent DN */	*fixed_dn = ldb_dn_copy(mem_ctx, parent_dn);	/* We need the attribute name in upper case */	upper_rdn_attr = strupper_talloc(*fixed_dn, 					 ldb_dn_get_rdn_name(newdn));	if (!upper_rdn_attr) {		return LDB_ERR_OPERATIONS_ERROR;	}					       	/* Create a new child */	if (ldb_dn_add_child_fmt(*fixed_dn, "X=X") == false) {		return LDB_ERR_OPERATIONS_ERROR;	}	/* And replace it with CN=foo (we need the attribute in upper case */	return ldb_dn_set_component(*fixed_dn, 0, upper_rdn_attr,				    *ldb_dn_get_rdn_val(newdn));}/* Fix all attribute names to be in the correct case, and check they are all valid per the schema */static int fix_attributes(struct ldb_context *ldb, const struct dsdb_schema *schema, struct ldb_message *msg) {	int i;	for (i=0; i < msg->num_elements; i++) {		const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema, msg->elements[i].name);		if (!attribute) {			ldb_asprintf_errstring(ldb, "attribute %s is not a valid attribute in schema", msg->elements[i].name);			return LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE;		}		msg->elements[i].name = attribute->lDAPDisplayName;	}	return LDB_SUCCESS;}static int objectclass_add(struct ldb_module *module, struct ldb_request *req){	static const char * const attrs[] = { NULL };	struct ldb_handle *h;	struct oc_context *ac;	struct ldb_dn *parent_dn;	int ret;		ldb_debug(module->ldb, LDB_DEBUG_TRACE, "objectclass_add\n");	/* do not manipulate our control entries */	if (ldb_dn_is_special(req->op.add.message->dn)) {		return ldb_next_request(module, req);	}	/* Need to object to this, but cn=rootdse doesn't hae an objectClass... */	if (ldb_msg_find_element(req->op.add.message, 				 "objectClass") == NULL) {		return ldb_next_request(module, req);	}

⌨️ 快捷键说明

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