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 a linear search; should be fine since the number of AVAs in
232 * a RDN should be limited.
235 AVA_Sort( LDAPRDN rdn, int iAVA )
238 LDAPAVA *ava_in = rdn[ iAVA ];
240 assert( rdn != NULL );
241 assert( ava_in != NULL );
243 for ( i = 0; i < iAVA; i++ ) {
244 LDAPAVA *ava = rdn[ i ];
247 assert( ava != NULL );
249 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
258 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
260 v = memcmp( ava_in->la_value.bv_val,
261 ava->la_value.bv_val,
262 d <= 0 ? ava_in->la_value.bv_len
263 : ava->la_value.bv_len );
265 if ( v == 0 && d != 0 ) {
284 a = strcmp( ava_in->la_attr.bv_val,
285 ava->la_attr.bv_val );
291 for ( j = iAVA; j > i; j-- ) {
292 rdn[ j ] = rdn[ j - 1 ];
299 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
304 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
305 LDAPAVA *ava = rdn[ iAVA ];
306 AttributeDescription *ad;
307 slap_syntax_validate_func *validf = NULL;
308 slap_mr_normalize_func *normf = NULL;
309 slap_syntax_transform_func *transf = NULL;
310 MatchingRule *mr = NULL;
311 struct berval bv = BER_BVNULL;
314 assert( ava != NULL );
316 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
317 const char *text = NULL;
319 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
320 if ( rc != LDAP_SUCCESS ) {
321 rc = slap_bv2undef_ad( &ava->la_attr,
323 SLAP_AD_PROXIED|slap_DN_strict );
324 if ( rc != LDAP_SUCCESS ) {
325 return LDAP_INVALID_SYNTAX;
329 ava->la_private = ( void * )ad;
334 * Replace attr oid/name with the canonical name
336 ava->la_attr = ad->ad_cname;
338 if( ava->la_flags & LDAP_AVA_BINARY ) {
339 if( ava->la_value.bv_len == 0 ) {
340 /* BER encoding is empty */
341 return LDAP_INVALID_SYNTAX;
344 /* Do not allow X-ORDERED 'VALUES' naming attributes */
345 } else if( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
346 return LDAP_INVALID_SYNTAX;
348 /* AVA is binary encoded, don't muck with it */
349 } else if( flags & SLAP_LDAPDN_PRETTY ) {
350 transf = ad->ad_type->sat_syntax->ssyn_pretty;
352 validf = ad->ad_type->sat_syntax->ssyn_validate;
354 } else { /* normalization */
355 validf = ad->ad_type->sat_syntax->ssyn_validate;
356 mr = ad->ad_type->sat_equality;
357 if( mr && (!( mr->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))) {
358 normf = mr->smr_normalize;
363 /* validate value before normalization */
364 rc = ( *validf )( ad->ad_type->sat_syntax,
367 : (struct berval *) &slap_empty_bv );
369 if ( rc != LDAP_SUCCESS ) {
370 return LDAP_INVALID_SYNTAX;
376 * transform value by pretty function
377 * if value is empty, use empty_bv
379 rc = ( *transf )( ad->ad_type->sat_syntax,
382 : (struct berval *) &slap_empty_bv,
385 if ( rc != LDAP_SUCCESS ) {
386 return LDAP_INVALID_SYNTAX;
393 * if value is empty, use empty_bv
396 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
397 ad->ad_type->sat_syntax,
401 : (struct berval *) &slap_empty_bv,
404 if ( rc != LDAP_SUCCESS ) {
405 return LDAP_INVALID_SYNTAX;
411 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
412 ber_memfree_x( ava->la_value.bv_val, ctx );
414 ava->la_flags |= LDAP_AVA_FREE_VALUE;
417 if( do_sort ) AVA_Sort( rdn, iAVA );
423 * In-place, schema-aware normalization / "pretty"ing of the
424 * structural representation of a distinguished name.
427 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
432 assert( dn != NULL );
434 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
435 rc = LDAPRDN_rewrite( dn[ iRDN ], flags, ctx );
436 if ( rc != LDAP_SUCCESS ) {
453 assert( val != NULL );
454 assert( out != NULL );
456 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
458 if ( val->bv_len != 0 ) {
463 * Go to structural representation
465 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
466 if ( rc != LDAP_SUCCESS ) {
467 return LDAP_INVALID_SYNTAX;
470 assert( strlen( val->bv_val ) == val->bv_len );
473 * Schema-aware rewrite
475 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
476 ldap_dnfree_x( dn, ctx );
477 return LDAP_INVALID_SYNTAX;
481 * Back to string representation
483 rc = ldap_dn2bv_x( dn, out,
484 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
486 ldap_dnfree_x( dn, ctx );
488 if ( rc != LDAP_SUCCESS ) {
489 return LDAP_INVALID_SYNTAX;
492 ber_dupbv_x( out, val, ctx );
495 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
509 assert( val != NULL );
510 assert( out != NULL );
512 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
513 if ( val->bv_len != 0 ) {
519 * Go to structural representation
521 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
522 LDAP_DN_FORMAT_LDAP, ctx);
524 if ( rc != LDAP_SUCCESS ) {
525 return LDAP_INVALID_SYNTAX;
528 assert( strlen( val->bv_val ) == val->bv_len );
531 * Schema-aware rewrite
533 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
534 ldap_rdnfree_x( rdn, ctx );
535 return LDAP_INVALID_SYNTAX;
539 * Back to string representation
541 rc = ldap_rdn2bv_x( rdn, out,
542 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
544 ldap_rdnfree_x( rdn, ctx );
546 if ( rc != LDAP_SUCCESS ) {
547 return LDAP_INVALID_SYNTAX;
550 ber_dupbv_x( out, val, ctx );
553 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
565 assert( val != NULL );
566 assert( out != NULL );
568 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
570 if ( val->bv_len == 0 ) {
571 ber_dupbv_x( out, val, ctx );
573 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
574 return LDAP_INVALID_SYNTAX;
580 /* FIXME: should be liberal in what we accept */
581 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
582 if ( rc != LDAP_SUCCESS ) {
583 return LDAP_INVALID_SYNTAX;
586 assert( strlen( val->bv_val ) == val->bv_len );
589 * Schema-aware rewrite
591 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
592 ldap_dnfree_x( dn, ctx );
593 return LDAP_INVALID_SYNTAX;
596 /* FIXME: not sure why the default isn't pretty */
597 /* RE: the default is the form that is used as
598 * an internal representation; the pretty form
600 rc = ldap_dn2bv_x( dn, out,
601 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
603 ldap_dnfree_x( dn, ctx );
605 if ( rc != LDAP_SUCCESS ) {
606 return LDAP_INVALID_SYNTAX;
610 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
622 assert( val != NULL );
623 assert( out != NULL );
625 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
627 if ( val->bv_len == 0 ) {
628 ber_dupbv_x( out, val, ctx );
630 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
631 return LDAP_INVALID_SYNTAX;
638 /* FIXME: should be liberal in what we accept */
639 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
640 LDAP_DN_FORMAT_LDAP, ctx);
641 if ( rc != LDAP_SUCCESS ) {
642 return LDAP_INVALID_SYNTAX;
645 assert( strlen( val->bv_val ) == val->bv_len );
648 * Schema-aware rewrite
650 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
651 ldap_rdnfree_x( rdn, ctx );
652 return LDAP_INVALID_SYNTAX;
655 /* FIXME: not sure why the default isn't pretty */
656 /* RE: the default is the form that is used as
657 * an internal representation; the pretty form
659 rc = ldap_rdn2bv_x( rdn, out,
660 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
662 ldap_rdnfree_x( rdn, ctx );
664 if ( rc != LDAP_SUCCESS ) {
665 return LDAP_INVALID_SYNTAX;
669 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
683 assert( val != NULL );
684 assert( dn != NULL );
686 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
687 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
690 if ( val->bv_len == 0 ) {
693 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
694 return LDAP_INVALID_SYNTAX;
699 /* FIXME: should be liberal in what we accept */
700 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
701 if ( rc != LDAP_SUCCESS ) {
702 return LDAP_INVALID_SYNTAX;
705 assert( strlen( val->bv_val ) == val->bv_len );
708 * Schema-aware rewrite
710 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
711 ldap_dnfree_x( *dn, ctx );
713 return LDAP_INVALID_SYNTAX;
717 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
718 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
725 * Combination of both dnPretty and dnNormalize
731 struct berval *pretty,
732 struct berval *normal,
735 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
737 assert( val != NULL );
738 assert( pretty != NULL );
739 assert( normal != NULL );
741 if ( val->bv_len == 0 ) {
742 ber_dupbv_x( pretty, val, ctx );
743 ber_dupbv_x( normal, val, ctx );
745 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
747 return LDAP_INVALID_SYNTAX;
753 pretty->bv_val = NULL;
754 normal->bv_val = NULL;
758 /* FIXME: should be liberal in what we accept */
759 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
760 if ( rc != LDAP_SUCCESS ) {
761 return LDAP_INVALID_SYNTAX;
764 assert( strlen( val->bv_val ) == val->bv_len );
767 * Schema-aware rewrite
769 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
770 ldap_dnfree_x( dn, ctx );
771 return LDAP_INVALID_SYNTAX;
774 rc = ldap_dn2bv_x( dn, pretty,
775 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
777 if ( rc != LDAP_SUCCESS ) {
778 ldap_dnfree_x( dn, ctx );
779 return LDAP_INVALID_SYNTAX;
782 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
783 ldap_dnfree_x( dn, ctx );
784 ber_memfree_x( pretty->bv_val, ctx );
785 pretty->bv_val = NULL;
787 return LDAP_INVALID_SYNTAX;
790 rc = ldap_dn2bv_x( dn, normal,
791 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
793 ldap_dnfree_x( dn, ctx );
794 if ( rc != LDAP_SUCCESS ) {
795 ber_memfree_x( pretty->bv_val, ctx );
796 pretty->bv_val = NULL;
798 return LDAP_INVALID_SYNTAX;
802 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
803 pretty->bv_val, normal->bv_val, 0 );
817 struct berval *value,
818 void *assertedValue )
821 struct berval *asserted = (struct berval *) assertedValue;
823 assert( matchp != NULL );
824 assert( value != NULL );
825 assert( assertedValue != NULL );
826 assert( !BER_BVISNULL( value ) );
827 assert( !BER_BVISNULL( asserted ) );
829 match = value->bv_len - asserted->bv_len;
832 match = memcmp( value->bv_val, asserted->bv_val,
836 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
837 match, value->bv_val, asserted->bv_val );
844 * dnRelativeMatch routine
852 struct berval *value,
853 void *assertedValue )
856 struct berval *asserted = (struct berval *) assertedValue;
858 assert( matchp != NULL );
859 assert( value != NULL );
860 assert( assertedValue != NULL );
861 assert( !BER_BVISNULL( value ) );
862 assert( !BER_BVISNULL( asserted ) );
864 if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
865 if( asserted->bv_len > value->bv_len ) {
867 } else if ( asserted->bv_len == value->bv_len ) {
868 match = memcmp( value->bv_val, asserted->bv_val,
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_dnSuperiorMatch ) {
889 value = (struct berval *) assertedValue;
890 mr = slap_schema.si_mr_dnSubordinateMatch;
893 if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
894 if( asserted->bv_len >= value->bv_len ) {
898 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
901 &value->bv_val[value->bv_len - asserted->bv_len],
913 if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
914 if( asserted->bv_len >= value->bv_len ) {
918 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
921 &value->bv_val[value->bv_len - asserted->bv_len],
927 rdn.bv_val = value->bv_val;
928 rdn.bv_len = value->bv_len - asserted->bv_len - 1;
929 match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
940 /* should not be reachable */
951 struct berval *value,
952 void *assertedValue )
955 struct berval *asserted = (struct berval *) assertedValue;
957 assert( matchp != NULL );
958 assert( value != NULL );
959 assert( assertedValue != NULL );
961 match = value->bv_len - asserted->bv_len;
964 match = memcmp( value->bv_val, asserted->bv_val,
968 Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
969 match, value->bv_val, asserted->bv_val );
977 * dnParent - dn's parent, in-place
978 * note: the incoming dn is assumed to be normalized/prettyfied,
979 * so that escaped rdn/ava separators are in '\'+hexpair form
981 * note: "dn" and "pdn" can point to the same berval;
982 * beware that, in this case, the pointer to the original buffer
992 p = ber_bvchr( dn, ',' );
997 pdn->bv_val = dn->bv_val + dn->bv_len;
1001 assert( DN_SEPARATOR( p[ 0 ] ) );
1004 assert( ATTR_LEADCHAR( p[ 0 ] ) );
1005 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
1012 * dnRdn - dn's rdn, in-place
1013 * note: the incoming dn is assumed to be normalized/prettyfied,
1014 * so that escaped rdn/ava separators are in '\'+hexpair form
1019 struct berval *rdn )
1024 p = ber_bvchr( dn, ',' );
1031 assert( DN_SEPARATOR( p[ 0 ] ) );
1032 assert( ATTR_LEADCHAR( p[ 1 ] ) );
1033 rdn->bv_len = p - dn->bv_val;
1048 assert( dn != NULL );
1049 assert( rdn != NULL );
1051 if( dn->bv_len == 0 ) {
1055 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1056 if ( rc != LDAP_SUCCESS ) {
1060 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1063 ldap_rdnfree_x( tmpRDN, ctx );
1068 * We can assume the input is a prettied or normalized DN
1073 struct berval *dn_in )
1077 assert( dn_in != NULL );
1079 if ( dn_in == NULL ) {
1083 if ( !dn_in->bv_len ) {
1087 if ( be != NULL && be_issuffix( be, dn_in ) ) {
1091 p = ber_bvchr( dn_in, ',' );
1093 return p ? p - dn_in->bv_val : dn_in->bv_len;
1099 * LDAP_SUCCESS if rdn is a legal rdn;
1100 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1103 rdn_validate( struct berval *rdn )
1107 * input is a pretty or normalized DN
1108 * hence, we can just search for ','
1110 if( rdn == NULL || rdn->bv_len == 0 ||
1111 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1113 return LDAP_INVALID_SYNTAX;
1115 return ber_bvchr( rdn, ',' ) == NULL
1116 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1119 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
1126 if ( rdn == NULL || rdn == '\0' ) {
1133 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1134 if ( rc != LDAP_SUCCESS ) {
1141 if ( p[ 0 ] != '\0' ) {
1146 * Schema-aware validate
1148 if ( rc == LDAP_SUCCESS ) {
1149 rc = LDAPDN_validate( DN );
1151 ldap_rdnfree( RDN );
1154 * Must validate (there's a repeated parsing ...)
1156 return ( rc == LDAP_SUCCESS );
1163 * Used by back-bdb back_modrdn to create the new dn of entries being
1166 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1170 build_new_dn( struct berval * new_dn,
1171 struct berval * parent_dn,
1172 struct berval * newrdn,
1177 if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1178 ber_dupbv_x( new_dn, newrdn, memctx );
1182 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1183 new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1185 ptr = lutil_strncopy( new_dn->bv_val, newrdn->bv_val, newrdn->bv_len );
1187 strcpy( ptr, parent_dn->bv_val );
1192 * dnIsSuffix - tells whether suffix is a suffix of dn.
1193 * Both dn and suffix must be normalized.
1197 const struct berval *dn,
1198 const struct berval *suffix )
1200 int d = dn->bv_len - suffix->bv_len;
1202 assert( dn != NULL );
1203 assert( suffix != NULL );
1205 /* empty suffix matches any dn */
1206 if ( suffix->bv_len == 0 ) {
1210 /* suffix longer than dn */
1215 /* no rdn separator or escaped rdn separator */
1216 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1220 /* no possible match or malformed dn */
1226 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1230 dnIsOneLevelRDN( struct berval *rdn )
1232 ber_len_t len = rdn->bv_len;
1234 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1243 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1246 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1249 if ( DNX509PeerNormalizeCertMap == NULL ) {
1250 DNX509PeerNormalizeCertMap = fn;
1259 * Convert an X.509 DN into a normalized LDAP DN
1262 dnX509normalize( void *x509_name, struct berval *out )
1264 /* Invoke the LDAP library's converter with our schema-rewriter */
1265 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1267 Debug( LDAP_DEBUG_TRACE,
1268 "dnX509Normalize: <%s> (%d)\n",
1269 BER_BVISNULL( out ) ? "(null)" : out->bv_val, rc, 0 );
1276 * Get the TLS session's peer's DN into a normalized LDAP DN
1279 dnX509peerNormalize( void *ssl, struct berval *dn )
1281 int rc = LDAP_INVALID_CREDENTIALS;
1283 if ( DNX509PeerNormalizeCertMap != NULL )
1284 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1286 if ( rc != LDAP_SUCCESS ) {
1287 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1288 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );