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 const struct berval slap_empty_bv = { 0, "" };
23 #define SLAP_LDAPDN_PRETTY 0x1
25 #define SLAP_LDAPDN_MAXLEN 8192
28 * The DN syntax-related functions take advantage of the dn representation
29 * handling functions ldap_str2dn/ldap_dn2str. The latter are not schema-
30 * aware, so the attributes and their values need be validated (and possibly
31 * normalized). In the current implementation the required validation/nor-
32 * malization/"pretty"ing are done on newly created DN structural represen-
33 * tations; however the idea is to move towards DN handling in structural
34 * representation instead of the current string representation. To this
35 * purpose, we need to do only the required operations and keep track of
36 * what has been done to minimize their impact on performances.
38 * Developers are strongly encouraged to use this feature, to speed-up
42 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
45 * In-place, schema-aware validation of the
46 * structural representation of a distinguished name.
49 LDAPDN_validate( LDAPDN *dn )
56 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
57 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
62 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
63 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
64 AttributeDescription *ad;
65 slap_syntax_validate_func *validate = NULL;
69 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
70 const char *text = NULL;
72 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
73 if ( rc != LDAP_SUCCESS ) {
74 return LDAP_INVALID_SYNTAX;
77 ava->la_private = ( void * )ad;
81 * Replace attr oid/name with the canonical name
83 ava->la_attr = ad->ad_cname;
85 validate = ad->ad_type->sat_syntax->ssyn_validate;
89 * validate value by validate function
91 rc = ( *validate )( ad->ad_type->sat_syntax,
94 if ( rc != LDAP_SUCCESS ) {
95 return LDAP_INVALID_SYNTAX;
105 * dn validate routine
117 if ( in->bv_len == 0 ) {
120 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
121 return LDAP_INVALID_SYNTAX;
124 rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
125 if ( rc != LDAP_SUCCESS ) {
126 return LDAP_INVALID_SYNTAX;
129 assert( strlen( in->bv_val ) == in->bv_len );
132 * Schema-aware validate
134 rc = LDAPDN_validate( dn );
137 if ( rc != LDAP_SUCCESS ) {
138 return LDAP_INVALID_SYNTAX;
145 * AVA sorting inside a RDN
147 * rule: sort attributeTypes in alphabetical order; in case of multiple
148 * occurrences of the same attributeType, sort values in byte order
149 * (use memcmp, which implies alphabetical order in case of IA5 value;
150 * this should guarantee the repeatability of the operation).
152 * Note: the sorting can be slightly improved by sorting first
153 * by attribute type length, then by alphabetical order.
155 * uses a linear search; should be fine since the number of AVAs in
156 * a RDN should be limited.
159 AVA_Sort( LDAPRDN *rdn, int iAVA )
162 LDAPAVA *ava_in = rdn[ 0 ][ iAVA ];
167 for ( i = 0; i < iAVA; i++ ) {
168 LDAPAVA *ava = rdn[ 0 ][ i ];
173 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
182 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
184 v = memcmp( ava_in->la_value.bv_val,
185 ava->la_value.bv_val,
186 d <= 0 ? ava_in->la_value.bv_len
187 : ava->la_value.bv_len );
189 if ( v == 0 && d != 0 ) {
208 a = strcmp( ava_in->la_attr.bv_val,
209 ava->la_attr.bv_val );
215 for ( j = iAVA; j > i; j-- ) {
216 rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
218 rdn[ 0 ][ i ] = ava_in;
225 * In-place, schema-aware normalization / "pretty"ing of the
226 * structural representation of a distinguished name.
229 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
236 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
237 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
242 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
243 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
244 AttributeDescription *ad;
245 slap_syntax_transform_func *transf = NULL;
247 struct berval bv = { 0, NULL };
252 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
253 const char *text = NULL;
255 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
256 if ( rc != LDAP_SUCCESS ) {
257 return LDAP_INVALID_SYNTAX;
260 ava->la_private = ( void * )ad;
265 * Replace attr oid/name with the canonical name
267 ava->la_attr = ad->ad_cname;
269 if( ava->la_flags & LDAP_AVA_BINARY ) {
270 /* AVA is binary encoded, don't muck with it */
274 } else if( flags & SLAP_LDAPDN_PRETTY ) {
275 transf = ad->ad_type->sat_syntax->ssyn_pretty;
278 transf = ad->ad_type->sat_syntax->ssyn_normalize;
279 mr = ad->ad_type->sat_equality;
284 * transform value by normalize/pretty function
285 * if value is empty, use empty_bv
287 rc = ( *transf )( ad->ad_type->sat_syntax,
290 : (struct berval *) &slap_empty_bv,
293 if ( rc != LDAP_SUCCESS ) {
294 return LDAP_INVALID_SYNTAX;
298 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
301 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
302 : &ava->la_value, LDAP_UTF8_CASEFOLD ),
308 free( ava->la_value.bv_val );
312 if( do_sort ) AVA_Sort( rdn, iAVA );
320 * dn normalize routine
326 struct berval **normalized )
331 assert( normalized && *normalized == NULL );
333 out = ch_malloc( sizeof( struct berval ) );
334 rc = dnNormalize2( syntax, val, out );
335 if ( rc != LDAP_SUCCESS )
351 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
353 if ( val->bv_len != 0 ) {
358 * Go to structural representation
360 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
361 if ( rc != LDAP_SUCCESS ) {
362 return LDAP_INVALID_SYNTAX;
365 assert( strlen( val->bv_val ) == val->bv_len );
368 * Schema-aware rewrite
370 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
372 return LDAP_INVALID_SYNTAX;
376 * Back to string representation
378 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
382 if ( rc != LDAP_SUCCESS ) {
383 return LDAP_INVALID_SYNTAX;
386 ber_dupbv( out, val );
389 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
395 * dn "pretty"ing routine
401 struct berval **pretty)
406 assert( pretty && *pretty == NULL );
408 out = ch_malloc( sizeof( struct berval ) );
409 rc = dnPretty2( syntax, val, out );
410 if ( rc != LDAP_SUCCESS )
426 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
428 if ( val->bv_len == 0 ) {
429 ber_dupbv( out, val );
431 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
432 return LDAP_INVALID_SYNTAX;
438 /* FIXME: should be liberal in what we accept */
439 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
440 if ( rc != LDAP_SUCCESS ) {
441 return LDAP_INVALID_SYNTAX;
444 assert( strlen( val->bv_val ) == val->bv_len );
447 * Schema-aware rewrite
449 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
451 return LDAP_INVALID_SYNTAX;
454 /* FIXME: not sure why the default isn't pretty */
455 /* RE: the default is the form that is used as
456 * an internal representation; the pretty form
458 rc = ldap_dn2bv( dn, out,
459 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
463 if ( rc != LDAP_SUCCESS ) {
464 return LDAP_INVALID_SYNTAX;
468 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
474 * Combination of both dnPretty and dnNormalize
480 struct berval *pretty,
481 struct berval *normal)
483 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
489 if ( val->bv_len == 0 ) {
490 ber_dupbv( pretty, val );
491 ber_dupbv( normal, val );
493 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
495 return LDAP_INVALID_SYNTAX;
501 pretty->bv_val = NULL;
502 normal->bv_val = NULL;
506 /* FIXME: should be liberal in what we accept */
507 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
508 if ( rc != LDAP_SUCCESS ) {
509 return LDAP_INVALID_SYNTAX;
512 assert( strlen( val->bv_val ) == val->bv_len );
515 * Schema-aware rewrite
517 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
519 return LDAP_INVALID_SYNTAX;
522 rc = ldap_dn2bv( dn, pretty,
523 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
525 if ( rc != LDAP_SUCCESS ) {
527 return LDAP_INVALID_SYNTAX;
530 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
532 free( pretty->bv_val );
533 pretty->bv_val = NULL;
535 return LDAP_INVALID_SYNTAX;
538 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
541 if ( rc != LDAP_SUCCESS ) {
542 free( pretty->bv_val );
543 pretty->bv_val = NULL;
545 return LDAP_INVALID_SYNTAX;
549 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
550 pretty->bv_val, normal->bv_val, 0 );
564 struct berval *value,
565 void *assertedValue )
568 struct berval *asserted = (struct berval *) assertedValue;
572 assert( assertedValue );
574 match = value->bv_len - asserted->bv_len;
577 match = memcmp( value->bv_val, asserted->bv_val,
582 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
583 "dnMatch: %d\n %s\n %s\n", match,
584 value->bv_val, asserted->bv_val ));
586 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
587 match, value->bv_val, asserted->bv_val );
591 return( LDAP_SUCCESS );
595 * dnParent - dn's parent, in-place
597 * note: the incoming dn is assumed to be normalized/prettyfied,
598 * so that escaped rdn/ava separators are in '\'+hexpair form
607 p = strchr( dn->bv_val, ',' );
611 *pdn = slap_empty_bv;
615 assert( DN_SEPARATOR( p[ 0 ] ) );
618 assert( ATTR_LEADCHAR( p[ 0 ] ) );
620 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
637 if( dn->bv_len == 0 ) {
641 rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
642 if ( rc != LDAP_SUCCESS ) {
646 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
647 ldap_rdnfree( tmpRDN );
648 if ( rc != LDAP_SUCCESS ) {
656 * We can assume the input is a prettied or normalized DN
661 struct berval *dn_in )
667 if ( dn_in == NULL ) {
671 if ( !dn_in->bv_len ) {
675 if ( be != NULL && be_issuffix( be, dn_in ) ) {
679 p = strchr( dn_in->bv_val, ',' );
681 return p ? p - dn_in->bv_val : dn_in->bv_len;
687 * LDAP_SUCCESS if rdn is a legal rdn;
688 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
691 rdnValidate( struct berval *rdn )
695 * input is a pretty or normalized DN
696 * hence, we can just search for ','
698 if( rdn == NULL || rdn->bv_len == 0 ||
699 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
701 return LDAP_INVALID_SYNTAX;
704 return strchr( rdn->bv_val, ',' ) == NULL
705 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
708 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
715 if ( rdn == NULL || rdn == '\0' ) {
722 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
723 if ( rc != LDAP_SUCCESS ) {
730 if ( p[ 0 ] != '\0' ) {
735 * Schema-aware validate
737 if ( rc == LDAP_SUCCESS ) {
738 rc = LDAPDN_validate( DN );
743 * Must validate (there's a repeated parsing ...)
745 return ( rc == LDAP_SUCCESS );
752 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
755 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
759 build_new_dn( struct berval * new_dn,
760 struct berval * parent_dn,
761 struct berval * newrdn )
765 if ( parent_dn == NULL ) {
766 ber_dupbv( new_dn, newrdn );
770 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
771 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
773 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
775 strcpy( ptr, parent_dn->bv_val );
780 * dnIsSuffix - tells whether suffix is a suffix of dn.
781 * Both dn and suffix must be normalized.
785 const struct berval *dn,
786 const struct berval *suffix )
788 int d = dn->bv_len - suffix->bv_len;
793 /* empty suffix matches any dn */
794 if ( suffix->bv_len == 0 ) {
798 /* suffix longer than dn */
803 /* no rdn separator or escaped rdn separator */
804 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
808 /* no possible match or malformed dn */
814 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );