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 /* FIXME: str2dn should take a bv and handle this */
120 if( strlen( val->bv_val ) != val->bv_len ) {
121 return LDAP_INVALID_SYNTAX;
124 rc = ldap_str2dn( in->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
127 * Schema-aware validate
129 if ( rc == LDAP_SUCCESS ) {
130 rc = LDAPDN_validate( dn );
134 if ( rc != LDAP_SUCCESS ) {
135 return( LDAP_INVALID_SYNTAX );
138 return( LDAP_SUCCESS );
142 * AVA sorting inside a RDN
144 * rule: sort attributeTypes in alphabetical order; in case of multiple
145 * occurrences of the same attributeType, sort values in byte order
146 * (use memcmp, which implies alphabetical order in case of IA5 value;
147 * this should guarantee the repeatability of the operation).
149 * Note: the sorting can be slightly improved by sorting first
150 * by attribute type length, then by alphabetical order.
152 * uses a linear search; should be fine since the number of AVAs in
153 * a RDN should be limited.
156 AVA_Sort( LDAPRDN *rdn, int iAVA )
159 LDAPAVA *ava_in = rdn[ 0 ][ iAVA ];
164 for ( i = 0; i < iAVA; i++ ) {
165 LDAPAVA *ava = rdn[ 0 ][ i ];
170 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
179 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
181 v = memcmp( ava_in->la_value.bv_val,
182 ava->la_value.bv_val,
183 d <= 0 ? ava_in->la_value.bv_len
184 : ava->la_value.bv_len );
186 if ( v == 0 && d != 0 ) {
205 a = strcmp( ava_in->la_attr.bv_val,
206 ava->la_attr.bv_val );
212 for ( j = iAVA; j > i; j-- ) {
213 rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
215 rdn[ 0 ][ i ] = ava_in;
222 * In-place, schema-aware normalization / "pretty"ing of the
223 * structural representation of a distinguished name.
226 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
233 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
234 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
239 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
240 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
241 AttributeDescription *ad;
242 slap_syntax_transform_func *transf = NULL;
244 struct berval bv = { 0, NULL };
249 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
250 const char *text = NULL;
252 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
253 if ( rc != LDAP_SUCCESS ) {
254 return LDAP_INVALID_SYNTAX;
257 ava->la_private = ( void * )ad;
262 * Replace attr oid/name with the canonical name
264 ava->la_attr = ad->ad_cname;
266 if( ava->la_flags & LDAP_AVA_BINARY ) {
267 /* AVA is binary encoded, don't muck with it */
271 } else if( flags & SLAP_LDAPDN_PRETTY ) {
272 transf = ad->ad_type->sat_syntax->ssyn_pretty;
275 transf = ad->ad_type->sat_syntax->ssyn_normalize;
276 mr = ad->ad_type->sat_equality;
281 * transform value by normalize/pretty function
282 * if value is empty, use empty_bv
284 rc = ( *transf )( ad->ad_type->sat_syntax,
287 : (struct berval *) &slap_empty_bv,
290 if ( rc != LDAP_SUCCESS ) {
291 return LDAP_INVALID_SYNTAX;
295 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
298 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
299 : &ava->la_value, LDAP_UTF8_CASEFOLD ),
305 free( ava->la_value.bv_val );
309 if( do_sort ) AVA_Sort( rdn, iAVA );
317 * dn normalize routine
323 struct berval **normalized )
328 assert( normalized && *normalized == NULL );
330 out = ch_malloc( sizeof( struct berval ) );
331 rc = dnNormalize2( syntax, val, out );
332 if ( rc != LDAP_SUCCESS )
348 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
350 if ( val->bv_len != 0 ) {
355 * Go to structural representation
357 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
358 if ( rc != LDAP_SUCCESS ) {
359 return LDAP_INVALID_SYNTAX;
363 * Schema-aware rewrite
365 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
367 return LDAP_INVALID_SYNTAX;
371 * Back to string representation
373 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
377 if ( rc != LDAP_SUCCESS ) {
378 return LDAP_INVALID_SYNTAX;
381 ber_dupbv( out, val );
384 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
390 * dn "pretty"ing routine
396 struct berval **pretty)
401 assert( pretty && *pretty == NULL );
403 out = ch_malloc( sizeof( struct berval ) );
404 rc = dnPretty2( syntax, val, out );
405 if ( rc != LDAP_SUCCESS )
421 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
423 if ( val->bv_len != 0 ) {
427 /* FIXME: should be liberal in what we accept */
428 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
429 if ( rc != LDAP_SUCCESS ) {
430 return LDAP_INVALID_SYNTAX;
434 * Schema-aware rewrite
436 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
438 return LDAP_INVALID_SYNTAX;
441 /* FIXME: not sure why the default isn't pretty */
442 /* RE: the default is the form that is used as
443 * an internal representation; the pretty form
445 rc = ldap_dn2bv( dn, out,
446 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
450 if ( rc != LDAP_SUCCESS ) {
451 return LDAP_INVALID_SYNTAX;
454 ber_dupbv( out, val );
457 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
463 * Combination of both dnPretty and dnNormalize
469 struct berval *pretty,
470 struct berval *normal)
472 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
478 if ( val->bv_len != 0 ) {
482 pretty->bv_val = NULL;
483 normal->bv_val = NULL;
487 /* FIXME: str2dn should take a bv and handle this */
488 if( strlen( val->bv_val ) != val->bv_len ) {
489 return LDAP_INVALID_SYNTAX;
492 /* FIXME: should be liberal in what we accept */
493 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
494 if ( rc != LDAP_SUCCESS ) {
495 return LDAP_INVALID_SYNTAX;
499 * Schema-aware rewrite
501 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
503 return LDAP_INVALID_SYNTAX;
506 rc = ldap_dn2bv( dn, pretty,
507 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
509 if ( rc != LDAP_SUCCESS ) {
511 return LDAP_INVALID_SYNTAX;
514 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
516 free( pretty->bv_val );
517 pretty->bv_val = NULL;
519 return LDAP_INVALID_SYNTAX;
522 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
525 if ( rc != LDAP_SUCCESS ) {
526 free( pretty->bv_val );
527 pretty->bv_val = NULL;
529 return LDAP_INVALID_SYNTAX;
532 ber_dupbv( pretty, val );
533 ber_dupbv( normal, val );
536 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
537 pretty->bv_val, normal->bv_val, 0 );
551 struct berval *value,
552 void *assertedValue )
555 struct berval *asserted = (struct berval *) assertedValue;
559 assert( assertedValue );
561 match = value->bv_len - asserted->bv_len;
564 match = strcmp( value->bv_val, asserted->bv_val );
568 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
569 "dnMatch: %d\n %s\n %s\n", match,
570 value->bv_val, asserted->bv_val ));
572 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
573 match, value->bv_val, asserted->bv_val );
577 return( LDAP_SUCCESS );
581 * dnParent - dn's parent, in-place
583 * note: the incoming dn is assumed to be normalized/prettyfied,
584 * so that escaped rdn/ava separators are in '\'+hexpair form
593 p = strchr( dn->bv_val, ',' );
597 *pdn = slap_empty_bv;
601 assert( DN_SEPARATOR( p[ 0 ] ) );
604 assert( ATTR_LEADCHAR( p[ 0 ] ) );
606 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
623 if( dn->bv_len == 0 ) {
627 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
628 if ( rc != LDAP_SUCCESS ) {
632 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
633 ldap_rdnfree( tmpRDN );
634 if ( rc != LDAP_SUCCESS ) {
642 * We can assume the input is a prettied or normalized DN
647 struct berval *dn_in )
653 if ( dn_in == NULL ) {
657 if ( !dn_in->bv_len ) {
661 if ( be != NULL && be_issuffix( be, dn_in ) ) {
665 p = strchr( dn_in->bv_val, ',' );
667 return p ? p - dn_in->bv_val : dn_in->bv_len;
673 * LDAP_SUCCESS if rdn is a legal rdn;
674 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
677 rdnValidate( struct berval *rdn )
681 * input is a pretty or normalized DN
682 * hence, we can just search for ','
684 if( rdn == NULL || rdn->bv_len == 0 ) {
685 return LDAP_INVALID_SYNTAX;
688 return strchr( rdn->bv_val, ',' ) == NULL
689 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
692 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
699 if ( rdn == NULL || rdn == '\0' ) {
706 rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
707 if ( rc != LDAP_SUCCESS ) {
714 if ( p[ 0 ] != '\0' ) {
719 * Schema-aware validate
721 if ( rc == LDAP_SUCCESS ) {
722 rc = LDAPDN_validate( DN );
727 * Must validate (there's a repeated parsing ...)
729 return ( rc == LDAP_SUCCESS );
736 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
739 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
743 build_new_dn( struct berval * new_dn,
744 struct berval * parent_dn,
745 struct berval * newrdn )
749 if ( parent_dn == NULL ) {
750 ber_dupbv( new_dn, newrdn );
754 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
755 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
757 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
759 strcpy( ptr, parent_dn->bv_val );
764 * dnIsSuffix - tells whether suffix is a suffix of dn.
765 * Both dn and suffix must be normalized.
769 const struct berval *dn,
770 const struct berval *suffix )
772 int d = dn->bv_len - suffix->bv_len;
777 /* empty suffix matches any dn */
778 if ( suffix->bv_len == 0 ) {
782 /* suffix longer than dn */
787 /* no rdn separator or escaped rdn separator */
788 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
792 /* no possible match or malformed dn */
798 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );