1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
21 #define SLAP_LDAPDN_PRETTY 0x1
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[ iRDN ]; iRDN++ ) {
53 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
58 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
59 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
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 free( ava->la_attr.bv_val );
80 ber_dupbv( &ava->la_attr, &ad->ad_cname );
82 validate = ad->ad_type->sat_syntax->ssyn_validate;
86 * validate value by validate function
88 rc = ( *validate )( ad->ad_type->sat_syntax,
91 if ( rc != LDAP_SUCCESS ) {
92 return LDAP_INVALID_SYNTAX;
102 * dn validate routine
114 if ( in->bv_len == 0 ) {
115 return( LDAP_SUCCESS );
118 rc = ldap_str2dn( in->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
121 * Schema-aware validate
123 if ( rc == LDAP_SUCCESS ) {
124 rc = LDAPDN_validate( dn );
128 if ( rc != LDAP_SUCCESS ) {
129 return( LDAP_INVALID_SYNTAX );
132 return( LDAP_SUCCESS );
136 * AVA sorting inside a RDN
138 * rule: sort attributeTypes in alphabetical order; in case of multiple
139 * occurrences of the same attributeType, sort values in byte order
140 * (use memcmp, which implies alphabetical order in case of IA5 value;
141 * this should guarantee the repeatability of the operation).
143 * uses a linear search; should be fine since the number of AVAs in
144 * a RDN should be limited.
147 AVA_Sort( LDAPRDN *rdn, int iAVA )
150 LDAPAVA *ava_in = rdn[ iAVA ][ 0 ];
155 for ( i = 0; i < iAVA; i++ ) {
156 LDAPAVA *ava = rdn[ i ][ 0 ];
161 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
170 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
172 v = memcmp( ava_in->la_value.bv_val,
173 ava->la_value.bv_val,
174 d <= 0 ? ava_in->la_value.bv_len
175 : ava->la_value.bv_len );
177 if ( v == 0 && d != 0 ) {
196 a = strcmp( ava_in->la_value.bv_val,
197 ava->la_value.bv_val );
203 for ( j = iAVA; j > i; j-- ) {
204 rdn[ j ][ 0 ] = rdn[ j - 1 ][ 0 ];
206 rdn[ i ][ 0 ] = ava_in;
213 * In-place, schema-aware normalization / "pretty"ing of the
214 * structural representation of a distinguished name.
217 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
224 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
225 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
230 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
231 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
232 AttributeDescription *ad;
233 slap_syntax_transform_func *transf = NULL;
239 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
240 const char *text = NULL;
242 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
243 if ( rc != LDAP_SUCCESS ) {
244 return LDAP_INVALID_SYNTAX;
247 ava->la_private = ( void * )ad;
251 * Replace attr oid/name with the canonical name
253 free( ava->la_attr.bv_val );
254 ber_dupbv( &ava->la_attr, &ad->ad_cname );
256 if( flags & SLAP_LDAPDN_PRETTY ) {
257 transf = ad->ad_type->sat_syntax->ssyn_pretty;
260 transf = ad->ad_type->sat_syntax->ssyn_normalize;
261 mr = ad->ad_type->sat_equality;
266 * transform value by normalize/pretty function
268 rc = ( *transf )( ad->ad_type->sat_syntax,
269 &ava->la_value, &bv );
271 if ( rc != LDAP_SUCCESS ) {
272 return LDAP_INVALID_SYNTAX;
276 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
279 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
280 : &ava->la_value, UTF8_CASEFOLD ),
286 free( ava->la_value.bv_val );
290 AVA_Sort( rdn, iAVA );
298 * dn normalize routine
304 struct berval **normalized )
309 assert( normalized && *normalized == NULL );
311 out = ch_malloc( sizeof( struct berval ) );
312 rc = dnNormalize2( syntax, val, out );
313 if ( rc != LDAP_SUCCESS )
329 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
331 if ( val->bv_len != 0 ) {
336 * Go to structural representation
338 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
339 if ( rc != LDAP_SUCCESS ) {
340 return LDAP_INVALID_SYNTAX;
344 * Schema-aware rewrite
346 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
348 return LDAP_INVALID_SYNTAX;
352 * Back to string representation
354 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
358 if ( rc != LDAP_SUCCESS ) {
359 return LDAP_INVALID_SYNTAX;
362 ber_dupbv( out, val );
365 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
371 * dn "pretty"ing routine
377 struct berval **pretty)
382 assert( pretty && *pretty == NULL );
384 out = ch_malloc( sizeof( struct berval ) );
385 rc = dnPretty2( syntax, val, out );
386 if ( rc != LDAP_SUCCESS )
402 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
404 if ( val->bv_len != 0 ) {
408 /* FIXME: should be liberal in what we accept */
409 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
410 if ( rc != LDAP_SUCCESS ) {
411 return LDAP_INVALID_SYNTAX;
415 * Schema-aware rewrite
417 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
419 return LDAP_INVALID_SYNTAX;
422 /* FIXME: not sure why the default isn't pretty */
423 /* RE: the default is the form that is used as
424 * an internal representation; the pretty form
426 rc = ldap_dn2bv( dn, out,
427 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
431 if ( rc != LDAP_SUCCESS ) {
432 return LDAP_INVALID_SYNTAX;
435 ber_dupbv( out, val );
438 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
444 * Combination of both dnPretty and dnNormalize
450 struct berval *pretty,
451 struct berval *normal)
453 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
459 if ( val->bv_len != 0 ) {
463 pretty->bv_val = NULL;
464 normal->bv_val = NULL;
468 /* FIXME: should be liberal in what we accept */
469 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
470 if ( rc != LDAP_SUCCESS ) {
471 return LDAP_INVALID_SYNTAX;
475 * Schema-aware rewrite
477 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
479 return LDAP_INVALID_SYNTAX;
482 rc = ldap_dn2bv( dn, pretty,
483 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
485 if ( rc != LDAP_SUCCESS ) {
487 return LDAP_INVALID_SYNTAX;
490 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
492 free( pretty->bv_val );
493 pretty->bv_val = NULL;
495 return LDAP_INVALID_SYNTAX;
498 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
501 if ( rc != LDAP_SUCCESS ) {
502 free( pretty->bv_val );
503 pretty->bv_val = NULL;
505 return LDAP_INVALID_SYNTAX;
508 ber_dupbv( pretty, val );
509 ber_dupbv( normal, val );
512 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
513 pretty->bv_val, normal->bv_val, 0 );
527 struct berval *value,
528 void *assertedValue )
531 struct berval *asserted = (struct berval *) assertedValue;
535 assert( assertedValue );
537 match = value->bv_len - asserted->bv_len;
540 match = strcmp( value->bv_val, asserted->bv_val );
544 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
545 "dnMatch: %d\n %s\n %s\n", match,
546 value->bv_val, asserted->bv_val ));
548 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
549 match, value->bv_val, asserted->bv_val );
553 return( LDAP_SUCCESS );
556 #ifdef SLAP_DN_MIGRATION
558 * these routines are provided for migration purposes only!
559 * dn_validate is deprecated in favor of dnValidate
560 * dn_normalize is deprecated in favor of dnNormalize
561 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
563 * other routines are likewise deprecated but may not yet have
564 * replacement functions.
568 * dn_validate - validate and compress dn. the dn is
569 * compressed in place are returned if valid.
570 * Deprecated in favor of dnValidate()
573 dn_validate( char *dn )
576 struct berval *pretty = NULL;
579 if ( dn == NULL || dn[0] == '\0' ) {
584 val.bv_len = strlen( dn );
586 rc = dnPretty( NULL, &val, &pretty );
587 if ( rc != LDAP_SUCCESS ) {
591 if ( val.bv_len < pretty->bv_len ) {
592 ber_bvfree( pretty );
596 AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
597 ber_bvfree( pretty );
603 * dn_normalize - put dn into a canonical form suitable for storing
604 * in a hash database. this involves normalizing the case as well as
605 * the format. the dn is normalized in place as well as returned if valid.
606 * Deprecated in favor of dnNormalize()
609 dn_normalize( char *dn )
612 struct berval *normalized = NULL;
615 if ( dn == NULL || dn[0] == '\0' ) {
620 val.bv_len = strlen( dn );
622 rc = dnNormalize( NULL, &val, &normalized );
623 if ( rc != LDAP_SUCCESS ) {
627 if ( val.bv_len < normalized->bv_len ) {
628 ber_bvfree( normalized );
632 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
633 ber_bvfree( normalized );
639 * dnParent - dn's parent, in-place
649 rc = ldap_str2rdn( dn, NULL, &p, LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
650 if ( rc != LDAP_SUCCESS ) {
654 assert( DN_SEPARATOR( p[ 0 ] ) );
657 while ( ASCII_SPACE( p[ 0 ] ) ) {
667 * dn_parent - return the dn's parent, in-place
668 * FIXME: should be replaced by dnParent()
681 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
685 if ( dn[ 0 ] == '\0' ) {
689 if ( be != NULL && be_issuffix( be, dn ) ) {
693 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
697 return ( char * )pdn;
712 if( dn->bv_len == 0 ) {
716 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
717 if ( rc != LDAP_SUCCESS ) {
721 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
722 ldap_rdnfree( tmpRDN );
723 if ( rc != LDAP_SUCCESS ) {
731 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
736 struct berval *dn_in )
743 if ( dn_in == NULL ) {
747 if ( !dn_in->bv_len ) {
751 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
755 rc = ldap_str2rdn( dn_in->bv_val, NULL, &p,
756 LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
757 if ( rc != LDAP_SUCCESS ) {
761 return p - dn_in->bv_val;
765 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
769 struct berval *dn_in )
775 if ( dn_in == NULL ) {
779 if ( !dn_in->bv_len ) {
783 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
787 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
795 * dn_issuffix - tells whether suffix is a suffix of dn.
796 * Both dn and suffix must be normalized.
797 * deprecated in favor of dnIsSuffix()
805 struct berval bvdn, bvsuffix;
810 bvdn.bv_val = (char *) dn;
811 bvdn.bv_len = strlen( dn );
812 bvsuffix.bv_val = (char *) suffix;
813 bvsuffix.bv_len = strlen( suffix );
815 return dnIsSuffix( &bvdn, &bvsuffix );
820 * Given a string (i.e. an rdn) of the form:
821 * "attribute_type = attribute_value"
822 * this function returns the type of an attribute, that is the
823 * string "attribute_type" which is placed in newly allocated
824 * memory. The returned string will be null-terminated.
829 char * rdn_attr_type( const char * s )
831 char **attrs = NULL, **values = NULL, *retval;
833 if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
837 retval = ch_strdup( attrs[ 0 ] );
839 charray_free( attrs );
840 charray_free( values );
848 * Given a string (i.e. an rdn) of the form:
849 * "attribute_type = attribute_value"
850 * this function returns "attribute_type" which is placed in newly allocated
851 * memory. The returned string will be null-terminated and may contain
852 * spaces (i.e. "John Doe\0").
858 rdn_attr_value( const char * rdn )
860 char **values = NULL, *retval;
862 if ( rdn_attrs( rdn, NULL, &values ) != LDAP_SUCCESS ) {
866 retval = ch_strdup( values[ 0 ] );
868 charray_free( values );
876 * Given a string (i.e. an rdn) of the form:
877 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
878 * this function stores the types of the attributes in ptypes, that is the
879 * array of strings "attribute_type" which is placed in newly allocated
880 * memory, and the values of the attributes in pvalues, that is the
881 * array of strings "attribute_value" which is placed in newly allocated
882 * memory. Returns 0 on success, -1 on failure.
884 * note: got part of the code from dn_validate
886 * Deprecated; directly use LDAPRDN from ldap_str2rdn
889 rdn_attrs( const char * rdn, char ***types, char ***values)
898 assert( *values == NULL );
899 assert( types == NULL || *types == NULL );
901 rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
902 if ( rc != LDAP_SUCCESS ) {
908 * FIXME: should we complain if the rdn is actually a dn?
910 if ( p[ 0 ] != '\0' ) {
911 ldap_rdnfree( tmpRDN );
912 return LDAP_INVALID_DN_SYNTAX;
916 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
917 LDAPAVA *ava = tmpRDN[ iAVA ][ 0 ];
920 assert( ava->la_attr.bv_val );
921 assert( ava->la_value.bv_val );
924 charray_add_n( types, ava->la_attr.bv_val,
925 ava->la_attr.bv_len );
927 charray_add_n( values, ava->la_value.bv_val,
928 ava->la_value.bv_len );
931 ldap_rdnfree( tmpRDN );
939 * LDAP_SUCCESS if rdn is a legal rdn;
940 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
943 rdnValidate( struct berval *rdn )
947 * input is a pretty or normalized DN
948 * hence, we can just search for ','
950 if( rdn == NULL || rdn->bv_len == 0 ) {
951 return LDAP_INVALID_SYNTAX;
954 return strchr( rdn->bv_val, ',' ) == NULL
955 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
958 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
965 if ( rdn == NULL || rdn == '\0' ) {
972 rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
973 if ( rc != LDAP_SUCCESS ) {
980 if ( p[ 0 ] != '\0' ) {
985 * Schema-aware validate
987 if ( rc == LDAP_SUCCESS ) {
988 rc = LDAPDN_validate( DN );
993 * Must validate (there's a repeated parsing ...)
995 return ( rc == LDAP_SUCCESS );
1002 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1005 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1009 build_new_dn( struct berval * new_dn,
1010 struct berval * parent_dn,
1011 struct berval * newrdn )
1015 if ( parent_dn == NULL ) {
1016 ber_dupbv( new_dn, newrdn );
1020 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1021 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
1023 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
1025 strcpy( ptr, parent_dn->bv_val );
1028 #endif /* SLAP_DN_MIGRATION */
1031 * dnIsSuffix - tells whether suffix is a suffix of dn.
1032 * Both dn and suffix must be normalized.
1036 const struct berval *dn,
1037 const struct berval *suffix )
1039 int d = dn->bv_len - suffix->bv_len;
1044 /* empty suffix matches any dn */
1045 if ( suffix->bv_len == 0 ) {
1049 /* suffix longer than dn */
1054 /* no rdn separator or escaped rdn separator */
1055 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
1056 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1060 /* no possible match or malformed dn */
1066 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );