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

📄 slapi_utils.c

📁 ldap服务器源码
💻 C
📖 第 1 页 / 共 5 页
字号:
			}			modp->mod_bvalues[j] = NULL;		} else {			modp->mod_bvalues = NULL;		}		i++;	}	mods[i] = NULL;	return mods;}/* * Convert a potentially modified array of LDAPMods back to a * Modification list. Unfortunately the values need to be * duplicated because slap_mods_check() will try to free them * before prettying (and we can't easily get out of calling * slap_mods_check() because we need normalized values). */Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods ){	Modifications *modlist = NULL, **modtail;	LDAPMod **modp;	char textbuf[SLAP_TEXT_BUFLEN];	const char *text;	if ( mods == NULL ) {		return NULL;	}	modtail = &modlist;	for ( modp = mods; *modp != NULL; modp++ ) {		Modifications *mod;		LDAPMod *lmod = *modp;		int i;		const char *text;		AttributeDescription *ad = NULL;		if ( slap_str2ad( lmod->mod_type, &ad, &text ) != LDAP_SUCCESS ) {			continue;		}		mod = (Modifications *) slapi_ch_malloc( sizeof(Modifications) );		mod->sml_op = lmod->mod_op & ~(LDAP_MOD_BVALUES);		mod->sml_flags = 0;		mod->sml_type = ad->ad_cname;		mod->sml_desc = ad;		mod->sml_next = NULL;		i = 0;		if ( lmod->mod_op & LDAP_MOD_BVALUES ) {			if ( lmod->mod_bvalues != NULL ) {				while ( lmod->mod_bvalues[i] != NULL )					i++;			}		} else {			if ( lmod->mod_values != NULL ) {				while ( lmod->mod_values[i] != NULL )					i++;			}		}		if ( i == 0 ) {			mod->sml_values = NULL;		} else {			mod->sml_values = (BerVarray) slapi_ch_malloc( (i + 1) * sizeof(struct berval) );			/* NB: This implicitly trusts a plugin to return valid modifications. */			if ( lmod->mod_op & LDAP_MOD_BVALUES ) {				for ( i = 0; lmod->mod_bvalues[i] != NULL; i++ ) {					ber_dupbv( &mod->sml_values[i], lmod->mod_bvalues[i] );				}			} else {				for ( i = 0; lmod->mod_values[i] != NULL; i++ ) {					mod->sml_values[i].bv_val = slapi_ch_strdup( lmod->mod_values[i] );					mod->sml_values[i].bv_len = strlen( lmod->mod_values[i] );				}			}			mod->sml_values[i].bv_val = NULL;			mod->sml_values[i].bv_len = 0;		}		mod->sml_nvalues = NULL;		*modtail = mod;		modtail = &mod->sml_next;	}	if ( slap_mods_check( modlist, &text, textbuf, sizeof( textbuf ), NULL ) != LDAP_SUCCESS ) {		slap_mods_free( modlist, 1 );		modlist = NULL;	}	return modlist;}/* * Sun ONE DS 5.x computed attribute support. Computed attributes * allow for dynamically generated operational attributes, a very * useful thing indeed. *//* * For some reason Sun don't use the normal plugin mechanism * registration path to register an "evaluator" function (an * "evaluator" is responsible for adding computed attributes; * the nomenclature is somewhat confusing). * * As such slapi_compute_add_evaluator() registers the  * function directly. */int slapi_compute_add_evaluator(slapi_compute_callback_t function){	Slapi_PBlock *pPlugin = NULL;	int rc;	int type = SLAPI_PLUGIN_OBJECT;	pPlugin = slapi_pblock_new();	if ( pPlugin == NULL ) {		rc = LDAP_NO_MEMORY;		goto done;	}	rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );	if ( rc != LDAP_SUCCESS ) {		goto done;	}	rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );	if ( rc != LDAP_SUCCESS ) {		goto done;	}	rc = slapi_int_register_plugin( frontendDB, pPlugin );	if ( rc != 0 ) {		rc = LDAP_OTHER;		goto done;	}done:	if ( rc != LDAP_SUCCESS ) {		if ( pPlugin != NULL ) {			slapi_pblock_destroy( pPlugin );		}		return -1;	}	return 0;}/* * See notes above regarding slapi_compute_add_evaluator(). */int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function){	Slapi_PBlock *pPlugin = NULL;	int rc;	int type = SLAPI_PLUGIN_OBJECT;	pPlugin = slapi_pblock_new();	if ( pPlugin == NULL ) {		rc = LDAP_NO_MEMORY;		goto done;	}	rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );	if ( rc != LDAP_SUCCESS ) {		goto done;	}	rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );	if ( rc != LDAP_SUCCESS ) {		goto done;	}	rc = slapi_int_register_plugin( frontendDB, pPlugin );	if ( rc != 0 ) {		rc = LDAP_OTHER;		goto done;	}done:	if ( rc != LDAP_SUCCESS ) {		if ( pPlugin != NULL ) {			slapi_pblock_destroy( pPlugin );		}		return -1;	}	return 0;}/* * Call compute evaluators */int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn){	int rc = 0;	slapi_compute_callback_t *pGetPlugin, *tmpPlugin;	rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );	if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {		/* Nothing to do; front-end should ignore. */		return 0;	}	for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {		/*		 * -1: no attribute matched requested type		 *  0: one attribute matched		 * >0: error happened		 */		rc = (*pGetPlugin)( c, type, e, outputfn );		if ( rc > 0 ) {			break;		}	}	slapi_ch_free( (void **)&tmpPlugin );	return rc;}intcompute_rewrite_search_filter( Slapi_PBlock *pb ){	if ( pb == NULL || pb->pb_op == NULL )		return LDAP_PARAM_ERROR;	return slapi_int_call_plugins( pb->pb_op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );}/* * New API to provide the plugin with access to the search * pblock. Have informed Sun DS team. */intslapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb){	if ( c == NULL )		return -1;	if ( c->cac_pb == NULL )		return -1;	*pb = c->cac_pb;	return 0;}Slapi_Mutex *slapi_new_mutex( void ){	Slapi_Mutex *m;	m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );	if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {		slapi_ch_free( (void **)&m );		return NULL;	}	return m;}void slapi_destroy_mutex( Slapi_Mutex *mutex ){	if ( mutex != NULL ) {		ldap_pvt_thread_mutex_destroy( &mutex->mutex );		slapi_ch_free( (void **)&mutex);	}}void slapi_lock_mutex( Slapi_Mutex *mutex ){	ldap_pvt_thread_mutex_lock( &mutex->mutex );}int slapi_unlock_mutex( Slapi_Mutex *mutex ){	return ldap_pvt_thread_mutex_unlock( &mutex->mutex );}Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex ){	Slapi_CondVar *cv;	if ( mutex == NULL ) {		return NULL;	}	cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );	if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {		slapi_ch_free( (void **)&cv );		return NULL;	}	cv->mutex = mutex->mutex;	return cv;}void slapi_destroy_condvar( Slapi_CondVar *cvar ){	if ( cvar != NULL ) {		ldap_pvt_thread_cond_destroy( &cvar->cond );		slapi_ch_free( (void **)&cvar );	}}int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout ){	if ( cvar == NULL ) {		return -1;	}	return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );}int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all ){	if ( cvar == NULL ) {		return -1;	}	if ( notify_all ) {		return ldap_pvt_thread_cond_broadcast( &cvar->cond );	}	return ldap_pvt_thread_cond_signal( &cvar->cond );}int slapi_int_access_allowed( Operation *op,	Entry *entry,	AttributeDescription *desc,	struct berval *val,	slap_access_t access,	AccessControlState *state ){	int rc, slap_access = 0;	slapi_acl_callback_t *pGetPlugin, *tmpPlugin;	Slapi_PBlock *pb;	pb = SLAPI_OPERATION_PBLOCK( op );	if ( pb == NULL ) {		/* internal operation */		return 1;	}	switch ( access ) {	case ACL_COMPARE:                slap_access |= SLAPI_ACL_COMPARE;		break;	case ACL_SEARCH:		slap_access |= SLAPI_ACL_SEARCH;		break;	case ACL_READ:		slap_access |= SLAPI_ACL_READ;		break;	case ACL_WRITE:		slap_access |= SLAPI_ACL_WRITE;		break;	case ACL_WDEL:		slap_access |= SLAPI_ACL_DELETE;		break;	case ACL_WADD:		slap_access |= SLAPI_ACL_ADD;		break;	default:		break;        }	rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );	if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {		/* nothing to do; allowed access */		return 1;	}	rc = 1; /* default allow policy */	for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {		/*		 * 0	access denied		 * 1	access granted		 */		rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,				    val, slap_access, (void *)state );		if ( rc == 0 ) {			break;		}	}	slapi_ch_free( (void **)&tmpPlugin );	return rc;}/* * There is no documentation for this. */int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv ){	LDAPRDN lrdn;	LDAPAVA *ava;	int rc;	char *p;	*type = NULL;	bv->bv_len = 0;	bv->bv_val = NULL;	rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );	if ( rc != LDAP_SUCCESS ) {		return -1;	}	if ( lrdn[1] != NULL ) {		return -1; /* not single valued */	}	ava = lrdn[0];	*type = slapi_ch_strdup( ava->la_attr.bv_val );	ber_dupbv( bv, &ava->la_value );	ldap_rdnfree(lrdn);	return 0;}char *slapi_dn_plus_rdn( const char *dn, const char *rdn ){	struct berval new_dn, parent_dn, newrdn;	new_dn.bv_val = NULL;	parent_dn.bv_val = (char *)dn;	parent_dn.bv_len = strlen( dn );	newrdn.bv_val = (char *)rdn;	newrdn.bv_len = strlen( rdn );	build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );	return new_dn.bv_val;}int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e ){	Backend *be_orig;	const char *text;	char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };	size_t textlen = sizeof textbuf;	int rc = LDAP_SUCCESS;	PBLOCK_ASSERT_OP( pb, 0 );	be_orig = pb->pb_op->o_bd;	pb->pb_op->o_bd = select_backend( &e->e_nname, 0, 0 );	if ( pb->pb_op->o_bd != NULL ) {		rc = entry_schema_check( pb->pb_op, e, NULL, 0,			&text, textbuf, textlen );	}	pb->pb_op->o_bd = be_orig;	return ( rc == LDAP_SUCCESS ) ? 0 : 1;}int slapi_entry_rdn_values_present( const Slapi_Entry *e ){	LDAPDN dn;	int rc;	int i = 0, match = 0;	rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );	if ( rc != LDAP_SUCCESS ) {		return 0;	}	if ( dn[0] != NULL ) {		LDAPRDN rdn = dn[0];		for ( i = 0; rdn[i] != NULL; i++ ) {			LDAPAVA *ava = &rdn[0][i];			Slapi_Attr *a = NULL;			if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&			     slapi_attr_value_find( a, &ava->la_value ) == 0 )				match++;		}	}	ldap_dnfree( dn );	return ( i == match );}int slapi_entry_add_rdn_values( Slapi_Entry *e ){	LDAPDN dn;	int i, rc;	rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );	if ( rc != LDAP_SUCCESS ) {		return rc;	}	if ( dn[0] != NULL ) {		LDAPRDN rdn = dn[0];		struct berval *vals[2];		for ( i = 0; rdn[i] != NULL; i++ ) {			LDAPAVA *ava = &rdn[0][i];			Slapi_Attr *a = NULL;			if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&			     slapi_attr_value_find( a, &ava->la_value ) == 0 )				continue;			vals[0] = &ava->la_value;			vals[1] = NULL;			slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );		}	}	ldap_dnfree( dn );	return LDAP_SUCCESS;}const char *slapi_entry_get_uniqueid( const Slapi_Entry *e ){	Attribute *attr;	attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );	if ( attr == NULL ) {		return NULL;	}	if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {		return slapi_value_get_string( &attr->a_vals[0] );	}	return NULL;}void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid ){	struct berval bv;	attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );	bv.bv_val = uniqueid;	bv.bv_len = strlen( uniqueid );	attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );}LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared ){	LDAP *ld;	char *url;	size_t size;	int rc;	size = sizeof("ldap:///");	if ( secure )		size++;	size += strlen( ldaphost );	if ( ldapport != 0 )		size += 32;	url = slapi_ch_malloc( size );	if ( ldapport != 0 ) {		sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );	} else {		sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );	}	rc = ldap_initialize( &ld, url );	slapi_ch_free_string( &url );	return ( rc == LDAP_SUCCESS ) ? ld : NULL;}void slapi_ldap_unbind( LDAP *ld ){	ldap_unbind_ext_s( ld, NULL, NULL );}int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags ){	if ( be == NULL )		return LDAP_PARAM_ERROR;	*flags = SLAP_DBFLAGS(be);	return LDAP_SUCCESS;}intslapi_int_count_controls( LDAPControl **ctrls ){	size_t i;	if ( ctrls == NULL )		return 0;	for ( i = 0; ctrls[i] != NULL; i++ )		;	return i;}intslapi_op_abandoned( Slapi_PBlock *pb ){	if ( pb->pb_op == NULL )		return 0;	return ( pb->pb_op->o_abandon );}char *slapi_op_type_to_string(unsigned long type){	char *str;	switch (type) {	case SLAPI_OPERATION_BIND:		str = "bind";		break;	case SLAPI_OPERATION_UNBIND:		str = "unbind";		break;	case SLAPI_OPERATION_SEARCH:		str = "search";		break;	case SLAPI_OPERATION_MODIFY:		str = "modify";		break;	case SLAPI_OPERATION_ADD:		str = "add";		break;	case SLAPI_OPERATION_DELETE:		str = "delete";		break;	case SLAPI_OPERATION_MODDN:		str = "modrdn";		break;	case SLAPI_OPERATION_COMPARE:		str = "compare";		break;	case SLAPI_OPERATION_ABANDON:		str = "abandon";		break;	case SLAPI_OPERATION_EXTENDED:		str = "extended";		break;	default:		str = "unknown operatio

⌨️ 快捷键说明

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