1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
23 const struct berval slap_empty_bv = { 0, "" };
26 * The DN syntax-related functions take advantage of the dn representation
27 * handling functions ldap_str2dn/ldap_dn2str. The latter are not schema-
28 * aware, so the attributes and their values need be validated (and possibly
29 * normalized). In the current implementation the required validation/nor-
30 * malization/"pretty"ing are done on newly created DN structural represen-
31 * tations; however the idea is to move towards DN handling in structural
32 * representation instead of the current string representation. To this
33 * purpose, we need to do only the required operations and keep track of
34 * what has been done to minimize their impact on performances.
36 * Developers are strongly encouraged to use this feature, to speed-up
40 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
43 * In-place, schema-aware validation of the
44 * structural representation of a distinguished name.
47 LDAPDN_validate( LDAPDN *dn )
54 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
55 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
60 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
61 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
62 AttributeDescription *ad;
63 slap_syntax_validate_func *validate = NULL;
67 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
68 const char *text = NULL;
70 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
71 if ( rc != LDAP_SUCCESS ) {
72 return LDAP_INVALID_SYNTAX;
75 ava->la_private = ( void * )ad;
79 * Replace attr oid/name with the canonical name
81 ava->la_attr = ad->ad_cname;
83 validate = ad->ad_type->sat_syntax->ssyn_validate;
87 * validate value by validate function
89 rc = ( *validate )( ad->ad_type->sat_syntax,
92 if ( rc != LDAP_SUCCESS ) {
93 return LDAP_INVALID_SYNTAX;
103 * dn validate routine
115 if ( in->bv_len == 0 ) {
118 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
119 return LDAP_INVALID_SYNTAX;
122 rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
123 if ( rc != LDAP_SUCCESS ) {
124 return LDAP_INVALID_SYNTAX;
127 assert( strlen( in->bv_val ) == in->bv_len );
130 * Schema-aware validate
132 rc = LDAPDN_validate( dn );
135 if ( rc != LDAP_SUCCESS ) {
136 return LDAP_INVALID_SYNTAX;
143 * AVA sorting inside a RDN
145 * rule: sort attributeTypes in alphabetical order; in case of multiple
146 * occurrences of the same attributeType, sort values in byte order
147 * (use memcmp, which implies alphabetical order in case of IA5 value;
148 * this should guarantee the repeatability of the operation).
150 * Note: the sorting can be slightly improved by sorting first
151 * by attribute type length, then by alphabetical order.
153 * uses a linear search; should be fine since the number of AVAs in
154 * a RDN should be limited.
157 AVA_Sort( LDAPRDN *rdn, int iAVA )
160 LDAPAVA *ava_in = rdn[ 0 ][ iAVA ];
165 for ( i = 0; i < iAVA; i++ ) {
166 LDAPAVA *ava = rdn[ 0 ][ i ];
171 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
180 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
182 v = memcmp( ava_in->la_value.bv_val,
183 ava->la_value.bv_val,
184 d <= 0 ? ava_in->la_value.bv_len
185 : ava->la_value.bv_len );
187 if ( v == 0 && d != 0 ) {
206 a = strcmp( ava_in->la_attr.bv_val,
207 ava->la_attr.bv_val );
213 for ( j = iAVA; j > i; j-- ) {
214 rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
216 rdn[ 0 ][ i ] = ava_in;
223 * In-place, schema-aware normalization / "pretty"ing of the
224 * structural representation of a distinguished name.
227 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
234 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
235 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
240 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
241 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
242 AttributeDescription *ad;
243 slap_syntax_validate_func *validf = NULL;
244 slap_syntax_transform_func *transf = NULL;
246 struct berval bv = { 0, NULL };
251 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
252 const char *text = NULL;
254 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
255 if ( rc != LDAP_SUCCESS ) {
256 return LDAP_INVALID_SYNTAX;
259 ava->la_private = ( void * )ad;
264 * Replace attr oid/name with the canonical name
266 ava->la_attr = ad->ad_cname;
268 if( ava->la_flags & LDAP_AVA_BINARY ) {
269 /* AVA is binary encoded, don't muck with it */
273 } else if( flags & SLAP_LDAPDN_PRETTY ) {
275 transf = ad->ad_type->sat_syntax->ssyn_pretty;
278 validf = ad->ad_type->sat_syntax->ssyn_validate;
279 transf = ad->ad_type->sat_syntax->ssyn_normalize;
280 mr = ad->ad_type->sat_equality;
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 normalize/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;
311 if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
314 if ( UTF8bvnormalize( &bv, &bv,
315 LDAP_UTF8_CASEFOLD ) == NULL ) {
316 return LDAP_INVALID_SYNTAX;
322 free( ava->la_value.bv_val );
326 if( do_sort ) AVA_Sort( rdn, iAVA );
334 * dn normalize routine
340 struct berval **normalized )
345 assert( normalized && *normalized == NULL );
347 out = ch_malloc( sizeof( struct berval ) );
348 rc = dnNormalize2( syntax, val, out );
349 if ( rc != LDAP_SUCCESS )
365 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
367 if ( val->bv_len != 0 ) {
372 * Go to structural representation
374 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
375 if ( rc != LDAP_SUCCESS ) {
376 return LDAP_INVALID_SYNTAX;
379 assert( strlen( val->bv_val ) == val->bv_len );
382 * Schema-aware rewrite
384 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
386 return LDAP_INVALID_SYNTAX;
390 * Back to string representation
392 rc = ldap_dn2bv( dn, out,
393 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
397 if ( rc != LDAP_SUCCESS ) {
398 return LDAP_INVALID_SYNTAX;
401 ber_dupbv( out, val );
404 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
410 * dn "pretty"ing routine
416 struct berval **pretty)
421 assert( pretty && *pretty == NULL );
423 out = ch_malloc( sizeof( struct berval ) );
424 rc = dnPretty2( syntax, val, out );
425 if ( rc != LDAP_SUCCESS )
442 LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
444 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
447 if ( val->bv_len == 0 ) {
448 ber_dupbv( out, val );
450 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
451 return LDAP_INVALID_SYNTAX;
457 /* FIXME: should be liberal in what we accept */
458 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
459 if ( rc != LDAP_SUCCESS ) {
460 return LDAP_INVALID_SYNTAX;
463 assert( strlen( val->bv_val ) == val->bv_len );
466 * Schema-aware rewrite
468 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
470 return LDAP_INVALID_SYNTAX;
473 /* FIXME: not sure why the default isn't pretty */
474 /* RE: the default is the form that is used as
475 * an internal representation; the pretty form
477 rc = ldap_dn2bv( dn, out,
478 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
482 if ( rc != LDAP_SUCCESS ) {
483 return LDAP_INVALID_SYNTAX;
487 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
503 LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n",
504 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
507 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
508 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
512 if ( val->bv_len == 0 ) {
515 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
516 return LDAP_INVALID_SYNTAX;
521 /* FIXME: should be liberal in what we accept */
522 rc = ldap_bv2dn( val, dn, LDAP_DN_FORMAT_LDAP );
523 if ( rc != LDAP_SUCCESS ) {
524 return LDAP_INVALID_SYNTAX;
527 assert( strlen( val->bv_val ) == val->bv_len );
530 * Schema-aware rewrite
532 if ( LDAPDN_rewrite( *dn, flags ) != LDAP_SUCCESS ) {
535 return LDAP_INVALID_SYNTAX;
539 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
540 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
547 * Combination of both dnPretty and dnNormalize
553 struct berval *pretty,
554 struct berval *normal)
557 LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
559 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
566 if ( val->bv_len == 0 ) {
567 ber_dupbv( pretty, val );
568 ber_dupbv( normal, val );
570 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
572 return LDAP_INVALID_SYNTAX;
578 pretty->bv_val = NULL;
579 normal->bv_val = NULL;
583 /* FIXME: should be liberal in what we accept */
584 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
585 if ( rc != LDAP_SUCCESS ) {
586 return LDAP_INVALID_SYNTAX;
589 assert( strlen( val->bv_val ) == val->bv_len );
592 * Schema-aware rewrite
594 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
596 return LDAP_INVALID_SYNTAX;
599 rc = ldap_dn2bv( dn, pretty,
600 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
602 if ( rc != LDAP_SUCCESS ) {
604 return LDAP_INVALID_SYNTAX;
607 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
609 free( pretty->bv_val );
610 pretty->bv_val = NULL;
612 return LDAP_INVALID_SYNTAX;
615 rc = ldap_dn2bv( dn, normal,
616 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
619 if ( rc != LDAP_SUCCESS ) {
620 free( pretty->bv_val );
621 pretty->bv_val = NULL;
623 return LDAP_INVALID_SYNTAX;
628 LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
629 pretty->bv_val, normal->bv_val, 0 );
631 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
632 pretty->bv_val, normal->bv_val, 0 );
647 struct berval *value,
648 void *assertedValue )
651 struct berval *asserted = (struct berval *) assertedValue;
655 assert( assertedValue );
657 match = value->bv_len - asserted->bv_len;
660 match = memcmp( value->bv_val, asserted->bv_val,
665 LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n %s\n %s\n",
666 match, value->bv_val, asserted->bv_val );
668 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
669 match, value->bv_val, asserted->bv_val );
673 return( LDAP_SUCCESS );
677 * dnParent - dn's parent, in-place
679 * note: the incoming dn is assumed to be normalized/prettyfied,
680 * so that escaped rdn/ava separators are in '\'+hexpair form
689 p = strchr( dn->bv_val, ',' );
694 pdn->bv_val = dn->bv_val + dn->bv_len;
698 assert( DN_SEPARATOR( p[ 0 ] ) );
701 assert( ATTR_LEADCHAR( p[ 0 ] ) );
703 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
720 if( dn->bv_len == 0 ) {
724 rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
725 if ( rc != LDAP_SUCCESS ) {
729 rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
731 ldap_rdnfree( tmpRDN );
732 if ( rc != LDAP_SUCCESS ) {
740 * We can assume the input is a prettied or normalized DN
745 struct berval *dn_in )
751 if ( dn_in == NULL ) {
755 if ( !dn_in->bv_len ) {
759 if ( be != NULL && be_issuffix( be, dn_in ) ) {
763 p = strchr( dn_in->bv_val, ',' );
765 return p ? p - dn_in->bv_val : dn_in->bv_len;
771 * LDAP_SUCCESS if rdn is a legal rdn;
772 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
775 rdnValidate( struct berval *rdn )
779 * input is a pretty or normalized DN
780 * hence, we can just search for ','
782 if( rdn == NULL || rdn->bv_len == 0 ||
783 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
785 return LDAP_INVALID_SYNTAX;
788 return strchr( rdn->bv_val, ',' ) == NULL
789 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
792 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
799 if ( rdn == NULL || rdn == '\0' ) {
806 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
807 if ( rc != LDAP_SUCCESS ) {
814 if ( p[ 0 ] != '\0' ) {
819 * Schema-aware validate
821 if ( rc == LDAP_SUCCESS ) {
822 rc = LDAPDN_validate( DN );
827 * Must validate (there's a repeated parsing ...)
829 return ( rc == LDAP_SUCCESS );
836 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
839 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
843 build_new_dn( struct berval * new_dn,
844 struct berval * parent_dn,
845 struct berval * newrdn )
849 if ( parent_dn == NULL ) {
850 ber_dupbv( new_dn, newrdn );
854 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
855 new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
857 ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
859 strcpy( ptr, parent_dn->bv_val );
864 * dnIsSuffix - tells whether suffix is a suffix of dn.
865 * Both dn and suffix must be normalized.
869 const struct berval *dn,
870 const struct berval *suffix )
872 int d = dn->bv_len - suffix->bv_len;
877 /* empty suffix matches any dn */
878 if ( suffix->bv_len == 0 ) {
882 /* suffix longer than dn */
887 /* no rdn separator or escaped rdn separator */
888 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
892 /* no possible match or malformed dn */
898 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
903 * Convert an X.509 DN into a normalized LDAP DN
906 dnX509normalize( void *x509_name, struct berval *out )
908 /* Invoke the LDAP library's converter with our schema-rewriter */
909 return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
913 * Get the TLS session's peer's DN into a normalized LDAP DN
916 dnX509peerNormalize( void *ssl, struct berval *dn )
919 return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );