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 /* 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;
274 validf = ad->ad_type->sat_syntax->ssyn_validate;
276 } else { /* normalization */
277 validf = ad->ad_type->sat_syntax->ssyn_validate;
278 mr = ad->ad_type->sat_equality;
280 if( mr ) normf = mr->smr_normalize;
282 transf = ad->ad_type->sat_syntax->ssyn_normalize;
287 /* validate value before normalization */
288 rc = ( *validf )( ad->ad_type->sat_syntax,
291 : (struct berval *) &slap_empty_bv );
293 if ( rc != LDAP_SUCCESS ) {
294 return LDAP_INVALID_SYNTAX;
301 * transform value by pretty function
303 * transform value by normalize/pretty function
305 * if value is empty, use empty_bv
307 rc = ( *transf )( ad->ad_type->sat_syntax,
310 : (struct berval *) &slap_empty_bv,
313 if ( rc != LDAP_SUCCESS ) {
314 return LDAP_INVALID_SYNTAX;
322 * if value is empty, use empty_bv
326 ad->ad_type->sat_syntax,
330 : (struct berval *) &slap_empty_bv,
333 if ( rc != LDAP_SUCCESS ) {
334 return LDAP_INVALID_SYNTAX;
339 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
342 if ( UTF8bvnormalize( &bv, &bv,
343 LDAP_UTF8_CASEFOLD ) == NULL ) {
344 return LDAP_INVALID_SYNTAX;
351 free( ava->la_value.bv_val );
355 if( do_sort ) AVA_Sort( rdn, iAVA );
380 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
382 if ( val->bv_len != 0 ) {
387 * Go to structural representation
389 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
390 if ( rc != LDAP_SUCCESS ) {
391 return LDAP_INVALID_SYNTAX;
394 assert( strlen( val->bv_val ) == val->bv_len );
397 * Schema-aware rewrite
399 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
401 return LDAP_INVALID_SYNTAX;
405 * Back to string representation
407 rc = ldap_dn2bv( dn, out,
408 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
412 if ( rc != LDAP_SUCCESS ) {
413 return LDAP_INVALID_SYNTAX;
416 ber_dupbv( out, val );
419 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
426 * dn "pretty"ing routine
432 struct berval **pretty)
437 assert( pretty && *pretty == NULL );
439 out = ch_malloc( sizeof( struct berval ) );
440 rc = dnPretty2( syntax, val, out );
441 if ( rc != LDAP_SUCCESS )
459 LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
461 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
464 if ( val->bv_len == 0 ) {
465 ber_dupbv( out, val );
467 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
468 return LDAP_INVALID_SYNTAX;
474 /* FIXME: should be liberal in what we accept */
475 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
476 if ( rc != LDAP_SUCCESS ) {
477 return LDAP_INVALID_SYNTAX;
480 assert( strlen( val->bv_val ) == val->bv_len );
483 * Schema-aware rewrite
485 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
487 return LDAP_INVALID_SYNTAX;
490 /* FIXME: not sure why the default isn't pretty */
491 /* RE: the default is the form that is used as
492 * an internal representation; the pretty form
494 rc = ldap_dn2bv( dn, out,
495 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
499 if ( rc != LDAP_SUCCESS ) {
500 return LDAP_INVALID_SYNTAX;
504 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
520 LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n",
521 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
524 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
525 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
529 if ( val->bv_len == 0 ) {
532 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
533 return LDAP_INVALID_SYNTAX;
538 /* FIXME: should be liberal in what we accept */
539 rc = ldap_bv2dn( val, dn, LDAP_DN_FORMAT_LDAP );
540 if ( rc != LDAP_SUCCESS ) {
541 return LDAP_INVALID_SYNTAX;
544 assert( strlen( val->bv_val ) == val->bv_len );
547 * Schema-aware rewrite
549 if ( LDAPDN_rewrite( *dn, flags ) != LDAP_SUCCESS ) {
552 return LDAP_INVALID_SYNTAX;
556 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
557 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
564 * Combination of both dnPretty and dnNormalize
570 struct berval *pretty,
571 struct berval *normal)
574 LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
576 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
583 if ( val->bv_len == 0 ) {
584 ber_dupbv( pretty, val );
585 ber_dupbv( normal, val );
587 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
589 return LDAP_INVALID_SYNTAX;
595 pretty->bv_val = NULL;
596 normal->bv_val = NULL;
600 /* FIXME: should be liberal in what we accept */
601 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
602 if ( rc != LDAP_SUCCESS ) {
603 return LDAP_INVALID_SYNTAX;
606 assert( strlen( val->bv_val ) == val->bv_len );
609 * Schema-aware rewrite
611 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
613 return LDAP_INVALID_SYNTAX;
616 rc = ldap_dn2bv( dn, pretty,
617 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
619 if ( rc != LDAP_SUCCESS ) {
621 return LDAP_INVALID_SYNTAX;
624 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
626 free( pretty->bv_val );
627 pretty->bv_val = NULL;
629 return LDAP_INVALID_SYNTAX;
632 rc = ldap_dn2bv( dn, normal,
633 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
636 if ( rc != LDAP_SUCCESS ) {
637 free( pretty->bv_val );
638 pretty->bv_val = NULL;
640 return LDAP_INVALID_SYNTAX;
645 LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
646 pretty->bv_val, normal->bv_val, 0 );
648 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
649 pretty->bv_val, normal->bv_val, 0 );
664 struct berval *value,
665 void *assertedValue )
668 struct berval *asserted = (struct berval *) assertedValue;
672 assert( assertedValue );
674 match = value->bv_len - asserted->bv_len;
677 match = memcmp( value->bv_val, asserted->bv_val,
682 LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n %s\n %s\n",
683 match, value->bv_val, asserted->bv_val );
685 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
686 match, value->bv_val, asserted->bv_val );
690 return( LDAP_SUCCESS );
694 * dnParent - dn's parent, in-place
696 * note: the incoming dn is assumed to be normalized/prettyfied,
697 * so that escaped rdn/ava separators are in '\'+hexpair form
706 p = strchr( dn->bv_val, ',' );
711 pdn->bv_val = dn->bv_val + dn->bv_len;
715 assert( DN_SEPARATOR( p[ 0 ] ) );
718 assert( ATTR_LEADCHAR( p[ 0 ] ) );
720 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
737 if( dn->bv_len == 0 ) {
741 rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
742 if ( rc != LDAP_SUCCESS ) {
746 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
748 ldap_rdnfree( tmpRDN );
749 if ( rc != LDAP_SUCCESS ) {
757 * We can assume the input is a prettied or normalized DN
762 struct berval *dn_in )
768 if ( dn_in == NULL ) {
772 if ( !dn_in->bv_len ) {
776 if ( be != NULL && be_issuffix( be, dn_in ) ) {
780 p = strchr( dn_in->bv_val, ',' );
782 return p ? p - dn_in->bv_val : dn_in->bv_len;
788 * LDAP_SUCCESS if rdn is a legal rdn;
789 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
792 rdnValidate( struct berval *rdn )
796 * input is a pretty or normalized DN
797 * hence, we can just search for ','
799 if( rdn == NULL || rdn->bv_len == 0 ||
800 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
802 return LDAP_INVALID_SYNTAX;
805 return strchr( rdn->bv_val, ',' ) == NULL
806 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
809 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
816 if ( rdn == NULL || rdn == '\0' ) {
823 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
824 if ( rc != LDAP_SUCCESS ) {
831 if ( p[ 0 ] != '\0' ) {
836 * Schema-aware validate
838 if ( rc == LDAP_SUCCESS ) {
839 rc = LDAPDN_validate( DN );
844 * Must validate (there's a repeated parsing ...)
846 return ( rc == LDAP_SUCCESS );
853 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
856 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
860 build_new_dn( struct berval * new_dn,
861 struct berval * parent_dn,
862 struct berval * newrdn )
866 if ( parent_dn == NULL ) {
867 ber_dupbv( new_dn, newrdn );
871 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
872 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
874 ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
876 strcpy( ptr, parent_dn->bv_val );
881 * dnIsSuffix - tells whether suffix is a suffix of dn.
882 * Both dn and suffix must be normalized.
886 const struct berval *dn,
887 const struct berval *suffix )
889 int d = dn->bv_len - suffix->bv_len;
894 /* empty suffix matches any dn */
895 if ( suffix->bv_len == 0 ) {
899 /* suffix longer than dn */
904 /* no rdn separator or escaped rdn separator */
905 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
909 /* no possible match or malformed dn */
915 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
920 * Convert an X.509 DN into a normalized LDAP DN
923 dnX509normalize( void *x509_name, struct berval *out )
925 /* Invoke the LDAP library's converter with our schema-rewriter */
926 return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
930 * Get the TLS session's peer's DN into a normalized LDAP DN
933 dnX509peerNormalize( void *ssl, struct berval *dn )
936 return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );