Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / dn.c
blobb10434922f9a9713eccc11b2a094e02e5062d12d
1 /* dn.c - routines for dealing with distinguished names */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/dn.c,v 1.182.2.8 2008/02/11 23:26:44 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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.
27 #include "portable.h"
29 #include <stdio.h>
31 #include <ac/ctype.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
34 #include <ac/time.h>
36 #include "slap.h"
37 #include "lutil.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
51 * its stabilization.
54 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
56 int slap_DN_strict = SLAP_AD_NOINSERT;
58 static int
59 LDAPRDN_validate( LDAPRDN rdn )
61 int iAVA;
62 int rc;
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,
79 &ad, &text,
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;
96 /*
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;
103 if ( validate ) {
105 * validate value by validate function
107 rc = ( *validate )( ad->ad_type->sat_syntax,
108 &ava->la_value );
110 if ( rc != LDAP_SUCCESS ) {
111 return LDAP_INVALID_SYNTAX;
116 return LDAP_SUCCESS;
120 * In-place, schema-aware validation of the
121 * structural representation of a distinguished name.
123 static int
124 LDAPDN_validate( LDAPDN dn )
126 int iRDN;
127 int rc;
129 assert( dn != NULL );
131 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
132 rc = LDAPRDN_validate( dn[ iRDN ] );
133 if ( rc != LDAP_SUCCESS ) {
134 return rc;
138 return LDAP_SUCCESS;
142 * dn validate routine
145 dnValidate(
146 Syntax *syntax,
147 struct berval *in )
149 int rc;
150 LDAPDN dn = NULL;
152 assert( in != NULL );
154 if ( in->bv_len == 0 ) {
155 return LDAP_SUCCESS;
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 );
172 ldap_dnfree( dn );
174 if ( rc != LDAP_SUCCESS ) {
175 return LDAP_INVALID_SYNTAX;
178 return LDAP_SUCCESS;
182 rdnValidate(
183 Syntax *syntax,
184 struct berval *in )
186 int rc;
187 LDAPRDN rdn;
188 char* p;
190 assert( in != NULL );
191 if ( in->bv_len == 0 ) {
192 return LDAP_SUCCESS;
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 );
210 ldap_rdnfree( rdn );
212 if ( rc != LDAP_SUCCESS ) {
213 return LDAP_INVALID_SYNTAX;
216 return LDAP_SUCCESS;
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 an insertion sort; should be fine since the number of AVAs in
232 * a RDN should be limited.
234 static int
235 AVA_Sort( LDAPRDN rdn, int nAVAs )
237 LDAPAVA *ava_i;
238 int i;
240 assert( rdn != NULL );
242 for ( i = 1; i < nAVAs; i++ ) {
243 LDAPAVA *ava_j;
244 int j;
246 ava_i = rdn[ i ];
247 for ( j = i-1; j >=0; j-- ) {
248 int a;
250 ava_j = rdn[ j ];
251 a = strcmp( ava_i->la_attr.bv_val, ava_j->la_attr.bv_val );
253 if ( a == 0 ) {
254 int d;
256 d = ava_i->la_value.bv_len - ava_j->la_value.bv_len;
258 a = memcmp( ava_i->la_value.bv_val,
259 ava_j->la_value.bv_val,
260 d <= 0 ? ava_i->la_value.bv_len
261 : ava_j->la_value.bv_len );
263 if ( a == 0 ) {
264 a = d;
267 /* Duplicates are not allowed */
268 if ( a == 0 )
269 return LDAP_INVALID_DN_SYNTAX;
271 if ( a > 0 )
272 break;
274 rdn[ j+1 ] = rdn[ j ];
276 rdn[ j+1 ] = ava_i;
278 return LDAP_SUCCESS;
281 static int
282 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
285 int rc, iAVA, do_sort = 0;
287 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
288 LDAPAVA *ava = rdn[ iAVA ];
289 AttributeDescription *ad;
290 slap_syntax_validate_func *validf = NULL;
291 slap_mr_normalize_func *normf = NULL;
292 slap_syntax_transform_func *transf = NULL;
293 MatchingRule *mr = NULL;
294 struct berval bv = BER_BVNULL;
296 assert( ava != NULL );
298 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
299 const char *text = NULL;
301 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
302 if ( rc != LDAP_SUCCESS ) {
303 rc = slap_bv2undef_ad( &ava->la_attr,
304 &ad, &text,
305 SLAP_AD_PROXIED|slap_DN_strict );
306 if ( rc != LDAP_SUCCESS ) {
307 return LDAP_INVALID_SYNTAX;
311 ava->la_private = ( void * )ad;
312 do_sort = 1;
316 * Replace attr oid/name with the canonical name
318 ava->la_attr = ad->ad_cname;
320 if( ava->la_flags & LDAP_AVA_BINARY ) {
321 if( ava->la_value.bv_len == 0 ) {
322 /* BER encoding is empty */
323 return LDAP_INVALID_SYNTAX;
326 /* Do not allow X-ORDERED 'VALUES' naming attributes */
327 } else if( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
328 return LDAP_INVALID_SYNTAX;
330 /* AVA is binary encoded, don't muck with it */
331 } else if( flags & SLAP_LDAPDN_PRETTY ) {
332 transf = ad->ad_type->sat_syntax->ssyn_pretty;
333 if( !transf ) {
334 validf = ad->ad_type->sat_syntax->ssyn_validate;
336 } else { /* normalization */
337 validf = ad->ad_type->sat_syntax->ssyn_validate;
338 mr = ad->ad_type->sat_equality;
339 if( mr && (!( mr->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))) {
340 normf = mr->smr_normalize;
344 if ( validf ) {
345 /* validate value before normalization */
346 rc = ( *validf )( ad->ad_type->sat_syntax,
347 ava->la_value.bv_len
348 ? &ava->la_value
349 : (struct berval *) &slap_empty_bv );
351 if ( rc != LDAP_SUCCESS ) {
352 return LDAP_INVALID_SYNTAX;
356 if ( transf ) {
358 * transform value by pretty function
359 * if value is empty, use empty_bv
361 rc = ( *transf )( ad->ad_type->sat_syntax,
362 ava->la_value.bv_len
363 ? &ava->la_value
364 : (struct berval *) &slap_empty_bv,
365 &bv, ctx );
367 if ( rc != LDAP_SUCCESS ) {
368 return LDAP_INVALID_SYNTAX;
372 if ( normf ) {
374 * normalize value
375 * if value is empty, use empty_bv
377 rc = ( *normf )(
378 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
379 ad->ad_type->sat_syntax,
381 ava->la_value.bv_len
382 ? &ava->la_value
383 : (struct berval *) &slap_empty_bv,
384 &bv, ctx );
386 if ( rc != LDAP_SUCCESS ) {
387 return LDAP_INVALID_SYNTAX;
392 if( bv.bv_val ) {
393 if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
394 ber_memfree_x( ava->la_value.bv_val, ctx );
395 ava->la_value = bv;
396 ava->la_flags |= LDAP_AVA_FREE_VALUE;
399 rc = LDAP_SUCCESS;
401 if ( do_sort ) {
402 rc = AVA_Sort( rdn, iAVA );
405 return rc;
409 * In-place, schema-aware normalization / "pretty"ing of the
410 * structural representation of a distinguished name.
412 static int
413 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
415 int iRDN;
416 int rc;
418 assert( dn != NULL );
420 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
421 rc = LDAPRDN_rewrite( dn[ iRDN ], flags, ctx );
422 if ( rc != LDAP_SUCCESS ) {
423 return rc;
427 return LDAP_SUCCESS;
431 dnNormalize(
432 slap_mask_t use,
433 Syntax *syntax,
434 MatchingRule *mr,
435 struct berval *val,
436 struct berval *out,
437 void *ctx)
439 assert( val != NULL );
440 assert( out != NULL );
442 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
444 if ( val->bv_len != 0 ) {
445 LDAPDN dn = NULL;
446 int rc;
449 * Go to structural representation
451 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
452 if ( rc != LDAP_SUCCESS ) {
453 return LDAP_INVALID_SYNTAX;
456 assert( strlen( val->bv_val ) == val->bv_len );
459 * Schema-aware rewrite
461 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
462 ldap_dnfree_x( dn, ctx );
463 return LDAP_INVALID_SYNTAX;
467 * Back to string representation
469 rc = ldap_dn2bv_x( dn, out,
470 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
472 ldap_dnfree_x( dn, ctx );
474 if ( rc != LDAP_SUCCESS ) {
475 return LDAP_INVALID_SYNTAX;
477 } else {
478 ber_dupbv_x( out, val, ctx );
481 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
483 return LDAP_SUCCESS;
487 rdnNormalize(
488 slap_mask_t use,
489 Syntax *syntax,
490 MatchingRule *mr,
491 struct berval *val,
492 struct berval *out,
493 void *ctx)
495 assert( val != NULL );
496 assert( out != NULL );
498 Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
499 if ( val->bv_len != 0 ) {
500 LDAPRDN rdn = NULL;
501 int rc;
502 char* p;
505 * Go to structural representation
507 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
508 LDAP_DN_FORMAT_LDAP, ctx);
510 if ( rc != LDAP_SUCCESS ) {
511 return LDAP_INVALID_SYNTAX;
514 assert( strlen( val->bv_val ) == val->bv_len );
517 * Schema-aware rewrite
519 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
520 ldap_rdnfree_x( rdn, ctx );
521 return LDAP_INVALID_SYNTAX;
525 * Back to string representation
527 rc = ldap_rdn2bv_x( rdn, out,
528 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
530 ldap_rdnfree_x( rdn, ctx );
532 if ( rc != LDAP_SUCCESS ) {
533 return LDAP_INVALID_SYNTAX;
535 } else {
536 ber_dupbv_x( out, val, ctx );
539 Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
541 return LDAP_SUCCESS;
545 dnPretty(
546 Syntax *syntax,
547 struct berval *val,
548 struct berval *out,
549 void *ctx)
551 assert( val != NULL );
552 assert( out != NULL );
554 Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
556 if ( val->bv_len == 0 ) {
557 ber_dupbv_x( out, val, ctx );
559 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
560 return LDAP_INVALID_SYNTAX;
562 } else {
563 LDAPDN dn = NULL;
564 int rc;
566 /* FIXME: should be liberal in what we accept */
567 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
568 if ( rc != LDAP_SUCCESS ) {
569 return LDAP_INVALID_SYNTAX;
572 assert( strlen( val->bv_val ) == val->bv_len );
575 * Schema-aware rewrite
577 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
578 ldap_dnfree_x( dn, ctx );
579 return LDAP_INVALID_SYNTAX;
582 /* FIXME: not sure why the default isn't pretty */
583 /* RE: the default is the form that is used as
584 * an internal representation; the pretty form
585 * is a variant */
586 rc = ldap_dn2bv_x( dn, out,
587 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
589 ldap_dnfree_x( dn, ctx );
591 if ( rc != LDAP_SUCCESS ) {
592 return LDAP_INVALID_SYNTAX;
596 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
598 return LDAP_SUCCESS;
602 rdnPretty(
603 Syntax *syntax,
604 struct berval *val,
605 struct berval *out,
606 void *ctx)
608 assert( val != NULL );
609 assert( out != NULL );
611 Debug( LDAP_DEBUG_TRACE, ">>> rdnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
613 if ( val->bv_len == 0 ) {
614 ber_dupbv_x( out, val, ctx );
616 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
617 return LDAP_INVALID_SYNTAX;
619 } else {
620 LDAPRDN rdn = NULL;
621 int rc;
622 char* p;
624 /* FIXME: should be liberal in what we accept */
625 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
626 LDAP_DN_FORMAT_LDAP, ctx);
627 if ( rc != LDAP_SUCCESS ) {
628 return LDAP_INVALID_SYNTAX;
631 assert( strlen( val->bv_val ) == val->bv_len );
634 * Schema-aware rewrite
636 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
637 ldap_rdnfree_x( rdn, ctx );
638 return LDAP_INVALID_SYNTAX;
641 /* FIXME: not sure why the default isn't pretty */
642 /* RE: the default is the form that is used as
643 * an internal representation; the pretty form
644 * is a variant */
645 rc = ldap_rdn2bv_x( rdn, out,
646 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
648 ldap_rdnfree_x( rdn, ctx );
650 if ( rc != LDAP_SUCCESS ) {
651 return LDAP_INVALID_SYNTAX;
655 Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
657 return LDAP_SUCCESS;
662 dnPrettyNormalDN(
663 Syntax *syntax,
664 struct berval *val,
665 LDAPDN *dn,
666 int flags,
667 void *ctx )
669 assert( val != NULL );
670 assert( dn != NULL );
672 Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n",
673 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
674 val->bv_val ? val->bv_val : "", 0 );
676 if ( val->bv_len == 0 ) {
677 return LDAP_SUCCESS;
679 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
680 return LDAP_INVALID_SYNTAX;
682 } else {
683 int rc;
685 /* FIXME: should be liberal in what we accept */
686 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
687 if ( rc != LDAP_SUCCESS ) {
688 return LDAP_INVALID_SYNTAX;
691 assert( strlen( val->bv_val ) == val->bv_len );
694 * Schema-aware rewrite
696 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
697 ldap_dnfree_x( *dn, ctx );
698 *dn = NULL;
699 return LDAP_INVALID_SYNTAX;
703 Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n",
704 flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
705 0, 0 );
707 return LDAP_SUCCESS;
711 * Combination of both dnPretty and dnNormalize
714 dnPrettyNormal(
715 Syntax *syntax,
716 struct berval *val,
717 struct berval *pretty,
718 struct berval *normal,
719 void *ctx)
721 Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
723 assert( val != NULL );
724 assert( pretty != NULL );
725 assert( normal != NULL );
727 if ( val->bv_len == 0 ) {
728 ber_dupbv_x( pretty, val, ctx );
729 ber_dupbv_x( normal, val, ctx );
731 } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
732 /* too big */
733 return LDAP_INVALID_SYNTAX;
735 } else {
736 LDAPDN dn = NULL;
737 int rc;
739 pretty->bv_val = NULL;
740 normal->bv_val = NULL;
741 pretty->bv_len = 0;
742 normal->bv_len = 0;
744 /* FIXME: should be liberal in what we accept */
745 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
746 if ( rc != LDAP_SUCCESS ) {
747 return LDAP_INVALID_SYNTAX;
750 assert( strlen( val->bv_val ) == val->bv_len );
753 * Schema-aware rewrite
755 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
756 ldap_dnfree_x( dn, ctx );
757 return LDAP_INVALID_SYNTAX;
760 rc = ldap_dn2bv_x( dn, pretty,
761 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
763 if ( rc != LDAP_SUCCESS ) {
764 ldap_dnfree_x( dn, ctx );
765 return LDAP_INVALID_SYNTAX;
768 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
769 ldap_dnfree_x( dn, ctx );
770 ber_memfree_x( pretty->bv_val, ctx );
771 pretty->bv_val = NULL;
772 pretty->bv_len = 0;
773 return LDAP_INVALID_SYNTAX;
776 rc = ldap_dn2bv_x( dn, normal,
777 LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
779 ldap_dnfree_x( dn, ctx );
780 if ( rc != LDAP_SUCCESS ) {
781 ber_memfree_x( pretty->bv_val, ctx );
782 pretty->bv_val = NULL;
783 pretty->bv_len = 0;
784 return LDAP_INVALID_SYNTAX;
788 Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
789 pretty->bv_val ? pretty->bv_val : "",
790 normal->bv_val ? normal->bv_val : "", 0 );
792 return LDAP_SUCCESS;
796 * dnMatch routine
799 dnMatch(
800 int *matchp,
801 slap_mask_t flags,
802 Syntax *syntax,
803 MatchingRule *mr,
804 struct berval *value,
805 void *assertedValue )
807 int match;
808 struct berval *asserted = (struct berval *) assertedValue;
810 assert( matchp != NULL );
811 assert( value != NULL );
812 assert( assertedValue != NULL );
813 assert( !BER_BVISNULL( value ) );
814 assert( !BER_BVISNULL( asserted ) );
816 match = value->bv_len - asserted->bv_len;
818 if ( match == 0 ) {
819 match = memcmp( value->bv_val, asserted->bv_val,
820 value->bv_len );
823 Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
824 match, value->bv_val, asserted->bv_val );
826 *matchp = match;
827 return LDAP_SUCCESS;
831 * dnRelativeMatch routine
834 dnRelativeMatch(
835 int *matchp,
836 slap_mask_t flags,
837 Syntax *syntax,
838 MatchingRule *mr,
839 struct berval *value,
840 void *assertedValue )
842 int match;
843 struct berval *asserted = (struct berval *) assertedValue;
845 assert( matchp != NULL );
846 assert( value != NULL );
847 assert( assertedValue != NULL );
848 assert( !BER_BVISNULL( value ) );
849 assert( !BER_BVISNULL( asserted ) );
851 if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
852 if( asserted->bv_len > value->bv_len ) {
853 match = -1;
854 } else if ( asserted->bv_len == value->bv_len ) {
855 match = memcmp( value->bv_val, asserted->bv_val,
856 value->bv_len );
857 } else {
858 if( DN_SEPARATOR(
859 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
861 match = memcmp(
862 &value->bv_val[value->bv_len - asserted->bv_len],
863 asserted->bv_val,
864 asserted->bv_len );
865 } else {
866 match = 1;
870 *matchp = match;
871 return LDAP_SUCCESS;
874 if( mr == slap_schema.si_mr_dnSuperiorMatch ) {
875 asserted = value;
876 value = (struct berval *) assertedValue;
877 mr = slap_schema.si_mr_dnSubordinateMatch;
880 if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
881 if( asserted->bv_len >= value->bv_len ) {
882 match = -1;
883 } else {
884 if( DN_SEPARATOR(
885 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
887 match = memcmp(
888 &value->bv_val[value->bv_len - asserted->bv_len],
889 asserted->bv_val,
890 asserted->bv_len );
891 } else {
892 match = 1;
896 *matchp = match;
897 return LDAP_SUCCESS;
900 if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
901 if( asserted->bv_len >= value->bv_len ) {
902 match = -1;
903 } else {
904 if( DN_SEPARATOR(
905 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
907 match = memcmp(
908 &value->bv_val[value->bv_len - asserted->bv_len],
909 asserted->bv_val,
910 asserted->bv_len );
912 if( !match ) {
913 struct berval rdn;
914 rdn.bv_val = value->bv_val;
915 rdn.bv_len = value->bv_len - asserted->bv_len - 1;
916 match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
918 } else {
919 match = 1;
923 *matchp = match;
924 return LDAP_SUCCESS;
927 /* should not be reachable */
928 assert( 0 );
929 return LDAP_OTHER;
933 rdnMatch(
934 int *matchp,
935 slap_mask_t flags,
936 Syntax *syntax,
937 MatchingRule *mr,
938 struct berval *value,
939 void *assertedValue )
941 int match;
942 struct berval *asserted = (struct berval *) assertedValue;
944 assert( matchp != NULL );
945 assert( value != NULL );
946 assert( assertedValue != NULL );
948 match = value->bv_len - asserted->bv_len;
950 if ( match == 0 ) {
951 match = memcmp( value->bv_val, asserted->bv_val,
952 value->bv_len );
955 Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
956 match, value->bv_val, asserted->bv_val );
958 *matchp = match;
959 return LDAP_SUCCESS;
964 * dnParent - dn's parent, in-place
965 * note: the incoming dn is assumed to be normalized/prettyfied,
966 * so that escaped rdn/ava separators are in '\'+hexpair form
968 * note: "dn" and "pdn" can point to the same berval;
969 * beware that, in this case, the pointer to the original buffer
970 * will get lost.
972 void
973 dnParent(
974 struct berval *dn,
975 struct berval *pdn )
977 char *p;
979 p = ber_bvchr( dn, ',' );
981 /* one-level dn */
982 if ( p == NULL ) {
983 pdn->bv_len = 0;
984 pdn->bv_val = dn->bv_val + dn->bv_len;
985 return;
988 assert( DN_SEPARATOR( p[ 0 ] ) );
989 p++;
991 assert( ATTR_LEADCHAR( p[ 0 ] ) );
992 pdn->bv_len = dn->bv_len - (p - dn->bv_val);
993 pdn->bv_val = p;
995 return;
999 * dnRdn - dn's rdn, in-place
1000 * note: the incoming dn is assumed to be normalized/prettyfied,
1001 * so that escaped rdn/ava separators are in '\'+hexpair form
1003 void
1004 dnRdn(
1005 struct berval *dn,
1006 struct berval *rdn )
1008 char *p;
1010 *rdn = *dn;
1011 p = ber_bvchr( dn, ',' );
1013 /* one-level dn */
1014 if ( p == NULL ) {
1015 return;
1018 assert( DN_SEPARATOR( p[ 0 ] ) );
1019 assert( ATTR_LEADCHAR( p[ 1 ] ) );
1020 rdn->bv_len = p - dn->bv_val;
1022 return;
1026 dnExtractRdn(
1027 struct berval *dn,
1028 struct berval *rdn,
1029 void *ctx )
1031 LDAPRDN tmpRDN;
1032 const char *p;
1033 int rc;
1035 assert( dn != NULL );
1036 assert( rdn != NULL );
1038 if( dn->bv_len == 0 ) {
1039 return LDAP_OTHER;
1042 rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1043 if ( rc != LDAP_SUCCESS ) {
1044 return rc;
1047 rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1048 ctx );
1050 ldap_rdnfree_x( tmpRDN, ctx );
1051 return rc;
1055 * We can assume the input is a prettied or normalized DN
1057 ber_len_t
1058 dn_rdnlen(
1059 Backend *be,
1060 struct berval *dn_in )
1062 const char *p;
1064 assert( dn_in != NULL );
1066 if ( dn_in == NULL ) {
1067 return 0;
1070 if ( !dn_in->bv_len ) {
1071 return 0;
1074 if ( be != NULL && be_issuffix( be, dn_in ) ) {
1075 return 0;
1078 p = ber_bvchr( dn_in, ',' );
1080 return p ? p - dn_in->bv_val : dn_in->bv_len;
1084 /* rdnValidate:
1086 * LDAP_SUCCESS if rdn is a legal rdn;
1087 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1090 rdn_validate( struct berval *rdn )
1092 #if 1
1093 /* Major cheat!
1094 * input is a pretty or normalized DN
1095 * hence, we can just search for ','
1097 if( rdn == NULL || rdn->bv_len == 0 ||
1098 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1100 return LDAP_INVALID_SYNTAX;
1102 return ber_bvchr( rdn, ',' ) == NULL
1103 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1105 #else
1106 LDAPRDN *RDN, **DN[ 2 ] = { &RDN, NULL };
1107 const char *p;
1108 int rc;
1111 * must be non-empty
1113 if ( rdn == NULL || rdn == '\0' ) {
1114 return 0;
1118 * must be parsable
1120 rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1121 if ( rc != LDAP_SUCCESS ) {
1122 return 0;
1126 * Must be one-level
1128 if ( p[ 0 ] != '\0' ) {
1129 return 0;
1133 * Schema-aware validate
1135 if ( rc == LDAP_SUCCESS ) {
1136 rc = LDAPDN_validate( DN );
1138 ldap_rdnfree( RDN );
1141 * Must validate (there's a repeated parsing ...)
1143 return ( rc == LDAP_SUCCESS );
1144 #endif
1148 /* build_new_dn:
1150 * Used by back-bdb back_modrdn to create the new dn of entries being
1151 * renamed.
1153 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1156 void
1157 build_new_dn( struct berval * new_dn,
1158 struct berval * parent_dn,
1159 struct berval * newrdn,
1160 void *memctx )
1162 char *ptr;
1164 if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1165 ber_dupbv_x( new_dn, newrdn, memctx );
1166 return;
1169 new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1170 new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1172 ptr = lutil_strncopy( new_dn->bv_val, newrdn->bv_val, newrdn->bv_len );
1173 *ptr++ = ',';
1174 strcpy( ptr, parent_dn->bv_val );
1179 * dnIsSuffix - tells whether suffix is a suffix of dn.
1180 * Both dn and suffix must be normalized.
1183 dnIsSuffix(
1184 const struct berval *dn,
1185 const struct berval *suffix )
1187 int d = dn->bv_len - suffix->bv_len;
1189 assert( dn != NULL );
1190 assert( suffix != NULL );
1192 /* empty suffix matches any dn */
1193 if ( suffix->bv_len == 0 ) {
1194 return 1;
1197 /* suffix longer than dn */
1198 if ( d < 0 ) {
1199 return 0;
1202 /* no rdn separator or escaped rdn separator */
1203 if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1204 return 0;
1207 /* no possible match or malformed dn */
1208 if ( d == 1 ) {
1209 return 0;
1212 /* compare */
1213 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1217 dnIsOneLevelRDN( struct berval *rdn )
1219 ber_len_t len = rdn->bv_len;
1220 for ( ; len--; ) {
1221 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1222 return 0;
1226 return 1;
1229 #ifdef HAVE_TLS
1230 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1231 #endif
1233 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1235 #ifdef HAVE_TLS
1236 if ( DNX509PeerNormalizeCertMap == NULL ) {
1237 DNX509PeerNormalizeCertMap = fn;
1238 return 0;
1240 #endif
1242 return -1;
1246 * Convert an X.509 DN into a normalized LDAP DN
1249 dnX509normalize( void *x509_name, struct berval *out )
1251 /* Invoke the LDAP library's converter with our schema-rewriter */
1252 int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1254 Debug( LDAP_DEBUG_TRACE,
1255 "dnX509Normalize: <%s> (%d)\n",
1256 BER_BVISNULL( out ) ? "(null)" : out->bv_val, rc, 0 );
1258 return rc;
1261 #ifdef HAVE_TLS
1263 * Get the TLS session's peer's DN into a normalized LDAP DN
1266 dnX509peerNormalize( void *ssl, struct berval *dn )
1268 int rc = LDAP_INVALID_CREDENTIALS;
1270 if ( DNX509PeerNormalizeCertMap != NULL )
1271 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1273 if ( rc != LDAP_SUCCESS ) {
1274 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1275 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
1278 return rc;
1280 #endif