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 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 rc = ldap_dn2str( dn, &dn_out, LDAP_DN_FORMAT_LDAPV3 );
342 if ( rc != LDAP_SUCCESS ) {
343 return LDAP_INVALID_SYNTAX;
346 out = ber_bvstr( dn_out );
349 out = ber_bvdup( val );
352 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
360 * dn "pretty"ing routine
366 struct berval **pretty)
368 struct berval *out = NULL;
370 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
375 if ( val->bv_len != 0 ) {
380 /* FIXME: should be liberal in what we accept */
381 rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
382 if ( rc != LDAP_SUCCESS ) {
383 return LDAP_INVALID_SYNTAX;
387 * Schema-aware rewrite
389 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
391 return LDAP_INVALID_SYNTAX;
394 /* FIXME: not sure why the default isn't pretty */
395 /* RE: the default is the form that is used as
396 * an internal representation; the pretty form
398 rc = ldap_dn2str( dn, &dn_out,
399 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
403 if ( rc != LDAP_SUCCESS ) {
404 return LDAP_INVALID_SYNTAX;
407 out = ber_bvstr( dn_out );
410 out = ber_bvdup( val );
413 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
423 * note: uses exact string match (strcmp) because it is supposed to work
432 struct berval *value,
433 void *assertedValue )
436 struct berval *asserted = (struct berval *) assertedValue;
440 assert( assertedValue );
442 match = value->bv_len - asserted->bv_len;
445 match = strcmp( value->bv_val, asserted->bv_val );
449 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
450 "dnMatch: %d\n %s\n %s\n", match,
451 value->bv_val, asserted->bv_val ));
453 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
454 match, value->bv_val, asserted->bv_val );
458 return( LDAP_SUCCESS );
461 #ifdef SLAP_DN_MIGRATION
463 * these routines are provided for migration purposes only!
464 * dn_validate is deprecated in favor of dnValidate
465 * dn_normalize is deprecated in favor of dnNormalize
466 * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
468 * other routines are likewise deprecated but may not yet have
469 * replacement functions.
473 * dn_validate - validate and compress dn. the dn is
474 * compressed in place are returned if valid.
475 * Deprecated in favor of dnValidate()
478 dn_validate( char *dn )
480 struct berval val, *pretty;
483 if ( dn == NULL || dn[0] == '\0' ) {
488 val.bv_len = strlen( dn );
490 rc = dnPretty( NULL, &val, &pretty );
491 if ( rc != LDAP_SUCCESS ) {
495 if ( val.bv_len < pretty->bv_len ) {
496 ber_bvfree( pretty );
500 AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
501 ber_bvfree( pretty );
507 * dn_normalize - put dn into a canonical form suitable for storing
508 * in a hash database. this involves normalizing the case as well as
509 * the format. the dn is normalized in place as well as returned if valid.
510 * Deprecated in favor of dnNormalize()
513 dn_normalize( char *dn )
515 struct berval val, *normalized;
518 if ( dn == NULL || dn[0] == '\0' ) {
523 val.bv_len = strlen( dn );
525 rc = dnNormalize( NULL, &val, &normalized );
526 if ( rc != LDAP_SUCCESS ) {
530 if ( val.bv_len < normalized->bv_len ) {
531 ber_bvfree( normalized );
535 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
536 ber_bvfree( normalized );
542 * dnParent - dn's parent, in-place
553 rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
554 if ( rc != LDAP_SUCCESS ) {
557 ldap_rdnfree( tmpRDN );
559 assert( DN_SEPARATOR( p[ 0 ] ) );
562 while ( ASCII_SPACE( p[ 0 ] ) ) {
572 * dn_parent - return the dn's parent, in-place
573 * FIXME: should be replaced by dnParent()
588 while(*dn != '\0' && ASCII_SPACE(*dn)) {
596 if ( be != NULL && be_issuffix( be, dn ) ) {
601 * assume it is an X.500-style name, which looks like
602 * foo=bar,sha=baz,...
606 for ( s = dn; *s; s++ ) {
620 } else if ( DN_SEPARATOR( *s ) ) {
621 return (char *)s + 1;
634 while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
638 if ( dn[ 0 ] == '\0' ) {
642 if ( be != NULL && be_issuffix( be, dn ) ) {
646 if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
650 return ( char * )pdn;
656 struct berval **rdn )
666 rc = ldap_str2rdn( dn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
667 if ( rc != LDAP_SUCCESS ) {
671 rc = ldap_rdn2str( tmpRDN, &rdnout, LDAP_DN_FORMAT_LDAPV3 );
672 ldap_rdnfree( tmpRDN );
673 if ( rc != LDAP_SUCCESS ) {
677 *rdn = ber_bvstr( rdnout );
678 if ( *rdn == NULL ) {
680 return LDAP_NO_MEMORY;
687 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
698 if( dn_in == NULL ) {
702 while(*dn_in && ASCII_SPACE(*dn_in)) {
706 if( *dn_in == '\0' ) {
710 if ( be != NULL && be_issuffix( be, dn_in ) ) {
716 for ( s = (char *)dn_in; *s; s++ ) {
730 } else if ( DN_SEPARATOR( *s ) ) {
738 struct berval *rdn = NULL;
743 if ( dn_in == NULL ) {
747 while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
751 if ( dn_in[ 0 ] == '\0' ) {
755 if ( be != NULL && be_issuffix( be, dn_in ) ) {
759 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
764 retval = rdn->bv_len;
771 * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
779 int i = dn_rdnlen( be, dn_in );
781 rdn = ch_malloc( i + 1 );
782 strncpy(rdn, dn_in, i);
786 struct berval *rdn = NULL;
791 if ( dn_in == NULL ) {
795 while ( dn_in[ 0 ] && ASCII_SPACE( dn_in[ 0 ] ) ) {
799 if ( dn_in[ 0 ] == '\0' ) {
803 if ( be != NULL && be_issuffix( be, dn_in ) ) {
807 if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
812 retval = rdn->bv_val;
819 * return a charray of all subtrees to which the DN resides in
825 char **subtree = NULL;
828 charray_add( &subtree, dn );
830 dn = dn_parent( be, dn );
832 } while ( dn != NULL );
838 * dn_issuffix - tells whether suffix is a suffix of dn.
839 * Both dn and suffix must be normalized.
840 * deprecated in favor of dnIsSuffix()
848 struct berval bvdn, bvsuffix;
853 bvdn.bv_val = (char *) dn;
854 bvdn.bv_len = strlen( dn );
855 bvsuffix.bv_val = (char *) suffix;
856 bvsuffix.bv_len = strlen( suffix );
858 return dnIsSuffix( &bvdn, &bvsuffix );
862 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
865 * Copyright 1999, Juan C. Gomez, All rights reserved.
866 * This software is not subject to any license of Silicon Graphics
867 * Inc. or Purdue University.
869 * Redistribution and use in source and binary forms are permitted
870 * without restriction or fee of any kind as long as this notice
875 /* get_next_substring:
877 * Gets next substring in s, using d (or the end of the string '\0') as a
878 * string delimiter, and places it in a duplicated memory space. Leading
879 * spaces are ignored. String s **must** be null-terminated.
883 get_next_substring( const char * s, char d )
888 r = str = ch_malloc( strlen(s) + 1 );
890 /* Skip leading spaces */
892 while ( *s && ASCII_SPACE(*s) ) {
898 while ( *s && (*s != d) ) {
900 /* Don't stop when you see trailing spaces may be a multi-word
901 * string, i.e. name=John Doe!
916 * Given a string (i.e. an rdn) of the form:
917 * "attribute_type = attribute_value"
918 * this function returns the type of an attribute, that is the
919 * string "attribute_type" which is placed in newly allocated
920 * memory. The returned string will be null-terminated.
923 char * rdn_attr_type( const char * s )
925 return get_next_substring( s, '=' );
931 * Given a string (i.e. an rdn) of the form:
932 * "attribute_type = attribute_value"
933 * this function returns "attribute_type" which is placed in newly allocated
934 * memory. The returned string will be null-terminated and may contain
935 * spaces (i.e. "John Doe\0").
939 rdn_attr_value( const char * rdn )
944 if ( (str = strchr( rdn, '=' )) != NULL ) {
945 return get_next_substring(++str, '\0');
955 * Given a string (i.e. an rdn) of the form:
956 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
957 * this function stores the types of the attributes in ptypes, that is the
958 * array of strings "attribute_type" which is placed in newly allocated
959 * memory, and the values of the attributes in pvalues, that is the
960 * array of strings "attribute_value" which is placed in newly allocated
961 * memory. Returns 0 on success, -1 on failure.
963 * note: got part of the code from dn_validate
967 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
975 * explode the rdn in parts
977 parts = ldap_explode_rdn( rdn_in, 0 );
979 if ( parts == NULL ) {
983 for ( p = parts; p[0]; p++ ) {
986 /* split each rdn part in type value */
987 s = strchr( p[0], '=' );
989 charray_free( *ptypes );
990 charray_free( *pvalues );
991 charray_free( parts );
995 /* type should be fine */
996 charray_add_n( ptypes, p[0], ( s-p[0] ) );
998 /* value needs to be unescaped
999 * (maybe this should be moved to ldap_explode_rdn?) */
1000 for ( e = d = s + 1; e[0]; e++ ) {
1006 charray_add( pvalues, s + 1 );
1010 charray_free( parts );
1018 * 1 if rdn is a legal rdn;
1019 * 0 otherwise (including a sequence of rdns)
1021 * note: got it from dn_rdn; it should be rewritten
1022 * according to dn_validate
1025 rdn_validate( const char * rdn )
1029 if ( rdn == NULL ) {
1033 if ( strchr( rdn, '=' ) == NULL ) {
1037 while ( *rdn && ASCII_SPACE( *rdn ) ) {
1041 if( *rdn == '\0' ) {
1047 for ( ; *rdn; rdn++ ) {
1048 if ( *rdn == '\\' ) {
1055 if ( *rdn == '"' ) {
1059 if ( *rdn == '"' ) {
1061 } else if ( DN_SEPARATOR( *rdn ) ) {
1073 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1076 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
1080 build_new_dn( char ** new_dn,
1083 const char * newrdn )
1086 if ( p_dn == NULL ) {
1087 *new_dn = ch_strdup( newrdn );
1091 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
1093 strcpy( *new_dn, newrdn );
1094 strcat( *new_dn, "," );
1095 strcat( *new_dn, p_dn );
1098 #endif /* SLAP_DN_MIGRATION */
1101 * dnIsSuffix - tells whether suffix is a suffix of dn.
1102 * Both dn and suffix must be normalized.
1106 const struct berval *dn,
1107 const struct berval *suffix )
1109 int d = dn->bv_len - suffix->bv_len;
1114 /* empty suffix matches any dn */
1115 if ( suffix->bv_len == 0 ) {
1119 /* suffix longer than dn */
1124 /* no rdn separator or escaped rdn separator */
1125 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
1126 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
1130 /* no possible match or malformed dn */
1136 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );