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

📄 slapi_utils.c

📁 ldap服务器源码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * DS 5.x compatability API follow */int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags ){	AttributeType *at;	if ( attr == NULL )		return LDAP_PARAM_ERROR;	at = attr->a_desc->ad_type;	*flags = SLAPI_ATTR_FLAG_STD_ATTR;	if ( is_at_single_value( at ) )		*flags |= SLAPI_ATTR_FLAG_SINGLE;	if ( is_at_operational( at ) )		*flags |= SLAPI_ATTR_FLAG_OPATTR;	if ( is_at_obsolete( at ) )		*flags |= SLAPI_ATTR_FLAG_OBSOLETE;	if ( is_at_collective( at ) )		*flags |= SLAPI_ATTR_FLAG_COLLECTIVE;	if ( is_at_no_user_mod( at ) )		*flags |= SLAPI_ATTR_FLAG_NOUSERMOD;	return LDAP_SUCCESS;}int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag ){	unsigned long flags;	if ( slapi_attr_get_flags( attr, &flags ) != 0 )		return 0;	return (flags & flag) ? 1 : 0;}Slapi_Attr *slapi_attr_new( void ){	Attribute *ad;	ad = (Attribute  *)slapi_ch_calloc( 1, sizeof(*ad) );	return ad;}Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type ){	const char *text;	AttributeDescription *ad = NULL;	if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {		return NULL;	}	a->a_desc = ad;	a->a_vals = NULL;	a->a_nvals = NULL;	a->a_next = NULL;	a->a_flags = 0;	return a;}void slapi_attr_free( Slapi_Attr **a ){	attr_free( *a );	*a = NULL;}Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr ){	return attr_dup( (Slapi_Attr *)attr );}int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v ){	struct berval nval;	struct berval *nvalp;	int rc;	AttributeDescription *desc = a->a_desc;	if ( desc->ad_type->sat_equality &&	     desc->ad_type->sat_equality->smr_normalize ) {		rc = (*desc->ad_type->sat_equality->smr_normalize)(			SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,			desc->ad_type->sat_syntax,			desc->ad_type->sat_equality,			(Slapi_Value *)v, &nval, NULL );		if ( rc != LDAP_SUCCESS ) {			return rc;		}		nvalp = &nval;	} else {		nvalp = NULL;	}	rc = value_add_one( &a->a_vals, (Slapi_Value *)v );	if ( rc == 0 && nvalp != NULL ) {		rc = value_add_one( &a->a_nvals, nvalp );	} else {		a->a_nvals = a->a_vals;	}	if ( nvalp != NULL ) {		slapi_ch_free_string( &nval.bv_val );	}	return rc;}int slapi_attr_type2plugin( const char *type, void **pi ){	*pi = NULL;	return LDAP_OTHER;}int slapi_attr_get_type( const Slapi_Attr *attr, char **type ){	if ( attr == NULL ) {		return LDAP_PARAM_ERROR;	}	*type = attr->a_desc->ad_cname.bv_val;	return LDAP_SUCCESS;}int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp ){	if ( attr == NULL ) {		return LDAP_PARAM_ERROR;	}	*oidp = attr->a_desc->ad_type->sat_oid;	return LDAP_SUCCESS;}int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 ){	MatchingRule *mr;	int ret;	int rc;	const char *text;	mr = a->a_desc->ad_type->sat_equality;	rc = value_match( &ret, a->a_desc, mr,			SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,		(struct berval *)v1, (void *)v2, &text );	if ( rc != LDAP_SUCCESS ) 		return -1;	return ( ret == 0 ) ? 0 : -1;}int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v ){	MatchingRule *mr;	struct berval *bv;	int j;	const char *text;	int rc;	int ret;	if ( a ->a_vals == NULL ) {		return -1;	}	mr = a->a_desc->ad_type->sat_equality;	for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {		rc = value_match( &ret, a->a_desc, mr,			SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );		if ( rc != LDAP_SUCCESS ) {			return -1;		}		if ( ret == 0 ) {			return 0;		}	}	return -1;}int slapi_attr_type_cmp( const char *t1, const char *t2, int opt ){	AttributeDescription *a1 = NULL;	AttributeDescription *a2 = NULL;	const char *text;	int ret;	if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {		return -1;	}	if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {		return 1;	}#define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \	? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \		? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))	switch ( opt ) {	case SLAPI_TYPE_CMP_EXACT:		ret = ad_cmp( a1, a2 );		break;	case SLAPI_TYPE_CMP_BASE:		ret = ad_base_cmp( a1, a2 );		break;	case SLAPI_TYPE_CMP_SUBTYPE:		ret = is_ad_subtype( a2, a2 );		break;	default:		ret = -1;		break;	}	return ret;}int slapi_attr_types_equivalent( const char *t1, const char *t2 ){	return ( slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ) == 0 );}int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v ){	return slapi_valueset_first_value( &a->a_vals, v );}int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v ){	return slapi_valueset_next_value( &a->a_vals, hint, v );}int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues ){	*numValues = slapi_valueset_count( &a->a_vals );	return 0;}int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs ){	*vs = &((Slapi_Attr *)a)->a_vals;	return 0;}int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals ){	return slapi_attr_get_values( a, vals );}char *slapi_attr_syntax_normalize( const char *s ){	AttributeDescription *ad = NULL;	const char *text;	if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {		return NULL;	}	return ad->ad_cname.bv_val;}Slapi_Value *slapi_value_new( void ){	struct berval *bv;	bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );	return bv;}Slapi_Value *slapi_value_new_berval(const struct berval *bval){	return ber_dupbv( NULL, (struct berval *)bval );}Slapi_Value *slapi_value_new_value(const Slapi_Value *v){	return slapi_value_new_berval( v );}Slapi_Value *slapi_value_new_string(const char *s){	struct berval bv;	bv.bv_val = (char *)s;	bv.bv_len = strlen( s );	return slapi_value_new_berval( &bv );}Slapi_Value *slapi_value_init(Slapi_Value *val){	val->bv_val = NULL;	val->bv_len = 0;	return val;}Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval){	return ber_dupbv( v, bval );}Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s){	v->bv_val = slapi_ch_strdup( s );	v->bv_len = strlen( s );	return v;}Slapi_Value *slapi_value_dup(const Slapi_Value *v){	return slapi_value_new_value( v );}void slapi_value_free(Slapi_Value **value){	if ( value == NULL ) {		return;	}	if ( (*value) != NULL ) {		slapi_ch_free( (void **)&(*value)->bv_val );		slapi_ch_free( (void **)value );	}}const struct berval *slapi_value_get_berval( const Slapi_Value *value ){	return value;}Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval ){	if ( value == NULL ) {		return NULL;	}	if ( value->bv_val != NULL ) {		slapi_ch_free( (void **)&value->bv_val );	}	slapi_value_init_berval( value, (struct berval *)bval );	return value;}Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom){	if ( value == NULL ) {		return NULL;	}	return slapi_value_set_berval( value, vfrom );}Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len){	if ( value == NULL ) {		return NULL;	}	if ( value->bv_val != NULL ) {		slapi_ch_free( (void **)&value->bv_val );	}	value->bv_val = slapi_ch_malloc( len );	value->bv_len = len;	AC_MEMCPY( value->bv_val, val, len );	return value;}int slapi_value_set_string(Slapi_Value *value, const char *strVal){	if ( value == NULL ) {		return -1;	}	slapi_value_set( value, (void *)strVal, strlen( strVal ) );	return 0;}int slapi_value_set_int(Slapi_Value *value, int intVal){	char buf[64];	snprintf( buf, sizeof( buf ), "%d", intVal );	return slapi_value_set_string( value, buf );}const char *slapi_value_get_string(const Slapi_Value *value){	if ( value == NULL ) return NULL;	if ( value->bv_val == NULL ) return NULL;	if ( !checkBVString( value ) ) return NULL;	return value->bv_val;}int slapi_value_get_int(const Slapi_Value *value){	if ( value == NULL ) return 0;	if ( value->bv_val == NULL ) return 0;	if ( !checkBVString( value ) ) return 0;	return (int)strtol( value->bv_val, NULL, 10 );}unsigned int slapi_value_get_uint(const Slapi_Value *value){	if ( value == NULL ) return 0;	if ( value->bv_val == NULL ) return 0;	if ( !checkBVString( value ) ) return 0;	return (unsigned int)strtoul( value->bv_val, NULL, 10 );}long slapi_value_get_long(const Slapi_Value *value){	if ( value == NULL ) return 0;	if ( value->bv_val == NULL ) return 0;	if ( !checkBVString( value ) ) return 0;	return strtol( value->bv_val, NULL, 10 );}unsigned long slapi_value_get_ulong(const Slapi_Value *value){	if ( value == NULL ) return 0;	if ( value->bv_val == NULL ) return 0;	if ( !checkBVString( value ) ) return 0;	return strtoul( value->bv_val, NULL, 10 );}size_t slapi_value_get_length(const Slapi_Value *value){	if ( value == NULL )		return 0;	return (size_t) value->bv_len;}int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2){	return slapi_attr_value_cmp( a, v1, v2 );}/* A ValueSet is a container for a BerVarray. */Slapi_ValueSet *slapi_valueset_new( void ){	Slapi_ValueSet *vs;	vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );	*vs = NULL;	return vs;}void slapi_valueset_free(Slapi_ValueSet *vs){	if ( vs != NULL ) {		BerVarray vp = *vs;		ber_bvarray_free( vp );		slapi_ch_free( (void **)&vp );		*vs = NULL;	}}void slapi_valueset_init(Slapi_ValueSet *vs){	if ( vs != NULL && *vs == NULL ) {		*vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );		(*vs)->bv_val = NULL;		(*vs)->bv_len = 0;	}}void slapi_valueset_done(Slapi_ValueSet *vs){	BerVarray vp;	if ( vs == NULL )		return;	for ( vp = *vs; vp->bv_val != NULL; vp++ ) {		vp->bv_len = 0;		slapi_ch_free( (void **)&vp->bv_val );	}	/* but don't free *vs or vs */}void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval){	struct berval bv;	ber_dupbv( &bv, (Slapi_Value *)addval );	ber_bvarray_add( vs, &bv );}int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v ){	return slapi_valueset_next_value( vs, 0, v );}int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v){	int i;	BerVarray vp;	if ( vs == NULL )		return -1;	vp = *vs;	for ( i = 0; vp[i].bv_val != NULL; i++ ) {		if ( i == index ) {			*v = &vp[i];			return index + 1;		}	}	return -1;}int slapi_valueset_count( const Slapi_ValueSet *vs ){	int i;	BerVarray vp;	if ( vs == NULL )		return 0;	vp = *vs;	for ( i = 0; vp[i].bv_val != NULL; i++ )		;	return i;}void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2){	BerVarray vp;	for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {		slapi_valueset_add_value( vs1, vp );	}}int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,	struct berval *val, int access ){	int rc;	slap_access_t slap_access;	AttributeDescription *ad = NULL;	const char *text;	rc = slap_str2ad( attr, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return rc;	}	/*	 * Whilst the SLAPI access types are arranged as a bitmask, the	 * documentation indicates that they are to be used separately.	 */	switch ( access & SLAPI_ACL_ALL ) {	case SLAPI_ACL_COMPARE:		slap_access = ACL_COMPARE;		break;	case SLAPI_ACL_SEARCH:		slap_access = ACL_SEARCH;		break;	case SLAPI_ACL_READ:		slap_access = ACL_READ;		break;	case SLAPI_ACL_WRITE:		slap_access = ACL_WRITE;		break;	case SLAPI_ACL_DELETE:		slap_access = ACL_WDEL;		break;	case SLAPI_ACL_ADD:		slap_access = ACL_WADD;		break;	case SLAPI_ACL_SELF:  /* not documented */	case SLAPI_ACL_PROXY: /* not documented */	default:		return LDAP_INSUFFICIENT_ACCESS;		break;	}	assert( pb->pb_op != NULL );	if ( access_allowed( pb->pb_op, e, ad, val, slap_access, NULL ) ) {		return LDAP_SUCCESS;	}	return LDAP_INSUFFICIENT_ACCESS;}int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf){	int rc = LDAP_SUCCESS;	Modifications *ml;	if ( pb == NULL || pb->pb_op == NULL )		return LDAP_PARAM_ERROR;	ml = slapi_int_ldapmods2modifications( mods );	if ( ml == NULL ) {		return LDAP_OTHER;	}	if ( rc == LDAP_SUCCESS ) {		rc = acl_check_modlist( pb->pb_op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;	}	slap_mods_free( ml, 1 );	return rc;}/* * Synthesise an LDAPMod array from a Modifications list to pass * to SLAPI. */LDAPMod **slapi_int_modifications2ldapmods( Modifications *modlist ){	Modifications *ml;	LDAPMod **mods, *modp;	int i, j;	for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )		;	mods = (LDAPMod **)slapi_ch_malloc( (i + 1) * sizeof(LDAPMod *) );	for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {		mods[i] = (LDAPMod *)slapi_ch_malloc( sizeof(LDAPMod) );		modp = mods[i];		modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;		if ( BER_BVISNULL( &ml->sml_type ) ) {			/* may happen for internally generated mods */			assert( ml->sml_desc != NULL );			modp->mod_type = slapi_ch_strdup( ml->sml_desc->ad_cname.bv_val );		} else {			modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );		}		if ( ml->sml_values != NULL ) {			for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )				;			modp->mod_bvalues = (struct berval **)slapi_ch_malloc( (j + 1) *				sizeof(struct berval *) );			for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {				modp->mod_bvalues[j] = (struct berval *)slapi_ch_malloc(						sizeof(struct berval) );				ber_dupbv( modp->mod_bvalues[j], &ml->sml_values[j] );

⌨️ 快捷键说明

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