1 /* dn.c - routines for dealing with distinguished names */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 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 );
253 /* RFC4512 does not allow multiple AVAs
254 * with the same attribute type in RDN (ITS#5968) */
256 return LDAP_INVALID_DN_SYNTAX;
261 rdn[ j+1 ] = rdn[ j ];
269 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
272 int rc, iAVA, do_sort = 0;
274 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
275 LDAPAVA *ava = rdn[ iAVA ];
276 AttributeDescription *ad;
277 slap_syntax_validate_func *validf = NULL;
278 slap_mr_normalize_func *normf = NULL;
279 slap_syntax_transform_func *transf = NULL;
280 MatchingRule *mr = NULL;
281 struct berval bv = BER_BVNULL;
283 assert( ava != NULL );
285 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
286 const char *text = NULL;
288 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
289 if ( rc != LDAP_SUCCESS ) {
290 rc = slap_bv2undef_ad( &ava->la_attr,
292 SLAP_AD_PROXIED|slap_DN_strict );
293 if ( rc != LDAP_SUCCESS ) {
294 return LDAP_INVALID_SYNTAX;
298 ava->la_private = ( void * )ad;
303 * Replace attr oid/name with the canonical name
305 ava->la_attr = ad->ad_cname;
307 if( ava->la_flags & LDAP_AVA_BINARY ) {
308 if( ava->la_value.bv_len == 0 ) {
309 /* BER encoding is empty */
310 return LDAP_INVALID_SYNTAX;
313 /* Do not allow X-ORDERED 'VALUES' naming attributes */
314 } else if( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
315 return LDAP_INVALID_SYNTAX;
317 /* AVA is binary encoded, don't muck with it */
318 } else if( flags & SLAP_LDAPDN_PRETTY ) {
319 transf = ad->ad_type->sat_syntax->ssyn_pretty;
321 validf = ad->ad_type->sat_syntax->ssyn_validate;
323 } else { /* normalization */
324 validf = ad->ad_type->sat_syntax->ssyn_validate;
325 mr = ad->ad_type->sat_equality;
326 if( mr && (!( mr->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))) {
327 normf = mr->smr_normalize;
332 /* validate value before normalization */
333 rc = ( *validf )( ad->ad_type->sat_syntax,
336 : (struct berval *) &slap_empty_bv );
338 if ( rc != LDAP_SUCCESS ) {
339 return LDAP_INVALID_SYNTAX;
345 * transform value by pretty function
346 * if value is empty, use empty_bv
348 rc = ( *transf )( ad->ad_type->sat_syntax,
351 : (struct berval *) &slap_empty_bv,
354 if ( rc != LDAP_SUCCESS ) {
355 return LDAP_INVALID_SYNTAX;
362 * if value is empty, use empty_bv
365 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
366 ad->ad_type->sat_syntax,
370 : (struct berval *) &slap_empty_bv,
373 if ( rc != LDAP_SUCCESS ) {
374 return LDAP_INVALID_SYNTAX;
380 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
381 ber_memfree_x( ava->la_value.bv_val, ctx );
383 ava->la_flags |= LDAP_AVA_FREE_VALUE;
389 rc = AVA_Sort( rdn, iAVA );
396 * In-place, schema-aware normalization / "pretty"ing of the
397 * structural representation of a distinguished name.
400 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
405 assert( dn != NULL );
407 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
408 rc = LDAPRDN_rewrite( dn[ iRDN ], flags, ctx );
409 if ( rc != LDAP_SUCCESS ) {
426 assert( val != NULL );
427 assert( out != NULL );
429 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
431 if ( val->bv_len != 0 ) {
436 * Go to structural representation
438 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
439 if ( rc != LDAP_SUCCESS ) {
440 return LDAP_INVALID_SYNTAX;
443 assert( strlen( val->bv_val ) == val->bv_len );
446 * Schema-aware rewrite
448 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
449 ldap_dnfree_x( dn, ctx );
450 return LDAP_INVALID_SYNTAX;
454 * Back to string representation
456 rc = ldap_dn2bv_x( dn, out,
457 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
459 ldap_dnfree_x( dn, ctx );
461 if ( rc != LDAP_SUCCESS ) {
462 return LDAP_INVALID_SYNTAX;
465 ber_dupbv_x( out, val, ctx );
468 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
482 assert( val != NULL );
483 assert( out != NULL );
485 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
486 if ( val->bv_len != 0 ) {
492 * Go to structural representation
494 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
495 LDAP_DN_FORMAT_LDAP, ctx);
497 if ( rc != LDAP_SUCCESS ) {
498 return LDAP_INVALID_SYNTAX;
501 assert( strlen( val->bv_val ) == val->bv_len );
504 * Schema-aware rewrite
506 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
507 ldap_rdnfree_x( rdn, ctx );
508 return LDAP_INVALID_SYNTAX;
512 * Back to string representation
514 rc = ldap_rdn2bv_x( rdn, out,
515 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
517 ldap_rdnfree_x( rdn, ctx );
519 if ( rc != LDAP_SUCCESS ) {
520 return LDAP_INVALID_SYNTAX;
523 ber_dupbv_x( out, val, ctx );
526 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
538 assert( val != NULL );
539 assert( out != NULL );
541 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
543 if ( val->bv_len == 0 ) {
544 ber_dupbv_x( out, val, ctx );
546 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
547 return LDAP_INVALID_SYNTAX;
553 /* FIXME: should be liberal in what we accept */
554 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
555 if ( rc != LDAP_SUCCESS ) {
556 return LDAP_INVALID_SYNTAX;
559 assert( strlen( val->bv_val ) == val->bv_len );
562 * Schema-aware rewrite
564 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
565 ldap_dnfree_x( dn, ctx );
566 return LDAP_INVALID_SYNTAX;
569 /* FIXME: not sure why the default isn't pretty */
570 /* RE: the default is the form that is used as
571 * an internal representation; the pretty form
573 rc = ldap_dn2bv_x( dn, out,
574 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
576 ldap_dnfree_x( dn, ctx );
578 if ( rc != LDAP_SUCCESS ) {
579 return LDAP_INVALID_SYNTAX;
583 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
595 assert( val != NULL );
596 assert( out != NULL );
598 Debug( LDAP_DEBUG_TRACE, ">>> rdnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
600 if ( val->bv_len == 0 ) {
601 ber_dupbv_x( out, val, ctx );
603 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
604 return LDAP_INVALID_SYNTAX;
611 /* FIXME: should be liberal in what we accept */
612 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
613 LDAP_DN_FORMAT_LDAP, ctx);
614 if ( rc != LDAP_SUCCESS ) {
615 return LDAP_INVALID_SYNTAX;
618 assert( strlen( val->bv_val ) == val->bv_len );
621 * Schema-aware rewrite
623 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
624 ldap_rdnfree_x( rdn, ctx );
625 return LDAP_INVALID_SYNTAX;
628 /* FIXME: not sure why the default isn't pretty */
629 /* RE: the default is the form that is used as
630 * an internal representation; the pretty form
632 rc = ldap_rdn2bv_x( rdn, out,
633 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
635 ldap_rdnfree_x( rdn, ctx );
637 if ( rc != LDAP_SUCCESS ) {
638 return LDAP_INVALID_SYNTAX;
642 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
656 assert( val != NULL );
657 assert( dn != NULL );
659 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
660 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
661 val->bv_val ? val->bv_val : "", 0 );
663 if ( val->bv_len == 0 ) {
666 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
667 return LDAP_INVALID_SYNTAX;
672 /* FIXME: should be liberal in what we accept */
673 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
674 if ( rc != LDAP_SUCCESS ) {
675 return LDAP_INVALID_SYNTAX;
678 assert( strlen( val->bv_val ) == val->bv_len );
681 * Schema-aware rewrite
683 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
684 ldap_dnfree_x( *dn, ctx );
686 return LDAP_INVALID_SYNTAX;
690 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
691 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
698 * Combination of both dnPretty and dnNormalize
704 struct berval *pretty,
705 struct berval *normal,
708 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
710 assert( val != NULL );
711 assert( pretty != NULL );
712 assert( normal != NULL );
714 if ( val->bv_len == 0 ) {
715 ber_dupbv_x( pretty, val, ctx );
716 ber_dupbv_x( normal, val, ctx );
718 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
720 return LDAP_INVALID_SYNTAX;
726 pretty->bv_val = NULL;
727 normal->bv_val = NULL;
731 /* FIXME: should be liberal in what we accept */
732 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
733 if ( rc != LDAP_SUCCESS ) {
734 return LDAP_INVALID_SYNTAX;
737 assert( strlen( val->bv_val ) == val->bv_len );
740 * Schema-aware rewrite
742 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
743 ldap_dnfree_x( dn, ctx );
744 return LDAP_INVALID_SYNTAX;
747 rc = ldap_dn2bv_x( dn, pretty,
748 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
750 if ( rc != LDAP_SUCCESS ) {
751 ldap_dnfree_x( dn, ctx );
752 return LDAP_INVALID_SYNTAX;
755 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
756 ldap_dnfree_x( dn, ctx );
757 ber_memfree_x( pretty->bv_val, ctx );
758 pretty->bv_val = NULL;
760 return LDAP_INVALID_SYNTAX;
763 rc = ldap_dn2bv_x( dn, normal,
764 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
766 ldap_dnfree_x( dn, ctx );
767 if ( rc != LDAP_SUCCESS ) {
768 ber_memfree_x( pretty->bv_val, ctx );
769 pretty->bv_val = NULL;
771 return LDAP_INVALID_SYNTAX;
775 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
776 pretty->bv_val ? pretty->bv_val : "",
777 normal->bv_val ? normal->bv_val : "", 0 );
791 struct berval *value,
792 void *assertedValue )
795 struct berval *asserted = (struct berval *) assertedValue;
797 assert( matchp != NULL );
798 assert( value != NULL );
799 assert( assertedValue != NULL );
800 assert( !BER_BVISNULL( value ) );
801 assert( !BER_BVISNULL( asserted ) );
803 match = value->bv_len - asserted->bv_len;
806 match = memcmp( value->bv_val, asserted->bv_val,
810 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
811 match, value->bv_val, asserted->bv_val );
818 * dnRelativeMatch routine
826 struct berval *value,
827 void *assertedValue )
830 struct berval *asserted = (struct berval *) assertedValue;
832 assert( matchp != NULL );
833 assert( value != NULL );
834 assert( assertedValue != NULL );
835 assert( !BER_BVISNULL( value ) );
836 assert( !BER_BVISNULL( asserted ) );
838 if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
839 if( asserted->bv_len > value->bv_len ) {
841 } else if ( asserted->bv_len == value->bv_len ) {
842 match = memcmp( value->bv_val, asserted->bv_val,
846 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
849 &value->bv_val[value->bv_len - asserted->bv_len],
861 if( mr == slap_schema.si_mr_dnSuperiorMatch ) {
863 value = (struct berval *) assertedValue;
864 mr = slap_schema.si_mr_dnSubordinateMatch;
867 if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
868 if( asserted->bv_len >= value->bv_len ) {
872 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
875 &value->bv_val[value->bv_len - asserted->bv_len],
887 if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
888 if( asserted->bv_len >= value->bv_len ) {
892 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
895 &value->bv_val[value->bv_len - asserted->bv_len],
901 rdn.bv_val = value->bv_val;
902 rdn.bv_len = value->bv_len - asserted->bv_len - 1;
903 match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
914 /* should not be reachable */
925 struct berval *value,
926 void *assertedValue )
929 struct berval *asserted = (struct berval *) assertedValue;
931 assert( matchp != NULL );
932 assert( value != NULL );
933 assert( assertedValue != NULL );
935 match = value->bv_len - asserted->bv_len;
938 match = memcmp( value->bv_val, asserted->bv_val,
942 Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
943 match, value->bv_val, asserted->bv_val );
951 * dnParent - dn's parent, in-place
952 * note: the incoming dn is assumed to be normalized/prettyfied,
953 * so that escaped rdn/ava separators are in '\'+hexpair form
955 * note: "dn" and "pdn" can point to the same berval;
956 * beware that, in this case, the pointer to the original buffer
966 p = ber_bvchr( dn, ',' );
971 pdn->bv_val = dn->bv_val + dn->bv_len;
975 assert( DN_SEPARATOR( p[ 0 ] ) );
978 assert( ATTR_LEADCHAR( p[ 0 ] ) );
979 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
986 * dnRdn - dn's rdn, in-place
987 * note: the incoming dn is assumed to be normalized/prettyfied,
988 * so that escaped rdn/ava separators are in '\'+hexpair form
998 p = ber_bvchr( dn, ',' );
1005 assert( DN_SEPARATOR( p[ 0 ] ) );
1006 assert( ATTR_LEADCHAR( p[ 1 ] ) );
1007 rdn->bv_len = p - dn->bv_val;
1022 assert( dn != NULL );
1023 assert( rdn != NULL );
1025 if( dn->bv_len == 0 ) {
1029 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1030 if ( rc != LDAP_SUCCESS ) {
1034 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1037 ldap_rdnfree_x( tmpRDN, ctx );
1042 * We can assume the input is a prettied or normalized DN
1047 struct berval *dn_in )
1051 assert( dn_in != NULL );
1053 if ( dn_in == NULL ) {
1057 if ( !dn_in->bv_len ) {
1061 if ( be != NULL && be_issuffix( be, dn_in ) ) {
1065 p = ber_bvchr( dn_in, ',' );
1067 return p ? (ber_len_t) (p - dn_in->bv_val) : dn_in->bv_len;
1073 * LDAP_SUCCESS if rdn is a legal rdn;
1074 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1077 rdn_validate( struct berval *rdn )
1081 * input is a pretty or normalized DN
1082 * hence, we can just search for ','
1084 if( rdn == NULL || rdn->bv_len == 0 ||
1085 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1087 return LDAP_INVALID_SYNTAX;
1089 return ber_bvchr( rdn, ',' ) == NULL
1090 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1093 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
1100 if ( rdn == NULL || rdn == '\0' ) {
1107 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1108 if ( rc != LDAP_SUCCESS ) {
1115 if ( p[ 0 ] != '\0' ) {
1120 * Schema-aware validate
1122 if ( rc == LDAP_SUCCESS ) {
1123 rc = LDAPDN_validate( DN );
1125 ldap_rdnfree( RDN );
1128 * Must validate (there's a repeated parsing ...)
1130 return ( rc == LDAP_SUCCESS );
1137 * Used by back-bdb back_modrdn to create the new dn of entries being
1140 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1144 build_new_dn( struct berval * new_dn,
1145 struct berval * parent_dn,
1146 struct berval * newrdn,
1151 if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1152 ber_dupbv_x( new_dn, newrdn, memctx );
1156 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1157 new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1159 ptr = lutil_strncopy( new_dn->bv_val, newrdn->bv_val, newrdn->bv_len );
1161 strcpy( ptr, parent_dn->bv_val );
1166 * dnIsSuffix - tells whether suffix is a suffix of dn.
1167 * Both dn and suffix must be normalized.
1171 const struct berval *dn,
1172 const struct berval *suffix )
1174 int d = dn->bv_len - suffix->bv_len;
1176 assert( dn != NULL );
1177 assert( suffix != NULL );
1179 /* empty suffix matches any dn */
1180 if ( suffix->bv_len == 0 ) {
1184 /* suffix longer than dn */
1189 /* no rdn separator or escaped rdn separator */
1190 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1194 /* no possible match or malformed dn */
1200 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1204 dnIsOneLevelRDN( struct berval *rdn )
1206 ber_len_t len = rdn->bv_len;
1208 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1217 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1220 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1223 if ( DNX509PeerNormalizeCertMap == NULL ) {
1224 DNX509PeerNormalizeCertMap = fn;
1233 * Convert an X.509 DN into a normalized LDAP DN
1236 dnX509normalize( void *x509_name, struct berval *out )
1238 /* Invoke the LDAP library's converter with our schema-rewriter */
1239 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1241 Debug( LDAP_DEBUG_TRACE,
1242 "dnX509Normalize: <%s> (%d)\n",
1243 BER_BVISNULL( out ) ? "(null)" : out->bv_val, rc, 0 );
1250 * Get the TLS session's peer's DN into a normalized LDAP DN
1253 dnX509peerNormalize( void *ssl, struct berval *dn )
1255 int rc = LDAP_INVALID_CREDENTIALS;
1257 if ( DNX509PeerNormalizeCertMap != NULL )
1258 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1260 if ( rc != LDAP_SUCCESS ) {
1261 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1262 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );