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;
235 struct berval *bv = 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 ) ) {
277 struct berval *s = bv;
279 bv = ber_bvstr( UTF8normalize( bv ? bv : &ava->la_value,
286 free( ava->la_value.bv_val );
291 AVA_Sort( rdn, iAVA );
299 * dn normalize routine
305 struct berval **normalized )
310 assert( normalized && *normalized == NULL );
312 out = ch_malloc( sizeof( struct berval ) );
313 rc = dnNormalize2( syntax, val, out );
314 if ( rc != LDAP_SUCCESS )
330 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
332 if ( val->bv_len != 0 ) {
337 * Go to structural representation
339 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
340 if ( rc != LDAP_SUCCESS ) {
341 return LDAP_INVALID_SYNTAX;
345 * Schema-aware rewrite
347 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
349 return LDAP_INVALID_SYNTAX;
353 * Back to string representation
355 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
359 if ( rc != LDAP_SUCCESS ) {
360 return LDAP_INVALID_SYNTAX;
363 ber_dupbv( out, val );
366 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
372 * dn "pretty"ing routine
378 struct berval **pretty)
383 assert( pretty && *pretty == NULL );
385 out = ch_malloc( sizeof( struct berval ) );
386 rc = dnPretty2( syntax, val, out );
387 if ( rc != LDAP_SUCCESS )
403 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
405 if ( val->bv_len != 0 ) {
409 /* FIXME: should be liberal in what we accept */
410 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
411 if ( rc != LDAP_SUCCESS ) {
412 return LDAP_INVALID_SYNTAX;
416 * Schema-aware rewrite
418 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
420 return LDAP_INVALID_SYNTAX;
423 /* FIXME: not sure why the default isn't pretty */
424 /* RE: the default is the form that is used as
425 * an internal representation; the pretty form
427 rc = ldap_dn2bv( dn, out,
428 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
432 if ( rc != LDAP_SUCCESS ) {
433 return LDAP_INVALID_SYNTAX;
436 ber_dupbv( out, val );
439 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
445 * Combination of both dnPretty and dnNormalize
451 struct berval *pretty,
452 struct berval *normal)
454 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
460 if ( val->bv_len != 0 ) {
464 pretty->bv_val = NULL;
465 normal->bv_val = NULL;
469 /* FIXME: should be liberal in what we accept */
470 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
471 if ( rc != LDAP_SUCCESS ) {
472 return LDAP_INVALID_SYNTAX;
476 * Schema-aware rewrite
478 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
480 return LDAP_INVALID_SYNTAX;
483 rc = ldap_dn2bv( dn, pretty,
484 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
486 if ( rc != LDAP_SUCCESS ) {
488 return LDAP_INVALID_SYNTAX;
491 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
493 free( pretty->bv_val );
494 pretty->bv_val = NULL;
496 return LDAP_INVALID_SYNTAX;
499 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
502 if ( rc != LDAP_SUCCESS ) {
503 free( pretty->bv_val );
504 pretty->bv_val = NULL;
506 return LDAP_INVALID_SYNTAX;
509 ber_dupbv( pretty, val );
510 ber_dupbv( normal, val );
513 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
514 pretty->bv_val, normal->bv_val, 0 );
528 struct berval *value,
529 void *assertedValue )
532 struct berval *asserted = (struct berval *) assertedValue;
536 assert( assertedValue );
538 match = value->bv_len - asserted->bv_len;
541 match = strcmp( value->bv_val, asserted->bv_val );
545 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
546 "dnMatch: %d\n %s\n %s\n", match,
547 value->bv_val, asserted->bv_val ));
549 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
550 match, value->bv_val, asserted->bv_val );
554 return( LDAP_SUCCESS );
557 #ifdef SLAP_DN_MIGRATION
559 * these routines are provided for migration purposes only!
560 * dn_validate is deprecated in favor of dnValidate
561 * dn_normalize is deprecated in favor of dnNormalize
562 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
564 * other routines are likewise deprecated but may not yet have
565 * replacement functions.
569 * dn_validate - validate and compress dn. the dn is
570 * compressed in place are returned if valid.
571 * Deprecated in favor of dnValidate()
574 dn_validate( char *dn )
577 struct berval *pretty = NULL;
580 if ( dn == NULL || dn[0] == '\0' ) {
585 val.bv_len = strlen( dn );
587 rc = dnPretty( NULL, &val, &pretty );
588 if ( rc != LDAP_SUCCESS ) {
592 if ( val.bv_len < pretty->bv_len ) {
593 ber_bvfree( pretty );
597 AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
598 ber_bvfree( pretty );
604 * dn_normalize - put dn into a canonical form suitable for storing
605 * in a hash database. this involves normalizing the case as well as
606 * the format. the dn is normalized in place as well as returned if valid.
607 * Deprecated in favor of dnNormalize()
610 dn_normalize( char *dn )
613 struct berval *normalized = NULL;
616 if ( dn == NULL || dn[0] == '\0' ) {
621 val.bv_len = strlen( dn );
623 rc = dnNormalize( NULL, &val, &normalized );
624 if ( rc != LDAP_SUCCESS ) {
628 if ( val.bv_len < normalized->bv_len ) {
629 ber_bvfree( normalized );
633 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
634 ber_bvfree( normalized );
640 * dnParent - dn's parent, in-place
650 rc = ldap_str2rdn( dn, NULL, &p, LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
651 if ( rc != LDAP_SUCCESS ) {
655 assert( DN_SEPARATOR( p[ 0 ] ) );
658 while ( ASCII_SPACE( p[ 0 ] ) ) {
668 * dn_parent - return the dn's parent, in-place
669 * FIXME: should be replaced by dnParent()
682 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
686 if ( dn[ 0 ] == '\0' ) {
690 if ( be != NULL && be_issuffix( be, dn ) ) {
694 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
698 return ( char * )pdn;
713 if( dn->bv_len == 0 ) {
717 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
718 if ( rc != LDAP_SUCCESS ) {
722 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
723 ldap_rdnfree( tmpRDN );
724 if ( rc != LDAP_SUCCESS ) {
732 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
737 struct berval *dn_in )
744 if ( dn_in == NULL ) {
748 if ( !dn_in->bv_len ) {
752 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
756 rc = ldap_str2rdn( dn_in->bv_val, NULL, &p,
757 LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
758 if ( rc != LDAP_SUCCESS ) {
762 return p - dn_in->bv_val;
766 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
770 struct berval *dn_in )
776 if ( dn_in == NULL ) {
780 if ( !dn_in->bv_len ) {
784 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
788 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
796 * dn_issuffix - tells whether suffix is a suffix of dn.
797 * Both dn and suffix must be normalized.
798 * deprecated in favor of dnIsSuffix()
806 struct berval bvdn, bvsuffix;
811 bvdn.bv_val = (char *) dn;
812 bvdn.bv_len = strlen( dn );
813 bvsuffix.bv_val = (char *) suffix;
814 bvsuffix.bv_len = strlen( suffix );
816 return dnIsSuffix( &bvdn, &bvsuffix );
821 * Given a string (i.e. an rdn) of the form:
822 * "attribute_type = attribute_value"
823 * this function returns the type of an attribute, that is the
824 * string "attribute_type" which is placed in newly allocated
825 * memory. The returned string will be null-terminated.
830 char * rdn_attr_type( const char * s )
832 char **attrs = NULL, **values = NULL, *retval;
834 if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
838 retval = ch_strdup( attrs[ 0 ] );
840 charray_free( attrs );
841 charray_free( values );
849 * Given a string (i.e. an rdn) of the form:
850 * "attribute_type = attribute_value"
851 * this function returns "attribute_type" which is placed in newly allocated
852 * memory. The returned string will be null-terminated and may contain
853 * spaces (i.e. "John Doe\0").
859 rdn_attr_value( const char * rdn )
861 char **values = NULL, *retval;
863 if ( rdn_attrs( rdn, NULL, &values ) != LDAP_SUCCESS ) {
867 retval = ch_strdup( values[ 0 ] );
869 charray_free( values );
877 * Given a string (i.e. an rdn) of the form:
878 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
879 * this function stores the types of the attributes in ptypes, that is the
880 * array of strings "attribute_type" which is placed in newly allocated
881 * memory, and the values of the attributes in pvalues, that is the
882 * array of strings "attribute_value" which is placed in newly allocated
883 * memory. Returns 0 on success, -1 on failure.
885 * note: got part of the code from dn_validate
887 * Deprecated; directly use LDAPRDN from ldap_str2rdn
890 rdn_attrs( const char * rdn, char ***types, char ***values)
899 assert( *values == NULL );
900 assert( types == NULL || *types == NULL );
902 rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
903 if ( rc != LDAP_SUCCESS ) {
909 * FIXME: should we complain if the rdn is actually a dn?
911 if ( p[ 0 ] != '\0' ) {
912 ldap_rdnfree( tmpRDN );
913 return LDAP_INVALID_DN_SYNTAX;
917 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
918 LDAPAVA *ava = tmpRDN[ iAVA ][ 0 ];
921 assert( ava->la_attr.bv_val );
922 assert( ava->la_value.bv_val );
925 charray_add_n( types, ava->la_attr.bv_val,
926 ava->la_attr.bv_len );
928 charray_add_n( values, ava->la_value.bv_val,
929 ava->la_value.bv_len );
932 ldap_rdnfree( tmpRDN );
940 * LDAP_SUCCESS if rdn is a legal rdn;
941 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
944 rdnValidate( struct berval *rdn )
948 * input is a pretty or normalized DN
949 * hence, we can just search for ','
951 if( rdn == NULL || rdn->bv_len == 0 ) {
952 return LDAP_INVALID_SYNTAX;
955 return strchr( rdn->bv_val, ',' ) == NULL
956 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
959 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
966 if ( rdn == NULL || rdn == '\0' ) {
973 rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
974 if ( rc != LDAP_SUCCESS ) {
981 if ( p[ 0 ] != '\0' ) {
986 * Schema-aware validate
988 if ( rc == LDAP_SUCCESS ) {
989 rc = LDAPDN_validate( DN );
994 * Must validate (there's a repeated parsing ...)
996 return ( rc == LDAP_SUCCESS );
1003 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1006 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1010 build_new_dn( struct berval * new_dn,
1011 struct berval * parent_dn,
1012 struct berval * newrdn )
1016 if ( parent_dn == NULL ) {
1017 ber_dupbv( new_dn, newrdn );
1021 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1022 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
1024 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
1026 strcpy( ptr, parent_dn->bv_val );
1029 #endif /* SLAP_DN_MIGRATION */
1032 * dnIsSuffix - tells whether suffix is a suffix of dn.
1033 * Both dn and suffix must be normalized.
1037 const struct berval *dn,
1038 const struct berval *suffix )
1040 int d = dn->bv_len - suffix->bv_len;
1045 /* empty suffix matches any dn */
1046 if ( suffix->bv_len == 0 ) {
1050 /* suffix longer than dn */
1055 /* no rdn separator or escaped rdn separator */
1056 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
1057 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1061 /* no possible match or malformed dn */
1067 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );