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_validate_func *validf = NULL;
246 slap_syntax_transform_func *transf = NULL;
248 struct berval bv = { 0, NULL };
253 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
254 const char *text = NULL;
256 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
257 if ( rc != LDAP_SUCCESS ) {
258 return LDAP_INVALID_SYNTAX;
261 ava->la_private = ( void * )ad;
266 * Replace attr oid/name with the canonical name
268 ava->la_attr = ad->ad_cname;
270 if( ava->la_flags & LDAP_AVA_BINARY ) {
271 /* AVA is binary encoded, don't muck with it */
275 } else if( flags & SLAP_LDAPDN_PRETTY ) {
277 transf = ad->ad_type->sat_syntax->ssyn_pretty;
280 validf = ad->ad_type->sat_syntax->ssyn_validate;
281 transf = ad->ad_type->sat_syntax->ssyn_normalize;
282 mr = ad->ad_type->sat_equality;
286 /* validate value before normalization */
287 rc = ( *validf )( ad->ad_type->sat_syntax,
290 : (struct berval *) &slap_empty_bv );
292 if ( rc != LDAP_SUCCESS ) {
293 return LDAP_INVALID_SYNTAX;
299 * transform value by normalize/pretty function
300 * if value is empty, use empty_bv
302 rc = ( *transf )( ad->ad_type->sat_syntax,
305 : (struct berval *) &slap_empty_bv,
308 if ( rc != LDAP_SUCCESS ) {
309 return LDAP_INVALID_SYNTAX;
313 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
316 if ( UTF8bvnormalize( &bv, &bv,
317 LDAP_UTF8_CASEFOLD ) == NULL ) {
318 return LDAP_INVALID_SYNTAX;
324 free( ava->la_value.bv_val );
328 if( do_sort ) AVA_Sort( rdn, iAVA );
336 * dn normalize routine
342 struct berval **normalized )
347 assert( normalized && *normalized == NULL );
349 out = ch_malloc( sizeof( struct berval ) );
350 rc = dnNormalize2( syntax, val, out );
351 if ( rc != LDAP_SUCCESS )
367 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
369 if ( val->bv_len != 0 ) {
374 * Go to structural representation
376 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
377 if ( rc != LDAP_SUCCESS ) {
378 return LDAP_INVALID_SYNTAX;
381 assert( strlen( val->bv_val ) == val->bv_len );
384 * Schema-aware rewrite
386 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
388 return LDAP_INVALID_SYNTAX;
392 * Back to string representation
394 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
398 if ( rc != LDAP_SUCCESS ) {
399 return LDAP_INVALID_SYNTAX;
402 ber_dupbv( out, val );
405 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
411 * dn "pretty"ing routine
417 struct berval **pretty)
422 assert( pretty && *pretty == NULL );
424 out = ch_malloc( sizeof( struct berval ) );
425 rc = dnPretty2( syntax, val, out );
426 if ( rc != LDAP_SUCCESS )
442 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
444 if ( val->bv_len == 0 ) {
445 ber_dupbv( out, val );
447 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
448 return LDAP_INVALID_SYNTAX;
454 /* FIXME: should be liberal in what we accept */
455 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
456 if ( rc != LDAP_SUCCESS ) {
457 return LDAP_INVALID_SYNTAX;
460 assert( strlen( val->bv_val ) == val->bv_len );
463 * Schema-aware rewrite
465 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
467 return LDAP_INVALID_SYNTAX;
470 /* FIXME: not sure why the default isn't pretty */
471 /* RE: the default is the form that is used as
472 * an internal representation; the pretty form
474 rc = ldap_dn2bv( dn, out,
475 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
479 if ( rc != LDAP_SUCCESS ) {
480 return LDAP_INVALID_SYNTAX;
484 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
490 * Combination of both dnPretty and dnNormalize
496 struct berval *pretty,
497 struct berval *normal)
499 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
505 if ( val->bv_len == 0 ) {
506 ber_dupbv( pretty, val );
507 ber_dupbv( normal, val );
509 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
511 return LDAP_INVALID_SYNTAX;
517 pretty->bv_val = NULL;
518 normal->bv_val = NULL;
522 /* FIXME: should be liberal in what we accept */
523 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
524 if ( rc != LDAP_SUCCESS ) {
525 return LDAP_INVALID_SYNTAX;
528 assert( strlen( val->bv_val ) == val->bv_len );
531 * Schema-aware rewrite
533 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
535 return LDAP_INVALID_SYNTAX;
538 rc = ldap_dn2bv( dn, pretty,
539 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
541 if ( rc != LDAP_SUCCESS ) {
543 return LDAP_INVALID_SYNTAX;
546 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
548 free( pretty->bv_val );
549 pretty->bv_val = NULL;
551 return LDAP_INVALID_SYNTAX;
554 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
557 if ( rc != LDAP_SUCCESS ) {
558 free( pretty->bv_val );
559 pretty->bv_val = NULL;
561 return LDAP_INVALID_SYNTAX;
565 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
566 pretty->bv_val, normal->bv_val, 0 );
580 struct berval *value,
581 void *assertedValue )
584 struct berval *asserted = (struct berval *) assertedValue;
588 assert( assertedValue );
590 match = value->bv_len - asserted->bv_len;
593 match = memcmp( value->bv_val, asserted->bv_val,
598 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
599 "dnMatch: %d\n %s\n %s\n", match,
600 value->bv_val, asserted->bv_val ));
602 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
603 match, value->bv_val, asserted->bv_val );
607 return( LDAP_SUCCESS );
611 * dnParent - dn's parent, in-place
613 * note: the incoming dn is assumed to be normalized/prettyfied,
614 * so that escaped rdn/ava separators are in '\'+hexpair form
623 p = strchr( dn->bv_val, ',' );
627 *pdn = slap_empty_bv;
631 assert( DN_SEPARATOR( p[ 0 ] ) );
634 assert( ATTR_LEADCHAR( p[ 0 ] ) );
636 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
653 if( dn->bv_len == 0 ) {
657 rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
658 if ( rc != LDAP_SUCCESS ) {
662 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
663 ldap_rdnfree( tmpRDN );
664 if ( rc != LDAP_SUCCESS ) {
672 * We can assume the input is a prettied or normalized DN
677 struct berval *dn_in )
683 if ( dn_in == NULL ) {
687 if ( !dn_in->bv_len ) {
691 if ( be != NULL && be_issuffix( be, dn_in ) ) {
695 p = strchr( dn_in->bv_val, ',' );
697 return p ? p - dn_in->bv_val : dn_in->bv_len;
703 * LDAP_SUCCESS if rdn is a legal rdn;
704 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
707 rdnValidate( struct berval *rdn )
711 * input is a pretty or normalized DN
712 * hence, we can just search for ','
714 if( rdn == NULL || rdn->bv_len == 0 ||
715 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
717 return LDAP_INVALID_SYNTAX;
720 return strchr( rdn->bv_val, ',' ) == NULL
721 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
724 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
731 if ( rdn == NULL || rdn == '\0' ) {
738 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
739 if ( rc != LDAP_SUCCESS ) {
746 if ( p[ 0 ] != '\0' ) {
751 * Schema-aware validate
753 if ( rc == LDAP_SUCCESS ) {
754 rc = LDAPDN_validate( DN );
759 * Must validate (there's a repeated parsing ...)
761 return ( rc == LDAP_SUCCESS );
768 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
771 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
775 build_new_dn( struct berval * new_dn,
776 struct berval * parent_dn,
777 struct berval * newrdn )
781 if ( parent_dn == NULL ) {
782 ber_dupbv( new_dn, newrdn );
786 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
787 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
789 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
791 strcpy( ptr, parent_dn->bv_val );
796 * dnIsSuffix - tells whether suffix is a suffix of dn.
797 * Both dn and suffix must be normalized.
801 const struct berval *dn,
802 const struct berval *suffix )
804 int d = dn->bv_len - suffix->bv_len;
809 /* empty suffix matches any dn */
810 if ( suffix->bv_len == 0 ) {
814 /* suffix longer than dn */
819 /* no rdn separator or escaped rdn separator */
820 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
824 /* no possible match or malformed dn */
830 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );