1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
21 #define SLAP_LDAPDN_PRETTY 0x1
24 * The DN syntax-related functions take advantage of the dn representation
25 * handling functions ldap_str2dn/ldap_dn2str. The latter are not schema-
26 * aware, so the attributes and their values need be validated (and possibly
27 * normalized). In the current implementation the required validation/nor-
28 * malization/"pretty"ing are done on newly created DN structural represen-
29 * tations; however the idea is to move towards DN handling in structural
30 * representation instead of the current string representation. To this
31 * purpose, we need to do only the required operations and keep track of
32 * what has been done to minimize their impact on performances.
34 * Developers are strongly encouraged to use this feature, to speed-up
38 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
41 * In-place, schema-aware validation of the
42 * structural representation of a distinguished name.
45 LDAPDN_validate( LDAPDN *dn )
52 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
53 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
58 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
59 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
60 AttributeDescription *ad;
61 slap_syntax_validate_func *validate = NULL;
65 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
66 const char *text = NULL;
68 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
69 if ( rc != LDAP_SUCCESS ) {
70 return LDAP_INVALID_SYNTAX;
73 ava->la_private = ( void * )ad;
77 * Replace attr oid/name with the canonical name
79 ava->la_attr = ad->ad_cname;
81 validate = ad->ad_type->sat_syntax->ssyn_validate;
85 * validate value by validate function
87 rc = ( *validate )( ad->ad_type->sat_syntax,
90 if ( rc != LDAP_SUCCESS ) {
91 return LDAP_INVALID_SYNTAX;
101 * dn validate routine
113 if ( in->bv_len == 0 ) {
114 return( LDAP_SUCCESS );
117 rc = ldap_str2dn( in->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
120 * Schema-aware validate
122 if ( rc == LDAP_SUCCESS ) {
123 rc = LDAPDN_validate( dn );
127 if ( rc != LDAP_SUCCESS ) {
128 return( LDAP_INVALID_SYNTAX );
131 return( LDAP_SUCCESS );
135 * AVA sorting inside a RDN
137 * rule: sort attributeTypes in alphabetical order; in case of multiple
138 * occurrences of the same attributeType, sort values in byte order
139 * (use memcmp, which implies alphabetical order in case of IA5 value;
140 * this should guarantee the repeatability of the operation).
142 * Note: the sorting can be slightly improved by sorting first
143 * by attribute type length, then by alphabetical order.
145 * uses a linear search; should be fine since the number of AVAs in
146 * a RDN should be limited.
149 AVA_Sort( LDAPRDN *rdn, int iAVA )
152 LDAPAVA *ava_in = rdn[ 0 ][ iAVA ];
157 for ( i = 0; i < iAVA; i++ ) {
158 LDAPAVA *ava = rdn[ 0 ][ i ];
163 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
172 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
174 v = memcmp( ava_in->la_value.bv_val,
175 ava->la_value.bv_val,
176 d <= 0 ? ava_in->la_value.bv_len
177 : ava->la_value.bv_len );
179 if ( v == 0 && d != 0 ) {
198 a = strcmp( ava_in->la_attr.bv_val,
199 ava->la_attr.bv_val );
205 for ( j = iAVA; j > i; j-- ) {
206 rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
208 rdn[ 0 ][ i ] = ava_in;
215 * In-place, schema-aware normalization / "pretty"ing of the
216 * structural representation of a distinguished name.
219 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
226 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
227 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
232 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
233 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
234 AttributeDescription *ad;
235 slap_syntax_transform_func *transf = NULL;
237 struct berval bv = { 0, NULL };
242 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
243 const char *text = NULL;
245 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
246 if ( rc != LDAP_SUCCESS ) {
247 return LDAP_INVALID_SYNTAX;
250 ava->la_private = ( void * )ad;
255 * Replace attr oid/name with the canonical name
257 ava->la_attr = ad->ad_cname;
259 if( flags & SLAP_LDAPDN_PRETTY ) {
260 transf = ad->ad_type->sat_syntax->ssyn_pretty;
263 transf = ad->ad_type->sat_syntax->ssyn_normalize;
264 mr = ad->ad_type->sat_equality;
269 * transform value by normalize/pretty function
271 rc = ( *transf )( ad->ad_type->sat_syntax,
272 &ava->la_value, &bv );
274 if ( rc != LDAP_SUCCESS ) {
275 return LDAP_INVALID_SYNTAX;
279 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
282 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
283 : &ava->la_value, LDAP_UTF8_CASEFOLD ),
289 free( ava->la_value.bv_val );
293 if( do_sort ) AVA_Sort( rdn, iAVA );
301 * dn normalize routine
307 struct berval **normalized )
312 assert( normalized && *normalized == NULL );
314 out = ch_malloc( sizeof( struct berval ) );
315 rc = dnNormalize2( syntax, val, out );
316 if ( rc != LDAP_SUCCESS )
332 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
334 if ( val->bv_len != 0 ) {
339 * Go to structural representation
341 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
342 if ( rc != LDAP_SUCCESS ) {
343 return LDAP_INVALID_SYNTAX;
347 * Schema-aware rewrite
349 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
351 return LDAP_INVALID_SYNTAX;
355 * Back to string representation
357 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
361 if ( rc != LDAP_SUCCESS ) {
362 return LDAP_INVALID_SYNTAX;
365 ber_dupbv( out, val );
368 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
374 * dn "pretty"ing routine
380 struct berval **pretty)
385 assert( pretty && *pretty == NULL );
387 out = ch_malloc( sizeof( struct berval ) );
388 rc = dnPretty2( syntax, val, out );
389 if ( rc != LDAP_SUCCESS )
405 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
407 if ( val->bv_len != 0 ) {
411 /* FIXME: should be liberal in what we accept */
412 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
413 if ( rc != LDAP_SUCCESS ) {
414 return LDAP_INVALID_SYNTAX;
418 * Schema-aware rewrite
420 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
422 return LDAP_INVALID_SYNTAX;
425 /* FIXME: not sure why the default isn't pretty */
426 /* RE: the default is the form that is used as
427 * an internal representation; the pretty form
429 rc = ldap_dn2bv( dn, out,
430 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
434 if ( rc != LDAP_SUCCESS ) {
435 return LDAP_INVALID_SYNTAX;
438 ber_dupbv( out, val );
441 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
447 * Combination of both dnPretty and dnNormalize
453 struct berval *pretty,
454 struct berval *normal)
456 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
462 if ( val->bv_len != 0 ) {
466 pretty->bv_val = NULL;
467 normal->bv_val = NULL;
471 /* FIXME: should be liberal in what we accept */
472 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
473 if ( rc != LDAP_SUCCESS ) {
474 return LDAP_INVALID_SYNTAX;
478 * Schema-aware rewrite
480 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
482 return LDAP_INVALID_SYNTAX;
485 rc = ldap_dn2bv( dn, pretty,
486 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
488 if ( rc != LDAP_SUCCESS ) {
490 return LDAP_INVALID_SYNTAX;
493 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
495 free( pretty->bv_val );
496 pretty->bv_val = NULL;
498 return LDAP_INVALID_SYNTAX;
501 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
504 if ( rc != LDAP_SUCCESS ) {
505 free( pretty->bv_val );
506 pretty->bv_val = NULL;
508 return LDAP_INVALID_SYNTAX;
511 ber_dupbv( pretty, val );
512 ber_dupbv( normal, val );
515 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
516 pretty->bv_val, normal->bv_val, 0 );
530 struct berval *value,
531 void *assertedValue )
534 struct berval *asserted = (struct berval *) assertedValue;
538 assert( assertedValue );
540 match = value->bv_len - asserted->bv_len;
543 match = strcmp( value->bv_val, asserted->bv_val );
547 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
548 "dnMatch: %d\n %s\n %s\n", match,
549 value->bv_val, asserted->bv_val ));
551 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
552 match, value->bv_val, asserted->bv_val );
556 return( LDAP_SUCCESS );
559 #ifdef SLAP_DN_MIGRATION
561 * these routines are provided for migration purposes only!
562 * dn_normalize is deprecated in favor of dnNormalize
563 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
565 * other routines are likewise deprecated but may not yet have
566 * replacement functions.
570 * dn_normalize - put dn into a canonical form suitable for storing
571 * in a hash database. this involves normalizing the case as well as
572 * the format. the dn is normalized in place as well as returned if valid.
573 * Deprecated in favor of dnNormalize()
576 dn_normalize( char *dn )
579 struct berval *normalized = NULL;
582 if ( dn == NULL || dn[0] == '\0' ) {
587 val.bv_len = strlen( dn );
589 rc = dnNormalize( NULL, &val, &normalized );
590 if ( rc != LDAP_SUCCESS ) {
594 if ( val.bv_len < normalized->bv_len ) {
595 ber_bvfree( normalized );
599 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
600 ber_bvfree( normalized );
606 * dnParent - dn's parent, in-place
616 rc = ldap_str2rdn( dn, NULL, (char **)&p,
617 LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
618 if ( rc != LDAP_SUCCESS ) {
628 assert( DN_SEPARATOR( p[ 0 ] ) );
631 while ( ASCII_SPACE( p[ 0 ] ) ) {
641 * dn_parent - return the dn's parent, in-place
642 * FIXME: should be replaced by dnParent()
655 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
659 if ( dn[ 0 ] == '\0' ) {
663 if ( be != NULL && be_issuffix( be, dn ) ) {
667 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
671 return ( char * )pdn;
673 #endif /* SLAP_DN_MIGRATION */
688 if( dn->bv_len == 0 ) {
692 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
693 if ( rc != LDAP_SUCCESS ) {
697 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
698 ldap_rdnfree( tmpRDN );
699 if ( rc != LDAP_SUCCESS ) {
707 * FIXME: should be replaced by dnExtractRdn()
712 struct berval *dn_in )
719 if ( dn_in == NULL ) {
723 if ( !dn_in->bv_len ) {
727 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
731 rc = ldap_str2rdn( dn_in->bv_val, NULL, (char **)&p,
732 LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
733 if ( rc != LDAP_SUCCESS ) {
737 return p - dn_in->bv_val;
743 * LDAP_SUCCESS if rdn is a legal rdn;
744 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
747 rdnValidate( struct berval *rdn )
751 * input is a pretty or normalized DN
752 * hence, we can just search for ','
754 if( rdn == NULL || rdn->bv_len == 0 ) {
755 return LDAP_INVALID_SYNTAX;
758 return strchr( rdn->bv_val, ',' ) == NULL
759 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
762 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
769 if ( rdn == NULL || rdn == '\0' ) {
776 rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
777 if ( rc != LDAP_SUCCESS ) {
784 if ( p[ 0 ] != '\0' ) {
789 * Schema-aware validate
791 if ( rc == LDAP_SUCCESS ) {
792 rc = LDAPDN_validate( DN );
797 * Must validate (there's a repeated parsing ...)
799 return ( rc == LDAP_SUCCESS );
806 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
809 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
813 build_new_dn( struct berval * new_dn,
814 struct berval * parent_dn,
815 struct berval * newrdn )
819 if ( parent_dn == NULL ) {
820 ber_dupbv( new_dn, newrdn );
824 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
825 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
827 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
829 strcpy( ptr, parent_dn->bv_val );
834 * dnIsSuffix - tells whether suffix is a suffix of dn.
835 * Both dn and suffix must be normalized.
839 const struct berval *dn,
840 const struct berval *suffix )
842 int d = dn->bv_len - suffix->bv_len;
847 /* empty suffix matches any dn */
848 if ( suffix->bv_len == 0 ) {
852 /* suffix longer than dn */
857 /* no rdn separator or escaped rdn separator */
858 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
859 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
863 /* no possible match or malformed dn */
869 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );