1 /* dn.c - routines for dealing with distinguished names */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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 )
57 LDAPRDN_validate( LDAPRDN rdn )
62 assert( rdn != NULL );
64 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
65 LDAPAVA *ava = rdn[ iAVA ];
66 AttributeDescription *ad;
67 slap_syntax_validate_func *validate = NULL;
69 assert( ava != NULL );
71 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
72 const char *text = NULL;
74 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
75 if ( rc != LDAP_SUCCESS ) {
76 rc = slap_bv2undef_ad( &ava->la_attr,
78 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
79 if ( rc != LDAP_SUCCESS ) {
80 return LDAP_INVALID_SYNTAX;
84 ava->la_private = ( void * )ad;
88 * Replace attr oid/name with the canonical name
90 ava->la_attr = ad->ad_cname;
92 validate = ad->ad_type->sat_syntax->ssyn_validate;
96 * validate value by validate function
98 rc = ( *validate )( ad->ad_type->sat_syntax,
101 if ( rc != LDAP_SUCCESS ) {
102 return LDAP_INVALID_SYNTAX;
111 * In-place, schema-aware validation of the
112 * structural representation of a distinguished name.
115 LDAPDN_validate( LDAPDN dn )
120 assert( dn != NULL );
122 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
123 LDAPRDN rdn = dn[ iRDN ];
126 assert( rdn != NULL );
128 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
129 LDAPAVA *ava = rdn[ iAVA ];
130 AttributeDescription *ad;
131 slap_syntax_validate_func *validate = NULL;
133 assert( ava != NULL );
135 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
136 const char *text = NULL;
138 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
139 if ( rc != LDAP_SUCCESS ) {
140 rc = slap_bv2undef_ad( &ava->la_attr,
142 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
143 if ( rc != LDAP_SUCCESS ) {
144 return LDAP_INVALID_SYNTAX;
148 ava->la_private = ( void * )ad;
152 * Replace attr oid/name with the canonical name
154 ava->la_attr = ad->ad_cname;
156 validate = ad->ad_type->sat_syntax->ssyn_validate;
160 * validate value by validate function
162 rc = ( *validate )( ad->ad_type->sat_syntax,
165 if ( rc != LDAP_SUCCESS ) {
166 return LDAP_INVALID_SYNTAX;
176 * dn validate routine
186 assert( in != NULL );
188 if ( in->bv_len == 0 ) {
191 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
192 return LDAP_INVALID_SYNTAX;
195 rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
196 if ( rc != LDAP_SUCCESS ) {
197 return LDAP_INVALID_SYNTAX;
200 assert( strlen( in->bv_val ) == in->bv_len );
203 * Schema-aware validate
205 rc = LDAPDN_validate( dn );
208 if ( rc != LDAP_SUCCESS ) {
209 return LDAP_INVALID_SYNTAX;
224 assert( in != NULL );
225 if ( in->bv_len == 0 ) {
228 } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
229 return LDAP_INVALID_SYNTAX;
232 rc = ldap_bv2rdn_x( in , &rdn, (char **) &p,
233 LDAP_DN_FORMAT_LDAP, NULL);
234 if ( rc != LDAP_SUCCESS ) {
235 return LDAP_INVALID_SYNTAX;
238 assert( strlen( in->bv_val ) == in->bv_len );
241 * Schema-aware validate
243 rc = LDAPRDN_validate( rdn );
246 if ( rc != LDAP_SUCCESS ) {
247 return LDAP_INVALID_SYNTAX;
255 * AVA sorting inside a RDN
257 * rule: sort attributeTypes in alphabetical order; in case of multiple
258 * occurrences of the same attributeType, sort values in byte order
259 * (use memcmp, which implies alphabetical order in case of IA5 value;
260 * this should guarantee the repeatability of the operation).
262 * Note: the sorting can be slightly improved by sorting first
263 * by attribute type length, then by alphabetical order.
265 * uses a linear search; should be fine since the number of AVAs in
266 * a RDN should be limited.
269 AVA_Sort( LDAPRDN rdn, int iAVA )
272 LDAPAVA *ava_in = rdn[ iAVA ];
274 assert( rdn != NULL );
275 assert( ava_in != NULL );
277 for ( i = 0; i < iAVA; i++ ) {
278 LDAPAVA *ava = rdn[ i ];
281 assert( ava != NULL );
283 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
292 d = ava_in->la_value.bv_len - ava->la_value.bv_len;
294 v = memcmp( ava_in->la_value.bv_val,
295 ava->la_value.bv_val,
296 d <= 0 ? ava_in->la_value.bv_len
297 : ava->la_value.bv_len );
299 if ( v == 0 && d != 0 ) {
318 a = strcmp( ava_in->la_attr.bv_val,
319 ava->la_attr.bv_val );
325 for ( j = iAVA; j > i; j-- ) {
326 rdn[ j ] = rdn[ j - 1 ];
335 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
340 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
341 LDAPAVA *ava = rdn[ iAVA ];
342 AttributeDescription *ad;
343 slap_syntax_validate_func *validf = NULL;
344 slap_mr_normalize_func *normf = NULL;
345 slap_syntax_transform_func *transf = NULL;
346 MatchingRule *mr = NULL;
347 struct berval bv = BER_BVNULL;
350 assert( ava != NULL );
352 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
353 const char *text = NULL;
355 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
356 if ( rc != LDAP_SUCCESS ) {
357 rc = slap_bv2undef_ad( &ava->la_attr,
359 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
360 if ( rc != LDAP_SUCCESS ) {
361 return LDAP_INVALID_SYNTAX;
365 ava->la_private = ( void * )ad;
370 * Replace attr oid/name with the canonical name
372 ava->la_attr = ad->ad_cname;
374 if( ava->la_flags & LDAP_AVA_BINARY ) {
375 if( ava->la_value.bv_len == 0 ) {
376 /* BER encoding is empty */
377 return LDAP_INVALID_SYNTAX;
380 /* AVA is binary encoded, don't muck with it */
381 } else if( flags & SLAP_LDAPDN_PRETTY ) {
382 transf = ad->ad_type->sat_syntax->ssyn_pretty;
384 validf = ad->ad_type->sat_syntax->ssyn_validate;
386 } else { /* normalization */
387 validf = ad->ad_type->sat_syntax->ssyn_validate;
388 mr = ad->ad_type->sat_equality;
389 if( mr ) normf = mr->smr_normalize;
393 /* validate value before normalization */
394 rc = ( *validf )( ad->ad_type->sat_syntax,
397 : (struct berval *) &slap_empty_bv );
399 if ( rc != LDAP_SUCCESS ) {
400 return LDAP_INVALID_SYNTAX;
406 * transform value by pretty function
407 * if value is empty, use empty_bv
409 rc = ( *transf )( ad->ad_type->sat_syntax,
412 : (struct berval *) &slap_empty_bv,
415 if ( rc != LDAP_SUCCESS ) {
416 return LDAP_INVALID_SYNTAX;
423 * if value is empty, use empty_bv
426 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
427 ad->ad_type->sat_syntax,
431 : (struct berval *) &slap_empty_bv,
434 if ( rc != LDAP_SUCCESS ) {
435 return LDAP_INVALID_SYNTAX;
441 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
442 ber_memfree_x( ava->la_value.bv_val, ctx );
444 ava->la_flags |= LDAP_AVA_FREE_VALUE;
447 if( do_sort ) AVA_Sort( rdn, iAVA );
453 * In-place, schema-aware normalization / "pretty"ing of the
454 * structural representation of a distinguished name.
457 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
462 assert( dn != NULL );
464 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
465 LDAPRDN rdn = dn[ iRDN ];
468 assert( rdn != NULL );
470 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
471 LDAPAVA *ava = rdn[ iAVA ];
472 AttributeDescription *ad;
473 slap_syntax_validate_func *validf = NULL;
474 slap_mr_normalize_func *normf = NULL;
475 slap_syntax_transform_func *transf = NULL;
476 MatchingRule *mr = NULL;
477 struct berval bv = BER_BVNULL;
480 assert( ava != NULL );
482 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
483 const char *text = NULL;
485 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
486 if ( rc != LDAP_SUCCESS ) {
487 rc = slap_bv2undef_ad( &ava->la_attr,
489 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
490 if ( rc != LDAP_SUCCESS ) {
491 return LDAP_INVALID_SYNTAX;
495 ava->la_private = ( void * )ad;
500 * Replace attr oid/name with the canonical name
502 ava->la_attr = ad->ad_cname;
504 if( ava->la_flags & LDAP_AVA_BINARY ) {
505 if( ava->la_value.bv_len == 0 ) {
506 /* BER encoding is empty */
507 return LDAP_INVALID_SYNTAX;
510 /* AVA is binary encoded, don't muck with it */
511 } else if( flags & SLAP_LDAPDN_PRETTY ) {
512 transf = ad->ad_type->sat_syntax->ssyn_pretty;
514 validf = ad->ad_type->sat_syntax->ssyn_validate;
516 } else { /* normalization */
517 validf = ad->ad_type->sat_syntax->ssyn_validate;
518 mr = ad->ad_type->sat_equality;
519 if( mr ) normf = mr->smr_normalize;
523 /* validate value before normalization */
524 rc = ( *validf )( ad->ad_type->sat_syntax,
527 : (struct berval *) &slap_empty_bv );
529 if ( rc != LDAP_SUCCESS ) {
530 return LDAP_INVALID_SYNTAX;
536 * transform value by pretty function
537 * if value is empty, use empty_bv
539 rc = ( *transf )( ad->ad_type->sat_syntax,
542 : (struct berval *) &slap_empty_bv,
545 if ( rc != LDAP_SUCCESS ) {
546 return LDAP_INVALID_SYNTAX;
553 * if value is empty, use empty_bv
556 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
557 ad->ad_type->sat_syntax,
561 : (struct berval *) &slap_empty_bv,
564 if ( rc != LDAP_SUCCESS ) {
565 return LDAP_INVALID_SYNTAX;
571 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
572 ber_memfree_x( ava->la_value.bv_val, ctx );
574 ava->la_flags |= LDAP_AVA_FREE_VALUE;
577 if( do_sort ) AVA_Sort( rdn, iAVA );
593 assert( val != NULL );
594 assert( out != NULL );
596 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
598 if ( val->bv_len != 0 ) {
603 * Go to structural representation
605 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
606 if ( rc != LDAP_SUCCESS ) {
607 return LDAP_INVALID_SYNTAX;
610 assert( strlen( val->bv_val ) == val->bv_len );
613 * Schema-aware rewrite
615 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
616 ldap_dnfree_x( dn, ctx );
617 return LDAP_INVALID_SYNTAX;
621 * Back to string representation
623 rc = ldap_dn2bv_x( dn, out,
624 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
626 ldap_dnfree_x( dn, ctx );
628 if ( rc != LDAP_SUCCESS ) {
629 return LDAP_INVALID_SYNTAX;
632 ber_dupbv_x( out, val, ctx );
635 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
649 assert( val != NULL );
650 assert( out != NULL );
652 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
653 if ( val->bv_len != 0 ) {
659 * Go to structural representation
661 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
662 LDAP_DN_FORMAT_LDAP, ctx);
664 if ( rc != LDAP_SUCCESS ) {
665 return LDAP_INVALID_SYNTAX;
668 assert( strlen( val->bv_val ) == val->bv_len );
671 * Schema-aware rewrite
673 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
674 ldap_rdnfree_x( rdn, ctx );
675 return LDAP_INVALID_SYNTAX;
679 * Back to string representation
681 rc = ldap_rdn2bv_x( rdn, out,
682 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
684 ldap_rdnfree_x( rdn, ctx );
686 if ( rc != LDAP_SUCCESS ) {
687 return LDAP_INVALID_SYNTAX;
690 ber_dupbv_x( out, val, ctx );
693 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
705 assert( val != NULL );
706 assert( out != NULL );
708 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
710 if ( val->bv_len == 0 ) {
711 ber_dupbv_x( out, val, ctx );
713 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
714 return LDAP_INVALID_SYNTAX;
720 /* FIXME: should be liberal in what we accept */
721 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
722 if ( rc != LDAP_SUCCESS ) {
723 return LDAP_INVALID_SYNTAX;
726 assert( strlen( val->bv_val ) == val->bv_len );
729 * Schema-aware rewrite
731 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
732 ldap_dnfree_x( dn, ctx );
733 return LDAP_INVALID_SYNTAX;
736 /* FIXME: not sure why the default isn't pretty */
737 /* RE: the default is the form that is used as
738 * an internal representation; the pretty form
740 rc = ldap_dn2bv_x( dn, out,
741 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
743 ldap_dnfree_x( dn, ctx );
745 if ( rc != LDAP_SUCCESS ) {
746 return LDAP_INVALID_SYNTAX;
750 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
762 assert( val != NULL );
763 assert( out != NULL );
765 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
767 if ( val->bv_len == 0 ) {
768 ber_dupbv_x( out, val, ctx );
770 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
771 return LDAP_INVALID_SYNTAX;
778 /* FIXME: should be liberal in what we accept */
779 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
780 LDAP_DN_FORMAT_LDAP, ctx);
781 if ( rc != LDAP_SUCCESS ) {
782 return LDAP_INVALID_SYNTAX;
785 assert( strlen( val->bv_val ) == val->bv_len );
788 * Schema-aware rewrite
790 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
791 ldap_rdnfree_x( rdn, ctx );
792 return LDAP_INVALID_SYNTAX;
795 /* FIXME: not sure why the default isn't pretty */
796 /* RE: the default is the form that is used as
797 * an internal representation; the pretty form
799 rc = ldap_rdn2bv_x( rdn, out,
800 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
802 ldap_rdnfree_x( rdn, ctx );
804 if ( rc != LDAP_SUCCESS ) {
805 return LDAP_INVALID_SYNTAX;
809 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
823 assert( val != NULL );
824 assert( dn != NULL );
826 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
827 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
830 if ( val->bv_len == 0 ) {
833 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
834 return LDAP_INVALID_SYNTAX;
839 /* FIXME: should be liberal in what we accept */
840 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
841 if ( rc != LDAP_SUCCESS ) {
842 return LDAP_INVALID_SYNTAX;
845 assert( strlen( val->bv_val ) == val->bv_len );
848 * Schema-aware rewrite
850 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
851 ldap_dnfree_x( *dn, ctx );
853 return LDAP_INVALID_SYNTAX;
857 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
858 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
865 * Combination of both dnPretty and dnNormalize
871 struct berval *pretty,
872 struct berval *normal,
875 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
877 assert( val != NULL );
878 assert( pretty != NULL );
879 assert( normal != NULL );
881 if ( val->bv_len == 0 ) {
882 ber_dupbv_x( pretty, val, ctx );
883 ber_dupbv_x( normal, val, ctx );
885 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
887 return LDAP_INVALID_SYNTAX;
893 pretty->bv_val = NULL;
894 normal->bv_val = NULL;
898 /* FIXME: should be liberal in what we accept */
899 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
900 if ( rc != LDAP_SUCCESS ) {
901 return LDAP_INVALID_SYNTAX;
904 assert( strlen( val->bv_val ) == val->bv_len );
907 * Schema-aware rewrite
909 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
910 ldap_dnfree_x( dn, ctx );
911 return LDAP_INVALID_SYNTAX;
914 rc = ldap_dn2bv_x( dn, pretty,
915 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
917 if ( rc != LDAP_SUCCESS ) {
918 ldap_dnfree_x( dn, ctx );
919 return LDAP_INVALID_SYNTAX;
922 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
923 ldap_dnfree_x( dn, ctx );
924 ber_memfree_x( pretty->bv_val, ctx );
925 pretty->bv_val = NULL;
927 return LDAP_INVALID_SYNTAX;
930 rc = ldap_dn2bv_x( dn, normal,
931 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
933 ldap_dnfree_x( dn, ctx );
934 if ( rc != LDAP_SUCCESS ) {
935 ber_memfree_x( pretty->bv_val, ctx );
936 pretty->bv_val = NULL;
938 return LDAP_INVALID_SYNTAX;
942 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
943 pretty->bv_val, normal->bv_val, 0 );
957 struct berval *value,
958 void *assertedValue )
961 struct berval *asserted = (struct berval *) assertedValue;
963 assert( matchp != NULL );
964 assert( value != NULL );
965 assert( assertedValue != NULL );
966 assert( !BER_BVISNULL( value ) );
967 assert( !BER_BVISNULL( asserted ) );
969 match = value->bv_len - asserted->bv_len;
972 match = memcmp( value->bv_val, asserted->bv_val,
976 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
977 match, value->bv_val, asserted->bv_val );
984 * dnRelativeMatch routine
992 struct berval *value,
993 void *assertedValue )
996 struct berval *asserted = (struct berval *) assertedValue;
998 assert( matchp != NULL );
999 assert( value != NULL );
1000 assert( assertedValue != NULL );
1001 assert( !BER_BVISNULL( value ) );
1002 assert( !BER_BVISNULL( asserted ) );
1004 if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
1005 if( asserted->bv_len > value->bv_len ) {
1007 } else if ( asserted->bv_len == value->bv_len ) {
1008 match = memcmp( value->bv_val, asserted->bv_val,
1012 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
1015 &value->bv_val[value->bv_len - asserted->bv_len],
1024 return LDAP_SUCCESS;
1027 if( mr == slap_schema.si_mr_dnSuperiorMatch ) {
1029 value = (struct berval *) assertedValue;
1030 mr = slap_schema.si_mr_dnSubordinateMatch;
1033 if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
1034 if( asserted->bv_len >= value->bv_len ) {
1038 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
1041 &value->bv_val[value->bv_len - asserted->bv_len],
1050 return LDAP_SUCCESS;
1053 if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
1054 if( asserted->bv_len >= value->bv_len ) {
1058 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
1061 &value->bv_val[value->bv_len - asserted->bv_len],
1067 rdn.bv_val = value->bv_val;
1068 rdn.bv_len = value->bv_len - asserted->bv_len - 1;
1069 match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
1077 return LDAP_SUCCESS;
1080 /* should not be reachable */
1091 struct berval *value,
1092 void *assertedValue )
1095 struct berval *asserted = (struct berval *) assertedValue;
1097 assert( matchp != NULL );
1098 assert( value != NULL );
1099 assert( assertedValue != NULL );
1101 match = value->bv_len - asserted->bv_len;
1104 match = memcmp( value->bv_val, asserted->bv_val,
1108 Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
1109 match, value->bv_val, asserted->bv_val );
1112 return LDAP_SUCCESS;
1117 * dnParent - dn's parent, in-place
1118 * note: the incoming dn is assumed to be normalized/prettyfied,
1119 * so that escaped rdn/ava separators are in '\'+hexpair form
1121 * note: "dn" and "pdn" can point to the same berval;
1122 * beware that, in this case, the pointer to the original buffer
1128 struct berval *pdn )
1132 p = ber_bvchr( dn, ',' );
1137 pdn->bv_val = dn->bv_val + dn->bv_len;
1141 assert( DN_SEPARATOR( p[ 0 ] ) );
1144 assert( ATTR_LEADCHAR( p[ 0 ] ) );
1145 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
1152 * dnRdn - dn's rdn, in-place
1153 * note: the incoming dn is assumed to be normalized/prettyfied,
1154 * so that escaped rdn/ava separators are in '\'+hexpair form
1159 struct berval *rdn )
1164 p = ber_bvchr( dn, ',' );
1171 assert( DN_SEPARATOR( p[ 0 ] ) );
1172 assert( ATTR_LEADCHAR( p[ 1 ] ) );
1173 rdn->bv_len = p - dn->bv_val;
1188 assert( dn != NULL );
1189 assert( rdn != NULL );
1191 if( dn->bv_len == 0 ) {
1195 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1196 if ( rc != LDAP_SUCCESS ) {
1200 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1203 ldap_rdnfree_x( tmpRDN, ctx );
1208 * We can assume the input is a prettied or normalized DN
1213 struct berval *dn_in )
1217 assert( dn_in != NULL );
1219 if ( dn_in == NULL ) {
1223 if ( !dn_in->bv_len ) {
1227 if ( be != NULL && be_issuffix( be, dn_in ) ) {
1231 p = ber_bvchr( dn_in, ',' );
1233 return p ? p - dn_in->bv_val : dn_in->bv_len;
1239 * LDAP_SUCCESS if rdn is a legal rdn;
1240 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1243 rdn_validate( struct berval *rdn )
1247 * input is a pretty or normalized DN
1248 * hence, we can just search for ','
1250 if( rdn == NULL || rdn->bv_len == 0 ||
1251 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1253 return LDAP_INVALID_SYNTAX;
1255 return ber_bvchr( rdn, ',' ) == NULL
1256 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1259 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
1266 if ( rdn == NULL || rdn == '\0' ) {
1273 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1274 if ( rc != LDAP_SUCCESS ) {
1281 if ( p[ 0 ] != '\0' ) {
1286 * Schema-aware validate
1288 if ( rc == LDAP_SUCCESS ) {
1289 rc = LDAPDN_validate( DN );
1291 ldap_rdnfree( RDN );
1294 * Must validate (there's a repeated parsing ...)
1296 return ( rc == LDAP_SUCCESS );
1303 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1306 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1310 build_new_dn( struct berval * new_dn,
1311 struct berval * parent_dn,
1312 struct berval * newrdn,
1317 if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1318 ber_dupbv( new_dn, newrdn );
1322 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1323 new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1325 ptr = lutil_strncopy( new_dn->bv_val, newrdn->bv_val, newrdn->bv_len );
1327 strcpy( ptr, parent_dn->bv_val );
1332 * dnIsSuffix - tells whether suffix is a suffix of dn.
1333 * Both dn and suffix must be normalized.
1337 const struct berval *dn,
1338 const struct berval *suffix )
1340 int d = dn->bv_len - suffix->bv_len;
1342 assert( dn != NULL );
1343 assert( suffix != NULL );
1345 /* empty suffix matches any dn */
1346 if ( suffix->bv_len == 0 ) {
1350 /* suffix longer than dn */
1355 /* no rdn separator or escaped rdn separator */
1356 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1360 /* no possible match or malformed dn */
1366 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1370 dnIsOneLevelRDN( struct berval *rdn )
1372 ber_len_t len = rdn->bv_len;
1374 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1383 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1386 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1389 if ( DNX509PeerNormalizeCertMap == NULL ) {
1390 DNX509PeerNormalizeCertMap = fn;
1400 * Convert an X.509 DN into a normalized LDAP DN
1403 dnX509normalize( void *x509_name, struct berval *out )
1405 /* Invoke the LDAP library's converter with our schema-rewriter */
1406 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1408 Debug( LDAP_DEBUG_TRACE,
1409 "dnX509Normalize: <%s>\n", out->bv_val, 0, 0 );
1415 * Get the TLS session's peer's DN into a normalized LDAP DN
1418 dnX509peerNormalize( void *ssl, struct berval *dn )
1420 int rc = LDAP_INVALID_CREDENTIALS;
1422 if ( DNX509PeerNormalizeCertMap != NULL )
1423 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1425 if ( rc != LDAP_SUCCESS ) {
1426 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1427 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );