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 );
267 /* Duplicates are not allowed */
269 return LDAP_INVALID_DN_SYNTAX;
274 rdn[ j+1 ] = rdn[ j ];
282 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
285 int rc, iAVA, do_sort = 0;
287 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
288 LDAPAVA *ava = rdn[ iAVA ];
289 AttributeDescription *ad;
290 slap_syntax_validate_func *validf = NULL;
291 slap_mr_normalize_func *normf = NULL;
292 slap_syntax_transform_func *transf = NULL;
293 MatchingRule *mr = NULL;
294 struct berval bv = BER_BVNULL;
296 assert( ava != NULL );
298 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
299 const char *text = NULL;
301 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
302 if ( rc != LDAP_SUCCESS ) {
303 rc = slap_bv2undef_ad( &ava->la_attr,
305 SLAP_AD_PROXIED|slap_DN_strict );
306 if ( rc != LDAP_SUCCESS ) {
307 return LDAP_INVALID_SYNTAX;
311 ava->la_private = ( void * )ad;
316 * Replace attr oid/name with the canonical name
318 ava->la_attr = ad->ad_cname;
320 if( ava->la_flags & LDAP_AVA_BINARY ) {
321 if( ava->la_value.bv_len == 0 ) {
322 /* BER encoding is empty */
323 return LDAP_INVALID_SYNTAX;
326 /* Do not allow X-ORDERED 'VALUES' naming attributes */
327 } else if( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
328 return LDAP_INVALID_SYNTAX;
330 /* AVA is binary encoded, don't muck with it */
331 } else if( flags & SLAP_LDAPDN_PRETTY ) {
332 transf = ad->ad_type->sat_syntax->ssyn_pretty;
334 validf = ad->ad_type->sat_syntax->ssyn_validate;
336 } else { /* normalization */
337 validf = ad->ad_type->sat_syntax->ssyn_validate;
338 mr = ad->ad_type->sat_equality;
339 if( mr && (!( mr->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))) {
340 normf = mr->smr_normalize;
345 /* validate value before normalization */
346 rc = ( *validf )( ad->ad_type->sat_syntax,
349 : (struct berval *) &slap_empty_bv );
351 if ( rc != LDAP_SUCCESS ) {
352 return LDAP_INVALID_SYNTAX;
358 * transform value by pretty function
359 * if value is empty, use empty_bv
361 rc = ( *transf )( ad->ad_type->sat_syntax,
364 : (struct berval *) &slap_empty_bv,
367 if ( rc != LDAP_SUCCESS ) {
368 return LDAP_INVALID_SYNTAX;
375 * if value is empty, use empty_bv
378 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
379 ad->ad_type->sat_syntax,
383 : (struct berval *) &slap_empty_bv,
386 if ( rc != LDAP_SUCCESS ) {
387 return LDAP_INVALID_SYNTAX;
393 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
394 ber_memfree_x( ava->la_value.bv_val, ctx );
396 ava->la_flags |= LDAP_AVA_FREE_VALUE;
402 rc = AVA_Sort( rdn, iAVA );
409 * In-place, schema-aware normalization / "pretty"ing of the
410 * structural representation of a distinguished name.
413 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
418 assert( dn != NULL );
420 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
421 rc = LDAPRDN_rewrite( dn[ iRDN ], flags, ctx );
422 if ( rc != LDAP_SUCCESS ) {
439 assert( val != NULL );
440 assert( out != NULL );
442 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
444 if ( val->bv_len != 0 ) {
449 * Go to structural representation
451 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
452 if ( rc != LDAP_SUCCESS ) {
453 return LDAP_INVALID_SYNTAX;
456 assert( strlen( val->bv_val ) == val->bv_len );
459 * Schema-aware rewrite
461 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
462 ldap_dnfree_x( dn, ctx );
463 return LDAP_INVALID_SYNTAX;
467 * Back to string representation
469 rc = ldap_dn2bv_x( dn, out,
470 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
472 ldap_dnfree_x( dn, ctx );
474 if ( rc != LDAP_SUCCESS ) {
475 return LDAP_INVALID_SYNTAX;
478 ber_dupbv_x( out, val, ctx );
481 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
495 assert( val != NULL );
496 assert( out != NULL );
498 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
499 if ( val->bv_len != 0 ) {
505 * Go to structural representation
507 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
508 LDAP_DN_FORMAT_LDAP, ctx);
510 if ( rc != LDAP_SUCCESS ) {
511 return LDAP_INVALID_SYNTAX;
514 assert( strlen( val->bv_val ) == val->bv_len );
517 * Schema-aware rewrite
519 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
520 ldap_rdnfree_x( rdn, ctx );
521 return LDAP_INVALID_SYNTAX;
525 * Back to string representation
527 rc = ldap_rdn2bv_x( rdn, out,
528 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
530 ldap_rdnfree_x( rdn, ctx );
532 if ( rc != LDAP_SUCCESS ) {
533 return LDAP_INVALID_SYNTAX;
536 ber_dupbv_x( out, val, ctx );
539 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
551 assert( val != NULL );
552 assert( out != NULL );
554 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
556 if ( val->bv_len == 0 ) {
557 ber_dupbv_x( out, val, ctx );
559 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
560 return LDAP_INVALID_SYNTAX;
566 /* FIXME: should be liberal in what we accept */
567 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
568 if ( rc != LDAP_SUCCESS ) {
569 return LDAP_INVALID_SYNTAX;
572 assert( strlen( val->bv_val ) == val->bv_len );
575 * Schema-aware rewrite
577 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
578 ldap_dnfree_x( dn, ctx );
579 return LDAP_INVALID_SYNTAX;
582 /* FIXME: not sure why the default isn't pretty */
583 /* RE: the default is the form that is used as
584 * an internal representation; the pretty form
586 rc = ldap_dn2bv_x( dn, out,
587 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
589 ldap_dnfree_x( dn, ctx );
591 if ( rc != LDAP_SUCCESS ) {
592 return LDAP_INVALID_SYNTAX;
596 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
608 assert( val != NULL );
609 assert( out != NULL );
611 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
613 if ( val->bv_len == 0 ) {
614 ber_dupbv_x( out, val, ctx );
616 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
617 return LDAP_INVALID_SYNTAX;
624 /* FIXME: should be liberal in what we accept */
625 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
626 LDAP_DN_FORMAT_LDAP, ctx);
627 if ( rc != LDAP_SUCCESS ) {
628 return LDAP_INVALID_SYNTAX;
631 assert( strlen( val->bv_val ) == val->bv_len );
634 * Schema-aware rewrite
636 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
637 ldap_rdnfree_x( rdn, ctx );
638 return LDAP_INVALID_SYNTAX;
641 /* FIXME: not sure why the default isn't pretty */
642 /* RE: the default is the form that is used as
643 * an internal representation; the pretty form
645 rc = ldap_rdn2bv_x( rdn, out,
646 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
648 ldap_rdnfree_x( rdn, ctx );
650 if ( rc != LDAP_SUCCESS ) {
651 return LDAP_INVALID_SYNTAX;
655 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
669 assert( val != NULL );
670 assert( dn != NULL );
672 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
673 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
676 if ( val->bv_len == 0 ) {
679 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
680 return LDAP_INVALID_SYNTAX;
685 /* FIXME: should be liberal in what we accept */
686 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
687 if ( rc != LDAP_SUCCESS ) {
688 return LDAP_INVALID_SYNTAX;
691 assert( strlen( val->bv_val ) == val->bv_len );
694 * Schema-aware rewrite
696 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
697 ldap_dnfree_x( *dn, ctx );
699 return LDAP_INVALID_SYNTAX;
703 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
704 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
711 * Combination of both dnPretty and dnNormalize
717 struct berval *pretty,
718 struct berval *normal,
721 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
723 assert( val != NULL );
724 assert( pretty != NULL );
725 assert( normal != NULL );
727 if ( val->bv_len == 0 ) {
728 ber_dupbv_x( pretty, val, ctx );
729 ber_dupbv_x( normal, val, ctx );
731 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
733 return LDAP_INVALID_SYNTAX;
739 pretty->bv_val = NULL;
740 normal->bv_val = NULL;
744 /* FIXME: should be liberal in what we accept */
745 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
746 if ( rc != LDAP_SUCCESS ) {
747 return LDAP_INVALID_SYNTAX;
750 assert( strlen( val->bv_val ) == val->bv_len );
753 * Schema-aware rewrite
755 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
756 ldap_dnfree_x( dn, ctx );
757 return LDAP_INVALID_SYNTAX;
760 rc = ldap_dn2bv_x( dn, pretty,
761 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
763 if ( rc != LDAP_SUCCESS ) {
764 ldap_dnfree_x( dn, ctx );
765 return LDAP_INVALID_SYNTAX;
768 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
769 ldap_dnfree_x( dn, ctx );
770 ber_memfree_x( pretty->bv_val, ctx );
771 pretty->bv_val = NULL;
773 return LDAP_INVALID_SYNTAX;
776 rc = ldap_dn2bv_x( dn, normal,
777 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
779 ldap_dnfree_x( dn, ctx );
780 if ( rc != LDAP_SUCCESS ) {
781 ber_memfree_x( pretty->bv_val, ctx );
782 pretty->bv_val = NULL;
784 return LDAP_INVALID_SYNTAX;
788 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
789 pretty->bv_val, normal->bv_val, 0 );
803 struct berval *value,
804 void *assertedValue )
807 struct berval *asserted = (struct berval *) assertedValue;
809 assert( matchp != NULL );
810 assert( value != NULL );
811 assert( assertedValue != NULL );
812 assert( !BER_BVISNULL( value ) );
813 assert( !BER_BVISNULL( asserted ) );
815 match = value->bv_len - asserted->bv_len;
818 match = memcmp( value->bv_val, asserted->bv_val,
822 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
823 match, value->bv_val, asserted->bv_val );
830 * dnRelativeMatch routine
838 struct berval *value,
839 void *assertedValue )
842 struct berval *asserted = (struct berval *) assertedValue;
844 assert( matchp != NULL );
845 assert( value != NULL );
846 assert( assertedValue != NULL );
847 assert( !BER_BVISNULL( value ) );
848 assert( !BER_BVISNULL( asserted ) );
850 if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
851 if( asserted->bv_len > value->bv_len ) {
853 } else if ( asserted->bv_len == value->bv_len ) {
854 match = memcmp( value->bv_val, asserted->bv_val,
858 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
861 &value->bv_val[value->bv_len - asserted->bv_len],
873 if( mr == slap_schema.si_mr_dnSuperiorMatch ) {
875 value = (struct berval *) assertedValue;
876 mr = slap_schema.si_mr_dnSubordinateMatch;
879 if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
880 if( asserted->bv_len >= value->bv_len ) {
884 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
887 &value->bv_val[value->bv_len - asserted->bv_len],
899 if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
900 if( asserted->bv_len >= value->bv_len ) {
904 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
907 &value->bv_val[value->bv_len - asserted->bv_len],
913 rdn.bv_val = value->bv_val;
914 rdn.bv_len = value->bv_len - asserted->bv_len - 1;
915 match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
926 /* should not be reachable */
937 struct berval *value,
938 void *assertedValue )
941 struct berval *asserted = (struct berval *) assertedValue;
943 assert( matchp != NULL );
944 assert( value != NULL );
945 assert( assertedValue != NULL );
947 match = value->bv_len - asserted->bv_len;
950 match = memcmp( value->bv_val, asserted->bv_val,
954 Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
955 match, value->bv_val, asserted->bv_val );
963 * dnParent - dn's parent, in-place
964 * note: the incoming dn is assumed to be normalized/prettyfied,
965 * so that escaped rdn/ava separators are in '\'+hexpair form
967 * note: "dn" and "pdn" can point to the same berval;
968 * beware that, in this case, the pointer to the original buffer
978 p = ber_bvchr( dn, ',' );
983 pdn->bv_val = dn->bv_val + dn->bv_len;
987 assert( DN_SEPARATOR( p[ 0 ] ) );
990 assert( ATTR_LEADCHAR( p[ 0 ] ) );
991 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
998 * dnRdn - dn's rdn, in-place
999 * note: the incoming dn is assumed to be normalized/prettyfied,
1000 * so that escaped rdn/ava separators are in '\'+hexpair form
1005 struct berval *rdn )
1010 p = ber_bvchr( dn, ',' );
1017 assert( DN_SEPARATOR( p[ 0 ] ) );
1018 assert( ATTR_LEADCHAR( p[ 1 ] ) );
1019 rdn->bv_len = p - dn->bv_val;
1034 assert( dn != NULL );
1035 assert( rdn != NULL );
1037 if( dn->bv_len == 0 ) {
1041 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1042 if ( rc != LDAP_SUCCESS ) {
1046 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1049 ldap_rdnfree_x( tmpRDN, ctx );
1054 * We can assume the input is a prettied or normalized DN
1059 struct berval *dn_in )
1063 assert( dn_in != NULL );
1065 if ( dn_in == NULL ) {
1069 if ( !dn_in->bv_len ) {
1073 if ( be != NULL && be_issuffix( be, dn_in ) ) {
1077 p = ber_bvchr( dn_in, ',' );
1079 return p ? p - dn_in->bv_val : dn_in->bv_len;
1085 * LDAP_SUCCESS if rdn is a legal rdn;
1086 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1089 rdn_validate( struct berval *rdn )
1093 * input is a pretty or normalized DN
1094 * hence, we can just search for ','
1096 if( rdn == NULL || rdn->bv_len == 0 ||
1097 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1099 return LDAP_INVALID_SYNTAX;
1101 return ber_bvchr( rdn, ',' ) == NULL
1102 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1105 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
1112 if ( rdn == NULL || rdn == '\0' ) {
1119 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1120 if ( rc != LDAP_SUCCESS ) {
1127 if ( p[ 0 ] != '\0' ) {
1132 * Schema-aware validate
1134 if ( rc == LDAP_SUCCESS ) {
1135 rc = LDAPDN_validate( DN );
1137 ldap_rdnfree( RDN );
1140 * Must validate (there's a repeated parsing ...)
1142 return ( rc == LDAP_SUCCESS );
1149 * Used by back-bdb back_modrdn to create the new dn of entries being
1152 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1156 build_new_dn( struct berval * new_dn,
1157 struct berval * parent_dn,
1158 struct berval * newrdn,
1163 if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1164 ber_dupbv_x( new_dn, newrdn, memctx );
1168 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1169 new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1171 ptr = lutil_strncopy( new_dn->bv_val, newrdn->bv_val, newrdn->bv_len );
1173 strcpy( ptr, parent_dn->bv_val );
1178 * dnIsSuffix - tells whether suffix is a suffix of dn.
1179 * Both dn and suffix must be normalized.
1183 const struct berval *dn,
1184 const struct berval *suffix )
1186 int d = dn->bv_len - suffix->bv_len;
1188 assert( dn != NULL );
1189 assert( suffix != NULL );
1191 /* empty suffix matches any dn */
1192 if ( suffix->bv_len == 0 ) {
1196 /* suffix longer than dn */
1201 /* no rdn separator or escaped rdn separator */
1202 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1206 /* no possible match or malformed dn */
1212 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1216 dnIsOneLevelRDN( struct berval *rdn )
1218 ber_len_t len = rdn->bv_len;
1220 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1229 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1232 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1235 if ( DNX509PeerNormalizeCertMap == NULL ) {
1236 DNX509PeerNormalizeCertMap = fn;
1245 * Convert an X.509 DN into a normalized LDAP DN
1248 dnX509normalize( void *x509_name, struct berval *out )
1250 /* Invoke the LDAP library's converter with our schema-rewriter */
1251 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1253 Debug( LDAP_DEBUG_TRACE,
1254 "dnX509Normalize: <%s> (%d)\n",
1255 BER_BVISNULL( out ) ? "(null)" : out->bv_val, rc, 0 );
1262 * Get the TLS session's peer's DN into a normalized LDAP DN
1265 dnX509peerNormalize( void *ssl, struct berval *dn )
1267 int rc = LDAP_INVALID_CREDENTIALS;
1269 if ( DNX509PeerNormalizeCertMap != NULL )
1270 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1272 if ( rc != LDAP_SUCCESS ) {
1273 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1274 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );