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

📄 slapi_utils.c

📁 ldap服务器源码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* $OpenLDAP: pkg/ldap/servers/slapd/slapi/slapi_utils.c,v 1.136.2.11 2007/01/02 21:44:11 kurt Exp $ *//* This work is part of OpenLDAP Software <http://www.openldap.org/>. * * Copyright 2002-2007 The OpenLDAP Foundation. * Portions Copyright 1997,2002-2003 IBM Corporation. * 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 IBM Corporation for use in * IBM products and subsequently ported to OpenLDAP Software by * Steve Omrani.  Additional significant contributors include: *   Luke Howard */#include "portable.h"#include <ac/string.h>#include <ac/stdarg.h>#include <ac/ctype.h>#include <ac/unistd.h>#include <slap.h>#include <slapi.h>#include <netdb.h>#ifdef LDAP_SLAPI/* * server start time (should we use a struct timeval also in slapd? */static struct			timeval base_time;ldap_pvt_thread_mutex_t		slapi_hn_mutex;ldap_pvt_thread_mutex_t		slapi_time_mutex;struct slapi_mutex {	ldap_pvt_thread_mutex_t mutex;};struct slapi_condvar {	ldap_pvt_thread_cond_t cond;	ldap_pvt_thread_mutex_t mutex;};static int checkBVString(const struct berval *bv){	int i;	for ( i = 0; i < bv->bv_len; i++ ) {		if ( bv->bv_val[i] == '\0' )			return 0;	}	if ( bv->bv_val[i] != '\0' )		return 0;	return 1;}/* * This function converts an array of pointers to berval objects to * an array of berval objects. */intbvptr2obj(	struct berval	**bvptr, 	BerVarray	*bvobj ){	int		rc = LDAP_SUCCESS;	int		i;	BerVarray	tmpberval;	if ( bvptr == NULL || *bvptr == NULL ) {		return LDAP_OTHER;	}	for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {		; /* EMPTY */	}	tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));	if ( tmpberval == NULL ) {		return LDAP_NO_MEMORY;	} 	for ( i = 0; bvptr[i] != NULL; i++ ) {		tmpberval[i].bv_val = bvptr[i]->bv_val;		tmpberval[i].bv_len = bvptr[i]->bv_len;	}	tmpberval[i].bv_val = NULL;	tmpberval[i].bv_len = 0;	if ( rc == LDAP_SUCCESS ) {		*bvobj = tmpberval;	}	return rc;}Slapi_Entry *slapi_str2entry(	char		*s, 	int		flags ){	return str2entry( s );}char *slapi_entry2str(	Slapi_Entry	*e, 	int		*len ) {	char		*ret = NULL;	char		*s;	ldap_pvt_thread_mutex_lock( &entry2str_mutex );	s = entry2str( e, len );	if ( s != NULL )		ret = slapi_ch_strdup( s );	ldap_pvt_thread_mutex_unlock( &entry2str_mutex );	return ret;}char *slapi_entry_get_dn( Slapi_Entry *e ) {	return e->e_name.bv_val;}intslapi_x_entry_get_id( Slapi_Entry *e ){	return e->e_id;}static intslapi_int_dn_pretty( struct berval *in, struct berval *out ){	Syntax		*syntax = slap_schema.si_syn_distinguishedName;	assert( syntax != NULL );	return (syntax->ssyn_pretty)( syntax, in, out, NULL );}static intslapi_int_dn_normalize( struct berval *in, struct berval *out ){	MatchingRule	*mr = slap_schema.si_mr_distinguishedNameMatch;	Syntax		*syntax = slap_schema.si_syn_distinguishedName;	assert( mr != NULL );	return (mr->smr_normalize)( 0, syntax, mr, in, out, NULL );}void slapi_entry_set_dn(	Slapi_Entry	*e, 	char		*ldn ){	struct berval	dn = BER_BVNULL;	dn.bv_val = ldn;	dn.bv_len = strlen( ldn );	slapi_int_dn_pretty( &dn, &e->e_name );	slapi_int_dn_normalize( &dn, &e->e_nname );}Slapi_Entry *slapi_entry_dup( Slapi_Entry *e ) {	return entry_dup( e );}int slapi_entry_attr_delete(	Slapi_Entry	*e, 			char		*type ) {	AttributeDescription	*ad = NULL;	const char		*text;	if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {		return 1;	/* LDAP_NO_SUCH_ATTRIBUTE */	}	if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {		return 0;	/* attribute is deleted */	} else {		return -1;	/* something went wrong */	}}Slapi_Entry *slapi_entry_alloc( void ) {	return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );}void slapi_entry_free( Slapi_Entry *e ) {	if ( e != NULL )		entry_free( e );}int slapi_entry_attr_merge(	Slapi_Entry	*e, 	char		*type, 	struct berval	**vals ) {	AttributeDescription	*ad = NULL;	const char		*text;	BerVarray		bv;	int			rc;	rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return -1;	}		rc = bvptr2obj( vals, &bv );	if ( rc != LDAP_SUCCESS ) {		return -1;	}		rc = attr_merge_normalize_one( e, ad, bv, NULL );	ch_free( bv );	return rc;}intslapi_entry_attr_find(	Slapi_Entry	*e, 	char		*type, 	Slapi_Attr	**attr ) {	AttributeDescription	*ad = NULL;	const char		*text;	int			rc;	rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return -1;	}	*attr = attr_find( e->e_attrs, ad );	if ( *attr == NULL ) {		return -1;	}	return 0;}char *slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type ){	AttributeDescription *ad = NULL;	const char *text;	int rc;	Attribute *attr;	rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return NULL;	}	attr = attr_find( e->e_attrs, ad );	if ( attr == NULL ) {		return NULL;	}	if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {		const char *p;		p = slapi_value_get_string( &attr->a_vals[0] );		if ( p != NULL ) {			return slapi_ch_strdup( p );		}	}	return NULL;}intslapi_entry_attr_get_int( const Slapi_Entry *e, const char *type ){	AttributeDescription *ad = NULL;	const char *text;	int rc;	Attribute *attr;	rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return 0;	}	attr = attr_find( e->e_attrs, ad );	if ( attr == NULL ) {		return 0;	}	return slapi_value_get_int( attr->a_vals );}longslapi_entry_attr_get_long( const Slapi_Entry *e, const char *type ){	AttributeDescription *ad = NULL;	const char *text;	int rc;	Attribute *attr;	rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return 0;	}	attr = attr_find( e->e_attrs, ad );	if ( attr == NULL ) {		return 0;	}	return slapi_value_get_long( attr->a_vals );}unsigned intslapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type ){	AttributeDescription *ad = NULL;	const char *text;	int rc;	Attribute *attr;	rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return 0;	}	attr = attr_find( e->e_attrs, ad );	if ( attr == NULL ) {		return 0;	}	return slapi_value_get_uint( attr->a_vals );}unsigned longslapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type ){	AttributeDescription *ad = NULL;	const char *text;	int rc;	Attribute *attr;	rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return 0;	}	attr = attr_find( e->e_attrs, ad );	if ( attr == NULL ) {		return 0;	}	return slapi_value_get_ulong( attr->a_vals );}intslapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value ){	struct berval bv;	AttributeDescription *ad = NULL;	const char *text;	int rc;	Attribute *attr;		rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return 0;	}	attr = attr_find( e->e_attrs, ad );	if ( attr == NULL ) {		return 0;	}	bv.bv_val = (char *)value;	bv.bv_len = strlen( value );	return ( slapi_attr_value_find( attr, &bv ) != -1 );}voidslapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value){	AttributeDescription	*ad = NULL;	const char		*text;	int			rc;	struct berval		bv;		rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return;	}		attr_delete ( &e->e_attrs, ad );	if ( value != NULL ) {		bv.bv_val = (char *)value;		bv.bv_len = strlen(value);		attr_merge_normalize_one( e, ad, &bv, NULL );	}}voidslapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l){	char buf[64];	snprintf( buf, sizeof( buf ), "%d", l );	slapi_entry_attr_set_charptr( e, type, buf );}voidslapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l){	char buf[64];	snprintf( buf, sizeof( buf ), "%u", l );	slapi_entry_attr_set_charptr( e, type, buf );}voidslapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l){	char buf[64];	snprintf( buf, sizeof( buf ), "%ld", l );	slapi_entry_attr_set_charptr( e, type, buf );}voidslapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l){	char buf[64];	snprintf( buf, sizeof( buf ), "%lu", l );	slapi_entry_attr_set_charptr( e, type, buf );}intslapi_is_rootdse( const char *dn ){	return ( dn == NULL || dn[0] == '\0' );}intslapi_entry_has_children( const Slapi_Entry *e ){	Slapi_PBlock *pb;	int hasSubordinates = 0;	pb = slapi_pblock_new();	slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );	slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn( (Entry *)e ) );	pb->pb_op->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );	if ( pb->pb_op->o_bd != NULL ) {		pb->pb_op->o_bd->be_has_subordinates( pb->pb_op, (Entry *)e, &hasSubordinates );	}	slapi_pblock_destroy( pb );	return ( hasSubordinates == LDAP_COMPARE_TRUE );}/* * Return approximate size of the entry rounded to the nearest * 1K. Only the size of the attribute values are counted in the * Sun implementation. * * http://docs.sun.com/source/816-6701-10/funcref.html#1017388 */size_t slapi_entry_size(Slapi_Entry *e){	size_t size;	Attribute *a;	int i;	for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {		for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {			size += a->a_vals[i].bv_len + 1;		}	}	size += 1023;	size -= (size % 1024);	return size;}/* * Add values to entry. * * Returns: *	LDAP_SUCCESS			Values added to entry *	LDAP_TYPE_OR_VALUE_EXISTS	One or more values exist in entry already *	LDAP_CONSTRAINT_VIOLATION	Any other error (odd, but it's the spec) */intslapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals ){	Modification		mod;	const char		*text;	int			rc;	char			textbuf[SLAP_TEXT_BUFLEN];	mod.sm_op = LDAP_MOD_ADD;	mod.sm_flags = 0;	mod.sm_desc = NULL;	mod.sm_type.bv_val = (char *)type;	mod.sm_type.bv_len = strlen( type );	rc = slap_str2ad( type, &mod.sm_desc, &text );	if ( rc != LDAP_SUCCESS ) {		return rc;	}	if ( vals == NULL ) {		/* Apparently vals can be NULL		 * FIXME: sm_values = NULL ? */		mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );		mod.sm_values->bv_val = NULL;	} else {		rc = bvptr2obj( vals, &mod.sm_values );		if ( rc != LDAP_SUCCESS ) {			return LDAP_CONSTRAINT_VIOLATION;		}	}	mod.sm_nvalues = NULL;	rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );	slapi_ch_free( (void **)&mod.sm_values );	return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;}intslapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals ){	return slapi_entry_add_values( e, type, vals );}intslapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs){	AttributeDescription	*ad = NULL;	const char		*text;	int			rc;		rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return -1;	}	return attr_merge_normalize( e, ad, *vs, NULL );}intslapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals ){	Modification		mod;	const char		*text;	int			rc;	char			textbuf[SLAP_TEXT_BUFLEN];	mod.sm_op = LDAP_MOD_DELETE;	mod.sm_flags = 0;	mod.sm_desc = NULL;	mod.sm_type.bv_val = (char *)type;	mod.sm_type.bv_len = strlen( type );	if ( vals == NULL ) {		/* If vals is NULL, this is a NOOP. */		return LDAP_SUCCESS;	}		rc = slap_str2ad( type, &mod.sm_desc, &text );	if ( rc != LDAP_SUCCESS ) {		return rc;	}	if ( vals[0] == NULL ) {		/* SLAPI doco says LDApb_opERATIONS_ERROR but LDAP_OTHER is better */		return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;	}	rc = bvptr2obj( vals, &mod.sm_values );	if ( rc != LDAP_SUCCESS ) {		return LDAP_CONSTRAINT_VIOLATION;	}	mod.sm_nvalues = NULL;	rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );	slapi_ch_free( (void **)&mod.sm_values );	return rc;}intslapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals ){	return slapi_entry_delete_values( e, type, vals );}intslapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals ){	return slapi_entry_attr_merge( e, (char *)type, vals );}intslapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value){	AttributeDescription	*ad = NULL;	int 			rc;	const char		*text;	rc = slap_str2ad( type, &ad, &text );	if ( rc != LDAP_SUCCESS ) {		return -1;	}	rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );	if ( rc != LDAP_SUCCESS ) {		return -1;	}	return 0;}intslapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value){	Slapi_Value val;	val.bv_val = (char *)value;	val.bv_len = strlen( value );

⌨️ 快捷键说明

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