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 );
355 ldap_pvt_str2upper( out->bv_val );
357 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
364 * dn "pretty"ing routine
370 struct berval **pretty)
372 struct berval *out = NULL;
374 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
378 assert( *pretty == NULL );
380 if ( val->bv_len != 0 ) {
385 /* FIXME: should be liberal in what we accept */
386 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
387 if ( rc != LDAP_SUCCESS ) {
388 return LDAP_INVALID_SYNTAX;
392 * Schema-aware rewrite
394 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
396 return LDAP_INVALID_SYNTAX;
399 /* FIXME: not sure why the default isn't pretty */
400 /* RE: the default is the form that is used as
401 * an internal representation; the pretty form
403 rc = ldap_dn2str( dn, &dn_out,
404 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
408 if ( rc != LDAP_SUCCESS ) {
409 return LDAP_INVALID_SYNTAX;
412 out = ber_bvstr( dn_out );
415 out = ber_bvdup( val );
418 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
428 * FIXME: strcasecmp should be replaced with strcmp once
429 * value normalization issues are sorted out.
437 struct berval *value,
438 void *assertedValue )
441 struct berval *asserted = (struct berval *) assertedValue;
445 assert( assertedValue );
447 match = value->bv_len - asserted->bv_len;
450 match = strcasecmp( value->bv_val, asserted->bv_val );
454 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
455 "dnMatch: %d\n %s\n %s\n", match,
456 value->bv_val, asserted->bv_val ));
458 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
459 match, value->bv_val, asserted->bv_val );
463 return( LDAP_SUCCESS );
466 #ifdef SLAP_DN_MIGRATION
468 * these routines are provided for migration purposes only!
469 * dn_validate is deprecated in favor of dnValidate
470 * dn_normalize is deprecated in favor of dnNormalize
471 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
473 * other routines are likewise deprecated but may not yet have
474 * replacement functions.
478 * dn_validate - validate and compress dn. the dn is
479 * compressed in place are returned if valid.
480 * Deprecated in favor of dnValidate()
483 dn_validate( char *dn )
486 struct berval *pretty = NULL;
489 if ( dn == NULL || dn[0] == '\0' ) {
494 val.bv_len = strlen( dn );
496 rc = dnPretty( NULL, &val, &pretty );
497 if ( rc != LDAP_SUCCESS ) {
501 if ( val.bv_len < pretty->bv_len ) {
502 ber_bvfree( pretty );
506 AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
507 ber_bvfree( pretty );
513 * dn_normalize - put dn into a canonical form suitable for storing
514 * in a hash database. this involves normalizing the case as well as
515 * the format. the dn is normalized in place as well as returned if valid.
516 * Deprecated in favor of dnNormalize()
519 dn_normalize( char *dn )
522 struct berval *normalized = NULL;
525 if ( dn == NULL || dn[0] == '\0' ) {
530 val.bv_len = strlen( dn );
532 rc = dnNormalize( NULL, &val, &normalized );
533 if ( rc != LDAP_SUCCESS ) {
537 if ( val.bv_len < normalized->bv_len ) {
538 ber_bvfree( normalized );
542 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
543 ber_bvfree( normalized );
549 * dnParent - dn's parent, in-place
560 rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
561 if ( rc != LDAP_SUCCESS ) {
564 ldap_rdnfree( tmpRDN );
566 assert( DN_SEPARATOR( p[ 0 ] ) );
569 while ( ASCII_SPACE( p[ 0 ] ) ) {
579 * dn_parent - return the dn's parent, in-place
580 * FIXME: should be replaced by dnParent()
593 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
597 if ( dn[ 0 ] == '\0' ) {
601 if ( be != NULL && be_issuffix( be, dn ) ) {
605 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
609 return ( char * )pdn;
615 struct berval **rdn )
625 rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
626 if ( rc != LDAP_SUCCESS ) {
630 rc = ldap_rdn2str( tmpRDN, &rdnout, LDAP_DN_FORMAT_LDAPV3 );
631 ldap_rdnfree( tmpRDN );
632 if ( rc != LDAP_SUCCESS ) {
636 *rdn = ber_bvstr( rdnout );
637 if ( *rdn == NULL ) {
639 return LDAP_NO_MEMORY;
646 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
653 struct berval *rdn = NULL;
658 if ( dn_in == NULL ) {
662 while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
666 if ( dn_in[ 0 ] == '\0' ) {
670 if ( be != NULL && be_issuffix( be, dn_in ) ) {
674 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
679 retval = rdn->bv_len;
686 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
692 struct berval *rdn = NULL;
697 if ( dn_in == NULL ) {
701 while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
705 if ( dn_in[ 0 ] == '\0' ) {
709 if ( be != NULL && be_issuffix( be, dn_in ) ) {
713 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
718 retval = rdn->bv_val;
725 * return a charray of all subtrees to which the DN resides in
731 char **subtree = NULL;
734 charray_add( &subtree, dn );
736 dn = dn_parent( be, dn );
738 } while ( dn != NULL );
744 * dn_issuffix - tells whether suffix is a suffix of dn.
745 * Both dn and suffix must be normalized.
746 * deprecated in favor of dnIsSuffix()
754 struct berval bvdn, bvsuffix;
759 bvdn.bv_val = (char *) dn;
760 bvdn.bv_len = strlen( dn );
761 bvsuffix.bv_val = (char *) suffix;
762 bvsuffix.bv_len = strlen( suffix );
764 return dnIsSuffix( &bvdn, &bvsuffix );
769 * Given a string (i.e. an rdn) of the form:
770 * "attribute_type = attribute_value"
771 * this function returns the type of an attribute, that is the
772 * string "attribute_type" which is placed in newly allocated
773 * memory. The returned string will be null-terminated.
778 char * rdn_attr_type( const char * s )
780 char **attrs, **values, *retval;
782 if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
786 retval = ch_strdup( attrs[ 0 ] );
788 charray_free( attrs );
789 charray_free( values );
797 * Given a string (i.e. an rdn) of the form:
798 * "attribute_type = attribute_value"
799 * this function returns "attribute_type" which is placed in newly allocated
800 * memory. The returned string will be null-terminated and may contain
801 * spaces (i.e. "John Doe\0").
807 rdn_attr_value( const char * rdn )
809 char **attrs, **values, *retval;
811 if ( rdn_attrs( rdn, &attrs, &values ) != LDAP_SUCCESS ) {
815 retval = ch_strdup( values[ 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[+attribute_type=attribute_value[...]]"
828 * this function stores the types of the attributes in ptypes, that is the
829 * array of strings "attribute_type" which is placed in newly allocated
830 * memory, and the values of the attributes in pvalues, that is the
831 * array of strings "attribute_value" which is placed in newly allocated
832 * memory. Returns 0 on success, -1 on failure.
834 * note: got part of the code from dn_validate
836 * Deprecated; directly use LDAPRDN from ldap_str2rdn
840 rdn_attrs( const char * rdn, char ***types, char ***values)
851 rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
852 if ( rc != LDAP_SUCCESS ) {
856 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
857 LDAPAVA *ava = tmpRDN[ iAVA ][ 0 ];
860 assert( ava->la_attr );
861 assert( ava->la_value );
863 charray_add_n( types, ava->la_attr->bv_val,
864 ava->la_attr->bv_len );
865 charray_add_n( values, ava->la_value->bv_val,
866 ava->la_value->bv_len );
869 ldap_rdnfree( tmpRDN );
877 * 1 if rdn is a legal rdn;
878 * 0 otherwise (including a sequence of rdns)
881 rdn_validate( const char *rdn )
883 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
890 if ( rdn == NULL || rdn == '\0' ) {
897 rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
898 if ( rc != LDAP_SUCCESS ) {
905 if ( p[ 0 ] != '\0' ) {
910 * Schema-aware validate
912 if ( rc == LDAP_SUCCESS ) {
913 rc = LDAPDN_validate( DN );
918 * Must validate (there's a repeated parsing ...)
920 return ( rc == LDAP_SUCCESS );
926 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
929 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
933 build_new_dn( char ** new_dn,
934 const char * entry_dn,
935 const char * parent_dn,
936 const char * newrdn )
939 if ( parent_dn == NULL ) {
940 *new_dn = ch_strdup( newrdn );
944 *new_dn = (char *) ch_malloc(
945 strlen( parent_dn ) + strlen( newrdn ) + 2 );
947 strcpy( *new_dn, newrdn );
948 strcat( *new_dn, "," );
949 strcat( *new_dn, parent_dn );
952 #endif /* SLAP_DN_MIGRATION */
955 * dnIsSuffix - tells whether suffix is a suffix of dn.
956 * Both dn and suffix must be normalized.
960 const struct berval *dn,
961 const struct berval *suffix )
963 int d = dn->bv_len - suffix->bv_len;
968 /* empty suffix matches any dn */
969 if ( suffix->bv_len == 0 ) {
973 /* suffix longer than dn */
978 /* no rdn separator or escaped rdn separator */
979 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
980 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
984 /* no possible match or malformed dn */
990 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );