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 ber_bvfree( ava->la_attr );
80 ava->la_attr = ber_bvdup( &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 ber_bvfree( ava->la_attr );
254 ava->la_attr = ber_bvdup( &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 ber_bvfree( ava->la_value );
290 AVA_Sort( rdn, iAVA );
298 * dn normalize routine
304 struct berval **normalized )
306 struct berval *out = NULL;
308 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
311 assert( normalized );
313 assert( *normalized == NULL );
315 if ( val->bv_len != 0 ) {
320 * Go to structural representation
322 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
323 if ( rc != LDAP_SUCCESS ) {
324 return LDAP_INVALID_SYNTAX;
328 * Schema-aware rewrite
330 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
332 return LDAP_INVALID_SYNTAX;
336 * Back to string representation
338 out = ch_malloc( sizeof(struct berval));
340 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
344 if ( rc != LDAP_SUCCESS ) {
346 return LDAP_INVALID_SYNTAX;
349 out = ber_bvdup( val );
352 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
359 * dn "pretty"ing routine
365 struct berval **pretty)
367 struct berval *out = NULL;
369 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
373 assert( *pretty == NULL );
375 if ( val->bv_len != 0 ) {
379 /* FIXME: should be liberal in what we accept */
380 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
381 if ( rc != LDAP_SUCCESS ) {
382 return LDAP_INVALID_SYNTAX;
386 * Schema-aware rewrite
388 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
390 return LDAP_INVALID_SYNTAX;
393 /* FIXME: not sure why the default isn't pretty */
394 /* RE: the default is the form that is used as
395 * an internal representation; the pretty form
398 out = ch_malloc( sizeof(struct berval));
400 rc = ldap_dn2bv( dn, out,
401 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
405 if ( rc != LDAP_SUCCESS ) {
407 return LDAP_INVALID_SYNTAX;
410 out = ber_bvdup( val );
413 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
421 * Combination of both dnPretty and dnNormalize
427 struct berval *pretty,
428 struct berval *normal)
430 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
436 if ( val->bv_len != 0 ) {
440 pretty->bv_val = NULL;
441 normal->bv_val = NULL;
445 /* FIXME: should be liberal in what we accept */
446 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
447 if ( rc != LDAP_SUCCESS ) {
448 return LDAP_INVALID_SYNTAX;
452 * Schema-aware rewrite
454 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
456 return LDAP_INVALID_SYNTAX;
459 rc = ldap_dn2bv( dn, pretty,
460 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
462 if ( rc != LDAP_SUCCESS ) {
464 return LDAP_INVALID_SYNTAX;
467 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
469 free( pretty->bv_val );
470 pretty->bv_val = NULL;
472 return LDAP_INVALID_SYNTAX;
475 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
478 if ( rc != LDAP_SUCCESS ) {
479 free( pretty->bv_val );
480 pretty->bv_val = NULL;
482 return LDAP_INVALID_SYNTAX;
485 ber_dupbv( pretty, val );
486 ber_dupbv( normal, val );
489 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
490 pretty->bv_val, normal->bv_val, 0 );
504 struct berval *value,
505 void *assertedValue )
508 struct berval *asserted = (struct berval *) assertedValue;
512 assert( assertedValue );
514 match = value->bv_len - asserted->bv_len;
517 match = strcmp( value->bv_val, asserted->bv_val );
521 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
522 "dnMatch: %d\n %s\n %s\n", match,
523 value->bv_val, asserted->bv_val ));
525 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
526 match, value->bv_val, asserted->bv_val );
530 return( LDAP_SUCCESS );
533 #ifdef SLAP_DN_MIGRATION
535 * these routines are provided for migration purposes only!
536 * dn_validate is deprecated in favor of dnValidate
537 * dn_normalize is deprecated in favor of dnNormalize
538 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
540 * other routines are likewise deprecated but may not yet have
541 * replacement functions.
545 * dn_validate - validate and compress dn. the dn is
546 * compressed in place are returned if valid.
547 * Deprecated in favor of dnValidate()
550 dn_validate( char *dn )
553 struct berval *pretty = NULL;
556 if ( dn == NULL || dn[0] == '\0' ) {
561 val.bv_len = strlen( dn );
563 rc = dnPretty( NULL, &val, &pretty );
564 if ( rc != LDAP_SUCCESS ) {
568 if ( val.bv_len < pretty->bv_len ) {
569 ber_bvfree( pretty );
573 AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
574 ber_bvfree( pretty );
580 * dn_normalize - put dn into a canonical form suitable for storing
581 * in a hash database. this involves normalizing the case as well as
582 * the format. the dn is normalized in place as well as returned if valid.
583 * Deprecated in favor of dnNormalize()
586 dn_normalize( char *dn )
589 struct berval *normalized = NULL;
592 if ( dn == NULL || dn[0] == '\0' ) {
597 val.bv_len = strlen( dn );
599 rc = dnNormalize( NULL, &val, &normalized );
600 if ( rc != LDAP_SUCCESS ) {
604 if ( val.bv_len < normalized->bv_len ) {
605 ber_bvfree( normalized );
609 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
610 ber_bvfree( normalized );
616 * dnParent - dn's parent, in-place
626 rc = ldap_str2rdn( dn, NULL, &p, LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
627 if ( rc != LDAP_SUCCESS ) {
631 assert( DN_SEPARATOR( p[ 0 ] ) );
634 while ( ASCII_SPACE( p[ 0 ] ) ) {
644 * dn_parent - return the dn's parent, in-place
645 * FIXME: should be replaced by dnParent()
658 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
662 if ( dn[ 0 ] == '\0' ) {
666 if ( be != NULL && be_issuffix( be, dn ) ) {
670 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
674 return ( char * )pdn;
689 if( dn->bv_len == 0 ) {
693 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
694 if ( rc != LDAP_SUCCESS ) {
698 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
699 ldap_rdnfree( tmpRDN );
700 if ( rc != LDAP_SUCCESS ) {
708 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
713 struct berval *dn_in )
720 if ( dn_in == NULL ) {
724 if ( !dn_in->bv_len ) {
728 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
732 rc = ldap_str2rdn( dn_in->bv_val, NULL, &p,
733 LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
734 if ( rc != LDAP_SUCCESS ) {
738 return p - dn_in->bv_val;
742 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
746 struct berval *dn_in )
752 if ( dn_in == NULL ) {
756 if ( !dn_in->bv_len ) {
760 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
764 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
772 * dn_issuffix - tells whether suffix is a suffix of dn.
773 * Both dn and suffix must be normalized.
774 * deprecated in favor of dnIsSuffix()
782 struct berval bvdn, bvsuffix;
787 bvdn.bv_val = (char *) dn;
788 bvdn.bv_len = strlen( dn );
789 bvsuffix.bv_val = (char *) suffix;
790 bvsuffix.bv_len = strlen( suffix );
792 return dnIsSuffix( &bvdn, &bvsuffix );
797 * Given a string (i.e. an rdn) of the form:
798 * "attribute_type = attribute_value"
799 * this function returns the type of an attribute, that is the
800 * string "attribute_type" which is placed in newly allocated
801 * memory. The returned string will be null-terminated.
806 char * rdn_attr_type( const char * s )
808 char **attrs, **values, *retval;
810 if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
814 retval = ch_strdup( attrs[ 0 ] );
816 charray_free( attrs );
817 charray_free( values );
825 * Given a string (i.e. an rdn) of the form:
826 * "attribute_type = attribute_value"
827 * this function returns "attribute_type" which is placed in newly allocated
828 * memory. The returned string will be null-terminated and may contain
829 * spaces (i.e. "John Doe\0").
835 rdn_attr_value( const char * rdn )
837 char **attrs, **values, *retval;
839 if ( rdn_attrs( rdn, &attrs, &values ) != LDAP_SUCCESS ) {
843 retval = ch_strdup( values[ 0 ] );
845 charray_free( attrs );
846 charray_free( values );
854 * Given a string (i.e. an rdn) of the form:
855 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
856 * this function stores the types of the attributes in ptypes, that is the
857 * array of strings "attribute_type" which is placed in newly allocated
858 * memory, and the values of the attributes in pvalues, that is the
859 * array of strings "attribute_value" which is placed in newly allocated
860 * memory. Returns 0 on success, -1 on failure.
862 * note: got part of the code from dn_validate
864 * Deprecated; directly use LDAPRDN from ldap_str2rdn
867 rdn_attrs( const char * rdn, char ***types, char ***values)
877 rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
878 if ( rc != LDAP_SUCCESS ) {
882 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
883 LDAPAVA *ava = tmpRDN[ iAVA ][ 0 ];
886 assert( ava->la_attr );
887 assert( ava->la_value );
890 charray_add_n( types, ava->la_attr->bv_val,
891 ava->la_attr->bv_len );
893 charray_add_n( values, ava->la_value->bv_val,
894 ava->la_value->bv_len );
897 ldap_rdnfree( tmpRDN );
905 * LDAP_SUCCESS if rdn is a legal rdn;
906 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
909 rdnValidate( struct berval *rdn )
913 * input is a pretty or normalized DN
914 * hence, we can just search for ','
916 if( rdn == NULL || rdn->bv_len == 0 ) {
917 return LDAP_INVALID_SYNTAX;
920 return strchr( rdn->bv_val, ',' ) == NULL
921 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
924 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
931 if ( rdn == NULL || rdn == '\0' ) {
938 rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
939 if ( rc != LDAP_SUCCESS ) {
946 if ( p[ 0 ] != '\0' ) {
951 * Schema-aware validate
953 if ( rc == LDAP_SUCCESS ) {
954 rc = LDAPDN_validate( DN );
959 * Must validate (there's a repeated parsing ...)
961 return ( rc == LDAP_SUCCESS );
968 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
971 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
975 build_new_dn( struct berval * new_dn,
976 struct berval * parent_dn,
977 struct berval * newrdn )
981 if ( parent_dn == NULL ) {
982 ber_dupbv( new_dn, newrdn );
986 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
987 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
989 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
991 strcpy( ptr, parent_dn->bv_val );
994 #endif /* SLAP_DN_MIGRATION */
997 * dnIsSuffix - tells whether suffix is a suffix of dn.
998 * Both dn and suffix must be normalized.
1002 const struct berval *dn,
1003 const struct berval *suffix )
1005 int d = dn->bv_len - suffix->bv_len;
1010 /* empty suffix matches any dn */
1011 if ( suffix->bv_len == 0 ) {
1015 /* suffix longer than dn */
1020 /* no rdn separator or escaped rdn separator */
1021 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
1022 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1026 /* no possible match or malformed dn */
1032 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );