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
627 rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
628 if ( rc != LDAP_SUCCESS ) {
631 ldap_rdnfree( tmpRDN );
633 assert( DN_SEPARATOR( p[ 0 ] ) );
636 while ( ASCII_SPACE( p[ 0 ] ) ) {
646 * dn_parent - return the dn's parent, in-place
647 * FIXME: should be replaced by dnParent()
660 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
664 if ( dn[ 0 ] == '\0' ) {
668 if ( be != NULL && be_issuffix( be, dn ) ) {
672 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
676 return ( char * )pdn;
691 if( dn->bv_len == 0 ) {
695 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
696 if ( rc != LDAP_SUCCESS ) {
700 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
701 ldap_rdnfree( tmpRDN );
702 if ( rc != LDAP_SUCCESS ) {
710 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
715 struct berval *dn_in )
721 if ( dn_in == NULL ) {
725 if ( !dn_in->bv_len ) {
729 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
733 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
743 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
747 struct berval *dn_in )
753 if ( dn_in == NULL ) {
757 if ( !dn_in->bv_len ) {
761 if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
765 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
773 * dn_issuffix - tells whether suffix is a suffix of dn.
774 * Both dn and suffix must be normalized.
775 * deprecated in favor of dnIsSuffix()
783 struct berval bvdn, bvsuffix;
788 bvdn.bv_val = (char *) dn;
789 bvdn.bv_len = strlen( dn );
790 bvsuffix.bv_val = (char *) suffix;
791 bvsuffix.bv_len = strlen( suffix );
793 return dnIsSuffix( &bvdn, &bvsuffix );
798 * Given a string (i.e. an rdn) of the form:
799 * "attribute_type = attribute_value"
800 * this function returns the type of an attribute, that is the
801 * string "attribute_type" which is placed in newly allocated
802 * memory. The returned string will be null-terminated.
807 char * rdn_attr_type( const char * s )
809 char **attrs, **values, *retval;
811 if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
815 retval = ch_strdup( attrs[ 0 ] );
817 charray_free( attrs );
818 charray_free( values );
826 * Given a string (i.e. an rdn) of the form:
827 * "attribute_type = attribute_value"
828 * this function returns "attribute_type" which is placed in newly allocated
829 * memory. The returned string will be null-terminated and may contain
830 * spaces (i.e. "John Doe\0").
836 rdn_attr_value( const char * rdn )
838 char **attrs, **values, *retval;
840 if ( rdn_attrs( rdn, &attrs, &values ) != LDAP_SUCCESS ) {
844 retval = ch_strdup( values[ 0 ] );
846 charray_free( attrs );
847 charray_free( values );
855 * Given a string (i.e. an rdn) of the form:
856 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
857 * this function stores the types of the attributes in ptypes, that is the
858 * array of strings "attribute_type" which is placed in newly allocated
859 * memory, and the values of the attributes in pvalues, that is the
860 * array of strings "attribute_value" which is placed in newly allocated
861 * memory. Returns 0 on success, -1 on failure.
863 * note: got part of the code from dn_validate
865 * Deprecated; directly use LDAPRDN from ldap_str2rdn
868 rdn_attrs( const char * rdn, char ***types, char ***values)
878 rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
879 if ( rc != LDAP_SUCCESS ) {
883 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
884 LDAPAVA *ava = tmpRDN[ iAVA ][ 0 ];
887 assert( ava->la_attr );
888 assert( ava->la_value );
891 charray_add_n( types, ava->la_attr->bv_val,
892 ava->la_attr->bv_len );
894 charray_add_n( values, ava->la_value->bv_val,
895 ava->la_value->bv_len );
898 ldap_rdnfree( tmpRDN );
906 * LDAP_SUCCESS if rdn is a legal rdn;
907 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
910 rdnValidate( struct berval *rdn )
914 * input is a pretty or normalized DN
915 * hence, we can just search for ','
917 if( rdn == NULL || rdn->bv_len == 0 ) {
918 return LDAP_INVALID_SYNTAX;
921 return strchr( rdn->bv_val, ',' ) == NULL
922 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
925 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
932 if ( rdn == NULL || rdn == '\0' ) {
939 rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
940 if ( rc != LDAP_SUCCESS ) {
947 if ( p[ 0 ] != '\0' ) {
952 * Schema-aware validate
954 if ( rc == LDAP_SUCCESS ) {
955 rc = LDAPDN_validate( DN );
960 * Must validate (there's a repeated parsing ...)
962 return ( rc == LDAP_SUCCESS );
969 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
972 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
976 build_new_dn( struct berval * new_dn,
977 struct berval * parent_dn,
978 struct berval * newrdn )
982 if ( parent_dn == NULL ) {
983 ber_dupbv( new_dn, newrdn );
987 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
988 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
990 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
992 strcpy( ptr, parent_dn->bv_val );
995 #endif /* SLAP_DN_MIGRATION */
998 * dnIsSuffix - tells whether suffix is a suffix of dn.
999 * Both dn and suffix must be normalized.
1003 const struct berval *dn,
1004 const struct berval *suffix )
1006 int d = dn->bv_len - suffix->bv_len;
1011 /* empty suffix matches any dn */
1012 if ( suffix->bv_len == 0 ) {
1016 /* suffix longer than dn */
1021 /* no rdn separator or escaped rdn separator */
1022 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
1023 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1027 /* no possible match or malformed dn */
1033 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );