1 /* dn.c - routines for dealing with distinguished names */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
32 #include <ac/socket.h>
33 #include <ac/string.h>
37 #include "ldap_pvt.h" /* must be after slap.h, to get ldap_bv2dn_x() & co */
41 * The DN syntax-related functions take advantage of the dn representation
42 * handling functions ldap_str2dn/ldap_dn2str. The latter are not schema-
43 * aware, so the attributes and their values need be validated (and possibly
44 * normalized). In the current implementation the required validation/nor-
45 * malization/"pretty"ing are done on newly created DN structural represen-
46 * tations; however the idea is to move towards DN handling in structural
47 * representation instead of the current string representation. To this
48 * purpose, we need to do only the required operations and keep track of
49 * what has been done to minimize their impact on performances.
51 * Developers are strongly encouraged to use this feature, to speed-up
55 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
58 LDAPRDN_validate( LDAPRDN rdn )
65 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
66 LDAPAVA *ava = rdn[ iAVA ];
67 AttributeDescription *ad;
68 slap_syntax_validate_func *validate = NULL;
72 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
73 const char *text = NULL;
75 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
76 if ( rc != LDAP_SUCCESS ) {
77 return LDAP_INVALID_SYNTAX;
80 ava->la_private = ( void * )ad;
84 * Replace attr oid/name with the canonical name
86 ava->la_attr = ad->ad_cname;
88 validate = ad->ad_type->sat_syntax->ssyn_validate;
92 * validate value by validate function
94 rc = ( *validate )( ad->ad_type->sat_syntax,
97 if ( rc != LDAP_SUCCESS ) {
98 return LDAP_INVALID_SYNTAX;
107 * In-place, schema-aware validation of the
108 * structural representation of a distinguished name.
111 LDAPDN_validate( LDAPDN dn )
118 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
119 LDAPRDN rdn = dn[ iRDN ];
124 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
125 LDAPAVA *ava = rdn[ iAVA ];
126 AttributeDescription *ad;
127 slap_syntax_validate_func *validate = NULL;
131 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
132 const char *text = NULL;
134 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
135 if ( rc != LDAP_SUCCESS ) {
136 return LDAP_INVALID_SYNTAX;
139 ava->la_private = ( void * )ad;
143 * Replace attr oid/name with the canonical name
145 ava->la_attr = ad->ad_cname;
147 validate = ad->ad_type->sat_syntax->ssyn_validate;
151 * validate value by validate function
153 rc = ( *validate )( ad->ad_type->sat_syntax,
156 if ( rc != LDAP_SUCCESS ) {
157 return LDAP_INVALID_SYNTAX;
167 * dn validate routine
179 if ( in->bv_len == 0 ) {
182 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
183 return LDAP_INVALID_SYNTAX;
186 rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
187 if ( rc != LDAP_SUCCESS ) {
188 return LDAP_INVALID_SYNTAX;
191 assert( strlen( in->bv_val ) == in->bv_len );
194 * Schema-aware validate
196 rc = LDAPDN_validate( dn );
199 if ( rc != LDAP_SUCCESS ) {
200 return LDAP_INVALID_SYNTAX;
216 if ( in->bv_len == 0 ) {
219 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
220 return LDAP_INVALID_SYNTAX;
223 rc = ldap_bv2rdn_x( in , &rdn, (char **) &p,
224 LDAP_DN_FORMAT_LDAP, NULL);
225 if ( rc != LDAP_SUCCESS ) {
226 return LDAP_INVALID_SYNTAX;
229 assert( strlen( in->bv_val ) == in->bv_len );
232 * Schema-aware validate
234 rc = LDAPRDN_validate( rdn );
237 if ( rc != LDAP_SUCCESS ) {
238 return LDAP_INVALID_SYNTAX;
246 * AVA sorting inside a RDN
248 * rule: sort attributeTypes in alphabetical order; in case of multiple
249 * occurrences of the same attributeType, sort values in byte order
250 * (use memcmp, which implies alphabetical order in case of IA5 value;
251 * this should guarantee the repeatability of the operation).
253 * Note: the sorting can be slightly improved by sorting first
254 * by attribute type length, then by alphabetical order.
256 * uses a linear search; should be fine since the number of AVAs in
257 * a RDN should be limited.
260 AVA_Sort( LDAPRDN rdn, int iAVA )
263 LDAPAVA *ava_in = rdn[ iAVA ];
268 for ( i = 0; i < iAVA; i++ ) {
269 LDAPAVA *ava = rdn[ i ];
274 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
283 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
285 v = memcmp( ava_in->la_value.bv_val,
286 ava->la_value.bv_val,
287 d <= 0 ? ava_in->la_value.bv_len
288 : ava->la_value.bv_len );
290 if ( v == 0 && d != 0 ) {
309 a = strcmp( ava_in->la_attr.bv_val,
310 ava->la_attr.bv_val );
316 for ( j = iAVA; j > i; j-- ) {
317 rdn[ j ] = rdn[ j - 1 ];
326 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
331 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
332 LDAPAVA *ava = rdn[ iAVA ];
333 AttributeDescription *ad;
334 slap_syntax_validate_func *validf = NULL;
335 slap_mr_normalize_func *normf = NULL;
336 slap_syntax_transform_func *transf = NULL;
337 MatchingRule *mr = NULL;
338 struct berval bv = BER_BVNULL;
343 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
344 const char *text = NULL;
346 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
347 if ( rc != LDAP_SUCCESS ) {
348 return LDAP_INVALID_SYNTAX;
351 ava->la_private = ( void * )ad;
356 * Replace attr oid/name with the canonical name
358 ava->la_attr = ad->ad_cname;
360 if( ava->la_flags & LDAP_AVA_BINARY ) {
361 if( ava->la_value.bv_len == 0 ) {
362 /* BER encoding is empty */
363 return LDAP_INVALID_SYNTAX;
366 /* AVA is binary encoded, don't muck with it */
367 } else if( flags & SLAP_LDAPDN_PRETTY ) {
368 transf = ad->ad_type->sat_syntax->ssyn_pretty;
370 validf = ad->ad_type->sat_syntax->ssyn_validate;
372 } else { /* normalization */
373 validf = ad->ad_type->sat_syntax->ssyn_validate;
374 mr = ad->ad_type->sat_equality;
375 if( mr ) normf = mr->smr_normalize;
379 /* validate value before normalization */
380 rc = ( *validf )( ad->ad_type->sat_syntax,
383 : (struct berval *) &slap_empty_bv );
385 if ( rc != LDAP_SUCCESS ) {
386 return LDAP_INVALID_SYNTAX;
392 * transform value by pretty function
393 * if value is empty, use empty_bv
395 rc = ( *transf )( ad->ad_type->sat_syntax,
398 : (struct berval *) &slap_empty_bv,
401 if ( rc != LDAP_SUCCESS ) {
402 return LDAP_INVALID_SYNTAX;
409 * if value is empty, use empty_bv
412 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
413 ad->ad_type->sat_syntax,
417 : (struct berval *) &slap_empty_bv,
420 if ( rc != LDAP_SUCCESS ) {
421 return LDAP_INVALID_SYNTAX;
427 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
428 ber_memfree_x( ava->la_value.bv_val, ctx );
430 ava->la_flags |= LDAP_AVA_FREE_VALUE;
433 if( do_sort ) AVA_Sort( rdn, iAVA );
439 * In-place, schema-aware normalization / "pretty"ing of the
440 * structural representation of a distinguished name.
443 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
450 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
451 LDAPRDN rdn = dn[ iRDN ];
456 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
457 LDAPAVA *ava = rdn[ iAVA ];
458 AttributeDescription *ad;
459 slap_syntax_validate_func *validf = NULL;
460 slap_mr_normalize_func *normf = NULL;
461 slap_syntax_transform_func *transf = NULL;
462 MatchingRule *mr = NULL;
463 struct berval bv = BER_BVNULL;
468 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
469 const char *text = NULL;
471 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
472 if ( rc != LDAP_SUCCESS ) {
473 return LDAP_INVALID_SYNTAX;
476 ava->la_private = ( void * )ad;
481 * Replace attr oid/name with the canonical name
483 ava->la_attr = ad->ad_cname;
485 if( ava->la_flags & LDAP_AVA_BINARY ) {
486 if( ava->la_value.bv_len == 0 ) {
487 /* BER encoding is empty */
488 return LDAP_INVALID_SYNTAX;
491 /* AVA is binary encoded, don't muck with it */
492 } else if( flags & SLAP_LDAPDN_PRETTY ) {
493 transf = ad->ad_type->sat_syntax->ssyn_pretty;
495 validf = ad->ad_type->sat_syntax->ssyn_validate;
497 } else { /* normalization */
498 validf = ad->ad_type->sat_syntax->ssyn_validate;
499 mr = ad->ad_type->sat_equality;
500 if( mr ) normf = mr->smr_normalize;
504 /* validate value before normalization */
505 rc = ( *validf )( ad->ad_type->sat_syntax,
508 : (struct berval *) &slap_empty_bv );
510 if ( rc != LDAP_SUCCESS ) {
511 return LDAP_INVALID_SYNTAX;
517 * transform value by pretty function
518 * if value is empty, use empty_bv
520 rc = ( *transf )( ad->ad_type->sat_syntax,
523 : (struct berval *) &slap_empty_bv,
526 if ( rc != LDAP_SUCCESS ) {
527 return LDAP_INVALID_SYNTAX;
534 * if value is empty, use empty_bv
537 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
538 ad->ad_type->sat_syntax,
542 : (struct berval *) &slap_empty_bv,
545 if ( rc != LDAP_SUCCESS ) {
546 return LDAP_INVALID_SYNTAX;
552 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
553 ber_memfree_x( ava->la_value.bv_val, ctx );
555 ava->la_flags |= LDAP_AVA_FREE_VALUE;
558 if( do_sort ) AVA_Sort( rdn, iAVA );
577 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
579 if ( val->bv_len != 0 ) {
584 * Go to structural representation
586 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
587 if ( rc != LDAP_SUCCESS ) {
588 return LDAP_INVALID_SYNTAX;
591 assert( strlen( val->bv_val ) == val->bv_len );
594 * Schema-aware rewrite
596 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
597 ldap_dnfree_x( dn, ctx );
598 return LDAP_INVALID_SYNTAX;
602 * Back to string representation
604 rc = ldap_dn2bv_x( dn, out,
605 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
607 ldap_dnfree_x( dn, ctx );
609 if ( rc != LDAP_SUCCESS ) {
610 return LDAP_INVALID_SYNTAX;
613 ber_dupbv_x( out, val, ctx );
616 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
633 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
634 if ( val->bv_len != 0 ) {
640 * Go to structural representation
642 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
643 LDAP_DN_FORMAT_LDAP, ctx);
645 if ( rc != LDAP_SUCCESS ) {
646 return LDAP_INVALID_SYNTAX;
649 assert( strlen( val->bv_val ) == val->bv_len );
652 * Schema-aware rewrite
654 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
655 ldap_rdnfree_x( rdn, ctx );
656 return LDAP_INVALID_SYNTAX;
660 * Back to string representation
662 rc = ldap_rdn2bv_x( rdn, out,
663 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
665 ldap_rdnfree_x( rdn, ctx );
667 if ( rc != LDAP_SUCCESS ) {
668 return LDAP_INVALID_SYNTAX;
671 ber_dupbv_x( out, val, ctx );
674 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
690 LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
692 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
695 if ( val->bv_len == 0 ) {
696 ber_dupbv_x( out, val, ctx );
698 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
699 return LDAP_INVALID_SYNTAX;
705 /* FIXME: should be liberal in what we accept */
706 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
707 if ( rc != LDAP_SUCCESS ) {
708 return LDAP_INVALID_SYNTAX;
711 assert( strlen( val->bv_val ) == val->bv_len );
714 * Schema-aware rewrite
716 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
717 ldap_dnfree_x( dn, ctx );
718 return LDAP_INVALID_SYNTAX;
721 /* FIXME: not sure why the default isn't pretty */
722 /* RE: the default is the form that is used as
723 * an internal representation; the pretty form
725 rc = ldap_dn2bv_x( dn, out,
726 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
728 ldap_dnfree_x( dn, ctx );
730 if ( rc != LDAP_SUCCESS ) {
731 return LDAP_INVALID_SYNTAX;
736 LDAP_LOG( OPERATION, ARGS, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
738 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
755 LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
757 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
760 if ( val->bv_len == 0 ) {
761 ber_dupbv_x( out, val, ctx );
763 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
764 return LDAP_INVALID_SYNTAX;
771 /* FIXME: should be liberal in what we accept */
772 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
773 LDAP_DN_FORMAT_LDAP, ctx);
774 if ( rc != LDAP_SUCCESS ) {
775 return LDAP_INVALID_SYNTAX;
778 assert( strlen( val->bv_val ) == val->bv_len );
781 * Schema-aware rewrite
783 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
784 ldap_rdnfree_x( rdn, ctx );
785 return LDAP_INVALID_SYNTAX;
788 /* FIXME: not sure why the default isn't pretty */
789 /* RE: the default is the form that is used as
790 * an internal representation; the pretty form
792 rc = ldap_rdn2bv_x( rdn, out,
793 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
795 ldap_rdnfree_x( rdn, ctx );
797 if ( rc != LDAP_SUCCESS ) {
798 return LDAP_INVALID_SYNTAX;
803 LDAP_LOG( OPERATION, ARGS, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
805 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
824 LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n",
825 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
828 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
829 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
833 if ( val->bv_len == 0 ) {
836 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
837 return LDAP_INVALID_SYNTAX;
842 /* FIXME: should be liberal in what we accept */
843 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
844 if ( rc != LDAP_SUCCESS ) {
845 return LDAP_INVALID_SYNTAX;
848 assert( strlen( val->bv_val ) == val->bv_len );
851 * Schema-aware rewrite
853 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
854 ldap_dnfree_x( *dn, ctx );
856 return LDAP_INVALID_SYNTAX;
860 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
861 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
868 * Combination of both dnPretty and dnNormalize
874 struct berval *pretty,
875 struct berval *normal,
879 LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
881 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
888 if ( val->bv_len == 0 ) {
889 ber_dupbv_x( pretty, val, ctx );
890 ber_dupbv_x( normal, val, ctx );
892 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
894 return LDAP_INVALID_SYNTAX;
900 pretty->bv_val = NULL;
901 normal->bv_val = NULL;
905 /* FIXME: should be liberal in what we accept */
906 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
907 if ( rc != LDAP_SUCCESS ) {
908 return LDAP_INVALID_SYNTAX;
911 assert( strlen( val->bv_val ) == val->bv_len );
914 * Schema-aware rewrite
916 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
917 ldap_dnfree_x( dn, ctx );
918 return LDAP_INVALID_SYNTAX;
921 rc = ldap_dn2bv_x( dn, pretty,
922 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
924 if ( rc != LDAP_SUCCESS ) {
925 ldap_dnfree_x( dn, ctx );
926 return LDAP_INVALID_SYNTAX;
929 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
930 ldap_dnfree_x( dn, ctx );
931 ber_memfree_x( pretty->bv_val, ctx );
932 pretty->bv_val = NULL;
934 return LDAP_INVALID_SYNTAX;
937 rc = ldap_dn2bv_x( dn, normal,
938 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
940 ldap_dnfree_x( dn, ctx );
941 if ( rc != LDAP_SUCCESS ) {
942 ber_memfree_x( pretty->bv_val, ctx );
943 pretty->bv_val = NULL;
945 return LDAP_INVALID_SYNTAX;
950 LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
951 pretty->bv_val, normal->bv_val, 0 );
953 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
954 pretty->bv_val, normal->bv_val, 0 );
969 struct berval *value,
970 void *assertedValue )
973 struct berval *asserted = (struct berval *) assertedValue;
977 assert( assertedValue );
978 assert( !BER_BVISNULL( value ) );
979 assert( !BER_BVISNULL( asserted ) );
981 match = value->bv_len - asserted->bv_len;
984 match = memcmp( value->bv_val, asserted->bv_val,
989 LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n %s\n %s\n",
990 match, value->bv_val, asserted->bv_val );
992 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
993 match, value->bv_val, asserted->bv_val );
997 return( LDAP_SUCCESS );
1006 struct berval *value,
1007 void *assertedValue )
1010 struct berval *asserted = (struct berval *) assertedValue;
1014 assert( assertedValue );
1016 match = value->bv_len - asserted->bv_len;
1019 match = memcmp( value->bv_val, asserted->bv_val,
1024 LDAP_LOG( CONFIG, ENTRY, "rdnMatch: %d\n %s\n %s\n",
1025 match, value->bv_val, asserted->bv_val );
1027 Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
1028 match, value->bv_val, asserted->bv_val );
1033 return( LDAP_SUCCESS );
1038 * dnParent - dn's parent, in-place
1039 * note: the incoming dn is assumed to be normalized/prettyfied,
1040 * so that escaped rdn/ava separators are in '\'+hexpair form
1045 struct berval *pdn )
1049 p = strchr( dn->bv_val, ',' );
1054 pdn->bv_val = dn->bv_val + dn->bv_len;
1058 assert( DN_SEPARATOR( p[ 0 ] ) );
1061 assert( ATTR_LEADCHAR( p[ 0 ] ) );
1063 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
1081 if( dn->bv_len == 0 ) {
1085 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1086 if ( rc != LDAP_SUCCESS ) {
1090 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
1092 ldap_rdnfree_x( tmpRDN, ctx );
1093 if ( rc != LDAP_SUCCESS ) {
1097 return LDAP_SUCCESS;
1101 * We can assume the input is a prettied or normalized DN
1106 struct berval *dn_in )
1112 if ( dn_in == NULL ) {
1116 if ( !dn_in->bv_len ) {
1120 if ( be != NULL && be_issuffix( be, dn_in ) ) {
1124 p = strchr( dn_in->bv_val, ',' );
1126 return p ? p - dn_in->bv_val : dn_in->bv_len;
1132 * LDAP_SUCCESS if rdn is a legal rdn;
1133 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1136 rdn_validate( struct berval *rdn )
1140 * input is a pretty or normalized DN
1141 * hence, we can just search for ','
1143 if( rdn == NULL || rdn->bv_len == 0 ||
1144 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1146 return LDAP_INVALID_SYNTAX;
1148 return strchr( rdn->bv_val, ',' ) == NULL
1149 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1152 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
1159 if ( rdn == NULL || rdn == '\0' ) {
1166 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1167 if ( rc != LDAP_SUCCESS ) {
1174 if ( p[ 0 ] != '\0' ) {
1179 * Schema-aware validate
1181 if ( rc == LDAP_SUCCESS ) {
1182 rc = LDAPDN_validate( DN );
1184 ldap_rdnfree( RDN );
1187 * Must validate (there's a repeated parsing ...)
1189 return ( rc == LDAP_SUCCESS );
1196 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1199 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1203 build_new_dn( struct berval * new_dn,
1204 struct berval * parent_dn,
1205 struct berval * newrdn,
1210 if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1211 ber_dupbv( new_dn, newrdn );
1215 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1216 new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1218 ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
1220 strcpy( ptr, parent_dn->bv_val );
1225 * dnIsSuffix - tells whether suffix is a suffix of dn.
1226 * Both dn and suffix must be normalized.
1230 const struct berval *dn,
1231 const struct berval *suffix )
1233 int d = dn->bv_len - suffix->bv_len;
1238 /* empty suffix matches any dn */
1239 if ( suffix->bv_len == 0 ) {
1243 /* suffix longer than dn */
1248 /* no rdn separator or escaped rdn separator */
1249 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1253 /* no possible match or malformed dn */
1259 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1263 dnIsOneLevelRDN( struct berval *rdn )
1265 ber_len_t len = rdn->bv_len;
1267 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1277 * Convert an X.509 DN into a normalized LDAP DN
1280 dnX509normalize( void *x509_name, struct berval *out )
1282 /* Invoke the LDAP library's converter with our schema-rewriter */
1283 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1285 Debug( LDAP_DEBUG_TRACE,
1286 "dnX509Normalize: <%s>\n", out->bv_val, 0, 0 );
1292 * Get the TLS session's peer's DN into a normalized LDAP DN
1295 dnX509peerNormalize( void *ssl, struct berval *dn )
1298 return ldap_pvt_tls_get_peer_dn( ssl, dn,
1299 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );