1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
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 ) {
116 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
117 return LDAP_INVALID_SYNTAX;
120 rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
121 if ( rc != LDAP_SUCCESS ) {
122 return LDAP_INVALID_SYNTAX;
125 assert( strlen( in->bv_val ) == in->bv_len );
128 * Schema-aware validate
130 rc = LDAPDN_validate( dn );
133 if ( rc != LDAP_SUCCESS ) {
134 return LDAP_INVALID_SYNTAX;
141 * AVA sorting inside a RDN
143 * rule: sort attributeTypes in alphabetical order; in case of multiple
144 * occurrences of the same attributeType, sort values in byte order
145 * (use memcmp, which implies alphabetical order in case of IA5 value;
146 * this should guarantee the repeatability of the operation).
148 * Note: the sorting can be slightly improved by sorting first
149 * by attribute type length, then by alphabetical order.
151 * uses a linear search; should be fine since the number of AVAs in
152 * a RDN should be limited.
155 AVA_Sort( LDAPRDN *rdn, int iAVA )
158 LDAPAVA *ava_in = rdn[ 0 ][ iAVA ];
163 for ( i = 0; i < iAVA; i++ ) {
164 LDAPAVA *ava = rdn[ 0 ][ i ];
169 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
178 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
180 v = memcmp( ava_in->la_value.bv_val,
181 ava->la_value.bv_val,
182 d <= 0 ? ava_in->la_value.bv_len
183 : ava->la_value.bv_len );
185 if ( v == 0 && d != 0 ) {
204 a = strcmp( ava_in->la_attr.bv_val,
205 ava->la_attr.bv_val );
211 for ( j = iAVA; j > i; j-- ) {
212 rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
214 rdn[ 0 ][ i ] = ava_in;
221 * In-place, schema-aware normalization / "pretty"ing of the
222 * structural representation of a distinguished name.
225 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
232 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
233 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
238 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
239 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
240 AttributeDescription *ad;
241 slap_syntax_validate_func *validf = NULL;
242 slap_mr_normalize_func *normf = NULL;
243 slap_syntax_transform_func *transf = NULL;
244 MatchingRule *mr = 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 if( ava->la_value.bv_len == 0 ) {
269 /* BER encoding is empty */
270 return LDAP_INVALID_SYNTAX;
273 /* AVA is binary encoded, don't muck with it */
274 } else if( flags & SLAP_LDAPDN_PRETTY ) {
275 transf = ad->ad_type->sat_syntax->ssyn_pretty;
277 validf = ad->ad_type->sat_syntax->ssyn_validate;
279 } else { /* normalization */
280 validf = ad->ad_type->sat_syntax->ssyn_validate;
281 mr = ad->ad_type->sat_equality;
282 if( mr ) normf = mr->smr_normalize;
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 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;
316 * if value is empty, use empty_bv
320 ad->ad_type->sat_syntax,
324 : (struct berval *) &slap_empty_bv,
327 if ( rc != LDAP_SUCCESS ) {
328 return LDAP_INVALID_SYNTAX;
334 free( ava->la_value.bv_val );
338 if( do_sort ) AVA_Sort( rdn, iAVA );
356 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
358 if ( val->bv_len != 0 ) {
363 * Go to structural representation
365 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
366 if ( rc != LDAP_SUCCESS ) {
367 return LDAP_INVALID_SYNTAX;
370 assert( strlen( val->bv_val ) == val->bv_len );
373 * Schema-aware rewrite
375 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
377 return LDAP_INVALID_SYNTAX;
381 * Back to string representation
383 rc = ldap_dn2bv( dn, out,
384 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
388 if ( rc != LDAP_SUCCESS ) {
389 return LDAP_INVALID_SYNTAX;
392 ber_dupbv( out, val );
395 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
402 * dn "pretty"ing routine
408 struct berval **pretty)
413 assert( pretty && *pretty == NULL );
415 out = ch_malloc( sizeof( struct berval ) );
416 rc = dnPretty2( syntax, val, out );
417 if ( rc != LDAP_SUCCESS )
435 LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
437 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
440 if ( val->bv_len == 0 ) {
441 ber_dupbv( out, val );
443 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
444 return LDAP_INVALID_SYNTAX;
450 /* FIXME: should be liberal in what we accept */
451 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
452 if ( rc != LDAP_SUCCESS ) {
453 return LDAP_INVALID_SYNTAX;
456 assert( strlen( val->bv_val ) == val->bv_len );
459 * Schema-aware rewrite
461 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
463 return LDAP_INVALID_SYNTAX;
466 /* FIXME: not sure why the default isn't pretty */
467 /* RE: the default is the form that is used as
468 * an internal representation; the pretty form
470 rc = ldap_dn2bv( dn, out,
471 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
475 if ( rc != LDAP_SUCCESS ) {
476 return LDAP_INVALID_SYNTAX;
480 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
496 LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n",
497 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
500 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
501 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
505 if ( val->bv_len == 0 ) {
508 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
509 return LDAP_INVALID_SYNTAX;
514 /* FIXME: should be liberal in what we accept */
515 rc = ldap_bv2dn( val, dn, LDAP_DN_FORMAT_LDAP );
516 if ( rc != LDAP_SUCCESS ) {
517 return LDAP_INVALID_SYNTAX;
520 assert( strlen( val->bv_val ) == val->bv_len );
523 * Schema-aware rewrite
525 if ( LDAPDN_rewrite( *dn, flags ) != LDAP_SUCCESS ) {
528 return LDAP_INVALID_SYNTAX;
532 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
533 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
540 * Combination of both dnPretty and dnNormalize
546 struct berval *pretty,
547 struct berval *normal)
550 LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
552 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
559 if ( val->bv_len == 0 ) {
560 ber_dupbv( pretty, val );
561 ber_dupbv( normal, val );
563 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
565 return LDAP_INVALID_SYNTAX;
571 pretty->bv_val = NULL;
572 normal->bv_val = NULL;
576 /* FIXME: should be liberal in what we accept */
577 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
578 if ( rc != LDAP_SUCCESS ) {
579 return LDAP_INVALID_SYNTAX;
582 assert( strlen( val->bv_val ) == val->bv_len );
585 * Schema-aware rewrite
587 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
589 return LDAP_INVALID_SYNTAX;
592 rc = ldap_dn2bv( dn, pretty,
593 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
595 if ( rc != LDAP_SUCCESS ) {
597 return LDAP_INVALID_SYNTAX;
600 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
602 free( pretty->bv_val );
603 pretty->bv_val = NULL;
605 return LDAP_INVALID_SYNTAX;
608 rc = ldap_dn2bv( dn, normal,
609 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
612 if ( rc != LDAP_SUCCESS ) {
613 free( pretty->bv_val );
614 pretty->bv_val = NULL;
616 return LDAP_INVALID_SYNTAX;
621 LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
622 pretty->bv_val, normal->bv_val, 0 );
624 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
625 pretty->bv_val, normal->bv_val, 0 );
640 struct berval *value,
641 void *assertedValue )
644 struct berval *asserted = (struct berval *) assertedValue;
648 assert( assertedValue );
650 match = value->bv_len - asserted->bv_len;
653 match = memcmp( value->bv_val, asserted->bv_val,
658 LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n %s\n %s\n",
659 match, value->bv_val, asserted->bv_val );
661 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
662 match, value->bv_val, asserted->bv_val );
666 return( LDAP_SUCCESS );
670 * dnParent - dn's parent, in-place
672 * note: the incoming dn is assumed to be normalized/prettyfied,
673 * so that escaped rdn/ava separators are in '\'+hexpair form
682 p = strchr( dn->bv_val, ',' );
687 pdn->bv_val = dn->bv_val + dn->bv_len;
691 assert( DN_SEPARATOR( p[ 0 ] ) );
694 assert( ATTR_LEADCHAR( p[ 0 ] ) );
696 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
713 if( dn->bv_len == 0 ) {
717 rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
718 if ( rc != LDAP_SUCCESS ) {
722 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
724 ldap_rdnfree( tmpRDN );
725 if ( rc != LDAP_SUCCESS ) {
733 * We can assume the input is a prettied or normalized DN
738 struct berval *dn_in )
744 if ( dn_in == NULL ) {
748 if ( !dn_in->bv_len ) {
752 if ( be != NULL && be_issuffix( be, dn_in ) ) {
756 p = strchr( dn_in->bv_val, ',' );
758 return p ? p - dn_in->bv_val : dn_in->bv_len;
764 * LDAP_SUCCESS if rdn is a legal rdn;
765 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
768 rdnValidate( struct berval *rdn )
772 * input is a pretty or normalized DN
773 * hence, we can just search for ','
775 if( rdn == NULL || rdn->bv_len == 0 ||
776 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
778 return LDAP_INVALID_SYNTAX;
781 return strchr( rdn->bv_val, ',' ) == NULL
782 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
785 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
792 if ( rdn == NULL || rdn == '\0' ) {
799 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
800 if ( rc != LDAP_SUCCESS ) {
807 if ( p[ 0 ] != '\0' ) {
812 * Schema-aware validate
814 if ( rc == LDAP_SUCCESS ) {
815 rc = LDAPDN_validate( DN );
820 * Must validate (there's a repeated parsing ...)
822 return ( rc == LDAP_SUCCESS );
829 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
832 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
836 build_new_dn( struct berval * new_dn,
837 struct berval * parent_dn,
838 struct berval * newrdn )
842 if ( parent_dn == NULL ) {
843 ber_dupbv( new_dn, newrdn );
847 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
848 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
850 ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
852 strcpy( ptr, parent_dn->bv_val );
857 * dnIsSuffix - tells whether suffix is a suffix of dn.
858 * Both dn and suffix must be normalized.
862 const struct berval *dn,
863 const struct berval *suffix )
865 int d = dn->bv_len - suffix->bv_len;
870 /* empty suffix matches any dn */
871 if ( suffix->bv_len == 0 ) {
875 /* suffix longer than dn */
880 /* no rdn separator or escaped rdn separator */
881 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
885 /* no possible match or malformed dn */
891 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
896 * Convert an X.509 DN into a normalized LDAP DN
899 dnX509normalize( void *x509_name, struct berval *out )
901 /* Invoke the LDAP library's converter with our schema-rewriter */
902 return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
906 * Get the TLS session's peer's DN into a normalized LDAP DN
909 dnX509peerNormalize( void *ssl, struct berval *dn )
912 return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );