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>
18 #include "ldap_pvt.h" /* must be after slap.h, to get ldap_bv2dn_x() & co */
22 * The DN syntax-related functions take advantage of the dn representation
23 * handling functions ldap_str2dn/ldap_dn2str. The latter are not schema-
24 * aware, so the attributes and their values need be validated (and possibly
25 * normalized). In the current implementation the required validation/nor-
26 * malization/"pretty"ing are done on newly created DN structural represen-
27 * tations; however the idea is to move towards DN handling in structural
28 * representation instead of the current string representation. To this
29 * purpose, we need to do only the required operations and keep track of
30 * what has been done to minimize their impact on performances.
32 * Developers are strongly encouraged to use this feature, to speed-up
36 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
39 * In-place, schema-aware validation of the
40 * structural representation of a distinguished name.
43 LDAPDN_validate( LDAPDN dn )
50 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
51 LDAPRDN rdn = dn[ iRDN ];
56 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
57 LDAPAVA *ava = rdn[ iAVA ];
58 AttributeDescription *ad;
59 slap_syntax_validate_func *validate = NULL;
63 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
64 const char *text = NULL;
66 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
67 if ( rc != LDAP_SUCCESS ) {
68 return LDAP_INVALID_SYNTAX;
71 ava->la_private = ( void * )ad;
75 * Replace attr oid/name with the canonical name
77 ava->la_attr = ad->ad_cname;
79 validate = ad->ad_type->sat_syntax->ssyn_validate;
83 * validate value by validate function
85 rc = ( *validate )( ad->ad_type->sat_syntax,
88 if ( rc != LDAP_SUCCESS ) {
89 return LDAP_INVALID_SYNTAX;
111 if ( in->bv_len == 0 ) {
114 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
115 return LDAP_INVALID_SYNTAX;
118 rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
119 if ( rc != LDAP_SUCCESS ) {
120 return LDAP_INVALID_SYNTAX;
123 assert( strlen( in->bv_val ) == in->bv_len );
126 * Schema-aware validate
128 rc = LDAPDN_validate( dn );
131 if ( rc != LDAP_SUCCESS ) {
132 return LDAP_INVALID_SYNTAX;
139 * AVA sorting inside a RDN
141 * rule: sort attributeTypes in alphabetical order; in case of multiple
142 * occurrences of the same attributeType, sort values in byte order
143 * (use memcmp, which implies alphabetical order in case of IA5 value;
144 * this should guarantee the repeatability of the operation).
146 * Note: the sorting can be slightly improved by sorting first
147 * by attribute type length, then by alphabetical order.
149 * uses a linear search; should be fine since the number of AVAs in
150 * a RDN should be limited.
153 AVA_Sort( LDAPRDN rdn, int iAVA )
156 LDAPAVA *ava_in = rdn[ iAVA ];
161 for ( i = 0; i < iAVA; i++ ) {
162 LDAPAVA *ava = rdn[ i ];
167 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
176 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
178 v = memcmp( ava_in->la_value.bv_val,
179 ava->la_value.bv_val,
180 d <= 0 ? ava_in->la_value.bv_len
181 : ava->la_value.bv_len );
183 if ( v == 0 && d != 0 ) {
202 a = strcmp( ava_in->la_attr.bv_val,
203 ava->la_attr.bv_val );
209 for ( j = iAVA; j > i; j-- ) {
210 rdn[ j ] = rdn[ j - 1 ];
219 * In-place, schema-aware normalization / "pretty"ing of the
220 * structural representation of a distinguished name.
223 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
230 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
231 LDAPRDN rdn = dn[ iRDN ];
236 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
237 LDAPAVA *ava = rdn[ iAVA ];
238 AttributeDescription *ad;
239 slap_syntax_validate_func *validf = NULL;
240 slap_mr_normalize_func *normf = NULL;
241 slap_syntax_transform_func *transf = NULL;
242 MatchingRule *mr = NULL;
243 struct berval bv = { 0, NULL };
248 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
249 const char *text = NULL;
251 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
252 if ( rc != LDAP_SUCCESS ) {
253 return LDAP_INVALID_SYNTAX;
256 ava->la_private = ( void * )ad;
261 * Replace attr oid/name with the canonical name
263 ava->la_attr = ad->ad_cname;
265 if( ava->la_flags & LDAP_AVA_BINARY ) {
266 if( ava->la_value.bv_len == 0 ) {
267 /* BER encoding is empty */
268 return LDAP_INVALID_SYNTAX;
271 /* AVA is binary encoded, don't muck with it */
272 } else if( flags & SLAP_LDAPDN_PRETTY ) {
273 transf = ad->ad_type->sat_syntax->ssyn_pretty;
275 validf = ad->ad_type->sat_syntax->ssyn_validate;
277 } else { /* normalization */
278 validf = ad->ad_type->sat_syntax->ssyn_validate;
279 mr = ad->ad_type->sat_equality;
280 if( mr ) normf = mr->smr_normalize;
284 /* validate value before normalization */
285 rc = ( *validf )( ad->ad_type->sat_syntax,
288 : (struct berval *) &slap_empty_bv );
290 if ( rc != LDAP_SUCCESS ) {
291 return LDAP_INVALID_SYNTAX;
297 * transform value by pretty function
298 * if value is empty, use empty_bv
300 rc = ( *transf )( ad->ad_type->sat_syntax,
303 : (struct berval *) &slap_empty_bv,
306 if ( rc != LDAP_SUCCESS ) {
307 return LDAP_INVALID_SYNTAX;
314 * if value is empty, use empty_bv
318 ad->ad_type->sat_syntax,
322 : (struct berval *) &slap_empty_bv,
325 if ( rc != LDAP_SUCCESS ) {
326 return LDAP_INVALID_SYNTAX;
332 ber_memfree_x( ava->la_value.bv_val, ctx );
336 if( do_sort ) AVA_Sort( rdn, iAVA );
355 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
357 if ( val->bv_len != 0 ) {
362 * Go to structural representation
364 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
365 if ( rc != LDAP_SUCCESS ) {
366 return LDAP_INVALID_SYNTAX;
369 assert( strlen( val->bv_val ) == val->bv_len );
372 * Schema-aware rewrite
374 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
375 ldap_dnfree_x( dn, ctx );
376 return LDAP_INVALID_SYNTAX;
380 * Back to string representation
382 rc = ldap_dn2bv_x( dn, out,
383 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
385 ldap_dnfree_x( dn, ctx );
387 if ( rc != LDAP_SUCCESS ) {
388 return LDAP_INVALID_SYNTAX;
391 ber_dupbv_x( out, val, ctx );
394 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
410 LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
412 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
415 if ( val->bv_len == 0 ) {
416 ber_dupbv_x( out, val, ctx );
418 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
419 return LDAP_INVALID_SYNTAX;
425 /* FIXME: should be liberal in what we accept */
426 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
427 if ( rc != LDAP_SUCCESS ) {
428 return LDAP_INVALID_SYNTAX;
431 assert( strlen( val->bv_val ) == val->bv_len );
434 * Schema-aware rewrite
436 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
437 ldap_dnfree_x( dn, ctx );
438 return LDAP_INVALID_SYNTAX;
441 /* FIXME: not sure why the default isn't pretty */
442 /* RE: the default is the form that is used as
443 * an internal representation; the pretty form
445 rc = ldap_dn2bv_x( dn, out,
446 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
448 ldap_dnfree_x( dn, ctx );
450 if ( rc != LDAP_SUCCESS ) {
451 return LDAP_INVALID_SYNTAX;
456 LDAP_LOG( OPERATION, ARGS, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
458 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
476 LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n",
477 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
480 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
481 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
485 if ( val->bv_len == 0 ) {
488 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
489 return LDAP_INVALID_SYNTAX;
494 /* FIXME: should be liberal in what we accept */
495 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
496 if ( rc != LDAP_SUCCESS ) {
497 return LDAP_INVALID_SYNTAX;
500 assert( strlen( val->bv_val ) == val->bv_len );
503 * Schema-aware rewrite
505 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
506 ldap_dnfree_x( *dn, ctx );
508 return LDAP_INVALID_SYNTAX;
512 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
513 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
520 * Combination of both dnPretty and dnNormalize
526 struct berval *pretty,
527 struct berval *normal,
531 LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
533 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
540 if ( val->bv_len == 0 ) {
541 ber_dupbv_x( pretty, val, ctx );
542 ber_dupbv_x( normal, val, ctx );
544 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
546 return LDAP_INVALID_SYNTAX;
552 pretty->bv_val = NULL;
553 normal->bv_val = NULL;
557 /* FIXME: should be liberal in what we accept */
558 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
559 if ( rc != LDAP_SUCCESS ) {
560 return LDAP_INVALID_SYNTAX;
563 assert( strlen( val->bv_val ) == val->bv_len );
566 * Schema-aware rewrite
568 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
569 ldap_dnfree_x( dn, ctx );
570 return LDAP_INVALID_SYNTAX;
573 rc = ldap_dn2bv_x( dn, pretty,
574 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
576 if ( rc != LDAP_SUCCESS ) {
577 ldap_dnfree_x( dn, ctx );
578 return LDAP_INVALID_SYNTAX;
581 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
582 ldap_dnfree_x( dn, ctx );
583 ber_memfree_x( pretty->bv_val, ctx );
584 pretty->bv_val = NULL;
586 return LDAP_INVALID_SYNTAX;
589 rc = ldap_dn2bv_x( dn, normal,
590 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
592 ldap_dnfree_x( dn, ctx );
593 if ( rc != LDAP_SUCCESS ) {
594 ber_memfree_x( pretty->bv_val, ctx );
595 pretty->bv_val = NULL;
597 return LDAP_INVALID_SYNTAX;
602 LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
603 pretty->bv_val, normal->bv_val, 0 );
605 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
606 pretty->bv_val, normal->bv_val, 0 );
621 struct berval *value,
622 void *assertedValue )
625 struct berval *asserted = (struct berval *) assertedValue;
629 assert( assertedValue );
631 match = value->bv_len - asserted->bv_len;
634 match = memcmp( value->bv_val, asserted->bv_val,
639 LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n %s\n %s\n",
640 match, value->bv_val, asserted->bv_val );
642 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
643 match, value->bv_val, asserted->bv_val );
647 return( LDAP_SUCCESS );
651 * dnParent - dn's parent, in-place
653 * note: the incoming dn is assumed to be normalized/prettyfied,
654 * so that escaped rdn/ava separators are in '\'+hexpair form
663 p = strchr( dn->bv_val, ',' );
668 pdn->bv_val = dn->bv_val + dn->bv_len;
672 assert( DN_SEPARATOR( p[ 0 ] ) );
675 assert( ATTR_LEADCHAR( p[ 0 ] ) );
677 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
695 if( dn->bv_len == 0 ) {
699 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
700 if ( rc != LDAP_SUCCESS ) {
704 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
706 ldap_rdnfree_x( tmpRDN, ctx );
707 if ( rc != LDAP_SUCCESS ) {
715 * We can assume the input is a prettied or normalized DN
720 struct berval *dn_in )
726 if ( dn_in == NULL ) {
730 if ( !dn_in->bv_len ) {
734 if ( be != NULL && be_issuffix( be, dn_in ) ) {
738 p = strchr( dn_in->bv_val, ',' );
740 return p ? p - dn_in->bv_val : dn_in->bv_len;
746 * LDAP_SUCCESS if rdn is a legal rdn;
747 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
750 rdnValidate( struct berval *rdn )
754 * input is a pretty or normalized DN
755 * hence, we can just search for ','
757 if( rdn == NULL || rdn->bv_len == 0 ||
758 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
760 return LDAP_INVALID_SYNTAX;
763 return strchr( rdn->bv_val, ',' ) == NULL
764 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
767 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
774 if ( rdn == NULL || rdn == '\0' ) {
781 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
782 if ( rc != LDAP_SUCCESS ) {
789 if ( p[ 0 ] != '\0' ) {
794 * Schema-aware validate
796 if ( rc == LDAP_SUCCESS ) {
797 rc = LDAPDN_validate( DN );
802 * Must validate (there's a repeated parsing ...)
804 return ( rc == LDAP_SUCCESS );
811 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
814 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
818 build_new_dn( struct berval * new_dn,
819 struct berval * parent_dn,
820 struct berval * newrdn,
825 if ( parent_dn == NULL ) {
826 ber_dupbv( new_dn, newrdn );
830 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
831 new_dn->bv_val = (char *) sl_malloc( new_dn->bv_len + 1, memctx );
833 ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
835 strcpy( ptr, parent_dn->bv_val );
840 * dnIsSuffix - tells whether suffix is a suffix of dn.
841 * Both dn and suffix must be normalized.
845 const struct berval *dn,
846 const struct berval *suffix )
848 int d = dn->bv_len - suffix->bv_len;
853 /* empty suffix matches any dn */
854 if ( suffix->bv_len == 0 ) {
858 /* suffix longer than dn */
863 /* no rdn separator or escaped rdn separator */
864 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
868 /* no possible match or malformed dn */
874 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
879 * Convert an X.509 DN into a normalized LDAP DN
882 dnX509normalize( void *x509_name, struct berval *out )
884 /* Invoke the LDAP library's converter with our schema-rewriter */
885 return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
889 * Get the TLS session's peer's DN into a normalized LDAP DN
892 dnX509peerNormalize( void *ssl, struct berval *dn )
895 return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );