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 );
129 if ( rc != LDAP_SUCCESS ) {
130 return( LDAP_INVALID_SYNTAX );
133 return( LDAP_SUCCESS );
137 * AVA sorting inside a RDN
139 * rule: sort attributeTypes in alphabetical order; in case of multiple
140 * occurrences of the same attributeType, sort values in byte order
141 * (use memcmp, which implies alphabetical order in case of IA5 value;
142 * this should guarantee the repeatability of the operation).
144 * uses a linear search; should be fine since the number of AVAs in
145 * a RDN should be limited.
148 AVA_Sort( LDAPRDN *rdn, int iAVA )
151 LDAPAVA *ava_in = rdn[ iAVA ][ 0 ];
156 for ( i = 0; i < iAVA; i++ ) {
157 LDAPAVA *ava = rdn[ i ][ 0 ];
162 a = strcmp( ava_in->la_attr->bv_val, ava->la_attr->bv_val );
171 d = ava_in->la_value->bv_len - ava->la_value->bv_len;
173 v = memcmp( ava_in->la_value->bv_val,
174 ava->la_value->bv_val,
175 d <= 0 ? ava_in->la_value->bv_len
176 : ava->la_value->bv_len );
178 if ( v == 0 && d != 0 ) {
197 a = strcmp( ava_in->la_value->bv_val,
198 ava->la_value->bv_val );
204 for ( j = iAVA; j > i; j-- ) {
205 rdn[ j ][ 0 ] = rdn[ j - 1 ][ 0 ];
207 rdn[ i ][ 0 ] = ava_in;
214 * In-place, schema-aware normalization / "pretty"ing of the
215 * structural representation of a distinguished name.
218 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
225 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
226 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
231 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
232 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
233 AttributeDescription *ad;
234 slap_syntax_transform_func *transf = NULL;
236 struct berval *bv = NULL;
240 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
241 const char *text = NULL;
243 rc = slap_bv2ad( ava->la_attr, &ad, &text );
244 if ( rc != LDAP_SUCCESS ) {
245 return LDAP_INVALID_SYNTAX;
248 ava->la_private = ( void * )ad;
252 * Replace attr oid/name with the canonical name
254 ber_bvfree( ava->la_attr );
255 ava->la_attr = ber_bvdup( &ad->ad_cname );
257 if( flags & SLAP_LDAPDN_PRETTY ) {
258 transf = ad->ad_type->sat_syntax->ssyn_pretty;
261 transf = ad->ad_type->sat_syntax->ssyn_normalize;
262 mr = ad->ad_type->sat_equality;
267 * transform value by normalize/pretty function
269 rc = ( *transf )( ad->ad_type->sat_syntax,
270 ava->la_value, &bv );
272 if ( rc != LDAP_SUCCESS ) {
273 return LDAP_INVALID_SYNTAX;
277 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
278 struct berval *s = bv;
280 bv = ber_bvstr( UTF8normalize( bv ? bv : ava->la_value,
287 ber_bvfree( ava->la_value );
291 AVA_Sort( rdn, iAVA );
299 * dn normalize routine
305 struct berval **normalized )
307 struct berval *out = NULL;
309 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
312 assert( normalized );
314 assert( *normalized == NULL );
316 if ( val->bv_len != 0 ) {
322 * Go to structural representation
324 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
325 if ( rc != LDAP_SUCCESS ) {
326 return LDAP_INVALID_SYNTAX;
330 * Schema-aware rewrite
332 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
334 return LDAP_INVALID_SYNTAX;
338 * Back to string representation
340 rc = ldap_dn2str( dn, &dn_out, LDAP_DN_FORMAT_LDAPV3 );
344 if ( rc != LDAP_SUCCESS ) {
345 return LDAP_INVALID_SYNTAX;
348 out = ber_bvstr( dn_out );
351 out = ber_bvdup( val );
354 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
361 * dn "pretty"ing routine
367 struct berval **pretty)
369 struct berval *out = NULL;
371 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
375 assert( *pretty == NULL );
377 if ( val->bv_len != 0 ) {
382 /* FIXME: should be liberal in what we accept */
383 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
384 if ( rc != LDAP_SUCCESS ) {
385 return LDAP_INVALID_SYNTAX;
389 * Schema-aware rewrite
391 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
393 return LDAP_INVALID_SYNTAX;
396 /* FIXME: not sure why the default isn't pretty */
397 /* RE: the default is the form that is used as
398 * an internal representation; the pretty form
400 rc = ldap_dn2str( dn, &dn_out,
401 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
405 if ( rc != LDAP_SUCCESS ) {
406 return LDAP_INVALID_SYNTAX;
409 out = ber_bvstr( dn_out );
412 out = ber_bvdup( val );
415 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
423 * Combination of both dnPretty and dnNormalize
429 struct berval *pretty,
430 struct berval *normal)
432 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
438 if ( val->bv_len != 0 ) {
443 pretty->bv_val = NULL;
444 normal->bv_val = NULL;
448 /* FIXME: should be liberal in what we accept */
449 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
450 if ( rc != LDAP_SUCCESS ) {
451 return LDAP_INVALID_SYNTAX;
455 * Schema-aware rewrite
457 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
459 return LDAP_INVALID_SYNTAX;
462 rc = ldap_dn2str( dn, &dn_out,
463 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
465 if ( rc != LDAP_SUCCESS ) {
467 return LDAP_INVALID_SYNTAX;
470 ber_str2bv( dn_out, 0, 0, pretty );
472 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
474 free( pretty->bv_val );
475 pretty->bv_val = NULL;
477 return LDAP_INVALID_SYNTAX;
480 rc = ldap_dn2str( dn, &dn_out, LDAP_DN_FORMAT_LDAPV3 );
483 if ( rc != LDAP_SUCCESS ) {
484 free( pretty->bv_val );
485 pretty->bv_val = NULL;
487 return LDAP_INVALID_SYNTAX;
490 ber_str2bv( dn_out, 0, 0, normal );
492 ber_dupbv( pretty, val );
493 ber_dupbv( normal, val );
496 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
497 pretty->bv_val, normal->bv_val, 0 );
511 struct berval *value,
512 void *assertedValue )
515 struct berval *asserted = (struct berval *) assertedValue;
519 assert( assertedValue );
521 match = value->bv_len - asserted->bv_len;
524 match = strcmp( value->bv_val, asserted->bv_val );
528 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
529 "dnMatch: %d\n %s\n %s\n", match,
530 value->bv_val, asserted->bv_val ));
532 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
533 match, value->bv_val, asserted->bv_val );
537 return( LDAP_SUCCESS );
540 #ifdef SLAP_DN_MIGRATION
542 * these routines are provided for migration purposes only!
543 * dn_validate is deprecated in favor of dnValidate
544 * dn_normalize is deprecated in favor of dnNormalize
545 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
547 * other routines are likewise deprecated but may not yet have
548 * replacement functions.
552 * dn_validate - validate and compress dn. the dn is
553 * compressed in place are returned if valid.
554 * Deprecated in favor of dnValidate()
557 dn_validate( char *dn )
560 struct berval *pretty = NULL;
563 if ( dn == NULL || dn[0] == '\0' ) {
568 val.bv_len = strlen( dn );
570 rc = dnPretty( NULL, &val, &pretty );
571 if ( rc != LDAP_SUCCESS ) {
575 if ( val.bv_len < pretty->bv_len ) {
576 ber_bvfree( pretty );
580 AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
581 ber_bvfree( pretty );
587 * dn_normalize - put dn into a canonical form suitable for storing
588 * in a hash database. this involves normalizing the case as well as
589 * the format. the dn is normalized in place as well as returned if valid.
590 * Deprecated in favor of dnNormalize()
593 dn_normalize( char *dn )
596 struct berval *normalized = NULL;
599 if ( dn == NULL || dn[0] == '\0' ) {
604 val.bv_len = strlen( dn );
606 rc = dnNormalize( NULL, &val, &normalized );
607 if ( rc != LDAP_SUCCESS ) {
611 if ( val.bv_len < normalized->bv_len ) {
612 ber_bvfree( normalized );
616 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
617 ber_bvfree( normalized );
623 * dnParent - dn's parent, in-place
634 rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
635 if ( rc != LDAP_SUCCESS ) {
638 ldap_rdnfree( tmpRDN );
640 assert( DN_SEPARATOR( p[ 0 ] ) );
643 while ( ASCII_SPACE( p[ 0 ] ) ) {
653 * dn_parent - return the dn's parent, in-place
654 * FIXME: should be replaced by dnParent()
667 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
671 if ( dn[ 0 ] == '\0' ) {
675 if ( be != NULL && be_issuffix( be, dn ) ) {
679 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
683 return ( char * )pdn;
689 struct berval **rdn )
699 if( dn->bv_len == 0 ) {
703 rc = ldap_str2rdn( dn->bv_val, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
704 if ( rc != LDAP_SUCCESS ) {
708 rc = ldap_rdn2str( tmpRDN, &rdnout, LDAP_DN_FORMAT_LDAPV3 );
709 ldap_rdnfree( tmpRDN );
710 if ( rc != LDAP_SUCCESS ) {
714 *rdn = ber_bvstr( rdnout );
715 if ( *rdn == NULL ) {
717 return LDAP_NO_MEMORY;
724 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
732 struct berval *rdn = NULL;
737 if ( dn_in == NULL ) {
741 while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
745 if ( dn_in[ 0 ] == '\0' ) {
749 if ( be != NULL && be_issuffix( be, dn_in ) ) {
753 bv.bv_val = (char *) dn_in;
754 bv.bv_len = strlen( bv.bv_val );
756 if ( dnExtractRdn( &bv, &rdn ) != LDAP_SUCCESS ) {
761 retval = rdn->bv_len;
768 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
775 struct berval *rdn = NULL;
780 if ( dn_in == NULL ) {
784 while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
788 if ( dn_in[ 0 ] == '\0' ) {
792 if ( be != NULL && be_issuffix( be, dn_in ) ) {
796 bv.bv_val = (char *) dn_in;
797 bv.bv_len = strlen( bv.bv_val );
799 if ( dnExtractRdn( &bv, &rdn ) != LDAP_SUCCESS ) {
804 retval = rdn->bv_val;
811 * dn_issuffix - tells whether suffix is a suffix of dn.
812 * Both dn and suffix must be normalized.
813 * deprecated in favor of dnIsSuffix()
821 struct berval bvdn, bvsuffix;
826 bvdn.bv_val = (char *) dn;
827 bvdn.bv_len = strlen( dn );
828 bvsuffix.bv_val = (char *) suffix;
829 bvsuffix.bv_len = strlen( suffix );
831 return dnIsSuffix( &bvdn, &bvsuffix );
836 * Given a string (i.e. an rdn) of the form:
837 * "attribute_type = attribute_value"
838 * this function returns the type of an attribute, that is the
839 * string "attribute_type" which is placed in newly allocated
840 * memory. The returned string will be null-terminated.
845 char * rdn_attr_type( const char * s )
847 char **attrs, **values, *retval;
849 if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
853 retval = ch_strdup( attrs[ 0 ] );
855 charray_free( attrs );
856 charray_free( values );
864 * Given a string (i.e. an rdn) of the form:
865 * "attribute_type = attribute_value"
866 * this function returns "attribute_type" which is placed in newly allocated
867 * memory. The returned string will be null-terminated and may contain
868 * spaces (i.e. "John Doe\0").
874 rdn_attr_value( const char * rdn )
876 char **attrs, **values, *retval;
878 if ( rdn_attrs( rdn, &attrs, &values ) != LDAP_SUCCESS ) {
882 retval = ch_strdup( values[ 0 ] );
884 charray_free( attrs );
885 charray_free( values );
893 * Given a string (i.e. an rdn) of the form:
894 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
895 * this function stores the types of the attributes in ptypes, that is the
896 * array of strings "attribute_type" which is placed in newly allocated
897 * memory, and the values of the attributes in pvalues, that is the
898 * array of strings "attribute_value" which is placed in newly allocated
899 * memory. Returns 0 on success, -1 on failure.
901 * note: got part of the code from dn_validate
903 * Deprecated; directly use LDAPRDN from ldap_str2rdn
906 rdn_attrs( const char * rdn, char ***types, char ***values)
916 rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
917 if ( rc != LDAP_SUCCESS ) {
921 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
922 LDAPAVA *ava = tmpRDN[ iAVA ][ 0 ];
925 assert( ava->la_attr );
926 assert( ava->la_value );
929 charray_add_n( types, ava->la_attr->bv_val,
930 ava->la_attr->bv_len );
932 charray_add_n( values, ava->la_value->bv_val,
933 ava->la_value->bv_len );
936 ldap_rdnfree( tmpRDN );
944 * LDAP_SUCCESS if rdn is a legal rdn;
945 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
948 rdnValidate( struct berval *rdn )
952 * input is a pretty or normalized DN
953 * hence, we can just search for ','
955 if( rdn == NULL || rdn->bv_len == 0 ) {
956 return LDAP_INVALID_SYNTAX;
959 return strchr( rdn->bv_val, ',' ) == NULL
960 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
963 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
970 if ( rdn == NULL || rdn == '\0' ) {
977 rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
978 if ( rc != LDAP_SUCCESS ) {
985 if ( p[ 0 ] != '\0' ) {
990 * Schema-aware validate
992 if ( rc == LDAP_SUCCESS ) {
993 rc = LDAPDN_validate( DN );
998 * Must validate (there's a repeated parsing ...)
1000 return ( rc == LDAP_SUCCESS );
1007 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1010 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1014 build_new_dn( struct berval * new_dn,
1015 struct berval * parent_dn,
1016 struct berval * newrdn )
1020 if ( parent_dn == NULL ) {
1021 ber_dupbv( new_dn, newrdn );
1025 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1026 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
1028 ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
1030 strcpy( ptr, parent_dn->bv_val );
1033 #endif /* SLAP_DN_MIGRATION */
1036 * dnIsSuffix - tells whether suffix is a suffix of dn.
1037 * Both dn and suffix must be normalized.
1041 const struct berval *dn,
1042 const struct berval *suffix )
1044 int d = dn->bv_len - suffix->bv_len;
1049 /* empty suffix matches any dn */
1050 if ( suffix->bv_len == 0 ) {
1054 /* suffix longer than dn */
1059 /* no rdn separator or escaped rdn separator */
1060 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
1061 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1065 /* no possible match or malformed dn */
1071 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );