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
26 * The DN syntax-related functions take advantage of the dn representation
27 * handling functions ldap_str2dn/ldap_dn2str. The latter are not schema-
28 * aware, so the attributes and their values need be validated (and possibly
29 * normalized). In the current implementation the required validation/nor-
30 * malization/"pretty"ing are done on newly created DN structural represen-
31 * tations; however the idea is to move towards DN handling in structural
32 * representation instead of the current string representation. To this
33 * purpose, we need to do only the required operations and keep track of
34 * what has been done to minimize their impact on performances.
36 * Developers are strongly encouraged to use this feature, to speed-up
40 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
43 * In-place, schema-aware validation of the
44 * structural representation of a distinguished name.
47 LDAPDN_validate( LDAPDN *dn )
54 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
55 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
60 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
61 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
62 AttributeDescription *ad;
63 slap_syntax_validate_func *validate = NULL;
67 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
68 const char *text = NULL;
70 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
71 if ( rc != LDAP_SUCCESS ) {
72 return LDAP_INVALID_SYNTAX;
75 ava->la_private = ( void * )ad;
79 * Replace attr oid/name with the canonical name
81 ava->la_attr = ad->ad_cname;
83 validate = ad->ad_type->sat_syntax->ssyn_validate;
87 * validate value by validate function
89 rc = ( *validate )( ad->ad_type->sat_syntax,
92 if ( rc != LDAP_SUCCESS ) {
93 return LDAP_INVALID_SYNTAX;
103 * dn validate routine
115 if ( in->bv_len == 0 ) {
116 return( LDAP_SUCCESS );
119 rc = ldap_str2dn( in->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
122 * Schema-aware validate
124 if ( rc == LDAP_SUCCESS ) {
125 rc = LDAPDN_validate( dn );
129 if ( rc != LDAP_SUCCESS ) {
130 return( LDAP_INVALID_SYNTAX );
133 return( LDAP_SUCCESS );
137 * AVA sorting inside a RDN
139 * rule: sort attributeTypes in alphabetical order; in case of multiple
140 * occurrences of the same attributeType, sort values in byte order
141 * (use memcmp, which implies alphabetical order in case of IA5 value;
142 * this should guarantee the repeatability of the operation).
144 * Note: the sorting can be slightly improved by sorting first
145 * by attribute type length, then by alphabetical order.
147 * uses a linear search; should be fine since the number of AVAs in
148 * a RDN should be limited.
151 AVA_Sort( LDAPRDN *rdn, int iAVA )
154 LDAPAVA *ava_in = rdn[ 0 ][ iAVA ];
159 for ( i = 0; i < iAVA; i++ ) {
160 LDAPAVA *ava = rdn[ 0 ][ i ];
165 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
174 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
176 v = memcmp( ava_in->la_value.bv_val,
177 ava->la_value.bv_val,
178 d <= 0 ? ava_in->la_value.bv_len
179 : ava->la_value.bv_len );
181 if ( v == 0 && d != 0 ) {
200 a = strcmp( ava_in->la_attr.bv_val,
201 ava->la_attr.bv_val );
207 for ( j = iAVA; j > i; j-- ) {
208 rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
210 rdn[ 0 ][ i ] = ava_in;
217 * In-place, schema-aware normalization / "pretty"ing of the
218 * structural representation of a distinguished name.
221 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
228 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
229 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
234 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
235 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
236 AttributeDescription *ad;
237 slap_syntax_transform_func *transf = NULL;
239 struct berval bv = { 0, NULL };
244 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
245 const char *text = NULL;
247 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
248 if ( rc != LDAP_SUCCESS ) {
249 return LDAP_INVALID_SYNTAX;
252 ava->la_private = ( void * )ad;
257 * Replace attr oid/name with the canonical name
259 ava->la_attr = ad->ad_cname;
261 if( flags & SLAP_LDAPDN_PRETTY ) {
262 transf = ad->ad_type->sat_syntax->ssyn_pretty;
265 transf = ad->ad_type->sat_syntax->ssyn_normalize;
266 mr = ad->ad_type->sat_equality;
271 * transform value by normalize/pretty function
273 rc = ( *transf )( ad->ad_type->sat_syntax,
274 &ava->la_value, &bv );
276 if ( rc != LDAP_SUCCESS ) {
277 return LDAP_INVALID_SYNTAX;
281 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
284 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
285 : &ava->la_value, LDAP_UTF8_CASEFOLD ),
291 free( ava->la_value.bv_val );
295 if( do_sort ) AVA_Sort( rdn, iAVA );
303 * dn normalize routine
309 struct berval **normalized )
314 assert( normalized && *normalized == NULL );
316 out = ch_malloc( sizeof( struct berval ) );
317 rc = dnNormalize2( syntax, val, out );
318 if ( rc != LDAP_SUCCESS )
334 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
336 if ( val->bv_len != 0 ) {
341 * Go to structural representation
343 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
344 if ( rc != LDAP_SUCCESS ) {
345 return LDAP_INVALID_SYNTAX;
349 * Schema-aware rewrite
351 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
353 return LDAP_INVALID_SYNTAX;
357 * Back to string representation
359 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
363 if ( rc != LDAP_SUCCESS ) {
364 return LDAP_INVALID_SYNTAX;
367 ber_dupbv( out, val );
370 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
376 * dn "pretty"ing routine
382 struct berval **pretty)
387 assert( pretty && *pretty == NULL );
389 out = ch_malloc( sizeof( struct berval ) );
390 rc = dnPretty2( syntax, val, out );
391 if ( rc != LDAP_SUCCESS )
407 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
409 if ( val->bv_len != 0 ) {
413 /* FIXME: should be liberal in what we accept */
414 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
415 if ( rc != LDAP_SUCCESS ) {
416 return LDAP_INVALID_SYNTAX;
420 * Schema-aware rewrite
422 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
424 return LDAP_INVALID_SYNTAX;
427 /* FIXME: not sure why the default isn't pretty */
428 /* RE: the default is the form that is used as
429 * an internal representation; the pretty form
431 rc = ldap_dn2bv( dn, out,
432 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
436 if ( rc != LDAP_SUCCESS ) {
437 return LDAP_INVALID_SYNTAX;
440 ber_dupbv( out, val );
443 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
449 * Combination of both dnPretty and dnNormalize
455 struct berval *pretty,
456 struct berval *normal)
458 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
464 if ( val->bv_len != 0 ) {
468 pretty->bv_val = NULL;
469 normal->bv_val = NULL;
473 /* FIXME: should be liberal in what we accept */
474 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
475 if ( rc != LDAP_SUCCESS ) {
476 return LDAP_INVALID_SYNTAX;
480 * Schema-aware rewrite
482 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
484 return LDAP_INVALID_SYNTAX;
487 rc = ldap_dn2bv( dn, pretty,
488 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
490 if ( rc != LDAP_SUCCESS ) {
492 return LDAP_INVALID_SYNTAX;
495 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
497 free( pretty->bv_val );
498 pretty->bv_val = NULL;
500 return LDAP_INVALID_SYNTAX;
503 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
506 if ( rc != LDAP_SUCCESS ) {
507 free( pretty->bv_val );
508 pretty->bv_val = NULL;
510 return LDAP_INVALID_SYNTAX;
513 ber_dupbv( pretty, val );
514 ber_dupbv( normal, val );
517 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
518 pretty->bv_val, normal->bv_val, 0 );
532 struct berval *value,
533 void *assertedValue )
536 struct berval *asserted = (struct berval *) assertedValue;
540 assert( assertedValue );
542 match = value->bv_len - asserted->bv_len;
545 match = strcmp( value->bv_val, asserted->bv_val );
549 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
550 "dnMatch: %d\n %s\n %s\n", match,
551 value->bv_val, asserted->bv_val ));
553 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
554 match, value->bv_val, asserted->bv_val );
558 return( LDAP_SUCCESS );
562 * dnParent - dn's parent, in-place
564 * note: the incoming dn is assumed to be normalized/prettyfied,
565 * so that escaped rdn/ava separators are in '\'+hexpair form
574 p = strchr( dn->bv_val, ',' );
578 *pdn = slap_empty_bv;
582 assert( DN_SEPARATOR( p[ 0 ] ) );
585 assert( ATTR_LEADCHAR( p[ 0 ] ) );
587 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
592 #ifdef SLAP_DN_MIGRATION
594 * these routines are provided for migration purposes only!
595 * dn_normalize is deprecated in favor of dnNormalize
596 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
598 * other routines are likewise deprecated but may not yet have
599 * replacement functions.
603 * dn_normalize - put dn into a canonical form suitable for storing
604 * in a hash database. this involves normalizing the case as well as
605 * the format. the dn is normalized in place as well as returned if valid.
606 * Deprecated in favor of dnNormalize()
609 dn_normalize( char *dn )
612 struct berval *normalized = NULL;
615 if ( dn == NULL || dn[0] == '\0' ) {
620 val.bv_len = strlen( dn );
622 rc = dnNormalize( NULL, &val, &normalized );
623 if ( rc != LDAP_SUCCESS ) {
627 if ( val.bv_len < normalized->bv_len ) {
628 ber_bvfree( normalized );
632 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
633 ber_bvfree( normalized );
638 #endif /* SLAP_DN_MIGRATION */
653 if( dn->bv_len == 0 ) {
657 rc = ldap_str2rdn( dn->bv_val, &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 return LDAP_INVALID_SYNTAX;
718 return strchr( rdn->bv_val, ',' ) == NULL
719 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
722 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
729 if ( rdn == NULL || rdn == '\0' ) {
736 rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
737 if ( rc != LDAP_SUCCESS ) {
744 if ( p[ 0 ] != '\0' ) {
749 * Schema-aware validate
751 if ( rc == LDAP_SUCCESS ) {
752 rc = LDAPDN_validate( DN );
757 * Must validate (there's a repeated parsing ...)
759 return ( rc == LDAP_SUCCESS );
766 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
769 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
773 build_new_dn( struct berval * new_dn,
774 struct berval * parent_dn,
775 struct berval * newrdn )
779 if ( parent_dn == NULL ) {
780 ber_dupbv( new_dn, newrdn );
784 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
785 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
787 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
789 strcpy( ptr, parent_dn->bv_val );
794 * dnIsSuffix - tells whether suffix is a suffix of dn.
795 * Both dn and suffix must be normalized.
799 const struct berval *dn,
800 const struct berval *suffix )
802 int d = dn->bv_len - suffix->bv_len;
807 /* empty suffix matches any dn */
808 if ( suffix->bv_len == 0 ) {
812 /* suffix longer than dn */
817 /* no rdn separator or escaped rdn separator */
818 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
822 /* no possible match or malformed dn */
828 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );