1 /* dn.c - routines for dealing with distinguished names */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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>
37 #include "ldap_pvt.h" /* must be after slap.h, to get ldap_bv2dn_x() & co */
41 * The DN syntax-related functions take advantage of the dn representation
42 * handling functions ldap_str2dn/ldap_dn2str. The latter are not schema-
43 * aware, so the attributes and their values need be validated (and possibly
44 * normalized). In the current implementation the required validation/nor-
45 * malization/"pretty"ing are done on newly created DN structural represen-
46 * tations; however the idea is to move towards DN handling in structural
47 * representation instead of the current string representation. To this
48 * purpose, we need to do only the required operations and keep track of
49 * what has been done to minimize their impact on performances.
51 * Developers are strongly encouraged to use this feature, to speed-up
55 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
58 LDAPRDN_validate( LDAPRDN rdn )
65 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
66 LDAPAVA *ava = rdn[ iAVA ];
67 AttributeDescription *ad;
68 slap_syntax_validate_func *validate = NULL;
72 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
73 const char *text = NULL;
75 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
76 if ( rc != LDAP_SUCCESS ) {
77 return LDAP_INVALID_SYNTAX;
80 ava->la_private = ( void * )ad;
84 * Replace attr oid/name with the canonical name
86 ava->la_attr = ad->ad_cname;
88 validate = ad->ad_type->sat_syntax->ssyn_validate;
92 * validate value by validate function
94 rc = ( *validate )( ad->ad_type->sat_syntax,
97 if ( rc != LDAP_SUCCESS ) {
98 return LDAP_INVALID_SYNTAX;
107 * In-place, schema-aware validation of the
108 * structural representation of a distinguished name.
111 LDAPDN_validate( LDAPDN dn )
118 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
119 LDAPRDN rdn = dn[ iRDN ];
124 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
125 LDAPAVA *ava = rdn[ iAVA ];
126 AttributeDescription *ad;
127 slap_syntax_validate_func *validate = NULL;
131 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
132 const char *text = NULL;
134 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
135 if ( rc != LDAP_SUCCESS ) {
136 return LDAP_INVALID_SYNTAX;
139 ava->la_private = ( void * )ad;
143 * Replace attr oid/name with the canonical name
145 ava->la_attr = ad->ad_cname;
147 validate = ad->ad_type->sat_syntax->ssyn_validate;
151 * validate value by validate function
153 rc = ( *validate )( ad->ad_type->sat_syntax,
156 if ( rc != LDAP_SUCCESS ) {
157 return LDAP_INVALID_SYNTAX;
167 * dn validate routine
179 if ( in->bv_len == 0 ) {
182 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
183 return LDAP_INVALID_SYNTAX;
186 rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
187 if ( rc != LDAP_SUCCESS ) {
188 return LDAP_INVALID_SYNTAX;
191 assert( strlen( in->bv_val ) == in->bv_len );
194 * Schema-aware validate
196 rc = LDAPDN_validate( dn );
199 if ( rc != LDAP_SUCCESS ) {
200 return LDAP_INVALID_SYNTAX;
216 if ( in->bv_len == 0 ) {
219 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
220 return LDAP_INVALID_SYNTAX;
223 rc = ldap_bv2rdn_x( in , &rdn, (char **) &p,
224 LDAP_DN_FORMAT_LDAP, NULL);
225 if ( rc != LDAP_SUCCESS ) {
226 return LDAP_INVALID_SYNTAX;
229 assert( strlen( in->bv_val ) == in->bv_len );
232 * Schema-aware validate
234 rc = LDAPRDN_validate( rdn );
237 if ( rc != LDAP_SUCCESS ) {
238 return LDAP_INVALID_SYNTAX;
246 * AVA sorting inside a RDN
248 * rule: sort attributeTypes in alphabetical order; in case of multiple
249 * occurrences of the same attributeType, sort values in byte order
250 * (use memcmp, which implies alphabetical order in case of IA5 value;
251 * this should guarantee the repeatability of the operation).
253 * Note: the sorting can be slightly improved by sorting first
254 * by attribute type length, then by alphabetical order.
256 * uses a linear search; should be fine since the number of AVAs in
257 * a RDN should be limited.
260 AVA_Sort( LDAPRDN rdn, int iAVA )
263 LDAPAVA *ava_in = rdn[ iAVA ];
268 for ( i = 0; i < iAVA; i++ ) {
269 LDAPAVA *ava = rdn[ i ];
274 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
283 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
285 v = memcmp( ava_in->la_value.bv_val,
286 ava->la_value.bv_val,
287 d <= 0 ? ava_in->la_value.bv_len
288 : ava->la_value.bv_len );
290 if ( v == 0 && d != 0 ) {
309 a = strcmp( ava_in->la_attr.bv_val,
310 ava->la_attr.bv_val );
316 for ( j = iAVA; j > i; j-- ) {
317 rdn[ j ] = rdn[ j - 1 ];
326 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
331 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
332 LDAPAVA *ava = rdn[ iAVA ];
333 AttributeDescription *ad;
334 slap_syntax_validate_func *validf = NULL;
335 slap_mr_normalize_func *normf = NULL;
336 slap_syntax_transform_func *transf = NULL;
337 MatchingRule *mr = NULL;
338 struct berval bv = BER_BVNULL;
343 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
344 const char *text = NULL;
346 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
347 if ( rc != LDAP_SUCCESS ) {
348 return LDAP_INVALID_SYNTAX;
351 ava->la_private = ( void * )ad;
356 * Replace attr oid/name with the canonical name
358 ava->la_attr = ad->ad_cname;
360 if( ava->la_flags & LDAP_AVA_BINARY ) {
361 if( ava->la_value.bv_len == 0 ) {
362 /* BER encoding is empty */
363 return LDAP_INVALID_SYNTAX;
366 /* AVA is binary encoded, don't muck with it */
367 } else if( flags & SLAP_LDAPDN_PRETTY ) {
368 transf = ad->ad_type->sat_syntax->ssyn_pretty;
370 validf = ad->ad_type->sat_syntax->ssyn_validate;
372 } else { /* normalization */
373 validf = ad->ad_type->sat_syntax->ssyn_validate;
374 mr = ad->ad_type->sat_equality;
375 if( mr ) normf = mr->smr_normalize;
379 /* validate value before normalization */
380 rc = ( *validf )( ad->ad_type->sat_syntax,
383 : (struct berval *) &slap_empty_bv );
385 if ( rc != LDAP_SUCCESS ) {
386 return LDAP_INVALID_SYNTAX;
392 * transform value by pretty function
393 * if value is empty, use empty_bv
395 rc = ( *transf )( ad->ad_type->sat_syntax,
398 : (struct berval *) &slap_empty_bv,
401 if ( rc != LDAP_SUCCESS ) {
402 return LDAP_INVALID_SYNTAX;
409 * if value is empty, use empty_bv
412 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
413 ad->ad_type->sat_syntax,
417 : (struct berval *) &slap_empty_bv,
420 if ( rc != LDAP_SUCCESS ) {
421 return LDAP_INVALID_SYNTAX;
427 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
428 ber_memfree_x( ava->la_value.bv_val, ctx );
430 ava->la_flags |= LDAP_AVA_FREE_VALUE;
433 if( do_sort ) AVA_Sort( rdn, iAVA );
439 * In-place, schema-aware normalization / "pretty"ing of the
440 * structural representation of a distinguished name.
443 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
450 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
451 LDAPRDN rdn = dn[ iRDN ];
456 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
457 LDAPAVA *ava = rdn[ iAVA ];
458 AttributeDescription *ad;
459 slap_syntax_validate_func *validf = NULL;
460 slap_mr_normalize_func *normf = NULL;
461 slap_syntax_transform_func *transf = NULL;
462 MatchingRule *mr = NULL;
463 struct berval bv = BER_BVNULL;
468 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
469 const char *text = NULL;
471 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
472 if ( rc != LDAP_SUCCESS ) {
473 return LDAP_INVALID_SYNTAX;
476 ava->la_private = ( void * )ad;
481 * Replace attr oid/name with the canonical name
483 ava->la_attr = ad->ad_cname;
485 if( ava->la_flags & LDAP_AVA_BINARY ) {
486 if( ava->la_value.bv_len == 0 ) {
487 /* BER encoding is empty */
488 return LDAP_INVALID_SYNTAX;
491 /* AVA is binary encoded, don't muck with it */
492 } else if( flags & SLAP_LDAPDN_PRETTY ) {
493 transf = ad->ad_type->sat_syntax->ssyn_pretty;
495 validf = ad->ad_type->sat_syntax->ssyn_validate;
497 } else { /* normalization */
498 validf = ad->ad_type->sat_syntax->ssyn_validate;
499 mr = ad->ad_type->sat_equality;
500 if( mr ) normf = mr->smr_normalize;
504 /* validate value before normalization */
505 rc = ( *validf )( ad->ad_type->sat_syntax,
508 : (struct berval *) &slap_empty_bv );
510 if ( rc != LDAP_SUCCESS ) {
511 return LDAP_INVALID_SYNTAX;
517 * transform value by pretty function
518 * if value is empty, use empty_bv
520 rc = ( *transf )( ad->ad_type->sat_syntax,
523 : (struct berval *) &slap_empty_bv,
526 if ( rc != LDAP_SUCCESS ) {
527 return LDAP_INVALID_SYNTAX;
534 * if value is empty, use empty_bv
537 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
538 ad->ad_type->sat_syntax,
542 : (struct berval *) &slap_empty_bv,
545 if ( rc != LDAP_SUCCESS ) {
546 return LDAP_INVALID_SYNTAX;
552 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
553 ber_memfree_x( ava->la_value.bv_val, ctx );
555 ava->la_flags |= LDAP_AVA_FREE_VALUE;
558 if( do_sort ) AVA_Sort( rdn, iAVA );
577 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
579 if ( val->bv_len != 0 ) {
584 * Go to structural representation
586 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
587 if ( rc != LDAP_SUCCESS ) {
588 return LDAP_INVALID_SYNTAX;
591 assert( strlen( val->bv_val ) == val->bv_len );
594 * Schema-aware rewrite
596 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
597 ldap_dnfree_x( dn, ctx );
598 return LDAP_INVALID_SYNTAX;
602 * Back to string representation
604 rc = ldap_dn2bv_x( dn, out,
605 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
607 ldap_dnfree_x( dn, ctx );
609 if ( rc != LDAP_SUCCESS ) {
610 return LDAP_INVALID_SYNTAX;
613 ber_dupbv_x( out, val, ctx );
616 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
633 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
634 if ( val->bv_len != 0 ) {
640 * Go to structural representation
642 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
643 LDAP_DN_FORMAT_LDAP, ctx);
645 if ( rc != LDAP_SUCCESS ) {
646 return LDAP_INVALID_SYNTAX;
649 assert( strlen( val->bv_val ) == val->bv_len );
652 * Schema-aware rewrite
654 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
655 ldap_rdnfree_x( rdn, ctx );
656 return LDAP_INVALID_SYNTAX;
660 * Back to string representation
662 rc = ldap_rdn2bv_x( rdn, out,
663 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
665 ldap_rdnfree_x( rdn, ctx );
667 if ( rc != LDAP_SUCCESS ) {
668 return LDAP_INVALID_SYNTAX;
671 ber_dupbv_x( out, val, ctx );
674 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
689 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
691 if ( val->bv_len == 0 ) {
692 ber_dupbv_x( out, val, ctx );
694 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
695 return LDAP_INVALID_SYNTAX;
701 /* FIXME: should be liberal in what we accept */
702 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
703 if ( rc != LDAP_SUCCESS ) {
704 return LDAP_INVALID_SYNTAX;
707 assert( strlen( val->bv_val ) == val->bv_len );
710 * Schema-aware rewrite
712 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
713 ldap_dnfree_x( dn, ctx );
714 return LDAP_INVALID_SYNTAX;
717 /* FIXME: not sure why the default isn't pretty */
718 /* RE: the default is the form that is used as
719 * an internal representation; the pretty form
721 rc = ldap_dn2bv_x( dn, out,
722 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
724 ldap_dnfree_x( dn, ctx );
726 if ( rc != LDAP_SUCCESS ) {
727 return LDAP_INVALID_SYNTAX;
731 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
746 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
748 if ( val->bv_len == 0 ) {
749 ber_dupbv_x( out, val, ctx );
751 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
752 return LDAP_INVALID_SYNTAX;
759 /* FIXME: should be liberal in what we accept */
760 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
761 LDAP_DN_FORMAT_LDAP, ctx);
762 if ( rc != LDAP_SUCCESS ) {
763 return LDAP_INVALID_SYNTAX;
766 assert( strlen( val->bv_val ) == val->bv_len );
769 * Schema-aware rewrite
771 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
772 ldap_rdnfree_x( rdn, ctx );
773 return LDAP_INVALID_SYNTAX;
776 /* FIXME: not sure why the default isn't pretty */
777 /* RE: the default is the form that is used as
778 * an internal representation; the pretty form
780 rc = ldap_rdn2bv_x( rdn, out,
781 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
783 ldap_rdnfree_x( rdn, ctx );
785 if ( rc != LDAP_SUCCESS ) {
786 return LDAP_INVALID_SYNTAX;
790 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
807 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
808 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
811 if ( val->bv_len == 0 ) {
814 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
815 return LDAP_INVALID_SYNTAX;
820 /* FIXME: should be liberal in what we accept */
821 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
822 if ( rc != LDAP_SUCCESS ) {
823 return LDAP_INVALID_SYNTAX;
826 assert( strlen( val->bv_val ) == val->bv_len );
829 * Schema-aware rewrite
831 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
832 ldap_dnfree_x( *dn, ctx );
834 return LDAP_INVALID_SYNTAX;
838 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
839 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
846 * Combination of both dnPretty and dnNormalize
852 struct berval *pretty,
853 struct berval *normal,
856 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
862 if ( val->bv_len == 0 ) {
863 ber_dupbv_x( pretty, val, ctx );
864 ber_dupbv_x( normal, val, ctx );
866 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
868 return LDAP_INVALID_SYNTAX;
874 pretty->bv_val = NULL;
875 normal->bv_val = NULL;
879 /* FIXME: should be liberal in what we accept */
880 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
881 if ( rc != LDAP_SUCCESS ) {
882 return LDAP_INVALID_SYNTAX;
885 assert( strlen( val->bv_val ) == val->bv_len );
888 * Schema-aware rewrite
890 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
891 ldap_dnfree_x( dn, ctx );
892 return LDAP_INVALID_SYNTAX;
895 rc = ldap_dn2bv_x( dn, pretty,
896 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
898 if ( rc != LDAP_SUCCESS ) {
899 ldap_dnfree_x( dn, ctx );
900 return LDAP_INVALID_SYNTAX;
903 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
904 ldap_dnfree_x( dn, ctx );
905 ber_memfree_x( pretty->bv_val, ctx );
906 pretty->bv_val = NULL;
908 return LDAP_INVALID_SYNTAX;
911 rc = ldap_dn2bv_x( dn, normal,
912 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
914 ldap_dnfree_x( dn, ctx );
915 if ( rc != LDAP_SUCCESS ) {
916 ber_memfree_x( pretty->bv_val, ctx );
917 pretty->bv_val = NULL;
919 return LDAP_INVALID_SYNTAX;
923 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
924 pretty->bv_val, normal->bv_val, 0 );
938 struct berval *value,
939 void *assertedValue )
942 struct berval *asserted = (struct berval *) assertedValue;
946 assert( assertedValue );
947 assert( !BER_BVISNULL( value ) );
948 assert( !BER_BVISNULL( asserted ) );
950 match = value->bv_len - asserted->bv_len;
953 match = memcmp( value->bv_val, asserted->bv_val,
957 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
958 match, value->bv_val, asserted->bv_val );
965 * dnRelativeMatch routine
973 struct berval *value,
974 void *assertedValue )
977 struct berval *asserted = (struct berval *) assertedValue;
981 assert( assertedValue );
982 assert( !BER_BVISNULL( value ) );
983 assert( !BER_BVISNULL( asserted ) );
985 if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
986 if( asserted->bv_len > value->bv_len ) {
988 } else if ( asserted->bv_len == value->bv_len ) {
989 match = memcmp( value->bv_val, asserted->bv_val,
993 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
996 &value->bv_val[value->bv_len - asserted->bv_len],
1005 return LDAP_SUCCESS;
1009 if( mr == slap_schema.si_mr_dnSuperiorMatch ) {
1011 value = (struct berval *) assertedValue;
1012 mr = slap_schema.si_mr_dnSubordinateMatch;
1015 if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
1016 if( asserted->bv_len >= value->bv_len ) {
1020 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
1023 &value->bv_val[value->bv_len - asserted->bv_len],
1032 return LDAP_SUCCESS;
1035 if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
1036 if( asserted->bv_len >= value->bv_len ) {
1040 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
1043 &value->bv_val[value->bv_len - asserted->bv_len],
1049 rdn.bv_val = value->bv_val;
1050 rdn.bv_len = value->bv_len - asserted->bv_len - 1;
1051 match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
1059 return LDAP_SUCCESS;
1062 /* should not be reachable */
1073 struct berval *value,
1074 void *assertedValue )
1077 struct berval *asserted = (struct berval *) assertedValue;
1081 assert( assertedValue );
1083 match = value->bv_len - asserted->bv_len;
1086 match = memcmp( value->bv_val, asserted->bv_val,
1090 Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
1091 match, value->bv_val, asserted->bv_val );
1094 return LDAP_SUCCESS;
1099 * dnParent - dn's parent, in-place
1100 * note: the incoming dn is assumed to be normalized/prettyfied,
1101 * so that escaped rdn/ava separators are in '\'+hexpair form
1106 struct berval *pdn )
1110 p = strchr( dn->bv_val, ',' );
1115 pdn->bv_val = dn->bv_val + dn->bv_len;
1119 assert( DN_SEPARATOR( p[ 0 ] ) );
1122 assert( ATTR_LEADCHAR( p[ 0 ] ) );
1124 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
1130 * dnRdn - dn's rdn, in-place
1131 * note: the incoming dn is assumed to be normalized/prettyfied,
1132 * so that escaped rdn/ava separators are in '\'+hexpair form
1137 struct berval *rdn )
1142 p = strchr( dn->bv_val, ',' );
1149 assert( DN_SEPARATOR( p[ 0 ] ) );
1150 assert( ATTR_LEADCHAR( p[ 1 ] ) );
1151 rdn->bv_len = p - dn->bv_val;
1169 if( dn->bv_len == 0 ) {
1173 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1174 if ( rc != LDAP_SUCCESS ) {
1178 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1181 ldap_rdnfree_x( tmpRDN, ctx );
1186 * We can assume the input is a prettied or normalized DN
1191 struct berval *dn_in )
1197 if ( dn_in == NULL ) {
1201 if ( !dn_in->bv_len ) {
1205 if ( be != NULL && be_issuffix( be, dn_in ) ) {
1209 p = strchr( dn_in->bv_val, ',' );
1211 return p ? p - dn_in->bv_val : dn_in->bv_len;
1217 * LDAP_SUCCESS if rdn is a legal rdn;
1218 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1221 rdn_validate( struct berval *rdn )
1225 * input is a pretty or normalized DN
1226 * hence, we can just search for ','
1228 if( rdn == NULL || rdn->bv_len == 0 ||
1229 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1231 return LDAP_INVALID_SYNTAX;
1233 return strchr( rdn->bv_val, ',' ) == NULL
1234 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1237 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
1244 if ( rdn == NULL || rdn == '\0' ) {
1251 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1252 if ( rc != LDAP_SUCCESS ) {
1259 if ( p[ 0 ] != '\0' ) {
1264 * Schema-aware validate
1266 if ( rc == LDAP_SUCCESS ) {
1267 rc = LDAPDN_validate( DN );
1269 ldap_rdnfree( RDN );
1272 * Must validate (there's a repeated parsing ...)
1274 return ( rc == LDAP_SUCCESS );
1281 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1284 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1288 build_new_dn( struct berval * new_dn,
1289 struct berval * parent_dn,
1290 struct berval * newrdn,
1295 if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1296 ber_dupbv( new_dn, newrdn );
1300 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1301 new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1303 ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
1305 strcpy( ptr, parent_dn->bv_val );
1310 * dnIsSuffix - tells whether suffix is a suffix of dn.
1311 * Both dn and suffix must be normalized.
1315 const struct berval *dn,
1316 const struct berval *suffix )
1318 int d = dn->bv_len - suffix->bv_len;
1323 /* empty suffix matches any dn */
1324 if ( suffix->bv_len == 0 ) {
1328 /* suffix longer than dn */
1333 /* no rdn separator or escaped rdn separator */
1334 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1338 /* no possible match or malformed dn */
1344 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1348 dnIsOneLevelRDN( struct berval *rdn )
1350 ber_len_t len = rdn->bv_len;
1352 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1360 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1362 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1365 if ( DNX509PeerNormalizeCertMap == NULL ) {
1366 DNX509PeerNormalizeCertMap = fn;
1376 * Convert an X.509 DN into a normalized LDAP DN
1379 dnX509normalize( void *x509_name, struct berval *out )
1381 /* Invoke the LDAP library's converter with our schema-rewriter */
1382 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1384 Debug( LDAP_DEBUG_TRACE,
1385 "dnX509Normalize: <%s>\n", out->bv_val, 0, 0 );
1391 * Get the TLS session's peer's DN into a normalized LDAP DN
1394 dnX509peerNormalize( void *ssl, struct berval *dn )
1396 int rc = LDAP_INVALID_CREDENTIALS;
1398 if ( DNX509PeerNormalizeCertMap != NULL )
1399 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1401 if ( rc != LDAP_SUCCESS ) {
1402 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1403 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );