1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
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 ];
58 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
59 LDAPAVA *ava = rdn[ iAVA ];
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 ava->la_attr = ad->ad_cname;
81 validate = ad->ad_type->sat_syntax->ssyn_validate;
85 * validate value by validate function
87 rc = ( *validate )( ad->ad_type->sat_syntax,
90 if ( rc != LDAP_SUCCESS ) {
91 return LDAP_INVALID_SYNTAX;
101 * dn validate routine
113 if ( in->bv_len == 0 ) {
116 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
117 return LDAP_INVALID_SYNTAX;
120 rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
121 if ( rc != LDAP_SUCCESS ) {
122 return LDAP_INVALID_SYNTAX;
125 assert( strlen( in->bv_val ) == in->bv_len );
128 * Schema-aware validate
130 rc = LDAPDN_validate( dn );
133 if ( rc != LDAP_SUCCESS ) {
134 return LDAP_INVALID_SYNTAX;
141 * AVA sorting inside a RDN
143 * rule: sort attributeTypes in alphabetical order; in case of multiple
144 * occurrences of the same attributeType, sort values in byte order
145 * (use memcmp, which implies alphabetical order in case of IA5 value;
146 * this should guarantee the repeatability of the operation).
148 * Note: the sorting can be slightly improved by sorting first
149 * by attribute type length, then by alphabetical order.
151 * uses a linear search; should be fine since the number of AVAs in
152 * a RDN should be limited.
155 AVA_Sort( LDAPRDN rdn, int iAVA )
158 LDAPAVA *ava_in = rdn[ iAVA ];
163 for ( i = 0; i < iAVA; i++ ) {
164 LDAPAVA *ava = rdn[ i ];
169 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
178 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
180 v = memcmp( ava_in->la_value.bv_val,
181 ava->la_value.bv_val,
182 d <= 0 ? ava_in->la_value.bv_len
183 : ava->la_value.bv_len );
185 if ( v == 0 && d != 0 ) {
204 a = strcmp( ava_in->la_attr.bv_val,
205 ava->la_attr.bv_val );
211 for ( j = iAVA; j > i; j-- ) {
212 rdn[ j ] = rdn[ j - 1 ];
221 * In-place, schema-aware normalization / "pretty"ing of the
222 * structural representation of a distinguished name.
225 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
232 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
233 LDAPRDN rdn = dn[ iRDN ];
238 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
239 LDAPAVA *ava = rdn[ iAVA ];
240 AttributeDescription *ad;
241 slap_syntax_validate_func *validf = NULL;
242 slap_mr_normalize_func *normf = NULL;
243 slap_syntax_transform_func *transf = NULL;
244 MatchingRule *mr = NULL;
245 struct berval bv = { 0, NULL };
250 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
251 const char *text = NULL;
253 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
254 if ( rc != LDAP_SUCCESS ) {
255 return LDAP_INVALID_SYNTAX;
258 ava->la_private = ( void * )ad;
263 * Replace attr oid/name with the canonical name
265 ava->la_attr = ad->ad_cname;
267 if( ava->la_flags & LDAP_AVA_BINARY ) {
268 if( ava->la_value.bv_len == 0 ) {
269 /* BER encoding is empty */
270 return LDAP_INVALID_SYNTAX;
273 /* AVA is binary encoded, don't muck with it */
274 } else if( flags & SLAP_LDAPDN_PRETTY ) {
275 transf = ad->ad_type->sat_syntax->ssyn_pretty;
277 validf = ad->ad_type->sat_syntax->ssyn_validate;
279 } else { /* normalization */
280 validf = ad->ad_type->sat_syntax->ssyn_validate;
281 mr = ad->ad_type->sat_equality;
282 if( mr ) normf = mr->smr_normalize;
286 /* validate value before normalization */
287 rc = ( *validf )( ad->ad_type->sat_syntax,
290 : (struct berval *) &slap_empty_bv );
292 if ( rc != LDAP_SUCCESS ) {
293 return LDAP_INVALID_SYNTAX;
299 * transform value by pretty function
300 * if value is empty, use empty_bv
302 rc = ( *transf )( ad->ad_type->sat_syntax,
305 : (struct berval *) &slap_empty_bv,
308 if ( rc != LDAP_SUCCESS ) {
309 return LDAP_INVALID_SYNTAX;
316 * if value is empty, use empty_bv
320 ad->ad_type->sat_syntax,
324 : (struct berval *) &slap_empty_bv,
327 if ( rc != LDAP_SUCCESS ) {
328 return LDAP_INVALID_SYNTAX;
334 ber_memfree_x( ava->la_value.bv_val, ctx );
338 if( do_sort ) AVA_Sort( rdn, iAVA );
357 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
359 if ( val->bv_len != 0 ) {
364 * Go to structural representation
366 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
367 if ( rc != LDAP_SUCCESS ) {
368 return LDAP_INVALID_SYNTAX;
371 assert( strlen( val->bv_val ) == val->bv_len );
374 * Schema-aware rewrite
376 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
378 return LDAP_INVALID_SYNTAX;
382 * Back to string representation
384 rc = ldap_dn2bv_x( dn, out,
385 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
387 ldap_dnfree_x( dn, ctx );
389 if ( rc != LDAP_SUCCESS ) {
390 return LDAP_INVALID_SYNTAX;
393 ber_dupbv_x( out, val, ctx );
396 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
412 LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
414 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
417 if ( val->bv_len == 0 ) {
418 ber_dupbv_x( out, val, ctx );
420 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
421 return LDAP_INVALID_SYNTAX;
427 /* FIXME: should be liberal in what we accept */
428 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
429 if ( rc != LDAP_SUCCESS ) {
430 return LDAP_INVALID_SYNTAX;
433 assert( strlen( val->bv_val ) == val->bv_len );
436 * Schema-aware rewrite
438 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
439 ldap_dnfree_x( dn, ctx );
440 return LDAP_INVALID_SYNTAX;
443 /* FIXME: not sure why the default isn't pretty */
444 /* RE: the default is the form that is used as
445 * an internal representation; the pretty form
447 rc = ldap_dn2bv_x( dn, out,
448 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
450 ldap_dnfree_x( dn, ctx );
452 if ( rc != LDAP_SUCCESS ) {
453 return LDAP_INVALID_SYNTAX;
457 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
474 LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n",
475 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
478 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
479 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
483 if ( val->bv_len == 0 ) {
486 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
487 return LDAP_INVALID_SYNTAX;
492 /* FIXME: should be liberal in what we accept */
493 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
494 if ( rc != LDAP_SUCCESS ) {
495 return LDAP_INVALID_SYNTAX;
498 assert( strlen( val->bv_val ) == val->bv_len );
501 * Schema-aware rewrite
503 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
504 ldap_dnfree_x( *dn, ctx );
506 return LDAP_INVALID_SYNTAX;
510 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
511 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
518 * Combination of both dnPretty and dnNormalize
524 struct berval *pretty,
525 struct berval *normal,
529 LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
531 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
538 if ( val->bv_len == 0 ) {
539 ber_dupbv_x( pretty, val, ctx );
540 ber_dupbv_x( normal, val, ctx );
542 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
544 return LDAP_INVALID_SYNTAX;
550 pretty->bv_val = NULL;
551 normal->bv_val = NULL;
555 /* FIXME: should be liberal in what we accept */
556 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
557 if ( rc != LDAP_SUCCESS ) {
558 return LDAP_INVALID_SYNTAX;
561 assert( strlen( val->bv_val ) == val->bv_len );
564 * Schema-aware rewrite
566 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
567 ldap_dnfree_x( dn, ctx );
568 return LDAP_INVALID_SYNTAX;
571 rc = ldap_dn2bv_x( dn, pretty,
572 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
574 if ( rc != LDAP_SUCCESS ) {
575 ldap_dnfree_x( dn, ctx );
576 return LDAP_INVALID_SYNTAX;
579 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
580 ldap_dnfree_x( dn, ctx );
581 ber_memfree_x( pretty->bv_val, ctx );
582 pretty->bv_val = NULL;
584 return LDAP_INVALID_SYNTAX;
587 rc = ldap_dn2bv_x( dn, normal,
588 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
590 ldap_dnfree_x( dn, ctx );
591 if ( rc != LDAP_SUCCESS ) {
592 ber_memfree_x( pretty->bv_val, ctx );
593 pretty->bv_val = NULL;
595 return LDAP_INVALID_SYNTAX;
600 LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
601 pretty->bv_val, normal->bv_val, 0 );
603 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
604 pretty->bv_val, normal->bv_val, 0 );
619 struct berval *value,
620 void *assertedValue )
623 struct berval *asserted = (struct berval *) assertedValue;
627 assert( assertedValue );
629 match = value->bv_len - asserted->bv_len;
632 match = memcmp( value->bv_val, asserted->bv_val,
637 LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n %s\n %s\n",
638 match, value->bv_val, asserted->bv_val );
640 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
641 match, value->bv_val, asserted->bv_val );
645 return( LDAP_SUCCESS );
649 * dnParent - dn's parent, in-place
651 * note: the incoming dn is assumed to be normalized/prettyfied,
652 * so that escaped rdn/ava separators are in '\'+hexpair form
661 p = strchr( dn->bv_val, ',' );
666 pdn->bv_val = dn->bv_val + dn->bv_len;
670 assert( DN_SEPARATOR( p[ 0 ] ) );
673 assert( ATTR_LEADCHAR( p[ 0 ] ) );
675 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
693 if( dn->bv_len == 0 ) {
697 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
698 if ( rc != LDAP_SUCCESS ) {
702 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
704 ldap_rdnfree_x( tmpRDN, ctx );
705 if ( rc != LDAP_SUCCESS ) {
713 * We can assume the input is a prettied or normalized DN
718 struct berval *dn_in )
724 if ( dn_in == NULL ) {
728 if ( !dn_in->bv_len ) {
732 if ( be != NULL && be_issuffix( be, dn_in ) ) {
736 p = strchr( dn_in->bv_val, ',' );
738 return p ? p - dn_in->bv_val : dn_in->bv_len;
744 * LDAP_SUCCESS if rdn is a legal rdn;
745 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
748 rdnValidate( struct berval *rdn )
752 * input is a pretty or normalized DN
753 * hence, we can just search for ','
755 if( rdn == NULL || rdn->bv_len == 0 ||
756 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
758 return LDAP_INVALID_SYNTAX;
761 return strchr( rdn->bv_val, ',' ) == NULL
762 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
765 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
772 if ( rdn == NULL || rdn == '\0' ) {
779 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
780 if ( rc != LDAP_SUCCESS ) {
787 if ( p[ 0 ] != '\0' ) {
792 * Schema-aware validate
794 if ( rc == LDAP_SUCCESS ) {
795 rc = LDAPDN_validate( DN );
800 * Must validate (there's a repeated parsing ...)
802 return ( rc == LDAP_SUCCESS );
809 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
812 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
816 build_new_dn( struct berval * new_dn,
817 struct berval * parent_dn,
818 struct berval * newrdn )
822 if ( parent_dn == NULL ) {
823 ber_dupbv( new_dn, newrdn );
827 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
828 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
830 ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
832 strcpy( ptr, parent_dn->bv_val );
837 * dnIsSuffix - tells whether suffix is a suffix of dn.
838 * Both dn and suffix must be normalized.
842 const struct berval *dn,
843 const struct berval *suffix )
845 int d = dn->bv_len - suffix->bv_len;
850 /* empty suffix matches any dn */
851 if ( suffix->bv_len == 0 ) {
855 /* suffix longer than dn */
860 /* no rdn separator or escaped rdn separator */
861 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
865 /* no possible match or malformed dn */
871 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
876 * Convert an X.509 DN into a normalized LDAP DN
879 dnX509normalize( void *x509_name, struct berval *out )
881 /* Invoke the LDAP library's converter with our schema-rewriter */
882 return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
886 * Get the TLS session's peer's DN into a normalized LDAP DN
889 dnX509peerNormalize( void *ssl, struct berval *dn )
892 return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );