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[ 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 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[ 0 ][ iAVA ];
155 for ( i = 0; i < iAVA; i++ ) {
156 LDAPAVA *ava = rdn[ 0 ][ i ];
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[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
206 rdn[ 0 ][ i ] = 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[ 0 ][ iRDN ]; iRDN++ ) {
225 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
230 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
231 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
232 AttributeDescription *ad;
233 slap_syntax_transform_func *transf = NULL;
235 struct berval bv = { 0, 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 ava->la_attr = ad->ad_cname;
255 if( flags & SLAP_LDAPDN_PRETTY ) {
256 transf = ad->ad_type->sat_syntax->ssyn_pretty;
259 transf = ad->ad_type->sat_syntax->ssyn_normalize;
260 mr = ad->ad_type->sat_equality;
265 * transform value by normalize/pretty function
267 rc = ( *transf )( ad->ad_type->sat_syntax,
268 &ava->la_value, &bv );
270 if ( rc != LDAP_SUCCESS ) {
271 return LDAP_INVALID_SYNTAX;
275 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
278 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
279 : &ava->la_value, UTF8_CASEFOLD ),
285 free( ava->la_value.bv_val );
289 AVA_Sort( rdn, iAVA );
297 * dn normalize routine
303 struct berval **normalized )
308 assert( normalized && *normalized == NULL );
310 out = ch_malloc( sizeof( struct berval ) );
311 rc = dnNormalize2( syntax, val, out );
312 if ( rc != LDAP_SUCCESS )
328 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
330 if ( val->bv_len != 0 ) {
335 * Go to structural representation
337 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
338 if ( rc != LDAP_SUCCESS ) {
339 return LDAP_INVALID_SYNTAX;
343 * Schema-aware rewrite
345 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
347 return LDAP_INVALID_SYNTAX;
351 * Back to string representation
353 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
357 if ( rc != LDAP_SUCCESS ) {
358 return LDAP_INVALID_SYNTAX;
361 ber_dupbv( out, val );
364 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
370 * dn "pretty"ing routine
376 struct berval **pretty)
381 assert( pretty && *pretty == NULL );
383 out = ch_malloc( sizeof( struct berval ) );
384 rc = dnPretty2( syntax, val, out );
385 if ( rc != LDAP_SUCCESS )
401 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
403 if ( val->bv_len != 0 ) {
407 /* FIXME: should be liberal in what we accept */
408 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
409 if ( rc != LDAP_SUCCESS ) {
410 return LDAP_INVALID_SYNTAX;
414 * Schema-aware rewrite
416 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
418 return LDAP_INVALID_SYNTAX;
421 /* FIXME: not sure why the default isn't pretty */
422 /* RE: the default is the form that is used as
423 * an internal representation; the pretty form
425 rc = ldap_dn2bv( dn, out,
426 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
430 if ( rc != LDAP_SUCCESS ) {
431 return LDAP_INVALID_SYNTAX;
434 ber_dupbv( out, val );
437 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
443 * Combination of both dnPretty and dnNormalize
449 struct berval *pretty,
450 struct berval *normal)
452 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
458 if ( val->bv_len != 0 ) {
462 pretty->bv_val = NULL;
463 normal->bv_val = NULL;
467 /* FIXME: should be liberal in what we accept */
468 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
469 if ( rc != LDAP_SUCCESS ) {
470 return LDAP_INVALID_SYNTAX;
474 * Schema-aware rewrite
476 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
478 return LDAP_INVALID_SYNTAX;
481 rc = ldap_dn2bv( dn, pretty,
482 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
484 if ( rc != LDAP_SUCCESS ) {
486 return LDAP_INVALID_SYNTAX;
489 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
491 free( pretty->bv_val );
492 pretty->bv_val = NULL;
494 return LDAP_INVALID_SYNTAX;
497 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
500 if ( rc != LDAP_SUCCESS ) {
501 free( pretty->bv_val );
502 pretty->bv_val = NULL;
504 return LDAP_INVALID_SYNTAX;
507 ber_dupbv( pretty, val );
508 ber_dupbv( normal, val );
511 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
512 pretty->bv_val, normal->bv_val, 0 );
526 struct berval *value,
527 void *assertedValue )
530 struct berval *asserted = (struct berval *) assertedValue;
534 assert( assertedValue );
536 match = value->bv_len - asserted->bv_len;
539 match = strcmp( value->bv_val, asserted->bv_val );
543 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
544 "dnMatch: %d\n %s\n %s\n", match,
545 value->bv_val, asserted->bv_val ));
547 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
548 match, value->bv_val, asserted->bv_val );
552 return( LDAP_SUCCESS );
555 #ifdef SLAP_DN_MIGRATION
557 * these routines are provided for migration purposes only!
558 * dn_validate is deprecated in favor of dnValidate
559 * dn_normalize is deprecated in favor of dnNormalize
560 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
562 * other routines are likewise deprecated but may not yet have
563 * replacement functions.
567 * dn_validate - validate and compress dn. the dn is
568 * compressed in place are returned if valid.
569 * Deprecated in favor of dnValidate()
572 dn_validate( char *dn )
575 struct berval *pretty = NULL;
578 if ( dn == NULL || dn[0] == '\0' ) {
583 val.bv_len = strlen( dn );
585 rc = dnPretty( NULL, &val, &pretty );
586 if ( rc != LDAP_SUCCESS ) {
590 if ( val.bv_len < pretty->bv_len ) {
591 ber_bvfree( pretty );
595 AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
596 ber_bvfree( pretty );
602 * dn_normalize - put dn into a canonical form suitable for storing
603 * in a hash database. this involves normalizing the case as well as
604 * the format. the dn is normalized in place as well as returned if valid.
605 * Deprecated in favor of dnNormalize()
608 dn_normalize( char *dn )
611 struct berval *normalized = NULL;
614 if ( dn == NULL || dn[0] == '\0' ) {
619 val.bv_len = strlen( dn );
621 rc = dnNormalize( NULL, &val, &normalized );
622 if ( rc != LDAP_SUCCESS ) {
626 if ( val.bv_len < normalized->bv_len ) {
627 ber_bvfree( normalized );
631 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
632 ber_bvfree( normalized );
638 * dnParent - dn's parent, in-place
648 rc = ldap_str2rdn( dn, NULL, &p, LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
649 if ( rc != LDAP_SUCCESS ) {
653 assert( DN_SEPARATOR( p[ 0 ] ) );
656 while ( ASCII_SPACE( p[ 0 ] ) ) {
666 * dn_parent - return the dn's parent, in-place
667 * FIXME: should be replaced by dnParent()
680 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
684 if ( dn[ 0 ] == '\0' ) {
688 if ( be != NULL && be_issuffix( be, dn ) ) {
692 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
696 return ( char * )pdn;
711 if( dn->bv_len == 0 ) {
715 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
716 if ( rc != LDAP_SUCCESS ) {
720 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
721 ldap_rdnfree( tmpRDN );
722 if ( rc != LDAP_SUCCESS ) {
730 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
735 struct berval *dn_in )
742 if ( dn_in == NULL ) {
746 if ( !dn_in->bv_len ) {
750 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
754 rc = ldap_str2rdn( dn_in->bv_val, NULL, &p,
755 LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
756 if ( rc != LDAP_SUCCESS ) {
760 return p - dn_in->bv_val;
764 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
768 struct berval *dn_in )
774 if ( dn_in == NULL ) {
778 if ( !dn_in->bv_len ) {
782 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
786 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
794 * dn_issuffix - tells whether suffix is a suffix of dn.
795 * Both dn and suffix must be normalized.
796 * deprecated in favor of dnIsSuffix()
804 struct berval bvdn, bvsuffix;
809 bvdn.bv_val = (char *) dn;
810 bvdn.bv_len = strlen( dn );
811 bvsuffix.bv_val = (char *) suffix;
812 bvsuffix.bv_len = strlen( suffix );
814 return dnIsSuffix( &bvdn, &bvsuffix );
819 * Given a string (i.e. an rdn) of the form:
820 * "attribute_type = attribute_value"
821 * this function returns the type of an attribute, that is the
822 * string "attribute_type" which is placed in newly allocated
823 * memory. The returned string will be null-terminated.
828 char * rdn_attr_type( const char * s )
830 char **attrs = NULL, **values = NULL, *retval;
832 if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
836 retval = ch_strdup( attrs[ 0 ] );
838 charray_free( attrs );
839 charray_free( values );
847 * Given a string (i.e. an rdn) of the form:
848 * "attribute_type = attribute_value"
849 * this function returns "attribute_type" which is placed in newly allocated
850 * memory. The returned string will be null-terminated and may contain
851 * spaces (i.e. "John Doe\0").
857 rdn_attr_value( const char * rdn )
859 char **values = NULL, *retval;
861 if ( rdn_attrs( rdn, NULL, &values ) != LDAP_SUCCESS ) {
865 retval = ch_strdup( values[ 0 ] );
867 charray_free( values );
875 * Given a string (i.e. an rdn) of the form:
876 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
877 * this function stores the types of the attributes in ptypes, that is the
878 * array of strings "attribute_type" which is placed in newly allocated
879 * memory, and the values of the attributes in pvalues, that is the
880 * array of strings "attribute_value" which is placed in newly allocated
881 * memory. Returns 0 on success, -1 on failure.
883 * note: got part of the code from dn_validate
885 * Deprecated; directly use LDAPRDN from ldap_str2rdn
888 rdn_attrs( const char * rdn, char ***types, char ***values)
897 assert( *values == NULL );
898 assert( types == NULL || *types == NULL );
900 rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
901 if ( rc != LDAP_SUCCESS ) {
907 * FIXME: should we complain if the rdn is actually a dn?
909 if ( p[ 0 ] != '\0' ) {
910 ldap_rdnfree( tmpRDN );
911 return LDAP_INVALID_DN_SYNTAX;
915 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
916 LDAPAVA *ava = tmpRDN[ 0 ][ iAVA ];
919 assert( ava->la_attr.bv_val );
920 assert( ava->la_value.bv_val );
923 charray_add_n( types, ava->la_attr.bv_val,
924 ava->la_attr.bv_len );
926 charray_add_n( values, ava->la_value.bv_val,
927 ava->la_value.bv_len );
930 ldap_rdnfree( tmpRDN );
938 * LDAP_SUCCESS if rdn is a legal rdn;
939 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
942 rdnValidate( struct berval *rdn )
946 * input is a pretty or normalized DN
947 * hence, we can just search for ','
949 if( rdn == NULL || rdn->bv_len == 0 ) {
950 return LDAP_INVALID_SYNTAX;
953 return strchr( rdn->bv_val, ',' ) == NULL
954 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
957 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
964 if ( rdn == NULL || rdn == '\0' ) {
971 rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
972 if ( rc != LDAP_SUCCESS ) {
979 if ( p[ 0 ] != '\0' ) {
984 * Schema-aware validate
986 if ( rc == LDAP_SUCCESS ) {
987 rc = LDAPDN_validate( DN );
992 * Must validate (there's a repeated parsing ...)
994 return ( rc == LDAP_SUCCESS );
1001 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1004 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1008 build_new_dn( struct berval * new_dn,
1009 struct berval * parent_dn,
1010 struct berval * newrdn )
1014 if ( parent_dn == NULL ) {
1015 ber_dupbv( new_dn, newrdn );
1019 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1020 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
1022 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
1024 strcpy( ptr, parent_dn->bv_val );
1027 #endif /* SLAP_DN_MIGRATION */
1030 * dnIsSuffix - tells whether suffix is a suffix of dn.
1031 * Both dn and suffix must be normalized.
1035 const struct berval *dn,
1036 const struct berval *suffix )
1038 int d = dn->bv_len - suffix->bv_len;
1043 /* empty suffix matches any dn */
1044 if ( suffix->bv_len == 0 ) {
1048 /* suffix longer than dn */
1053 /* no rdn separator or escaped rdn separator */
1054 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
1055 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1059 /* no possible match or malformed dn */
1065 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );