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;
243 slap_mr_normalize_func *normf = NULL;
245 slap_syntax_transform_func *transf = NULL;
246 MatchingRule *mr = NULL;
247 struct berval bv = { 0, NULL };
252 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
253 const char *text = NULL;
255 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
256 if ( rc != LDAP_SUCCESS ) {
257 return LDAP_INVALID_SYNTAX;
260 ava->la_private = ( void * )ad;
265 * Replace attr oid/name with the canonical name
267 ava->la_attr = ad->ad_cname;
269 if( ava->la_flags & LDAP_AVA_BINARY ) {
270 if( ava->la_value.bv_len == 0 ) {
271 /* BER encoding is empty */
272 return LDAP_INVALID_SYNTAX;
275 /* AVA is binary encoded, don't muck with it */
276 } else if( flags & SLAP_LDAPDN_PRETTY ) {
277 transf = ad->ad_type->sat_syntax->ssyn_pretty;
279 validf = ad->ad_type->sat_syntax->ssyn_validate;
281 } else { /* normalization */
282 validf = ad->ad_type->sat_syntax->ssyn_validate;
283 mr = ad->ad_type->sat_equality;
285 if( mr ) normf = mr->smr_normalize;
287 transf = ad->ad_type->sat_syntax->ssyn_normalize;
292 /* validate value before normalization */
293 rc = ( *validf )( ad->ad_type->sat_syntax,
296 : (struct berval *) &slap_empty_bv );
298 if ( rc != LDAP_SUCCESS ) {
299 return LDAP_INVALID_SYNTAX;
306 * transform value by pretty function
308 * transform value by normalize/pretty function
310 * if value is empty, use empty_bv
312 rc = ( *transf )( ad->ad_type->sat_syntax,
315 : (struct berval *) &slap_empty_bv,
318 if ( rc != LDAP_SUCCESS ) {
319 return LDAP_INVALID_SYNTAX;
327 * if value is empty, use empty_bv
331 ad->ad_type->sat_syntax,
335 : (struct berval *) &slap_empty_bv,
338 if ( rc != LDAP_SUCCESS ) {
339 return LDAP_INVALID_SYNTAX;
344 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
347 if ( UTF8bvnormalize( &bv, &bv,
348 LDAP_UTF8_CASEFOLD ) == NULL ) {
349 return LDAP_INVALID_SYNTAX;
356 free( ava->la_value.bv_val );
360 if( do_sort ) AVA_Sort( rdn, iAVA );
385 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
387 if ( val->bv_len != 0 ) {
392 * Go to structural representation
394 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
395 if ( rc != LDAP_SUCCESS ) {
396 return LDAP_INVALID_SYNTAX;
399 assert( strlen( val->bv_val ) == val->bv_len );
402 * Schema-aware rewrite
404 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
406 return LDAP_INVALID_SYNTAX;
410 * Back to string representation
412 rc = ldap_dn2bv( dn, out,
413 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
417 if ( rc != LDAP_SUCCESS ) {
418 return LDAP_INVALID_SYNTAX;
421 ber_dupbv( out, val );
424 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
431 * dn "pretty"ing routine
437 struct berval **pretty)
442 assert( pretty && *pretty == NULL );
444 out = ch_malloc( sizeof( struct berval ) );
445 rc = dnPretty2( syntax, val, out );
446 if ( rc != LDAP_SUCCESS )
464 LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
466 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
469 if ( val->bv_len == 0 ) {
470 ber_dupbv( out, val );
472 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
473 return LDAP_INVALID_SYNTAX;
479 /* FIXME: should be liberal in what we accept */
480 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
481 if ( rc != LDAP_SUCCESS ) {
482 return LDAP_INVALID_SYNTAX;
485 assert( strlen( val->bv_val ) == val->bv_len );
488 * Schema-aware rewrite
490 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
492 return LDAP_INVALID_SYNTAX;
495 /* FIXME: not sure why the default isn't pretty */
496 /* RE: the default is the form that is used as
497 * an internal representation; the pretty form
499 rc = ldap_dn2bv( dn, out,
500 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
504 if ( rc != LDAP_SUCCESS ) {
505 return LDAP_INVALID_SYNTAX;
509 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
525 LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n",
526 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
529 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
530 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
534 if ( val->bv_len == 0 ) {
537 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
538 return LDAP_INVALID_SYNTAX;
543 /* FIXME: should be liberal in what we accept */
544 rc = ldap_bv2dn( val, dn, LDAP_DN_FORMAT_LDAP );
545 if ( rc != LDAP_SUCCESS ) {
546 return LDAP_INVALID_SYNTAX;
549 assert( strlen( val->bv_val ) == val->bv_len );
552 * Schema-aware rewrite
554 if ( LDAPDN_rewrite( *dn, flags ) != LDAP_SUCCESS ) {
557 return LDAP_INVALID_SYNTAX;
561 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
562 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
569 * Combination of both dnPretty and dnNormalize
575 struct berval *pretty,
576 struct berval *normal)
579 LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
581 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
588 if ( val->bv_len == 0 ) {
589 ber_dupbv( pretty, val );
590 ber_dupbv( normal, val );
592 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
594 return LDAP_INVALID_SYNTAX;
600 pretty->bv_val = NULL;
601 normal->bv_val = NULL;
605 /* FIXME: should be liberal in what we accept */
606 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
607 if ( rc != LDAP_SUCCESS ) {
608 return LDAP_INVALID_SYNTAX;
611 assert( strlen( val->bv_val ) == val->bv_len );
614 * Schema-aware rewrite
616 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
618 return LDAP_INVALID_SYNTAX;
621 rc = ldap_dn2bv( dn, pretty,
622 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
624 if ( rc != LDAP_SUCCESS ) {
626 return LDAP_INVALID_SYNTAX;
629 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
631 free( pretty->bv_val );
632 pretty->bv_val = NULL;
634 return LDAP_INVALID_SYNTAX;
637 rc = ldap_dn2bv( dn, normal,
638 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
641 if ( rc != LDAP_SUCCESS ) {
642 free( pretty->bv_val );
643 pretty->bv_val = NULL;
645 return LDAP_INVALID_SYNTAX;
650 LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
651 pretty->bv_val, normal->bv_val, 0 );
653 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
654 pretty->bv_val, normal->bv_val, 0 );
669 struct berval *value,
670 void *assertedValue )
673 struct berval *asserted = (struct berval *) assertedValue;
677 assert( assertedValue );
679 match = value->bv_len - asserted->bv_len;
682 match = memcmp( value->bv_val, asserted->bv_val,
687 LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n %s\n %s\n",
688 match, value->bv_val, asserted->bv_val );
690 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
691 match, value->bv_val, asserted->bv_val );
695 return( LDAP_SUCCESS );
699 * dnParent - dn's parent, in-place
701 * note: the incoming dn is assumed to be normalized/prettyfied,
702 * so that escaped rdn/ava separators are in '\'+hexpair form
711 p = strchr( dn->bv_val, ',' );
716 pdn->bv_val = dn->bv_val + dn->bv_len;
720 assert( DN_SEPARATOR( p[ 0 ] ) );
723 assert( ATTR_LEADCHAR( p[ 0 ] ) );
725 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
742 if( dn->bv_len == 0 ) {
746 rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
747 if ( rc != LDAP_SUCCESS ) {
751 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
753 ldap_rdnfree( tmpRDN );
754 if ( rc != LDAP_SUCCESS ) {
762 * We can assume the input is a prettied or normalized DN
767 struct berval *dn_in )
773 if ( dn_in == NULL ) {
777 if ( !dn_in->bv_len ) {
781 if ( be != NULL && be_issuffix( be, dn_in ) ) {
785 p = strchr( dn_in->bv_val, ',' );
787 return p ? p - dn_in->bv_val : dn_in->bv_len;
793 * LDAP_SUCCESS if rdn is a legal rdn;
794 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
797 rdnValidate( struct berval *rdn )
801 * input is a pretty or normalized DN
802 * hence, we can just search for ','
804 if( rdn == NULL || rdn->bv_len == 0 ||
805 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
807 return LDAP_INVALID_SYNTAX;
810 return strchr( rdn->bv_val, ',' ) == NULL
811 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
814 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
821 if ( rdn == NULL || rdn == '\0' ) {
828 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
829 if ( rc != LDAP_SUCCESS ) {
836 if ( p[ 0 ] != '\0' ) {
841 * Schema-aware validate
843 if ( rc == LDAP_SUCCESS ) {
844 rc = LDAPDN_validate( DN );
849 * Must validate (there's a repeated parsing ...)
851 return ( rc == LDAP_SUCCESS );
858 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
861 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
865 build_new_dn( struct berval * new_dn,
866 struct berval * parent_dn,
867 struct berval * newrdn )
871 if ( parent_dn == NULL ) {
872 ber_dupbv( new_dn, newrdn );
876 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
877 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
879 ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
881 strcpy( ptr, parent_dn->bv_val );
886 * dnIsSuffix - tells whether suffix is a suffix of dn.
887 * Both dn and suffix must be normalized.
891 const struct berval *dn,
892 const struct berval *suffix )
894 int d = dn->bv_len - suffix->bv_len;
899 /* empty suffix matches any dn */
900 if ( suffix->bv_len == 0 ) {
904 /* suffix longer than dn */
909 /* no rdn separator or escaped rdn separator */
910 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
914 /* no possible match or malformed dn */
920 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
925 * Convert an X.509 DN into a normalized LDAP DN
928 dnX509normalize( void *x509_name, struct berval *out )
930 /* Invoke the LDAP library's converter with our schema-rewriter */
931 return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
935 * Get the TLS session's peer's DN into a normalized LDAP DN
938 dnX509peerNormalize( void *ssl, struct berval *dn )
941 return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );