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.
225 * Note: the sorting can be slightly improved by sorting first
226 * by attribute type length, then by alphabetical order.
228 * uses an insertion sort; should be fine since the number of AVAs in
229 * a RDN should be limited.
232 AVA_Sort( LDAPRDN rdn, int nAVAs )
237 assert( rdn != NULL );
239 for ( i = 1; i < nAVAs; i++ ) {
244 for ( j = i-1; j >=0; j-- ) {
248 a = strcmp( ava_i->la_attr.bv_val, ava_j->la_attr.bv_val );
250 /* RFC4512 does not allow multiple AVAs
251 * with the same attribute type in RDN (ITS#5968) */
253 return LDAP_INVALID_DN_SYNTAX;
258 rdn[ j+1 ] = rdn[ j ];
266 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
269 int rc, iAVA, do_sort = 0;
271 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
272 LDAPAVA *ava = rdn[ iAVA ];
273 AttributeDescription *ad;
274 slap_syntax_validate_func *validf = NULL;
275 slap_mr_normalize_func *normf = NULL;
276 slap_syntax_transform_func *transf = NULL;
277 MatchingRule *mr = NULL;
278 struct berval bv = BER_BVNULL;
280 assert( ava != NULL );
282 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
283 const char *text = NULL;
285 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
286 if ( rc != LDAP_SUCCESS ) {
287 rc = slap_bv2undef_ad( &ava->la_attr,
289 SLAP_AD_PROXIED|slap_DN_strict );
290 if ( rc != LDAP_SUCCESS ) {
291 return LDAP_INVALID_SYNTAX;
295 ava->la_private = ( void * )ad;
300 * Replace attr oid/name with the canonical name
302 ava->la_attr = ad->ad_cname;
304 if( ava->la_flags & LDAP_AVA_BINARY ) {
305 if( ava->la_value.bv_len == 0 ) {
306 /* BER encoding is empty */
307 return LDAP_INVALID_SYNTAX;
310 /* Do not allow X-ORDERED 'VALUES' naming attributes */
311 } else if( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
312 return LDAP_INVALID_SYNTAX;
314 /* AVA is binary encoded, don't muck with it */
315 } else if( flags & SLAP_LDAPDN_PRETTY ) {
316 transf = ad->ad_type->sat_syntax->ssyn_pretty;
318 validf = ad->ad_type->sat_syntax->ssyn_validate;
320 } else { /* normalization */
321 validf = ad->ad_type->sat_syntax->ssyn_validate;
322 mr = ad->ad_type->sat_equality;
323 if( mr && (!( mr->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))) {
324 normf = mr->smr_normalize;
329 /* validate value before normalization */
330 rc = ( *validf )( ad->ad_type->sat_syntax,
333 : (struct berval *) &slap_empty_bv );
335 if ( rc != LDAP_SUCCESS ) {
336 return LDAP_INVALID_SYNTAX;
342 * transform value by pretty function
343 * if value is empty, use empty_bv
345 rc = ( *transf )( ad->ad_type->sat_syntax,
348 : (struct berval *) &slap_empty_bv,
351 if ( rc != LDAP_SUCCESS ) {
352 return LDAP_INVALID_SYNTAX;
359 * if value is empty, use empty_bv
362 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
363 ad->ad_type->sat_syntax,
367 : (struct berval *) &slap_empty_bv,
370 if ( rc != LDAP_SUCCESS ) {
371 return LDAP_INVALID_SYNTAX;
377 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
378 ber_memfree_x( ava->la_value.bv_val, ctx );
380 ava->la_flags |= LDAP_AVA_FREE_VALUE;
386 rc = AVA_Sort( rdn, iAVA );
393 * In-place, schema-aware normalization / "pretty"ing of the
394 * structural representation of a distinguished name.
397 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
402 assert( dn != NULL );
404 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
405 rc = LDAPRDN_rewrite( dn[ iRDN ], flags, ctx );
406 if ( rc != LDAP_SUCCESS ) {
423 assert( val != NULL );
424 assert( out != NULL );
426 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
428 if ( val->bv_len != 0 ) {
433 * Go to structural representation
435 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
436 if ( rc != LDAP_SUCCESS ) {
437 return LDAP_INVALID_SYNTAX;
440 assert( strlen( val->bv_val ) == val->bv_len );
443 * Schema-aware rewrite
445 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
446 ldap_dnfree_x( dn, ctx );
447 return LDAP_INVALID_SYNTAX;
451 * Back to string representation
453 rc = ldap_dn2bv_x( dn, out,
454 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
456 ldap_dnfree_x( dn, ctx );
458 if ( rc != LDAP_SUCCESS ) {
459 return LDAP_INVALID_SYNTAX;
462 ber_dupbv_x( out, val, ctx );
465 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
479 assert( val != NULL );
480 assert( out != NULL );
482 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
483 if ( val->bv_len != 0 ) {
489 * Go to structural representation
491 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
492 LDAP_DN_FORMAT_LDAP, ctx);
494 if ( rc != LDAP_SUCCESS ) {
495 return LDAP_INVALID_SYNTAX;
498 assert( strlen( val->bv_val ) == val->bv_len );
501 * Schema-aware rewrite
503 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
504 ldap_rdnfree_x( rdn, ctx );
505 return LDAP_INVALID_SYNTAX;
509 * Back to string representation
511 rc = ldap_rdn2bv_x( rdn, out,
512 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
514 ldap_rdnfree_x( rdn, ctx );
516 if ( rc != LDAP_SUCCESS ) {
517 return LDAP_INVALID_SYNTAX;
520 ber_dupbv_x( out, val, ctx );
523 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
535 assert( val != NULL );
536 assert( out != NULL );
538 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
540 if ( val->bv_len == 0 ) {
541 ber_dupbv_x( out, val, ctx );
543 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
544 return LDAP_INVALID_SYNTAX;
550 /* FIXME: should be liberal in what we accept */
551 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
552 if ( rc != LDAP_SUCCESS ) {
553 return LDAP_INVALID_SYNTAX;
556 assert( strlen( val->bv_val ) == val->bv_len );
559 * Schema-aware rewrite
561 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
562 ldap_dnfree_x( dn, ctx );
563 return LDAP_INVALID_SYNTAX;
566 /* FIXME: not sure why the default isn't pretty */
567 /* RE: the default is the form that is used as
568 * an internal representation; the pretty form
570 rc = ldap_dn2bv_x( dn, out,
571 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
573 ldap_dnfree_x( dn, ctx );
575 if ( rc != LDAP_SUCCESS ) {
576 return LDAP_INVALID_SYNTAX;
580 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
592 assert( val != NULL );
593 assert( out != NULL );
595 Debug( LDAP_DEBUG_TRACE, ">>> rdnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
597 if ( val->bv_len == 0 ) {
598 ber_dupbv_x( out, val, ctx );
600 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
601 return LDAP_INVALID_SYNTAX;
608 /* FIXME: should be liberal in what we accept */
609 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
610 LDAP_DN_FORMAT_LDAP, ctx);
611 if ( rc != LDAP_SUCCESS ) {
612 return LDAP_INVALID_SYNTAX;
615 assert( strlen( val->bv_val ) == val->bv_len );
618 * Schema-aware rewrite
620 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
621 ldap_rdnfree_x( rdn, ctx );
622 return LDAP_INVALID_SYNTAX;
625 /* FIXME: not sure why the default isn't pretty */
626 /* RE: the default is the form that is used as
627 * an internal representation; the pretty form
629 rc = ldap_rdn2bv_x( rdn, out,
630 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
632 ldap_rdnfree_x( rdn, ctx );
634 if ( rc != LDAP_SUCCESS ) {
635 return LDAP_INVALID_SYNTAX;
639 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
653 assert( val != NULL );
654 assert( dn != NULL );
656 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
657 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
658 val->bv_val ? val->bv_val : "", 0 );
660 if ( val->bv_len == 0 ) {
663 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
664 return LDAP_INVALID_SYNTAX;
669 /* FIXME: should be liberal in what we accept */
670 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
671 if ( rc != LDAP_SUCCESS ) {
672 return LDAP_INVALID_SYNTAX;
675 assert( strlen( val->bv_val ) == val->bv_len );
678 * Schema-aware rewrite
680 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
681 ldap_dnfree_x( *dn, ctx );
683 return LDAP_INVALID_SYNTAX;
687 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
688 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
695 * Combination of both dnPretty and dnNormalize
701 struct berval *pretty,
702 struct berval *normal,
705 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
707 assert( val != NULL );
708 assert( pretty != NULL );
709 assert( normal != NULL );
711 if ( val->bv_len == 0 ) {
712 ber_dupbv_x( pretty, val, ctx );
713 ber_dupbv_x( normal, val, ctx );
715 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
717 return LDAP_INVALID_SYNTAX;
723 pretty->bv_val = NULL;
724 normal->bv_val = NULL;
728 /* FIXME: should be liberal in what we accept */
729 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
730 if ( rc != LDAP_SUCCESS ) {
731 return LDAP_INVALID_SYNTAX;
734 assert( strlen( val->bv_val ) == val->bv_len );
737 * Schema-aware rewrite
739 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
740 ldap_dnfree_x( dn, ctx );
741 return LDAP_INVALID_SYNTAX;
744 rc = ldap_dn2bv_x( dn, pretty,
745 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
747 if ( rc != LDAP_SUCCESS ) {
748 ldap_dnfree_x( dn, ctx );
749 return LDAP_INVALID_SYNTAX;
752 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
753 ldap_dnfree_x( dn, ctx );
754 ber_memfree_x( pretty->bv_val, ctx );
755 pretty->bv_val = NULL;
757 return LDAP_INVALID_SYNTAX;
760 rc = ldap_dn2bv_x( dn, normal,
761 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
763 ldap_dnfree_x( dn, ctx );
764 if ( rc != LDAP_SUCCESS ) {
765 ber_memfree_x( pretty->bv_val, ctx );
766 pretty->bv_val = NULL;
768 return LDAP_INVALID_SYNTAX;
772 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
773 pretty->bv_val ? pretty->bv_val : "",
774 normal->bv_val ? normal->bv_val : "", 0 );
788 struct berval *value,
789 void *assertedValue )
792 struct berval *asserted = (struct berval *) assertedValue;
794 assert( matchp != NULL );
795 assert( value != NULL );
796 assert( assertedValue != NULL );
797 assert( !BER_BVISNULL( value ) );
798 assert( !BER_BVISNULL( asserted ) );
800 match = value->bv_len - asserted->bv_len;
803 match = memcmp( value->bv_val, asserted->bv_val,
807 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
808 match, value->bv_val, asserted->bv_val );
815 * dnRelativeMatch routine
823 struct berval *value,
824 void *assertedValue )
827 struct berval *asserted = (struct berval *) assertedValue;
829 assert( matchp != NULL );
830 assert( value != NULL );
831 assert( assertedValue != NULL );
832 assert( !BER_BVISNULL( value ) );
833 assert( !BER_BVISNULL( asserted ) );
835 if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
836 if( asserted->bv_len > value->bv_len ) {
838 } else if ( asserted->bv_len == value->bv_len ) {
839 match = memcmp( value->bv_val, asserted->bv_val,
843 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
846 &value->bv_val[value->bv_len - asserted->bv_len],
858 if( mr == slap_schema.si_mr_dnSuperiorMatch ) {
860 value = (struct berval *) assertedValue;
861 mr = slap_schema.si_mr_dnSubordinateMatch;
864 if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
865 if( asserted->bv_len >= value->bv_len ) {
869 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
872 &value->bv_val[value->bv_len - asserted->bv_len],
884 if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
885 if( asserted->bv_len >= value->bv_len ) {
889 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
892 &value->bv_val[value->bv_len - asserted->bv_len],
898 rdn.bv_val = value->bv_val;
899 rdn.bv_len = value->bv_len - asserted->bv_len - 1;
900 match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
911 /* should not be reachable */
922 struct berval *value,
923 void *assertedValue )
926 struct berval *asserted = (struct berval *) assertedValue;
928 assert( matchp != NULL );
929 assert( value != NULL );
930 assert( assertedValue != NULL );
932 match = value->bv_len - asserted->bv_len;
935 match = memcmp( value->bv_val, asserted->bv_val,
939 Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
940 match, value->bv_val, asserted->bv_val );
948 * dnParent - dn's parent, in-place
949 * note: the incoming dn is assumed to be normalized/prettyfied,
950 * so that escaped rdn/ava separators are in '\'+hexpair form
952 * note: "dn" and "pdn" can point to the same berval;
953 * beware that, in this case, the pointer to the original buffer
963 p = ber_bvchr( dn, ',' );
967 pdn->bv_val = dn->bv_val + dn->bv_len;
972 assert( DN_SEPARATOR( p[ 0 ] ) );
975 assert( ATTR_LEADCHAR( p[ 0 ] ) );
976 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
983 * dnRdn - dn's rdn, in-place
984 * note: the incoming dn is assumed to be normalized/prettyfied,
985 * so that escaped rdn/ava separators are in '\'+hexpair form
995 p = ber_bvchr( dn, ',' );
1002 assert( DN_SEPARATOR( p[ 0 ] ) );
1003 assert( ATTR_LEADCHAR( p[ 1 ] ) );
1004 rdn->bv_len = p - dn->bv_val;
1019 assert( dn != NULL );
1020 assert( rdn != NULL );
1022 if( dn->bv_len == 0 ) {
1026 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1027 if ( rc != LDAP_SUCCESS ) {
1031 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1034 ldap_rdnfree_x( tmpRDN, ctx );
1039 * We can assume the input is a prettied or normalized DN
1044 struct berval *dn_in )
1048 assert( dn_in != NULL );
1050 if ( dn_in == NULL ) {
1054 if ( !dn_in->bv_len ) {
1058 if ( be != NULL && be_issuffix( be, dn_in ) ) {
1062 p = ber_bvchr( dn_in, ',' );
1064 return p ? (ber_len_t) (p - dn_in->bv_val) : dn_in->bv_len;
1070 * LDAP_SUCCESS if rdn is a legal rdn;
1071 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1074 rdn_validate( struct berval *rdn )
1078 * input is a pretty or normalized DN
1079 * hence, we can just search for ','
1081 if( rdn == NULL || rdn->bv_len == 0 ||
1082 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1084 return LDAP_INVALID_SYNTAX;
1086 return ber_bvchr( rdn, ',' ) == NULL
1087 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1090 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
1097 if ( rdn == NULL || rdn == '\0' ) {
1104 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1105 if ( rc != LDAP_SUCCESS ) {
1112 if ( p[ 0 ] != '\0' ) {
1117 * Schema-aware validate
1119 if ( rc == LDAP_SUCCESS ) {
1120 rc = LDAPDN_validate( DN );
1122 ldap_rdnfree( RDN );
1125 * Must validate (there's a repeated parsing ...)
1127 return ( rc == LDAP_SUCCESS );
1134 * Used by back-bdb back_modrdn to create the new dn of entries being
1137 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1141 build_new_dn( struct berval * new_dn,
1142 struct berval * parent_dn,
1143 struct berval * newrdn,
1148 if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1149 ber_dupbv_x( new_dn, newrdn, memctx );
1153 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1154 new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1156 ptr = lutil_strncopy( new_dn->bv_val, newrdn->bv_val, newrdn->bv_len );
1158 strcpy( ptr, parent_dn->bv_val );
1163 * dnIsSuffix - tells whether suffix is a suffix of dn.
1164 * Both dn and suffix must be normalized.
1168 const struct berval *dn,
1169 const struct berval *suffix )
1171 int d = dn->bv_len - suffix->bv_len;
1173 assert( dn != NULL );
1174 assert( suffix != NULL );
1176 /* empty suffix matches any dn */
1177 if ( suffix->bv_len == 0 ) {
1181 /* suffix longer than dn */
1186 /* no rdn separator or escaped rdn separator */
1187 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1191 /* no possible match or malformed dn */
1197 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1201 * In place; assumes:
1202 * - ndn is normalized
1203 * - nbase is normalized
1204 * - dnIsSuffix( ndn, nbase ) == TRUE
1205 * - LDAP_SCOPE_DEFAULT == LDAP_SCOPE_SUBTREE
1208 dnIsWithinScope( struct berval *ndn, struct berval *nbase, int scope )
1210 assert( ndn != NULL );
1211 assert( nbase != NULL );
1212 assert( !BER_BVISNULL( ndn ) );
1213 assert( !BER_BVISNULL( nbase ) );
1216 case LDAP_SCOPE_DEFAULT:
1217 case LDAP_SCOPE_SUBTREE:
1220 case LDAP_SCOPE_BASE:
1221 if ( ndn->bv_len != nbase->bv_len ) {
1226 case LDAP_SCOPE_ONELEVEL: {
1228 dnParent( ndn, &pndn );
1229 if ( pndn.bv_len != nbase->bv_len ) {
1234 case LDAP_SCOPE_SUBORDINATE:
1235 if ( ndn->bv_len == nbase->bv_len ) {
1249 * In place; assumes:
1250 * - ndn is normalized
1251 * - nbase is normalized
1252 * - LDAP_SCOPE_DEFAULT == LDAP_SCOPE_SUBTREE
1255 dnIsSuffixScope( struct berval *ndn, struct berval *nbase, int scope )
1257 if ( !dnIsSuffix( ndn, nbase ) ) {
1261 return dnIsWithinScope( ndn, nbase, scope );
1265 dnIsOneLevelRDN( struct berval *rdn )
1267 ber_len_t len = rdn->bv_len;
1269 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1278 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1281 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1284 if ( DNX509PeerNormalizeCertMap == NULL ) {
1285 DNX509PeerNormalizeCertMap = fn;
1294 * Convert an X.509 DN into a normalized LDAP DN
1297 dnX509normalize( void *x509_name, struct berval *out )
1299 /* Invoke the LDAP library's converter with our schema-rewriter */
1300 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1302 Debug( LDAP_DEBUG_TRACE,
1303 "dnX509Normalize: <%s> (%d)\n",
1304 BER_BVISNULL( out ) ? "(null)" : out->bv_val, rc, 0 );
1311 * Get the TLS session's peer's DN into a normalized LDAP DN
1314 dnX509peerNormalize( void *ssl, struct berval *dn )
1316 int rc = LDAP_INVALID_CREDENTIALS;
1318 if ( DNX509PeerNormalizeCertMap != NULL )
1319 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1321 if ( rc != LDAP_SUCCESS ) {
1322 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1323 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );