1 /* dn.c - routines for dealing with distinguished names */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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>
40 * The DN syntax-related functions take advantage of the dn representation
41 * handling functions ldap_str2dn/ldap_dn2str. The latter are not schema-
42 * aware, so the attributes and their values need be validated (and possibly
43 * normalized). In the current implementation the required validation/nor-
44 * malization/"pretty"ing are done on newly created DN structural represen-
45 * tations; however the idea is to move towards DN handling in structural
46 * representation instead of the current string representation. To this
47 * purpose, we need to do only the required operations and keep track of
48 * what has been done to minimize their impact on performances.
50 * Developers are strongly encouraged to use this feature, to speed-up
54 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
56 int slap_DN_strict = SLAP_AD_NOINSERT;
59 LDAPRDN_validate( LDAPRDN rdn )
64 assert( rdn != NULL );
66 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
67 LDAPAVA *ava = rdn[ iAVA ];
68 AttributeDescription *ad;
69 slap_syntax_validate_func *validate = NULL;
71 assert( ava != NULL );
73 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
74 const char *text = NULL;
76 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
77 if ( rc != LDAP_SUCCESS ) {
78 rc = slap_bv2undef_ad( &ava->la_attr,
80 SLAP_AD_PROXIED|slap_DN_strict );
81 if ( rc != LDAP_SUCCESS ) {
82 return LDAP_INVALID_SYNTAX;
86 ava->la_private = ( void * )ad;
90 * Do not allow X-ORDERED 'VALUES' naming attributes
92 if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
93 return LDAP_INVALID_SYNTAX;
97 * Replace attr oid/name with the canonical name
99 ava->la_attr = ad->ad_cname;
101 validate = ad->ad_type->sat_syntax->ssyn_validate;
105 * validate value by validate function
107 rc = ( *validate )( ad->ad_type->sat_syntax,
110 if ( rc != LDAP_SUCCESS ) {
111 return LDAP_INVALID_SYNTAX;
120 * In-place, schema-aware validation of the
121 * structural representation of a distinguished name.
124 LDAPDN_validate( LDAPDN dn )
129 assert( dn != NULL );
131 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
132 rc = LDAPRDN_validate( dn[ iRDN ] );
133 if ( rc != LDAP_SUCCESS ) {
142 * dn validate routine
152 assert( in != NULL );
154 if ( in->bv_len == 0 ) {
157 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
158 return LDAP_INVALID_SYNTAX;
161 rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
162 if ( rc != LDAP_SUCCESS ) {
163 return LDAP_INVALID_SYNTAX;
166 assert( strlen( in->bv_val ) == in->bv_len );
169 * Schema-aware validate
171 rc = LDAPDN_validate( dn );
174 if ( rc != LDAP_SUCCESS ) {
175 return LDAP_INVALID_SYNTAX;
190 assert( in != NULL );
191 if ( in->bv_len == 0 ) {
194 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
195 return LDAP_INVALID_SYNTAX;
198 rc = ldap_bv2rdn_x( in , &rdn, (char **) &p,
199 LDAP_DN_FORMAT_LDAP, NULL);
200 if ( rc != LDAP_SUCCESS ) {
201 return LDAP_INVALID_SYNTAX;
204 assert( strlen( in->bv_val ) == in->bv_len );
207 * Schema-aware validate
209 rc = LDAPRDN_validate( rdn );
212 if ( rc != LDAP_SUCCESS ) {
213 return LDAP_INVALID_SYNTAX;
221 * AVA sorting inside a RDN
223 * rule: sort attributeTypes in alphabetical order; in case of multiple
224 * occurrences of the same attributeType, sort values in byte order
225 * (use memcmp, which implies alphabetical order in case of IA5 value;
226 * this should guarantee the repeatability of the operation).
228 * Note: the sorting can be slightly improved by sorting first
229 * by attribute type length, then by alphabetical order.
231 * uses an insertion sort; should be fine since the number of AVAs in
232 * a RDN should be limited.
235 AVA_Sort( LDAPRDN rdn, int nAVAs )
240 assert( rdn != NULL );
242 for ( i = 1; i < nAVAs; i++ ) {
247 for ( j = i-1; j >=0; j-- ) {
251 a = strcmp( ava_i->la_attr.bv_val, ava_j->la_attr.bv_val );
256 d = ava_i->la_value.bv_len - ava_j->la_value.bv_len;
258 a = memcmp( ava_i->la_value.bv_val,
259 ava_j->la_value.bv_val,
260 d <= 0 ? ava_i->la_value.bv_len
261 : ava_j->la_value.bv_len );
263 if ( a == 0 && d != 0 ) {
270 rdn[ j+1 ] = rdn[ j ];
277 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
280 int rc, iAVA, do_sort = 0;
282 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
283 LDAPAVA *ava = rdn[ iAVA ];
284 AttributeDescription *ad;
285 slap_syntax_validate_func *validf = NULL;
286 slap_mr_normalize_func *normf = NULL;
287 slap_syntax_transform_func *transf = NULL;
288 MatchingRule *mr = NULL;
289 struct berval bv = BER_BVNULL;
291 assert( ava != NULL );
293 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
294 const char *text = NULL;
296 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
297 if ( rc != LDAP_SUCCESS ) {
298 rc = slap_bv2undef_ad( &ava->la_attr,
300 SLAP_AD_PROXIED|slap_DN_strict );
301 if ( rc != LDAP_SUCCESS ) {
302 return LDAP_INVALID_SYNTAX;
306 ava->la_private = ( void * )ad;
311 * Replace attr oid/name with the canonical name
313 ava->la_attr = ad->ad_cname;
315 if( ava->la_flags & LDAP_AVA_BINARY ) {
316 if( ava->la_value.bv_len == 0 ) {
317 /* BER encoding is empty */
318 return LDAP_INVALID_SYNTAX;
321 /* Do not allow X-ORDERED 'VALUES' naming attributes */
322 } else if( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
323 return LDAP_INVALID_SYNTAX;
325 /* AVA is binary encoded, don't muck with it */
326 } else if( flags & SLAP_LDAPDN_PRETTY ) {
327 transf = ad->ad_type->sat_syntax->ssyn_pretty;
329 validf = ad->ad_type->sat_syntax->ssyn_validate;
331 } else { /* normalization */
332 validf = ad->ad_type->sat_syntax->ssyn_validate;
333 mr = ad->ad_type->sat_equality;
334 if( mr && (!( mr->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))) {
335 normf = mr->smr_normalize;
340 /* validate value before normalization */
341 rc = ( *validf )( ad->ad_type->sat_syntax,
344 : (struct berval *) &slap_empty_bv );
346 if ( rc != LDAP_SUCCESS ) {
347 return LDAP_INVALID_SYNTAX;
353 * transform value by pretty function
354 * if value is empty, use empty_bv
356 rc = ( *transf )( ad->ad_type->sat_syntax,
359 : (struct berval *) &slap_empty_bv,
362 if ( rc != LDAP_SUCCESS ) {
363 return LDAP_INVALID_SYNTAX;
370 * if value is empty, use empty_bv
373 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
374 ad->ad_type->sat_syntax,
378 : (struct berval *) &slap_empty_bv,
381 if ( rc != LDAP_SUCCESS ) {
382 return LDAP_INVALID_SYNTAX;
388 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
389 ber_memfree_x( ava->la_value.bv_val, ctx );
391 ava->la_flags |= LDAP_AVA_FREE_VALUE;
395 AVA_Sort( rdn, iAVA );
401 * In-place, schema-aware normalization / "pretty"ing of the
402 * structural representation of a distinguished name.
405 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
410 assert( dn != NULL );
412 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
413 rc = LDAPRDN_rewrite( dn[ iRDN ], flags, ctx );
414 if ( rc != LDAP_SUCCESS ) {
431 assert( val != NULL );
432 assert( out != NULL );
434 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
436 if ( val->bv_len != 0 ) {
441 * Go to structural representation
443 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
444 if ( rc != LDAP_SUCCESS ) {
445 return LDAP_INVALID_SYNTAX;
448 assert( strlen( val->bv_val ) == val->bv_len );
451 * Schema-aware rewrite
453 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
454 ldap_dnfree_x( dn, ctx );
455 return LDAP_INVALID_SYNTAX;
459 * Back to string representation
461 rc = ldap_dn2bv_x( dn, out,
462 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
464 ldap_dnfree_x( dn, ctx );
466 if ( rc != LDAP_SUCCESS ) {
467 return LDAP_INVALID_SYNTAX;
470 ber_dupbv_x( out, val, ctx );
473 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
487 assert( val != NULL );
488 assert( out != NULL );
490 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
491 if ( val->bv_len != 0 ) {
497 * Go to structural representation
499 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
500 LDAP_DN_FORMAT_LDAP, ctx);
502 if ( rc != LDAP_SUCCESS ) {
503 return LDAP_INVALID_SYNTAX;
506 assert( strlen( val->bv_val ) == val->bv_len );
509 * Schema-aware rewrite
511 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
512 ldap_rdnfree_x( rdn, ctx );
513 return LDAP_INVALID_SYNTAX;
517 * Back to string representation
519 rc = ldap_rdn2bv_x( rdn, out,
520 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
522 ldap_rdnfree_x( rdn, ctx );
524 if ( rc != LDAP_SUCCESS ) {
525 return LDAP_INVALID_SYNTAX;
528 ber_dupbv_x( out, val, ctx );
531 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
543 assert( val != NULL );
544 assert( out != NULL );
546 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
548 if ( val->bv_len == 0 ) {
549 ber_dupbv_x( out, val, ctx );
551 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
552 return LDAP_INVALID_SYNTAX;
558 /* FIXME: should be liberal in what we accept */
559 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
560 if ( rc != LDAP_SUCCESS ) {
561 return LDAP_INVALID_SYNTAX;
564 assert( strlen( val->bv_val ) == val->bv_len );
567 * Schema-aware rewrite
569 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
570 ldap_dnfree_x( dn, ctx );
571 return LDAP_INVALID_SYNTAX;
574 /* FIXME: not sure why the default isn't pretty */
575 /* RE: the default is the form that is used as
576 * an internal representation; the pretty form
578 rc = ldap_dn2bv_x( dn, out,
579 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
581 ldap_dnfree_x( dn, ctx );
583 if ( rc != LDAP_SUCCESS ) {
584 return LDAP_INVALID_SYNTAX;
588 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
600 assert( val != NULL );
601 assert( out != NULL );
603 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
605 if ( val->bv_len == 0 ) {
606 ber_dupbv_x( out, val, ctx );
608 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
609 return LDAP_INVALID_SYNTAX;
616 /* FIXME: should be liberal in what we accept */
617 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
618 LDAP_DN_FORMAT_LDAP, ctx);
619 if ( rc != LDAP_SUCCESS ) {
620 return LDAP_INVALID_SYNTAX;
623 assert( strlen( val->bv_val ) == val->bv_len );
626 * Schema-aware rewrite
628 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
629 ldap_rdnfree_x( rdn, ctx );
630 return LDAP_INVALID_SYNTAX;
633 /* FIXME: not sure why the default isn't pretty */
634 /* RE: the default is the form that is used as
635 * an internal representation; the pretty form
637 rc = ldap_rdn2bv_x( rdn, out,
638 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
640 ldap_rdnfree_x( rdn, ctx );
642 if ( rc != LDAP_SUCCESS ) {
643 return LDAP_INVALID_SYNTAX;
647 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
661 assert( val != NULL );
662 assert( dn != NULL );
664 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
665 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
668 if ( val->bv_len == 0 ) {
671 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
672 return LDAP_INVALID_SYNTAX;
677 /* FIXME: should be liberal in what we accept */
678 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
679 if ( rc != LDAP_SUCCESS ) {
680 return LDAP_INVALID_SYNTAX;
683 assert( strlen( val->bv_val ) == val->bv_len );
686 * Schema-aware rewrite
688 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
689 ldap_dnfree_x( *dn, ctx );
691 return LDAP_INVALID_SYNTAX;
695 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
696 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
703 * Combination of both dnPretty and dnNormalize
709 struct berval *pretty,
710 struct berval *normal,
713 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
715 assert( val != NULL );
716 assert( pretty != NULL );
717 assert( normal != NULL );
719 if ( val->bv_len == 0 ) {
720 ber_dupbv_x( pretty, val, ctx );
721 ber_dupbv_x( normal, val, ctx );
723 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
725 return LDAP_INVALID_SYNTAX;
731 pretty->bv_val = NULL;
732 normal->bv_val = NULL;
736 /* FIXME: should be liberal in what we accept */
737 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
738 if ( rc != LDAP_SUCCESS ) {
739 return LDAP_INVALID_SYNTAX;
742 assert( strlen( val->bv_val ) == val->bv_len );
745 * Schema-aware rewrite
747 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
748 ldap_dnfree_x( dn, ctx );
749 return LDAP_INVALID_SYNTAX;
752 rc = ldap_dn2bv_x( dn, pretty,
753 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
755 if ( rc != LDAP_SUCCESS ) {
756 ldap_dnfree_x( dn, ctx );
757 return LDAP_INVALID_SYNTAX;
760 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
761 ldap_dnfree_x( dn, ctx );
762 ber_memfree_x( pretty->bv_val, ctx );
763 pretty->bv_val = NULL;
765 return LDAP_INVALID_SYNTAX;
768 rc = ldap_dn2bv_x( dn, normal,
769 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
771 ldap_dnfree_x( dn, ctx );
772 if ( rc != LDAP_SUCCESS ) {
773 ber_memfree_x( pretty->bv_val, ctx );
774 pretty->bv_val = NULL;
776 return LDAP_INVALID_SYNTAX;
780 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
781 pretty->bv_val, normal->bv_val, 0 );
795 struct berval *value,
796 void *assertedValue )
799 struct berval *asserted = (struct berval *) assertedValue;
801 assert( matchp != NULL );
802 assert( value != NULL );
803 assert( assertedValue != NULL );
804 assert( !BER_BVISNULL( value ) );
805 assert( !BER_BVISNULL( asserted ) );
807 match = value->bv_len - asserted->bv_len;
810 match = memcmp( value->bv_val, asserted->bv_val,
814 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
815 match, value->bv_val, asserted->bv_val );
822 * dnRelativeMatch routine
830 struct berval *value,
831 void *assertedValue )
834 struct berval *asserted = (struct berval *) assertedValue;
836 assert( matchp != NULL );
837 assert( value != NULL );
838 assert( assertedValue != NULL );
839 assert( !BER_BVISNULL( value ) );
840 assert( !BER_BVISNULL( asserted ) );
842 if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
843 if( asserted->bv_len > value->bv_len ) {
845 } else if ( asserted->bv_len == value->bv_len ) {
846 match = memcmp( value->bv_val, asserted->bv_val,
850 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
853 &value->bv_val[value->bv_len - asserted->bv_len],
865 if( mr == slap_schema.si_mr_dnSuperiorMatch ) {
867 value = (struct berval *) assertedValue;
868 mr = slap_schema.si_mr_dnSubordinateMatch;
871 if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
872 if( asserted->bv_len >= value->bv_len ) {
876 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
879 &value->bv_val[value->bv_len - asserted->bv_len],
891 if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
892 if( asserted->bv_len >= value->bv_len ) {
896 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
899 &value->bv_val[value->bv_len - asserted->bv_len],
905 rdn.bv_val = value->bv_val;
906 rdn.bv_len = value->bv_len - asserted->bv_len - 1;
907 match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
918 /* should not be reachable */
929 struct berval *value,
930 void *assertedValue )
933 struct berval *asserted = (struct berval *) assertedValue;
935 assert( matchp != NULL );
936 assert( value != NULL );
937 assert( assertedValue != NULL );
939 match = value->bv_len - asserted->bv_len;
942 match = memcmp( value->bv_val, asserted->bv_val,
946 Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
947 match, value->bv_val, asserted->bv_val );
955 * dnParent - dn's parent, in-place
956 * note: the incoming dn is assumed to be normalized/prettyfied,
957 * so that escaped rdn/ava separators are in '\'+hexpair form
959 * note: "dn" and "pdn" can point to the same berval;
960 * beware that, in this case, the pointer to the original buffer
970 p = ber_bvchr( dn, ',' );
975 pdn->bv_val = dn->bv_val + dn->bv_len;
979 assert( DN_SEPARATOR( p[ 0 ] ) );
982 assert( ATTR_LEADCHAR( p[ 0 ] ) );
983 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
990 * dnRdn - dn's rdn, in-place
991 * note: the incoming dn is assumed to be normalized/prettyfied,
992 * so that escaped rdn/ava separators are in '\'+hexpair form
1002 p = ber_bvchr( dn, ',' );
1009 assert( DN_SEPARATOR( p[ 0 ] ) );
1010 assert( ATTR_LEADCHAR( p[ 1 ] ) );
1011 rdn->bv_len = p - dn->bv_val;
1026 assert( dn != NULL );
1027 assert( rdn != NULL );
1029 if( dn->bv_len == 0 ) {
1033 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1034 if ( rc != LDAP_SUCCESS ) {
1038 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1041 ldap_rdnfree_x( tmpRDN, ctx );
1046 * We can assume the input is a prettied or normalized DN
1051 struct berval *dn_in )
1055 assert( dn_in != NULL );
1057 if ( dn_in == NULL ) {
1061 if ( !dn_in->bv_len ) {
1065 if ( be != NULL && be_issuffix( be, dn_in ) ) {
1069 p = ber_bvchr( dn_in, ',' );
1071 return p ? p - dn_in->bv_val : dn_in->bv_len;
1077 * LDAP_SUCCESS if rdn is a legal rdn;
1078 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1081 rdn_validate( struct berval *rdn )
1085 * input is a pretty or normalized DN
1086 * hence, we can just search for ','
1088 if( rdn == NULL || rdn->bv_len == 0 ||
1089 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1091 return LDAP_INVALID_SYNTAX;
1093 return ber_bvchr( rdn, ',' ) == NULL
1094 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1097 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
1104 if ( rdn == NULL || rdn == '\0' ) {
1111 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1112 if ( rc != LDAP_SUCCESS ) {
1119 if ( p[ 0 ] != '\0' ) {
1124 * Schema-aware validate
1126 if ( rc == LDAP_SUCCESS ) {
1127 rc = LDAPDN_validate( DN );
1129 ldap_rdnfree( RDN );
1132 * Must validate (there's a repeated parsing ...)
1134 return ( rc == LDAP_SUCCESS );
1141 * Used by back-bdb back_modrdn to create the new dn of entries being
1144 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1148 build_new_dn( struct berval * new_dn,
1149 struct berval * parent_dn,
1150 struct berval * newrdn,
1155 if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1156 ber_dupbv_x( new_dn, newrdn, memctx );
1160 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1161 new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1163 ptr = lutil_strncopy( new_dn->bv_val, newrdn->bv_val, newrdn->bv_len );
1165 strcpy( ptr, parent_dn->bv_val );
1170 * dnIsSuffix - tells whether suffix is a suffix of dn.
1171 * Both dn and suffix must be normalized.
1175 const struct berval *dn,
1176 const struct berval *suffix )
1178 int d = dn->bv_len - suffix->bv_len;
1180 assert( dn != NULL );
1181 assert( suffix != NULL );
1183 /* empty suffix matches any dn */
1184 if ( suffix->bv_len == 0 ) {
1188 /* suffix longer than dn */
1193 /* no rdn separator or escaped rdn separator */
1194 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1198 /* no possible match or malformed dn */
1204 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1208 dnIsOneLevelRDN( struct berval *rdn )
1210 ber_len_t len = rdn->bv_len;
1212 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1221 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1224 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1227 if ( DNX509PeerNormalizeCertMap == NULL ) {
1228 DNX509PeerNormalizeCertMap = fn;
1237 * Convert an X.509 DN into a normalized LDAP DN
1240 dnX509normalize( void *x509_name, struct berval *out )
1242 /* Invoke the LDAP library's converter with our schema-rewriter */
1243 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1245 Debug( LDAP_DEBUG_TRACE,
1246 "dnX509Normalize: <%s> (%d)\n",
1247 BER_BVISNULL( out ) ? "(null)" : out->bv_val, rc, 0 );
1254 * Get the TLS session's peer's DN into a normalized LDAP DN
1257 dnX509peerNormalize( void *ssl, struct berval *dn )
1259 int rc = LDAP_INVALID_CREDENTIALS;
1261 if ( DNX509PeerNormalizeCertMap != NULL )
1262 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1264 if ( rc != LDAP_SUCCESS ) {
1265 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1266 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );