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 );
560 * dnParent - dn's parent, in-place
562 * note: the incoming dn is assumed to be normalized/prettyfied,
563 * so that escaped rdn/ava separators are in '\'+hexpair form
572 p = strchr( dn, ',' );
580 assert( DN_SEPARATOR( p[ 0 ] ) );
583 assert( ATTR_LEADCHAR( p[ 0 ] ) );
589 #ifdef SLAP_DN_MIGRATION
591 * these routines are provided for migration purposes only!
592 * dn_normalize is deprecated in favor of dnNormalize
593 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
595 * other routines are likewise deprecated but may not yet have
596 * replacement functions.
600 * dn_normalize - put dn into a canonical form suitable for storing
601 * in a hash database. this involves normalizing the case as well as
602 * the format. the dn is normalized in place as well as returned if valid.
603 * Deprecated in favor of dnNormalize()
606 dn_normalize( char *dn )
609 struct berval *normalized = NULL;
612 if ( dn == NULL || dn[0] == '\0' ) {
617 val.bv_len = strlen( dn );
619 rc = dnNormalize( NULL, &val, &normalized );
620 if ( rc != LDAP_SUCCESS ) {
624 if ( val.bv_len < normalized->bv_len ) {
625 ber_bvfree( normalized );
629 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
630 ber_bvfree( normalized );
636 * dn_parent - return the dn's parent, in-place
637 * FIXME: should be replaced by dnParent()
650 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
654 if ( dn[ 0 ] == '\0' ) {
658 if ( be != NULL && be_issuffix( be, dn ) ) {
662 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
666 return ( char * )pdn;
668 #endif /* SLAP_DN_MIGRATION */
683 if( dn->bv_len == 0 ) {
687 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
688 if ( rc != LDAP_SUCCESS ) {
692 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
693 ldap_rdnfree( tmpRDN );
694 if ( rc != LDAP_SUCCESS ) {
702 * FIXME: should be replaced by dnExtractRdn()
707 struct berval *dn_in )
714 if ( dn_in == NULL ) {
718 if ( !dn_in->bv_len ) {
722 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
726 rc = ldap_str2rdn( dn_in->bv_val, NULL, (char **)&p,
727 LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
728 if ( rc != LDAP_SUCCESS ) {
732 return p - dn_in->bv_val;
738 * LDAP_SUCCESS if rdn is a legal rdn;
739 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
742 rdnValidate( struct berval *rdn )
746 * input is a pretty or normalized DN
747 * hence, we can just search for ','
749 if( rdn == NULL || rdn->bv_len == 0 ) {
750 return LDAP_INVALID_SYNTAX;
753 return strchr( rdn->bv_val, ',' ) == NULL
754 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
757 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
764 if ( rdn == NULL || rdn == '\0' ) {
771 rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
772 if ( rc != LDAP_SUCCESS ) {
779 if ( p[ 0 ] != '\0' ) {
784 * Schema-aware validate
786 if ( rc == LDAP_SUCCESS ) {
787 rc = LDAPDN_validate( DN );
792 * Must validate (there's a repeated parsing ...)
794 return ( rc == LDAP_SUCCESS );
801 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
804 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
808 build_new_dn( struct berval * new_dn,
809 struct berval * parent_dn,
810 struct berval * newrdn )
814 if ( parent_dn == NULL ) {
815 ber_dupbv( new_dn, newrdn );
819 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
820 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
822 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
824 strcpy( ptr, parent_dn->bv_val );
829 * dnIsSuffix - tells whether suffix is a suffix of dn.
830 * Both dn and suffix must be normalized.
834 const struct berval *dn,
835 const struct berval *suffix )
837 int d = dn->bv_len - suffix->bv_len;
842 /* empty suffix matches any dn */
843 if ( suffix->bv_len == 0 ) {
847 /* suffix longer than dn */
852 /* no rdn separator or escaped rdn separator */
853 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
854 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
858 /* no possible match or malformed dn */
864 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );