map.c

来自「ldap服务器源码」· C语言 代码 · 共 805 行 · 第 1/2 页

C
805
字号
/* map.c - ldap backend mapping routines *//* $OpenLDAP: pkg/ldap/servers/slapd/back-meta/map.c,v 1.1.2.13 2007/01/05 09:47:10 ando Exp $ *//* This work is part of OpenLDAP Software <http://www.openldap.org/>. * * Copyright 1998-2007 The OpenLDAP Foundation. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted only as authorized by the OpenLDAP * Public License. * * A copy of this license is available in the file LICENSE in the * top-level directory of the distribution or, alternatively, at * <http://www.OpenLDAP.org/license.html>. *//* ACKNOWLEDGEMENTS: * This work was initially developed by the Howard Chu for inclusion * in OpenLDAP Software and subsequently enhanced by Pierangelo * Masarati. *//* This is an altered version *//* * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com> *  * Permission is granted to anyone to use this software for any purpose * on any computer system, and to alter it and redistribute it, subject * to the following restrictions: *  * 1. The author is not responsible for the consequences of use of this *    software, no matter how awful, even if they arise from flaws in it. *  * 2. The origin of this software must not be misrepresented, either by *    explicit claim or by omission.  Since few users ever read sources, *    credits should appear in the documentation. *  * 3. Altered versions must be plainly marked as such, and must not be *    misrepresented as being the original software.  Since few users *    ever read sources, credits should appear in the documentation. *  * 4. This notice may not be removed or altered. * * * * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it> *  * This software is being modified by Pierangelo Masarati. * The previously reported conditions apply to the modified code as well. * Changes in the original code are highlighted where required. * Credits for the original code go to the author, Howard Chu. */#include "portable.h"#include <stdio.h>#include <ac/string.h>#include <ac/socket.h>#include "slap.h"#include "../back-ldap/back-ldap.h"#include "back-meta.h"#undef ldap_debug	/* silence a warning in ldap-int.h */#include "../../../libraries/libldap/ldap-int.h"intmapping_cmp ( const void *c1, const void *c2 ){	struct ldapmapping *map1 = (struct ldapmapping *)c1;	struct ldapmapping *map2 = (struct ldapmapping *)c2;	int rc = map1->src.bv_len - map2->src.bv_len;	if (rc) return rc;	return ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) );}intmapping_dup ( void *c1, void *c2 ){	struct ldapmapping *map1 = (struct ldapmapping *)c1;	struct ldapmapping *map2 = (struct ldapmapping *)c2;	return ( ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) == 0 ) ? -1 : 0 );}voidldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m ){	struct ldapmapping *mapping;	assert( m != NULL );	*m = NULL;	mapping = (struct ldapmapping *)ch_calloc( 2, 			sizeof( struct ldapmapping ) );	if ( mapping == NULL ) {		return;	}	ber_str2bv( "objectclass", STRLENOF("objectclass"), 1, &mapping[0].src);	ber_dupbv( &mapping[0].dst, &mapping[0].src );	mapping[1].src = mapping[0].src;	mapping[1].dst = mapping[0].dst;	avl_insert( &lm->map, (caddr_t)&mapping[0], 			mapping_cmp, mapping_dup );	avl_insert( &lm->remap, (caddr_t)&mapping[1], 			mapping_cmp, mapping_dup );	*m = mapping;}intldap_back_mapping ( struct ldapmap *map, struct berval *s, struct ldapmapping **m,	int remap ){	Avlnode *tree;	struct ldapmapping fmapping;	assert( m != NULL );	if ( remap == BACKLDAP_REMAP ) {		tree = map->remap;	} else {		tree = map->map;	}	fmapping.src = *s;	*m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );	if ( *m == NULL ) {		return map->drop_missing;	}	return 0;}voidldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,	int remap ){	struct ldapmapping *mapping;	/* map->map may be NULL when mapping is configured,	 * but map->remap can't */	if ( map->remap == NULL ) {		*bv = *s;		return;	}	BER_BVZERO( bv );	( void )ldap_back_mapping( map, s, &mapping, remap );	if ( mapping != NULL ) {		if ( !BER_BVISNULL( &mapping->dst ) ) {			*bv = mapping->dst;		}		return;	}	if ( !map->drop_missing ) {		*bv = *s;	}}intldap_back_map_attrs(		struct ldapmap *at_map,		AttributeName *an,		int remap,		char ***mapped_attrs){	int i, j;	char **na;	struct berval mapped;	if ( an == NULL ) {		*mapped_attrs = NULL;		return LDAP_SUCCESS;	}	for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )		/*  */ ;	na = (char **)ch_calloc( i + 1, sizeof(char *) );	if ( na == NULL ) {		*mapped_attrs = NULL;		return LDAP_NO_MEMORY;	}	for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {		ldap_back_map( at_map, &an[i].an_name, &mapped, remap );		if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {			na[j++] = mapped.bv_val;		}	}	if ( j == 0 && i != 0 ) {		na[j++] = LDAP_NO_ATTRS;	}	na[j] = NULL;	*mapped_attrs = na;	return LDAP_SUCCESS;}intmap_attr_value(		dncookie		*dc,		AttributeDescription 	*ad,		struct berval		*mapped_attr,		struct berval		*value,		struct berval		*mapped_value,		int			remap ){	struct berval		vtmp;	int			freeval = 0;	ldap_back_map( &dc->target->mt_rwmap.rwm_at, &ad->ad_cname, mapped_attr, remap );	if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {#if 0		/*		 * FIXME: are we sure we need to search oc_map if at_map fails?		 */		ldap_back_map( &dc->target->mt_rwmap.rwm_oc, &ad->ad_cname, mapped_attr, remap );		if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {			*mapped_attr = ad->ad_cname;		}#endif		if ( dc->target->mt_rwmap.rwm_at.drop_missing ) {			return -1;		}		*mapped_attr = ad->ad_cname;	}	if ( value == NULL ) {		return 0;	}	if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )	{		dncookie fdc = *dc;#ifdef ENABLE_REWRITE		fdc.ctx = "searchFilterAttrDN";#endif		switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) {		case LDAP_SUCCESS:			if ( vtmp.bv_val != value->bv_val ) {				freeval = 1;			}			break;				case LDAP_UNWILLING_TO_PERFORM:			return -1;		case LDAP_OTHER:			return -1;		}	} else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {		ldap_back_map( &dc->target->mt_rwmap.rwm_oc, value, &vtmp, remap );		if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {			vtmp = *value;		}			} else {		vtmp = *value;	}	filter_escape_value( &vtmp, mapped_value );	if ( freeval ) {		ber_memfree( vtmp.bv_val );	}		return 0;}static intldap_back_int_filter_map_rewrite(		dncookie		*dc,		Filter			*f,		struct berval		*fstr,		int			remap ){	int		i;	Filter		*p;	struct berval	atmp,			vtmp,			*tmp;	static struct berval			/* better than nothing... */			ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),			ber_bvtf_false = BER_BVC( "(|)" ),			/* better than nothing... */			ber_bvtrue = BER_BVC( "(objectClass=*)" ),			ber_bvtf_true = BER_BVC( "(&)" ),#if 0			/* no longer needed; preserved for completeness */			ber_bvundefined = BER_BVC( "(?=undefined)" ),#endif			ber_bverror = BER_BVC( "(?=error)" ),			ber_bvunknown = BER_BVC( "(?=unknown)" ),			ber_bvnone = BER_BVC( "(?=none)" );	ber_len_t	len;	assert( fstr != NULL );	BER_BVZERO( fstr );	if ( f == NULL ) {		ber_dupbv( fstr, &ber_bvnone );		return LDAP_OTHER;	}	switch ( f->f_choice ) {	case LDAP_FILTER_EQUALITY:		if ( map_attr_value( dc, f->f_av_desc, &atmp,					&f->f_av_value, &vtmp, remap ) )		{			goto computed;		}		fstr->bv_len = atmp.bv_len + vtmp.bv_len			+ ( sizeof("(=)") - 1 );		fstr->bv_val = malloc( fstr->bv_len + 1 );		snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",			atmp.bv_val, vtmp.bv_val );		ber_memfree( vtmp.bv_val );		break;	case LDAP_FILTER_GE:		if ( map_attr_value( dc, f->f_av_desc, &atmp,					&f->f_av_value, &vtmp, remap ) )		{			goto computed;		}		fstr->bv_len = atmp.bv_len + vtmp.bv_len			+ ( sizeof("(>=)") - 1 );		fstr->bv_val = malloc( fstr->bv_len + 1 );		snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",			atmp.bv_val, vtmp.bv_val );		ber_memfree( vtmp.bv_val );		break;	case LDAP_FILTER_LE:		if ( map_attr_value( dc, f->f_av_desc, &atmp,					&f->f_av_value, &vtmp, remap ) )		{			goto computed;		}		fstr->bv_len = atmp.bv_len + vtmp.bv_len			+ ( sizeof("(<=)") - 1 );		fstr->bv_val = malloc( fstr->bv_len + 1 );		snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",			atmp.bv_val, vtmp.bv_val );		ber_memfree( vtmp.bv_val );		break;	case LDAP_FILTER_APPROX:		if ( map_attr_value( dc, f->f_av_desc, &atmp,					&f->f_av_value, &vtmp, remap ) )		{			goto computed;		}		fstr->bv_len = atmp.bv_len + vtmp.bv_len			+ ( sizeof("(~=)") - 1 );		fstr->bv_val = malloc( fstr->bv_len + 1 );		snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",			atmp.bv_val, vtmp.bv_val );		ber_memfree( vtmp.bv_val );		break;	case LDAP_FILTER_SUBSTRINGS:		if ( map_attr_value( dc, f->f_sub_desc, &atmp,					NULL, NULL, remap ) )		{			goto computed;		}		/* cannot be a DN ... */		fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );		fstr->bv_val = malloc( fstr->bv_len + 128 ); /* FIXME: why 128 ? */		snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",			atmp.bv_val );		if ( !BER_BVISNULL( &f->f_sub_initial ) ) {			len = fstr->bv_len;			filter_escape_value( &f->f_sub_initial, &vtmp );

⌨️ 快捷键说明

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