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_bv2dn( in, &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( ava->la_flags & LDAP_AVA_BINARY ) {
262 /* AVA is binary encoded, don't muck with it */
266 } else if( flags & SLAP_LDAPDN_PRETTY ) {
267 transf = ad->ad_type->sat_syntax->ssyn_pretty;
270 transf = ad->ad_type->sat_syntax->ssyn_normalize;
271 mr = ad->ad_type->sat_equality;
276 * transform value by normalize/pretty function
277 * if value is empty, use empty_bv
279 rc = ( *transf )( ad->ad_type->sat_syntax,
282 : (struct berval *) &slap_empty_bv,
285 if ( rc != LDAP_SUCCESS ) {
286 return LDAP_INVALID_SYNTAX;
290 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
293 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
294 : &ava->la_value, LDAP_UTF8_CASEFOLD ),
300 free( ava->la_value.bv_val );
304 if( do_sort ) AVA_Sort( rdn, iAVA );
312 * dn normalize routine
318 struct berval **normalized )
323 assert( normalized && *normalized == NULL );
325 out = ch_malloc( sizeof( struct berval ) );
326 rc = dnNormalize2( syntax, val, out );
327 if ( rc != LDAP_SUCCESS )
343 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
345 if ( val->bv_len != 0 ) {
350 * Go to structural representation
352 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
353 if ( rc != LDAP_SUCCESS ) {
354 return LDAP_INVALID_SYNTAX;
358 * Schema-aware rewrite
360 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
362 return LDAP_INVALID_SYNTAX;
366 * Back to string representation
368 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
372 if ( rc != LDAP_SUCCESS ) {
373 return LDAP_INVALID_SYNTAX;
376 ber_dupbv( out, val );
379 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
385 * dn "pretty"ing routine
391 struct berval **pretty)
396 assert( pretty && *pretty == NULL );
398 out = ch_malloc( sizeof( struct berval ) );
399 rc = dnPretty2( syntax, val, out );
400 if ( rc != LDAP_SUCCESS )
416 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
418 if ( val->bv_len != 0 ) {
422 /* FIXME: should be liberal in what we accept */
423 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
424 if ( rc != LDAP_SUCCESS ) {
425 return LDAP_INVALID_SYNTAX;
429 * Schema-aware rewrite
431 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
433 return LDAP_INVALID_SYNTAX;
436 /* FIXME: not sure why the default isn't pretty */
437 /* RE: the default is the form that is used as
438 * an internal representation; the pretty form
440 rc = ldap_dn2bv( dn, out,
441 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
445 if ( rc != LDAP_SUCCESS ) {
446 return LDAP_INVALID_SYNTAX;
449 ber_dupbv( out, val );
452 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
458 * Combination of both dnPretty and dnNormalize
464 struct berval *pretty,
465 struct berval *normal)
467 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
473 if ( val->bv_len != 0 ) {
477 pretty->bv_val = NULL;
478 normal->bv_val = NULL;
482 /* FIXME: should be liberal in what we accept */
483 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
484 if ( rc != LDAP_SUCCESS ) {
485 return LDAP_INVALID_SYNTAX;
489 * Schema-aware rewrite
491 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
493 return LDAP_INVALID_SYNTAX;
496 rc = ldap_dn2bv( dn, pretty,
497 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
499 if ( rc != LDAP_SUCCESS ) {
501 return LDAP_INVALID_SYNTAX;
504 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
506 free( pretty->bv_val );
507 pretty->bv_val = NULL;
509 return LDAP_INVALID_SYNTAX;
512 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
515 if ( rc != LDAP_SUCCESS ) {
516 free( pretty->bv_val );
517 pretty->bv_val = NULL;
519 return LDAP_INVALID_SYNTAX;
522 ber_dupbv( pretty, val );
523 ber_dupbv( normal, val );
526 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
527 pretty->bv_val, normal->bv_val, 0 );
541 struct berval *value,
542 void *assertedValue )
545 struct berval *asserted = (struct berval *) assertedValue;
549 assert( assertedValue );
551 match = value->bv_len - asserted->bv_len;
554 match = strcmp( value->bv_val, asserted->bv_val );
558 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
559 "dnMatch: %d\n %s\n %s\n", match,
560 value->bv_val, asserted->bv_val ));
562 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
563 match, value->bv_val, asserted->bv_val );
567 return( LDAP_SUCCESS );
571 * dnParent - dn's parent, in-place
573 * note: the incoming dn is assumed to be normalized/prettyfied,
574 * so that escaped rdn/ava separators are in '\'+hexpair form
583 p = strchr( dn->bv_val, ',' );
587 *pdn = slap_empty_bv;
591 assert( DN_SEPARATOR( p[ 0 ] ) );
594 assert( ATTR_LEADCHAR( p[ 0 ] ) );
596 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
613 if( dn->bv_len == 0 ) {
617 rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
618 if ( rc != LDAP_SUCCESS ) {
622 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
623 ldap_rdnfree( tmpRDN );
624 if ( rc != LDAP_SUCCESS ) {
632 * We can assume the input is a prettied or normalized DN
637 struct berval *dn_in )
643 if ( dn_in == NULL ) {
647 if ( !dn_in->bv_len ) {
651 if ( be != NULL && be_issuffix( be, dn_in ) ) {
655 p = strchr( dn_in->bv_val, ',' );
657 return p ? p - dn_in->bv_val : dn_in->bv_len;
663 * LDAP_SUCCESS if rdn is a legal rdn;
664 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
667 rdnValidate( struct berval *rdn )
671 * input is a pretty or normalized DN
672 * hence, we can just search for ','
674 if( rdn == NULL || rdn->bv_len == 0 ) {
675 return LDAP_INVALID_SYNTAX;
678 return strchr( rdn->bv_val, ',' ) == NULL
679 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
682 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
689 if ( rdn == NULL || rdn == '\0' ) {
696 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
697 if ( rc != LDAP_SUCCESS ) {
704 if ( p[ 0 ] != '\0' ) {
709 * Schema-aware validate
711 if ( rc == LDAP_SUCCESS ) {
712 rc = LDAPDN_validate( DN );
717 * Must validate (there's a repeated parsing ...)
719 return ( rc == LDAP_SUCCESS );
726 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
729 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
733 build_new_dn( struct berval * new_dn,
734 struct berval * parent_dn,
735 struct berval * newrdn )
739 if ( parent_dn == NULL ) {
740 ber_dupbv( new_dn, newrdn );
744 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
745 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
747 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
749 strcpy( ptr, parent_dn->bv_val );
754 * dnIsSuffix - tells whether suffix is a suffix of dn.
755 * Both dn and suffix must be normalized.
759 const struct berval *dn,
760 const struct berval *suffix )
762 int d = dn->bv_len - suffix->bv_len;
767 /* empty suffix matches any dn */
768 if ( suffix->bv_len == 0 ) {
772 /* suffix longer than dn */
777 /* no rdn separator or escaped rdn separator */
778 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
782 /* no possible match or malformed dn */
788 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );