From: Luke Howard Date: Sat, 27 Sep 2003 03:29:05 +0000 (+0000) Subject: Add slapi_dn_beparent, slapi_dn_parent and slapi_dn_isparent API. X-Git-Tag: OPENLDAP_REL_ENG_2_1_MP~646 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=613f493cae52585a7daefdeba3ef8b02e03675ff;p=openldap Add slapi_dn_beparent, slapi_dn_parent and slapi_dn_isparent API. --- diff --git a/include/slapi-plugin.h b/include/slapi-plugin.h index a2b525efee..fc6ac13ad2 100644 --- a/include/slapi-plugin.h +++ b/include/slapi-plugin.h @@ -49,6 +49,9 @@ int slapi_attr_get_values( Slapi_Attr *attr, struct berval ***vals ); char *slapi_dn_normalize( char *dn ); char *slapi_dn_normalize_case( char *dn ); int slapi_dn_issuffix( char *dn, char *suffix ); +char *slapi_dn_beparent( Slapi_PBlock *pb, const char *dn ); +char *slapi_dn_parent( const char *dn ); +int slapi_dn_isparent( const char *parentdn, const char *childdn ); char *slapi_dn_ignore_case( char *dn ); /* DS 5.x SLAPI */ diff --git a/servers/slapd/slapi/proto-slapi.h b/servers/slapd/slapi/proto-slapi.h index 9d330d91df..8fc2edd62c 100644 --- a/servers/slapd/slapi/proto-slapi.h +++ b/servers/slapd/slapi/proto-slapi.h @@ -156,6 +156,9 @@ extern char * slapi_esc_dn_normalize( char *dn ); extern char * slapi_esc_dn_normalize_case( char *dn ); extern int slapi_dn_isroot( Slapi_PBlock *pb, char *dn ); extern int slapi_dn_issuffix( char *dn, char *suffix ); +char *slapi_dn_beparent( Slapi_PBlock *pb, const char *dn ); +char *slapi_dn_parent( const char *dn ); +int slapi_dn_isparent( const char *parentdn, const char *childdn ); extern char *slapi_dn_ignore_case( char *dn ); extern char *slapi_get_hostname(); extern void slapi_register_supported_saslmechanism( char *mechanism ); diff --git a/servers/slapd/slapi/slapi_utils.c b/servers/slapd/slapi/slapi_utils.c index 8d3d735e16..b3c53ab074 100644 --- a/servers/slapd/slapi/slapi_utils.c +++ b/servers/slapd/slapi/slapi_utils.c @@ -922,6 +922,139 @@ slapi_dn_issuffix( #endif /* LDAP_SLAPI */ } +int +slapi_dn_isparent( + const char *parentdn, + const char *childdn ) +{ +#ifdef LDAP_SLAPI + struct berval assertedParentDN, normalizedAssertedParentDN; + struct berval childDN, normalizedChildDN; + struct berval normalizedParentDN; + int match; + + assert( parentdn != NULL ); + assert( childdn != NULL ); + + assertedParentDN.bv_val = (char *)parentdn; + assertedParentDN.bv_len = strlen( parentdn ); + + if ( dnNormalize( 0, NULL, NULL, &assertedParentDN, + &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS ) + { + return 0; + } + + childDN.bv_val = (char *)childdn; + childDN.bv_len = strlen( childdn ); + + if ( dnNormalize( 0, NULL, NULL, &childDN, + &normalizedChildDN, NULL ) != LDAP_SUCCESS ) + { + slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val ); + return 0; + } + + dnParent( &normalizedChildDN, &normalizedParentDN ); + + if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL, + &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS ) + { + match = -1; + } + + slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val ); + slapi_ch_free( (void **)&normalizedChildDN.bv_val ); + + return ( match == 0 ); +#else + return 0; +#endif /* LDAP_SLAPI */ +} + +/* + * Returns DN of the parent entry, or NULL if the DN is + * an empty string or NULL, or has no parent. + */ +char * +slapi_dn_parent( const char *_dn ) +{ +#ifdef LDAP_SLAPI + struct berval dn, prettyDN; + struct berval parentDN; + + if ( _dn == NULL ) { + return NULL; + } + + dn.bv_val = (char *)_dn; + dn.bv_len = strlen( _dn ); + + if ( dn.bv_len == 0 ) { + return NULL; + } + + if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) { + return NULL; + } + + dnParent( &prettyDN, &parentDN ); /* in-place */ + + slapi_ch_free( (void **)&prettyDN.bv_val ); + + if ( parentDN.bv_len == 0 ) { + return NULL; + } + + return slapi_ch_strdup( parentDN.bv_val ); +#else + return NULL; +#endif /* LDAP_SLAPI */ +} + +/* + * Returns DN of the parent entry; or NULL if the DN is + * an empty string, if the DN has no parent, or if the + * DN is the suffix of the backend database + */ +char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn ) +{ +#ifdef LDAP_SLAPI + Backend *be; + struct berval dn, prettyDN; + struct berval normalizedDN, parentDN; + + if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 ) + be = NULL; + + dn.bv_val = (char *)_dn; + dn.bv_len = strlen( _dn ); + + if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) { + return NULL; + } + + if ( be != NULL && be_issuffix( be, &normalizedDN ) ) { + slapi_ch_free( (void **)&prettyDN.bv_val ); + slapi_ch_free( (void **)&normalizedDN.bv_val ); + return NULL; + } + + dnParent( &prettyDN, &parentDN ); + + slapi_ch_free( (void **)&prettyDN.bv_val ); + slapi_ch_free( (void **)&normalizedDN.bv_val ); + + if ( parentDN.bv_len == 0 ) { + return NULL; + } + + return slapi_ch_strdup( parentDN.bv_val ); +#else + return NULL; +#endif /* LDAP_SLAPI */ +} + char * slapi_dn_ignore_case( char *dn ) {