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 );
137 if ( rc != LDAP_SUCCESS ) {
138 return LDAP_INVALID_SYNTAX;
145 * AVA sorting inside a RDN
147 * rule: sort attributeTypes in alphabetical order; in case of multiple
148 * occurrences of the same attributeType, sort values in byte order
149 * (use memcmp, which implies alphabetical order in case of IA5 value;
150 * this should guarantee the repeatability of the operation).
152 * Note: the sorting can be slightly improved by sorting first
153 * by attribute type length, then by alphabetical order.
155 * uses a linear search; should be fine since the number of AVAs in
156 * a RDN should be limited.
159 AVA_Sort( LDAPRDN *rdn, int iAVA )
162 LDAPAVA *ava_in = rdn[ 0 ][ iAVA ];
167 for ( i = 0; i < iAVA; i++ ) {
168 LDAPAVA *ava = rdn[ 0 ][ i ];
173 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
182 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
184 v = memcmp( ava_in->la_value.bv_val,
185 ava->la_value.bv_val,
186 d <= 0 ? ava_in->la_value.bv_len
187 : ava->la_value.bv_len );
189 if ( v == 0 && d != 0 ) {
208 a = strcmp( ava_in->la_attr.bv_val,
209 ava->la_attr.bv_val );
215 for ( j = iAVA; j > i; j-- ) {
216 rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
218 rdn[ 0 ][ i ] = ava_in;
225 * In-place, schema-aware normalization / "pretty"ing of the
226 * structural representation of a distinguished name.
229 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
236 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
237 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
242 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
243 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
244 AttributeDescription *ad;
245 slap_syntax_validate_func *validf = NULL;
246 slap_syntax_transform_func *transf = NULL;
248 struct berval bv = { 0, NULL };
253 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
254 const char *text = NULL;
256 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
257 if ( rc != LDAP_SUCCESS ) {
258 return LDAP_INVALID_SYNTAX;
261 ava->la_private = ( void * )ad;
266 * Replace attr oid/name with the canonical name
268 ava->la_attr = ad->ad_cname;
270 if( ava->la_flags & LDAP_AVA_BINARY ) {
271 /* AVA is binary encoded, don't muck with it */
275 } else if( flags & SLAP_LDAPDN_PRETTY ) {
277 transf = ad->ad_type->sat_syntax->ssyn_pretty;
280 validf = ad->ad_type->sat_syntax->ssyn_validate;
281 transf = ad->ad_type->sat_syntax->ssyn_normalize;
282 mr = ad->ad_type->sat_equality;
286 /* validate value before normalization */
287 rc = ( *validf )( ad->ad_type->sat_syntax,
290 : (struct berval *) &slap_empty_bv );
292 if ( rc != LDAP_SUCCESS ) {
293 return LDAP_INVALID_SYNTAX;
299 * transform value by normalize/pretty function
300 * if value is empty, use empty_bv
302 rc = ( *transf )( ad->ad_type->sat_syntax,
305 : (struct berval *) &slap_empty_bv,
308 if ( rc != LDAP_SUCCESS ) {
309 return LDAP_INVALID_SYNTAX;
313 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
316 if ( UTF8bvnormalize( &bv, &bv,
317 LDAP_UTF8_CASEFOLD ) == NULL ) {
318 return LDAP_INVALID_SYNTAX;
324 free( ava->la_value.bv_val );
328 if( do_sort ) AVA_Sort( rdn, iAVA );
336 * dn normalize routine
342 struct berval **normalized )
347 assert( normalized && *normalized == NULL );
349 out = ch_malloc( sizeof( struct berval ) );
350 rc = dnNormalize2( syntax, val, out );
351 if ( rc != LDAP_SUCCESS )
367 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
369 if ( val->bv_len != 0 ) {
374 * Go to structural representation
376 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
377 if ( rc != LDAP_SUCCESS ) {
378 return LDAP_INVALID_SYNTAX;
381 assert( strlen( val->bv_val ) == val->bv_len );
384 * Schema-aware rewrite
386 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
388 return LDAP_INVALID_SYNTAX;
392 * Back to string representation
394 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
398 if ( rc != LDAP_SUCCESS ) {
399 return LDAP_INVALID_SYNTAX;
402 ber_dupbv( out, val );
405 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
411 * dn "pretty"ing routine
417 struct berval **pretty)
422 assert( pretty && *pretty == NULL );
424 out = ch_malloc( sizeof( struct berval ) );
425 rc = dnPretty2( syntax, val, out );
426 if ( rc != LDAP_SUCCESS )
443 LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
445 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
448 if ( val->bv_len == 0 ) {
449 ber_dupbv( out, val );
451 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
452 return LDAP_INVALID_SYNTAX;
458 /* FIXME: should be liberal in what we accept */
459 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
460 if ( rc != LDAP_SUCCESS ) {
461 return LDAP_INVALID_SYNTAX;
464 assert( strlen( val->bv_val ) == val->bv_len );
467 * Schema-aware rewrite
469 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
471 return LDAP_INVALID_SYNTAX;
474 /* FIXME: not sure why the default isn't pretty */
475 /* RE: the default is the form that is used as
476 * an internal representation; the pretty form
478 rc = ldap_dn2bv( dn, out,
479 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
483 if ( rc != LDAP_SUCCESS ) {
484 return LDAP_INVALID_SYNTAX;
488 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
494 * Combination of both dnPretty and dnNormalize
500 struct berval *pretty,
501 struct berval *normal)
504 LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
506 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
513 if ( val->bv_len == 0 ) {
514 ber_dupbv( pretty, val );
515 ber_dupbv( normal, val );
517 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
519 return LDAP_INVALID_SYNTAX;
525 pretty->bv_val = NULL;
526 normal->bv_val = NULL;
530 /* FIXME: should be liberal in what we accept */
531 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
532 if ( rc != LDAP_SUCCESS ) {
533 return LDAP_INVALID_SYNTAX;
536 assert( strlen( val->bv_val ) == val->bv_len );
539 * Schema-aware rewrite
541 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
543 return LDAP_INVALID_SYNTAX;
546 rc = ldap_dn2bv( dn, pretty,
547 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
549 if ( rc != LDAP_SUCCESS ) {
551 return LDAP_INVALID_SYNTAX;
554 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
556 free( pretty->bv_val );
557 pretty->bv_val = NULL;
559 return LDAP_INVALID_SYNTAX;
562 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
565 if ( rc != LDAP_SUCCESS ) {
566 free( pretty->bv_val );
567 pretty->bv_val = NULL;
569 return LDAP_INVALID_SYNTAX;
574 LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
575 pretty->bv_val, normal->bv_val, 0 );
577 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
578 pretty->bv_val, normal->bv_val, 0 );
593 struct berval *value,
594 void *assertedValue )
597 struct berval *asserted = (struct berval *) assertedValue;
601 assert( assertedValue );
603 match = value->bv_len - asserted->bv_len;
606 match = memcmp( value->bv_val, asserted->bv_val,
611 LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n %s\n %s\n",
612 match, value->bv_val, asserted->bv_val );
614 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
615 match, value->bv_val, asserted->bv_val );
619 return( LDAP_SUCCESS );
623 * dnParent - dn's parent, in-place
625 * note: the incoming dn is assumed to be normalized/prettyfied,
626 * so that escaped rdn/ava separators are in '\'+hexpair form
635 p = strchr( dn->bv_val, ',' );
640 pdn->bv_val = dn->bv_val + dn->bv_len;
644 assert( DN_SEPARATOR( p[ 0 ] ) );
647 assert( ATTR_LEADCHAR( p[ 0 ] ) );
649 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
666 if( dn->bv_len == 0 ) {
670 rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
671 if ( rc != LDAP_SUCCESS ) {
675 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
676 ldap_rdnfree( tmpRDN );
677 if ( rc != LDAP_SUCCESS ) {
685 * We can assume the input is a prettied or normalized DN
690 struct berval *dn_in )
696 if ( dn_in == NULL ) {
700 if ( !dn_in->bv_len ) {
704 if ( be != NULL && be_issuffix( be, dn_in ) ) {
708 p = strchr( dn_in->bv_val, ',' );
710 return p ? p - dn_in->bv_val : dn_in->bv_len;
716 * LDAP_SUCCESS if rdn is a legal rdn;
717 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
720 rdnValidate( struct berval *rdn )
724 * input is a pretty or normalized DN
725 * hence, we can just search for ','
727 if( rdn == NULL || rdn->bv_len == 0 ||
728 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
730 return LDAP_INVALID_SYNTAX;
733 return strchr( rdn->bv_val, ',' ) == NULL
734 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
737 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
744 if ( rdn == NULL || rdn == '\0' ) {
751 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
752 if ( rc != LDAP_SUCCESS ) {
759 if ( p[ 0 ] != '\0' ) {
764 * Schema-aware validate
766 if ( rc == LDAP_SUCCESS ) {
767 rc = LDAPDN_validate( DN );
772 * Must validate (there's a repeated parsing ...)
774 return ( rc == LDAP_SUCCESS );
781 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
784 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
788 build_new_dn( struct berval * new_dn,
789 struct berval * parent_dn,
790 struct berval * newrdn )
794 if ( parent_dn == NULL ) {
795 ber_dupbv( new_dn, newrdn );
799 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
800 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
802 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
804 strcpy( ptr, parent_dn->bv_val );
809 * dnIsSuffix - tells whether suffix is a suffix of dn.
810 * Both dn and suffix must be normalized.
814 const struct berval *dn,
815 const struct berval *suffix )
817 int d = dn->bv_len - suffix->bv_len;
822 /* empty suffix matches any dn */
823 if ( suffix->bv_len == 0 ) {
827 /* suffix longer than dn */
832 /* no rdn separator or escaped rdn separator */
833 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
837 /* no possible match or malformed dn */
843 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
848 * Convert an X.509 DN into a normalized LDAP DN
851 dnX509normalize( void *x509_name, struct berval *out )
853 /* Invoke the LDAP library's converter with our schema-rewriter */
854 return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
858 * Get the TLS session's peer's DN into a normalized LDAP DN
861 dnX509peerNormalize( void *ssl, struct berval *dn )
864 return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );