]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
896419dfbc2602d9cbe34b678effb37c47d85dc0
[openldap] / servers / slapd / dn.c
1 /* dn.c - routines for dealing with distinguished names */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16
17 #include "ldap_pvt.h"
18
19 #include "slap.h"
20
21 const struct berval slap_empty_bv = { 0, "" };
22
23 #define SLAP_LDAPDN_PRETTY 0x1
24
25 #define SLAP_LDAPDN_MAXLEN 8192
26
27 /*
28  * The DN syntax-related functions take advantage of the dn representation
29  * handling functions ldap_str2dn/ldap_dn2str.  The latter are not schema-
30  * aware, so the attributes and their values need be validated (and possibly
31  * normalized).  In the current implementation the required validation/nor-
32  * malization/"pretty"ing are done on newly created DN structural represen-
33  * tations; however the idea is to move towards DN handling in structural
34  * representation instead of the current string representation.  To this
35  * purpose, we need to do only the required operations and keep track of
36  * what has been done to minimize their impact on performances.
37  *
38  * Developers are strongly encouraged to use this feature, to speed-up
39  * its stabilization.
40  */
41
42 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
43
44 /*
45  * In-place, schema-aware validation of the
46  * structural representation of a distinguished name.
47  */
48 static int
49 LDAPDN_validate( LDAPDN *dn )
50 {
51         int             iRDN;
52         int             rc;
53
54         assert( dn );
55
56         for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
57                 LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
58                 int             iAVA;
59
60                 assert( rdn );
61
62                 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
63                         LDAPAVA                 *ava = rdn[ 0 ][ iAVA ];
64                         AttributeDescription    *ad;
65                         slap_syntax_validate_func *validate = NULL;
66
67                         assert( ava );
68                         
69                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
70                                 const char      *text = NULL;
71
72                                 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
73                                 if ( rc != LDAP_SUCCESS ) {
74                                         return LDAP_INVALID_SYNTAX;
75                                 }
76
77                                 ava->la_private = ( void * )ad;
78                         }
79
80                         /* 
81                          * Replace attr oid/name with the canonical name
82                          */
83                         ava->la_attr = ad->ad_cname;
84
85                         validate = ad->ad_type->sat_syntax->ssyn_validate;
86
87                         if ( validate ) {
88                                 /*
89                                  * validate value by validate function
90                                  */
91                                 rc = ( *validate )( ad->ad_type->sat_syntax,
92                                         &ava->la_value );
93                         
94                                 if ( rc != LDAP_SUCCESS ) {
95                                         return LDAP_INVALID_SYNTAX;
96                                 }
97                         }
98                 }
99         }
100
101         return LDAP_SUCCESS;
102 }
103
104 /*
105  * dn validate routine
106  */
107 int
108 dnValidate(
109         Syntax *syntax,
110         struct berval *in )
111 {
112         int             rc;
113         LDAPDN          *dn = NULL;
114
115         assert( in );
116
117         if ( in->bv_len == 0 ) {
118                 return LDAP_SUCCESS;
119
120         } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
121                 return LDAP_INVALID_SYNTAX;
122         }
123
124         rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
125         if ( rc != LDAP_SUCCESS ) {
126                 return LDAP_INVALID_SYNTAX;
127         }
128
129         assert( strlen( in->bv_val ) == in->bv_len );
130
131         /*
132          * Schema-aware validate
133          */
134         rc = LDAPDN_validate( dn );
135         if ( rc == LDAP_SUCCESS ) {
136                 ldap_dnfree( dn );
137         }
138         
139         return LDAP_INVALID_SYNTAX;
140 }
141
142 /*
143  * AVA sorting inside a RDN
144  *
145  * rule: sort attributeTypes in alphabetical order; in case of multiple
146  * occurrences of the same attributeType, sort values in byte order
147  * (use memcmp, which implies alphabetical order in case of IA5 value;
148  * this should guarantee the repeatability of the operation).
149  *
150  * Note: the sorting can be slightly improved by sorting first
151  * by attribute type length, then by alphabetical order.
152  *
153  * uses a linear search; should be fine since the number of AVAs in
154  * a RDN should be limited.
155  */
156 static void
157 AVA_Sort( LDAPRDN *rdn, int iAVA )
158 {
159         int             i;
160         LDAPAVA         *ava_in = rdn[ 0 ][ iAVA ];
161
162         assert( rdn );
163         assert( ava_in );
164         
165         for ( i = 0; i < iAVA; i++ ) {
166                 LDAPAVA         *ava = rdn[ 0 ][ i ];
167                 int             a, j;
168
169                 assert( ava );
170
171                 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
172
173                 if ( a > 0 ) {
174                         break;
175                 }
176
177                 while ( a == 0 ) {
178                         int             v, d;
179
180                         d = ava_in->la_value.bv_len - ava->la_value.bv_len;
181
182                         v = memcmp( ava_in->la_value.bv_val, 
183                                         ava->la_value.bv_val,
184                                         d <= 0 ? ava_in->la_value.bv_len 
185                                                 : ava->la_value.bv_len );
186
187                         if ( v == 0 && d != 0 ) {
188                                 v = d;
189                         }
190
191                         if ( v <= 0 ) {
192                                 /* 
193                                  * got it!
194                                  */
195                                 break;
196                         }
197
198                         if ( ++i == iAVA ) {
199                                 /*
200                                  * already sorted
201                                  */
202                                 return;
203                         }
204
205                         ava = rdn[ 0 ][ i ];
206                         a = strcmp( ava_in->la_attr.bv_val, 
207                                         ava->la_attr.bv_val );
208                 }
209
210                 /*
211                  * move ahead
212                  */
213                 for ( j = iAVA; j > i; j-- ) {
214                         rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
215                 }
216                 rdn[ 0 ][ i ] = ava_in;
217
218                 return;
219         }
220 }
221
222 /*
223  * In-place, schema-aware normalization / "pretty"ing of the
224  * structural representation of a distinguished name.
225  */
226 static int
227 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
228 {
229         int             iRDN;
230         int             rc;
231
232         assert( dn );
233
234         for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
235                 LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
236                 int             iAVA;
237
238                 assert( rdn );
239
240                 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
241                         LDAPAVA                 *ava = rdn[ 0 ][ iAVA ];
242                         AttributeDescription    *ad;
243                         slap_syntax_transform_func *transf = NULL;
244                         MatchingRule *mr;
245                         struct berval           bv = { 0, NULL };
246                         int                     do_sort = 0;
247
248                         assert( ava );
249
250                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
251                                 const char      *text = NULL;
252
253                                 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
254                                 if ( rc != LDAP_SUCCESS ) {
255                                         return LDAP_INVALID_SYNTAX;
256                                 }
257                                 
258                                 ava->la_private = ( void * )ad;
259                                 do_sort = 1;
260                         }
261
262                         /* 
263                          * Replace attr oid/name with the canonical name
264                          */
265                         ava->la_attr = ad->ad_cname;
266
267                         if( ava->la_flags & LDAP_AVA_BINARY ) {
268                                 /* AVA is binary encoded, don't muck with it */
269                                 transf = NULL;
270                                 mr = NULL;
271
272                         } else if( flags & SLAP_LDAPDN_PRETTY ) {
273                                 transf = ad->ad_type->sat_syntax->ssyn_pretty;
274                                 mr = NULL;
275                         } else {
276                                 transf = ad->ad_type->sat_syntax->ssyn_normalize;
277                                 mr = ad->ad_type->sat_equality;
278                         }
279
280                         if ( transf ) {
281                                 /*
282                                  * transform value by normalize/pretty function
283                                  *      if value is empty, use empty_bv
284                                  */
285                                 rc = ( *transf )( ad->ad_type->sat_syntax,
286                                         ava->la_value.bv_len
287                                                 ? &ava->la_value
288                                                 : (struct berval *) &slap_empty_bv,
289                                         &bv );
290                         
291                                 if ( rc != LDAP_SUCCESS ) {
292                                         return LDAP_INVALID_SYNTAX;
293                                 }
294                         }
295
296                         if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
297                                 char *s = bv.bv_val;
298
299                                 ber_str2bv( UTF8normalize( bv.bv_val ? &bv
300                                         : &ava->la_value, LDAP_UTF8_CASEFOLD ),
301                                         0, 0, &bv );
302                                 free( s );
303                         }
304
305                         if( bv.bv_val ) {
306                                 free( ava->la_value.bv_val );
307                                 ava->la_value = bv;
308                         }
309
310                         if( do_sort ) AVA_Sort( rdn, iAVA );
311                 }
312         }
313
314         return LDAP_SUCCESS;
315 }
316
317 /*
318  * dn normalize routine
319  */
320 int
321 dnNormalize(
322         Syntax *syntax,
323         struct berval *val,
324         struct berval **normalized )
325 {
326         struct berval *out;
327         int rc;
328
329         assert( normalized && *normalized == NULL );
330
331         out = ch_malloc( sizeof( struct berval ) );
332         rc = dnNormalize2( syntax, val, out );
333         if ( rc != LDAP_SUCCESS )
334                 free( out );
335         else
336                 *normalized = out;
337         return rc;
338 }
339
340 int
341 dnNormalize2(
342         Syntax *syntax,
343         struct berval *val,
344         struct berval *out )
345 {
346         assert( val );
347         assert( out );
348
349         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
350
351         if ( val->bv_len != 0 ) {
352                 LDAPDN          *dn = NULL;
353                 int             rc;
354
355                 /*
356                  * Go to structural representation
357                  */
358                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
359                 if ( rc != LDAP_SUCCESS ) {
360                         return LDAP_INVALID_SYNTAX;
361                 }
362
363                 assert( strlen( val->bv_val ) == val->bv_len );
364
365                 /*
366                  * Schema-aware rewrite
367                  */
368                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
369                         ldap_dnfree( dn );
370                         return LDAP_INVALID_SYNTAX;
371                 }
372
373                 /*
374                  * Back to string representation
375                  */
376                 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
377
378                 ldap_dnfree( dn );
379
380                 if ( rc != LDAP_SUCCESS ) {
381                         return LDAP_INVALID_SYNTAX;
382                 }
383         } else {
384                 ber_dupbv( out, val );
385         }
386
387         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
388
389         return LDAP_SUCCESS;
390 }
391
392 /*
393  * dn "pretty"ing routine
394  */
395 int
396 dnPretty(
397         Syntax *syntax,
398         struct berval *val,
399         struct berval **pretty)
400 {
401         struct berval *out;
402         int rc;
403
404         assert( pretty && *pretty == NULL );
405
406         out = ch_malloc( sizeof( struct berval ) );
407         rc = dnPretty2( syntax, val, out );
408         if ( rc != LDAP_SUCCESS )
409                 free( out );
410         else
411                 *pretty = out;
412         return rc;
413 }
414
415 int
416 dnPretty2(
417         Syntax *syntax,
418         struct berval *val,
419         struct berval *out)
420 {
421         assert( val );
422         assert( out );
423
424         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
425
426         if ( val->bv_len == 0 ) {
427                 ber_dupbv( out, val );
428
429         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
430                 return LDAP_INVALID_SYNTAX;
431
432         } else {
433                 LDAPDN          *dn = NULL;
434                 int             rc;
435
436                 /* FIXME: should be liberal in what we accept */
437                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
438                 if ( rc != LDAP_SUCCESS ) {
439                         return LDAP_INVALID_SYNTAX;
440                 }
441
442                 assert( strlen( val->bv_val ) == val->bv_len );
443
444                 /*
445                  * Schema-aware rewrite
446                  */
447                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
448                         ldap_dnfree( dn );
449                         return LDAP_INVALID_SYNTAX;
450                 }
451
452                 /* FIXME: not sure why the default isn't pretty */
453                 /* RE: the default is the form that is used as
454                  * an internal representation; the pretty form
455                  * is a variant */
456                 rc = ldap_dn2bv( dn, out,
457                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
458
459                 ldap_dnfree( dn );
460
461                 if ( rc != LDAP_SUCCESS ) {
462                         return LDAP_INVALID_SYNTAX;
463                 }
464         }
465
466         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
467
468         return LDAP_SUCCESS;
469 }
470
471 /*
472  * Combination of both dnPretty and dnNormalize
473  */
474 int
475 dnPrettyNormal(
476         Syntax *syntax,
477         struct berval *val,
478         struct berval *pretty,
479         struct berval *normal)
480 {
481         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
482
483         assert( val );
484         assert( pretty );
485         assert( normal );
486
487         if ( val->bv_len == 0 ) {
488                 ber_dupbv( pretty, val );
489                 ber_dupbv( normal, val );
490
491         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
492                 /* too big */
493                 return LDAP_INVALID_SYNTAX;
494
495         } else {
496                 LDAPDN          *dn = NULL;
497                 int             rc;
498
499                 pretty->bv_val = NULL;
500                 normal->bv_val = NULL;
501                 pretty->bv_len = 0;
502                 normal->bv_len = 0;
503
504                 /* FIXME: should be liberal in what we accept */
505                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
506                 if ( rc != LDAP_SUCCESS ) {
507                         return LDAP_INVALID_SYNTAX;
508                 }
509
510                 assert( strlen( val->bv_val ) == val->bv_len );
511
512                 /*
513                  * Schema-aware rewrite
514                  */
515                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
516                         ldap_dnfree( dn );
517                         return LDAP_INVALID_SYNTAX;
518                 }
519
520                 rc = ldap_dn2bv( dn, pretty,
521                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
522
523                 if ( rc != LDAP_SUCCESS ) {
524                         ldap_dnfree( dn );
525                         return LDAP_INVALID_SYNTAX;
526                 }
527
528                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
529                         ldap_dnfree( dn );
530                         free( pretty->bv_val );
531                         pretty->bv_val = NULL;
532                         pretty->bv_len = 0;
533                         return LDAP_INVALID_SYNTAX;
534                 }
535
536                 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
537
538                 ldap_dnfree( dn );
539                 if ( rc != LDAP_SUCCESS ) {
540                         free( pretty->bv_val );
541                         pretty->bv_val = NULL;
542                         pretty->bv_len = 0;
543                         return LDAP_INVALID_SYNTAX;
544                 }
545         }
546
547         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
548                 pretty->bv_val, normal->bv_val, 0 );
549
550         return LDAP_SUCCESS;
551 }
552
553 /*
554  * dnMatch routine
555  */
556 int
557 dnMatch(
558         int *matchp,
559         slap_mask_t flags,
560         Syntax *syntax,
561         MatchingRule *mr,
562         struct berval *value,
563         void *assertedValue )
564 {
565         int match;
566         struct berval *asserted = (struct berval *) assertedValue;
567
568         assert( matchp );
569         assert( value );
570         assert( assertedValue );
571         
572         match = value->bv_len - asserted->bv_len;
573
574         if ( match == 0 ) {
575                 match = memcmp( value->bv_val, asserted->bv_val, 
576                                 value->bv_len );
577         }
578
579 #ifdef NEW_LOGGING
580         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
581                 "dnMatch: %d\n    %s\n    %s\n", match,
582                 value->bv_val, asserted->bv_val ));
583 #else
584         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
585                 match, value->bv_val, asserted->bv_val );
586 #endif
587
588         *matchp = match;
589         return( LDAP_SUCCESS );
590 }
591
592 /*
593  * dnParent - dn's parent, in-place
594  *
595  * note: the incoming dn is assumed to be normalized/prettyfied,
596  * so that escaped rdn/ava separators are in '\'+hexpair form
597  */
598 void
599 dnParent( 
600         struct berval   *dn, 
601         struct berval   *pdn )
602 {
603         char    *p;
604
605         p = strchr( dn->bv_val, ',' );
606
607         /* one-level dn */
608         if ( p == NULL ) {
609                 *pdn = slap_empty_bv;
610                 return;
611         }
612
613         assert( DN_SEPARATOR( p[ 0 ] ) );
614         p++;
615
616         assert( ATTR_LEADCHAR( p[ 0 ] ) );
617         pdn->bv_val = p;
618         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
619
620         return;
621 }
622
623 int
624 dnExtractRdn( 
625         struct berval   *dn, 
626         struct berval   *rdn )
627 {
628         LDAPRDN         *tmpRDN;
629         const char      *p;
630         int             rc;
631
632         assert( dn );
633         assert( rdn );
634
635         if( dn->bv_len == 0 ) {
636                 return LDAP_OTHER;
637         }
638
639         rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
640         if ( rc != LDAP_SUCCESS ) {
641                 return rc;
642         }
643
644         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
645         ldap_rdnfree( tmpRDN );
646         if ( rc != LDAP_SUCCESS ) {
647                 return rc;
648         }
649
650         return LDAP_SUCCESS;
651 }
652
653 /*
654  * We can assume the input is a prettied or normalized DN
655  */
656 int 
657 dn_rdnlen(
658         Backend         *be,
659         struct berval   *dn_in )
660 {
661         const char      *p;
662
663         assert( dn_in );
664
665         if ( dn_in == NULL ) {
666                 return 0;
667         }
668
669         if ( !dn_in->bv_len ) {
670                 return 0;
671         }
672
673         if ( be != NULL && be_issuffix( be, dn_in ) ) {
674                 return 0;
675         }
676
677         p = strchr( dn_in->bv_val, ',' );
678
679         return p ? p - dn_in->bv_val : dn_in->bv_len;
680 }
681
682
683 /* rdnValidate:
684  *
685  * LDAP_SUCCESS if rdn is a legal rdn;
686  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
687  */
688 int
689 rdnValidate( struct berval *rdn )
690 {
691 #if 1
692         /* Major cheat!
693          * input is a pretty or normalized DN
694          * hence, we can just search for ','
695          */
696         if( rdn == NULL || rdn->bv_len == 0 ||
697                 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
698         {
699                 return LDAP_INVALID_SYNTAX;
700         }
701
702         return strchr( rdn->bv_val, ',' ) == NULL
703                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
704
705 #else
706         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
707         const char      *p;
708         int             rc;
709
710         /*
711          * must be non-empty
712          */
713         if ( rdn == NULL || rdn == '\0' ) {
714                 return 0;
715         }
716
717         /*
718          * must be parsable
719          */
720         rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
721         if ( rc != LDAP_SUCCESS ) {
722                 return 0;
723         }
724
725         /*
726          * Must be one-level
727          */
728         if ( p[ 0 ] != '\0' ) {
729                 return 0;
730         }
731
732         /*
733          * Schema-aware validate
734          */
735         if ( rc == LDAP_SUCCESS ) {
736                 rc = LDAPDN_validate( DN );
737         }
738         ldap_rdnfree( RDN );
739
740         /*
741          * Must validate (there's a repeated parsing ...)
742          */
743         return ( rc == LDAP_SUCCESS );
744 #endif
745 }
746
747
748 /* build_new_dn:
749  *
750  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
751  * renamed.
752  *
753  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
754  */
755
756 void
757 build_new_dn( struct berval * new_dn,
758         struct berval * parent_dn,
759         struct berval * newrdn )
760 {
761         char *ptr;
762
763         if ( parent_dn == NULL ) {
764                 ber_dupbv( new_dn, newrdn );
765                 return;
766         }
767
768         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
769         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
770
771         ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
772         *ptr++ = ',';
773         strcpy( ptr, parent_dn->bv_val );
774 }
775
776
777 /*
778  * dnIsSuffix - tells whether suffix is a suffix of dn.
779  * Both dn and suffix must be normalized.
780  */
781 int
782 dnIsSuffix(
783         const struct berval *dn,
784         const struct berval *suffix )
785 {
786         int     d = dn->bv_len - suffix->bv_len;
787
788         assert( dn );
789         assert( suffix );
790
791         /* empty suffix matches any dn */
792         if ( suffix->bv_len == 0 ) {
793                 return 1;
794         }
795
796         /* suffix longer than dn */
797         if ( d < 0 ) {
798                 return 0;
799         }
800
801         /* no rdn separator or escaped rdn separator */
802         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
803                 return 0;
804         }
805
806         /* no possible match or malformed dn */
807         if ( d == 1 ) {
808                 return 0;
809         }
810
811         /* compare */
812         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
813 }