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 );
135 if ( rc == LDAP_SUCCESS ) {
139 return LDAP_INVALID_SYNTAX;
143 * AVA sorting inside a RDN
145 * rule: sort attributeTypes in alphabetical order; in case of multiple
146 * occurrences of the same attributeType, sort values in byte order
147 * (use memcmp, which implies alphabetical order in case of IA5 value;
148 * this should guarantee the repeatability of the operation).
150 * Note: the sorting can be slightly improved by sorting first
151 * by attribute type length, then by alphabetical order.
153 * uses a linear search; should be fine since the number of AVAs in
154 * a RDN should be limited.
157 AVA_Sort( LDAPRDN *rdn, int iAVA )
160 LDAPAVA *ava_in = rdn[ 0 ][ iAVA ];
165 for ( i = 0; i < iAVA; i++ ) {
166 LDAPAVA *ava = rdn[ 0 ][ i ];
171 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
180 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
182 v = memcmp( ava_in->la_value.bv_val,
183 ava->la_value.bv_val,
184 d <= 0 ? ava_in->la_value.bv_len
185 : ava->la_value.bv_len );
187 if ( v == 0 && d != 0 ) {
206 a = strcmp( ava_in->la_attr.bv_val,
207 ava->la_attr.bv_val );
213 for ( j = iAVA; j > i; j-- ) {
214 rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
216 rdn[ 0 ][ i ] = ava_in;
223 * In-place, schema-aware normalization / "pretty"ing of the
224 * structural representation of a distinguished name.
227 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
234 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
235 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
240 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
241 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
242 AttributeDescription *ad;
243 slap_syntax_transform_func *transf = NULL;
245 struct berval bv = { 0, NULL };
250 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
251 const char *text = NULL;
253 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
254 if ( rc != LDAP_SUCCESS ) {
255 return LDAP_INVALID_SYNTAX;
258 ava->la_private = ( void * )ad;
263 * Replace attr oid/name with the canonical name
265 ava->la_attr = ad->ad_cname;
267 if( ava->la_flags & LDAP_AVA_BINARY ) {
268 /* AVA is binary encoded, don't muck with it */
272 } else if( flags & SLAP_LDAPDN_PRETTY ) {
273 transf = ad->ad_type->sat_syntax->ssyn_pretty;
276 transf = ad->ad_type->sat_syntax->ssyn_normalize;
277 mr = ad->ad_type->sat_equality;
282 * transform value by normalize/pretty function
283 * if value is empty, use empty_bv
285 rc = ( *transf )( ad->ad_type->sat_syntax,
288 : (struct berval *) &slap_empty_bv,
291 if ( rc != LDAP_SUCCESS ) {
292 return LDAP_INVALID_SYNTAX;
296 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
299 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
300 : &ava->la_value, LDAP_UTF8_CASEFOLD ),
306 free( ava->la_value.bv_val );
310 if( do_sort ) AVA_Sort( rdn, iAVA );
318 * dn normalize routine
324 struct berval **normalized )
329 assert( normalized && *normalized == NULL );
331 out = ch_malloc( sizeof( struct berval ) );
332 rc = dnNormalize2( syntax, val, out );
333 if ( rc != LDAP_SUCCESS )
349 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
351 if ( val->bv_len != 0 ) {
356 * Go to structural representation
358 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
359 if ( rc != LDAP_SUCCESS ) {
360 return LDAP_INVALID_SYNTAX;
363 assert( strlen( val->bv_val ) == val->bv_len );
366 * Schema-aware rewrite
368 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
370 return LDAP_INVALID_SYNTAX;
374 * Back to string representation
376 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
380 if ( rc != LDAP_SUCCESS ) {
381 return LDAP_INVALID_SYNTAX;
384 ber_dupbv( out, val );
387 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
393 * dn "pretty"ing routine
399 struct berval **pretty)
404 assert( pretty && *pretty == NULL );
406 out = ch_malloc( sizeof( struct berval ) );
407 rc = dnPretty2( syntax, val, out );
408 if ( rc != LDAP_SUCCESS )
424 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
426 if ( val->bv_len == 0 ) {
427 ber_dupbv( out, val );
429 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
430 return LDAP_INVALID_SYNTAX;
436 /* FIXME: should be liberal in what we accept */
437 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
438 if ( rc != LDAP_SUCCESS ) {
439 return LDAP_INVALID_SYNTAX;
442 assert( strlen( val->bv_val ) == val->bv_len );
445 * Schema-aware rewrite
447 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
449 return LDAP_INVALID_SYNTAX;
452 /* FIXME: not sure why the default isn't pretty */
453 /* RE: the default is the form that is used as
454 * an internal representation; the pretty form
456 rc = ldap_dn2bv( dn, out,
457 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
461 if ( rc != LDAP_SUCCESS ) {
462 return LDAP_INVALID_SYNTAX;
466 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
472 * Combination of both dnPretty and dnNormalize
478 struct berval *pretty,
479 struct berval *normal)
481 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
487 if ( val->bv_len == 0 ) {
488 ber_dupbv( pretty, val );
489 ber_dupbv( normal, val );
491 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
493 return LDAP_INVALID_SYNTAX;
499 pretty->bv_val = NULL;
500 normal->bv_val = NULL;
504 /* FIXME: should be liberal in what we accept */
505 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
506 if ( rc != LDAP_SUCCESS ) {
507 return LDAP_INVALID_SYNTAX;
510 assert( strlen( val->bv_val ) == val->bv_len );
513 * Schema-aware rewrite
515 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
517 return LDAP_INVALID_SYNTAX;
520 rc = ldap_dn2bv( dn, pretty,
521 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
523 if ( rc != LDAP_SUCCESS ) {
525 return LDAP_INVALID_SYNTAX;
528 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
530 free( pretty->bv_val );
531 pretty->bv_val = NULL;
533 return LDAP_INVALID_SYNTAX;
536 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
539 if ( rc != LDAP_SUCCESS ) {
540 free( pretty->bv_val );
541 pretty->bv_val = NULL;
543 return LDAP_INVALID_SYNTAX;
547 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
548 pretty->bv_val, normal->bv_val, 0 );
562 struct berval *value,
563 void *assertedValue )
566 struct berval *asserted = (struct berval *) assertedValue;
570 assert( assertedValue );
572 match = value->bv_len - asserted->bv_len;
575 match = memcmp( value->bv_val, asserted->bv_val,
580 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
581 "dnMatch: %d\n %s\n %s\n", match,
582 value->bv_val, asserted->bv_val ));
584 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
585 match, value->bv_val, asserted->bv_val );
589 return( LDAP_SUCCESS );
593 * dnParent - dn's parent, in-place
595 * note: the incoming dn is assumed to be normalized/prettyfied,
596 * so that escaped rdn/ava separators are in '\'+hexpair form
605 p = strchr( dn->bv_val, ',' );
609 *pdn = slap_empty_bv;
613 assert( DN_SEPARATOR( p[ 0 ] ) );
616 assert( ATTR_LEADCHAR( p[ 0 ] ) );
618 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
635 if( dn->bv_len == 0 ) {
639 rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
640 if ( rc != LDAP_SUCCESS ) {
644 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
645 ldap_rdnfree( tmpRDN );
646 if ( rc != LDAP_SUCCESS ) {
654 * We can assume the input is a prettied or normalized DN
659 struct berval *dn_in )
665 if ( dn_in == NULL ) {
669 if ( !dn_in->bv_len ) {
673 if ( be != NULL && be_issuffix( be, dn_in ) ) {
677 p = strchr( dn_in->bv_val, ',' );
679 return p ? p - dn_in->bv_val : dn_in->bv_len;
685 * LDAP_SUCCESS if rdn is a legal rdn;
686 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
689 rdnValidate( struct berval *rdn )
693 * input is a pretty or normalized DN
694 * hence, we can just search for ','
696 if( rdn == NULL || rdn->bv_len == 0 ||
697 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
699 return LDAP_INVALID_SYNTAX;
702 return strchr( rdn->bv_val, ',' ) == NULL
703 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
706 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
713 if ( rdn == NULL || rdn == '\0' ) {
720 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
721 if ( rc != LDAP_SUCCESS ) {
728 if ( p[ 0 ] != '\0' ) {
733 * Schema-aware validate
735 if ( rc == LDAP_SUCCESS ) {
736 rc = LDAPDN_validate( DN );
741 * Must validate (there's a repeated parsing ...)
743 return ( rc == LDAP_SUCCESS );
750 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
753 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
757 build_new_dn( struct berval * new_dn,
758 struct berval * parent_dn,
759 struct berval * newrdn )
763 if ( parent_dn == NULL ) {
764 ber_dupbv( new_dn, newrdn );
768 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
769 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
771 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
773 strcpy( ptr, parent_dn->bv_val );
778 * dnIsSuffix - tells whether suffix is a suffix of dn.
779 * Both dn and suffix must be normalized.
783 const struct berval *dn,
784 const struct berval *suffix )
786 int d = dn->bv_len - suffix->bv_len;
791 /* empty suffix matches any dn */
792 if ( suffix->bv_len == 0 ) {
796 /* suffix longer than dn */
801 /* no rdn separator or escaped rdn separator */
802 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
806 /* no possible match or malformed dn */
812 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );