1 /* dn.c - routines for dealing with distinguished names */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2013 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 /* AVA is binary encoded, not supported */
306 return LDAP_INVALID_SYNTAX;
308 /* Do not allow X-ORDERED 'VALUES' naming attributes */
309 } else if( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
310 return LDAP_INVALID_SYNTAX;
312 } else if( flags & SLAP_LDAPDN_PRETTY ) {
313 transf = ad->ad_type->sat_syntax->ssyn_pretty;
315 validf = ad->ad_type->sat_syntax->ssyn_validate;
317 } else { /* normalization */
318 validf = ad->ad_type->sat_syntax->ssyn_validate;
319 mr = ad->ad_type->sat_equality;
320 if( mr && (!( mr->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))) {
321 normf = mr->smr_normalize;
326 /* validate value before normalization */
327 rc = ( *validf )( ad->ad_type->sat_syntax,
330 : (struct berval *) &slap_empty_bv );
332 if ( rc != LDAP_SUCCESS ) {
333 return LDAP_INVALID_SYNTAX;
339 * transform value by pretty function
340 * if value is empty, use empty_bv
342 rc = ( *transf )( ad->ad_type->sat_syntax,
345 : (struct berval *) &slap_empty_bv,
348 if ( rc != LDAP_SUCCESS ) {
349 return LDAP_INVALID_SYNTAX;
356 * if value is empty, use empty_bv
359 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
360 ad->ad_type->sat_syntax,
364 : (struct berval *) &slap_empty_bv,
367 if ( rc != LDAP_SUCCESS ) {
368 return LDAP_INVALID_SYNTAX;
374 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
375 ber_memfree_x( ava->la_value.bv_val, ctx );
377 ava->la_flags |= LDAP_AVA_FREE_VALUE;
379 /* reject empty values */
380 if (!ava->la_value.bv_len) {
381 return LDAP_INVALID_SYNTAX;
387 rc = AVA_Sort( rdn, iAVA );
394 * In-place, schema-aware normalization / "pretty"ing of the
395 * structural representation of a distinguished name.
398 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
403 assert( dn != NULL );
405 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
406 rc = LDAPRDN_rewrite( dn[ iRDN ], flags, ctx );
407 if ( rc != LDAP_SUCCESS ) {
424 assert( val != NULL );
425 assert( out != NULL );
427 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
429 if ( val->bv_len != 0 ) {
434 * Go to structural representation
436 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
437 if ( rc != LDAP_SUCCESS ) {
438 return LDAP_INVALID_SYNTAX;
441 assert( strlen( val->bv_val ) == val->bv_len );
444 * Schema-aware rewrite
446 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
447 ldap_dnfree_x( dn, ctx );
448 return LDAP_INVALID_SYNTAX;
452 * Back to string representation
454 rc = ldap_dn2bv_x( dn, out,
455 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
457 ldap_dnfree_x( dn, ctx );
459 if ( rc != LDAP_SUCCESS ) {
460 return LDAP_INVALID_SYNTAX;
463 ber_dupbv_x( out, val, ctx );
466 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
480 assert( val != NULL );
481 assert( out != NULL );
483 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
484 if ( val->bv_len != 0 ) {
490 * Go to structural representation
492 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
493 LDAP_DN_FORMAT_LDAP, ctx);
495 if ( rc != LDAP_SUCCESS ) {
496 return LDAP_INVALID_SYNTAX;
499 assert( strlen( val->bv_val ) == val->bv_len );
502 * Schema-aware rewrite
504 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
505 ldap_rdnfree_x( rdn, ctx );
506 return LDAP_INVALID_SYNTAX;
510 * Back to string representation
512 rc = ldap_rdn2bv_x( rdn, out,
513 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
515 ldap_rdnfree_x( rdn, ctx );
517 if ( rc != LDAP_SUCCESS ) {
518 return LDAP_INVALID_SYNTAX;
521 ber_dupbv_x( out, val, ctx );
524 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
536 assert( val != NULL );
537 assert( out != NULL );
539 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
541 if ( val->bv_len == 0 ) {
542 ber_dupbv_x( out, val, ctx );
544 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
545 return LDAP_INVALID_SYNTAX;
551 /* FIXME: should be liberal in what we accept */
552 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
553 if ( rc != LDAP_SUCCESS ) {
554 return LDAP_INVALID_SYNTAX;
557 assert( strlen( val->bv_val ) == val->bv_len );
560 * Schema-aware rewrite
562 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
563 ldap_dnfree_x( dn, ctx );
564 return LDAP_INVALID_SYNTAX;
567 /* FIXME: not sure why the default isn't pretty */
568 /* RE: the default is the form that is used as
569 * an internal representation; the pretty form
571 rc = ldap_dn2bv_x( dn, out,
572 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
574 ldap_dnfree_x( dn, ctx );
576 if ( rc != LDAP_SUCCESS ) {
577 return LDAP_INVALID_SYNTAX;
581 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
593 assert( val != NULL );
594 assert( out != NULL );
596 Debug( LDAP_DEBUG_TRACE, ">>> rdnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
598 if ( val->bv_len == 0 ) {
599 ber_dupbv_x( out, val, ctx );
601 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
602 return LDAP_INVALID_SYNTAX;
609 /* FIXME: should be liberal in what we accept */
610 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
611 LDAP_DN_FORMAT_LDAP, ctx);
612 if ( rc != LDAP_SUCCESS ) {
613 return LDAP_INVALID_SYNTAX;
616 assert( strlen( val->bv_val ) == val->bv_len );
619 * Schema-aware rewrite
621 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
622 ldap_rdnfree_x( rdn, ctx );
623 return LDAP_INVALID_SYNTAX;
626 /* FIXME: not sure why the default isn't pretty */
627 /* RE: the default is the form that is used as
628 * an internal representation; the pretty form
630 rc = ldap_rdn2bv_x( rdn, out,
631 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
633 ldap_rdnfree_x( rdn, ctx );
635 if ( rc != LDAP_SUCCESS ) {
636 return LDAP_INVALID_SYNTAX;
640 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
654 assert( val != NULL );
655 assert( dn != NULL );
657 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
658 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
659 val->bv_val ? val->bv_val : "", 0 );
661 if ( val->bv_len == 0 ) {
664 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
665 return LDAP_INVALID_SYNTAX;
670 /* FIXME: should be liberal in what we accept */
671 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
672 if ( rc != LDAP_SUCCESS ) {
673 return LDAP_INVALID_SYNTAX;
676 assert( strlen( val->bv_val ) == val->bv_len );
679 * Schema-aware rewrite
681 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
682 ldap_dnfree_x( *dn, ctx );
684 return LDAP_INVALID_SYNTAX;
688 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
689 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
696 * Combination of both dnPretty and dnNormalize
702 struct berval *pretty,
703 struct berval *normal,
706 assert( val != NULL );
707 assert( pretty != NULL );
708 assert( normal != NULL );
709 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
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 )
1173 assert( dn != NULL );
1174 assert( suffix != NULL );
1176 d = dn->bv_len - suffix->bv_len;
1178 /* empty suffix matches any dn */
1179 if ( suffix->bv_len == 0 ) {
1183 /* suffix longer than dn */
1188 /* no rdn separator or escaped rdn separator */
1189 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1193 /* no possible match or malformed dn */
1199 return( strncmp( dn->bv_val + d, suffix->bv_val, suffix->bv_len ) == 0 );
1203 * In place; assumes:
1204 * - ndn is normalized
1205 * - nbase is normalized
1206 * - dnIsSuffix( ndn, nbase ) == TRUE
1207 * - LDAP_SCOPE_DEFAULT == LDAP_SCOPE_SUBTREE
1210 dnIsWithinScope( struct berval *ndn, struct berval *nbase, int scope )
1212 assert( ndn != NULL );
1213 assert( nbase != NULL );
1214 assert( !BER_BVISNULL( ndn ) );
1215 assert( !BER_BVISNULL( nbase ) );
1218 case LDAP_SCOPE_DEFAULT:
1219 case LDAP_SCOPE_SUBTREE:
1222 case LDAP_SCOPE_BASE:
1223 if ( ndn->bv_len != nbase->bv_len ) {
1228 case LDAP_SCOPE_ONELEVEL: {
1230 dnParent( ndn, &pndn );
1231 if ( pndn.bv_len != nbase->bv_len ) {
1236 case LDAP_SCOPE_SUBORDINATE:
1237 if ( ndn->bv_len == nbase->bv_len ) {
1251 * In place; assumes:
1252 * - ndn is normalized
1253 * - nbase is normalized
1254 * - LDAP_SCOPE_DEFAULT == LDAP_SCOPE_SUBTREE
1257 dnIsSuffixScope( struct berval *ndn, struct berval *nbase, int scope )
1259 if ( !dnIsSuffix( ndn, nbase ) ) {
1263 return dnIsWithinScope( ndn, nbase, scope );
1267 dnIsOneLevelRDN( struct berval *rdn )
1269 ber_len_t len = rdn->bv_len;
1271 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1280 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1283 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1286 if ( DNX509PeerNormalizeCertMap == NULL ) {
1287 DNX509PeerNormalizeCertMap = fn;
1296 * Convert an X.509 DN into a normalized LDAP DN
1299 dnX509normalize( void *x509_name, struct berval *out )
1301 /* Invoke the LDAP library's converter with our schema-rewriter */
1302 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1304 Debug( LDAP_DEBUG_TRACE,
1305 "dnX509Normalize: <%s> (%d)\n",
1306 BER_BVISNULL( out ) ? "(null)" : out->bv_val, rc, 0 );
1313 * Get the TLS session's peer's DN into a normalized LDAP DN
1316 dnX509peerNormalize( void *ssl, struct berval *dn )
1318 int rc = LDAP_INVALID_CREDENTIALS;
1320 if ( DNX509PeerNormalizeCertMap != NULL )
1321 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1323 if ( rc != LDAP_SUCCESS ) {
1324 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1325 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );