1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-2002 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 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 ) {
114 return( LDAP_SUCCESS );
117 rc = ldap_str2dn( in->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
120 * Schema-aware validate
122 if ( rc == LDAP_SUCCESS ) {
123 rc = LDAPDN_validate( dn );
127 if ( rc != LDAP_SUCCESS ) {
128 return( LDAP_INVALID_SYNTAX );
131 return( LDAP_SUCCESS );
135 * AVA sorting inside a RDN
137 * rule: sort attributeTypes in alphabetical order; in case of multiple
138 * occurrences of the same attributeType, sort values in byte order
139 * (use memcmp, which implies alphabetical order in case of IA5 value;
140 * this should guarantee the repeatability of the operation).
142 * uses a linear search; should be fine since the number of AVAs in
143 * a RDN should be limited.
146 AVA_Sort( LDAPRDN *rdn, int iAVA )
149 LDAPAVA *ava_in = rdn[ 0 ][ iAVA ];
154 for ( i = 0; i < iAVA; i++ ) {
155 LDAPAVA *ava = rdn[ 0 ][ i ];
160 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
169 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
171 v = memcmp( ava_in->la_value.bv_val,
172 ava->la_value.bv_val,
173 d <= 0 ? ava_in->la_value.bv_len
174 : ava->la_value.bv_len );
176 if ( v == 0 && d != 0 ) {
195 a = strcmp( ava_in->la_value.bv_val,
196 ava->la_value.bv_val );
202 for ( j = iAVA; j > i; j-- ) {
203 rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
205 rdn[ 0 ][ i ] = ava_in;
212 * In-place, schema-aware normalization / "pretty"ing of the
213 * structural representation of a distinguished name.
216 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
223 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
224 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
229 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
230 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
231 AttributeDescription *ad;
232 slap_syntax_transform_func *transf = NULL;
234 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;
252 * Replace attr oid/name with the canonical name
254 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, LDAP_UTF8_CASEFOLD ),
286 free( ava->la_value.bv_val );
290 if( do_sort ) 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, (char **)&p,
650 LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
651 if ( rc != LDAP_SUCCESS ) {
661 assert( DN_SEPARATOR( p[ 0 ] ) );
664 while ( ASCII_SPACE( p[ 0 ] ) ) {
674 * dn_parent - return the dn's parent, in-place
675 * FIXME: should be replaced by dnParent()
688 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
692 if ( dn[ 0 ] == '\0' ) {
696 if ( be != NULL && be_issuffix( be, dn ) ) {
700 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
704 return ( char * )pdn;
719 if( dn->bv_len == 0 ) {
723 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
724 if ( rc != LDAP_SUCCESS ) {
728 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
729 ldap_rdnfree( tmpRDN );
730 if ( rc != LDAP_SUCCESS ) {
738 * FIXME: should be replaced by dnExtractRdn()
743 struct berval *dn_in )
750 if ( dn_in == NULL ) {
754 if ( !dn_in->bv_len ) {
758 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
762 rc = ldap_str2rdn( dn_in->bv_val, NULL, (char **)&p,
763 LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
764 if ( rc != LDAP_SUCCESS ) {
768 return p - dn_in->bv_val;
773 * Given a string (i.e. an rdn) of the form:
774 * "attribute_type = attribute_value"
775 * this function returns the type of an attribute, that is the
776 * string "attribute_type" which is placed in newly allocated
777 * memory. The returned string will be null-terminated.
782 char * rdn_attr_type( const char * s )
784 char **attrs = NULL, **values = NULL, *retval;
786 if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
790 retval = ch_strdup( attrs[ 0 ] );
792 charray_free( attrs );
793 charray_free( values );
801 * Given a string (i.e. an rdn) of the form:
802 * "attribute_type = attribute_value"
803 * this function returns "attribute_type" which is placed in newly allocated
804 * memory. The returned string will be null-terminated and may contain
805 * spaces (i.e. "John Doe\0").
811 rdn_attr_value( const char * rdn )
813 char **values = NULL, *retval;
815 if ( rdn_attrs( rdn, NULL, &values ) != LDAP_SUCCESS ) {
819 retval = ch_strdup( values[ 0 ] );
821 charray_free( values );
829 * Given a string (i.e. an rdn) of the form:
830 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
831 * this function stores the types of the attributes in ptypes, that is the
832 * array of strings "attribute_type" which is placed in newly allocated
833 * memory, and the values of the attributes in pvalues, that is the
834 * array of strings "attribute_value" which is placed in newly allocated
835 * memory. Returns 0 on success, -1 on failure.
837 * note: got part of the code from dn_validate
839 * Deprecated; directly use LDAPRDN from ldap_str2rdn
842 rdn_attrs( const char * rdn, char ***types, char ***values)
851 assert( *values == NULL );
852 assert( types == NULL || *types == NULL );
854 rc = ldap_str2rdn( rdn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
855 if ( rc != LDAP_SUCCESS ) {
861 * FIXME: should we complain if the rdn is actually a dn?
863 if ( p[ 0 ] != '\0' ) {
864 ldap_rdnfree( tmpRDN );
865 return LDAP_INVALID_DN_SYNTAX;
869 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
870 LDAPAVA *ava = tmpRDN[ 0 ][ iAVA ];
873 assert( ava->la_attr.bv_val );
874 assert( ava->la_value.bv_val );
877 charray_add_n( types, ava->la_attr.bv_val,
878 ava->la_attr.bv_len );
880 charray_add_n( values, ava->la_value.bv_val,
881 ava->la_value.bv_len );
884 ldap_rdnfree( tmpRDN );
892 * LDAP_SUCCESS if rdn is a legal rdn;
893 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
896 rdnValidate( struct berval *rdn )
900 * input is a pretty or normalized DN
901 * hence, we can just search for ','
903 if( rdn == NULL || rdn->bv_len == 0 ) {
904 return LDAP_INVALID_SYNTAX;
907 return strchr( rdn->bv_val, ',' ) == NULL
908 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
911 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
918 if ( rdn == NULL || rdn == '\0' ) {
925 rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
926 if ( rc != LDAP_SUCCESS ) {
933 if ( p[ 0 ] != '\0' ) {
938 * Schema-aware validate
940 if ( rc == LDAP_SUCCESS ) {
941 rc = LDAPDN_validate( DN );
946 * Must validate (there's a repeated parsing ...)
948 return ( rc == LDAP_SUCCESS );
955 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
958 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
962 build_new_dn( struct berval * new_dn,
963 struct berval * parent_dn,
964 struct berval * newrdn )
968 if ( parent_dn == NULL ) {
969 ber_dupbv( new_dn, newrdn );
973 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
974 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
976 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
978 strcpy( ptr, parent_dn->bv_val );
981 #endif /* SLAP_DN_MIGRATION */
984 * dnIsSuffix - tells whether suffix is a suffix of dn.
985 * Both dn and suffix must be normalized.
989 const struct berval *dn,
990 const struct berval *suffix )
992 int d = dn->bv_len - suffix->bv_len;
997 /* empty suffix matches any dn */
998 if ( suffix->bv_len == 0 ) {
1002 /* suffix longer than dn */
1007 /* no rdn separator or escaped rdn separator */
1008 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
1009 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1013 /* no possible match or malformed dn */
1019 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );