]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
3c68a0b98ab55b6ad7ef3ae815a397b2ec59e8a6
[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         ldap_dnfree( dn );
136
137         if ( rc != LDAP_SUCCESS ) {
138                 return LDAP_INVALID_SYNTAX;
139         }
140
141         return LDAP_SUCCESS;
142 }
143
144 /*
145  * AVA sorting inside a RDN
146  *
147  * rule: sort attributeTypes in alphabetical order; in case of multiple
148  * occurrences of the same attributeType, sort values in byte order
149  * (use memcmp, which implies alphabetical order in case of IA5 value;
150  * this should guarantee the repeatability of the operation).
151  *
152  * Note: the sorting can be slightly improved by sorting first
153  * by attribute type length, then by alphabetical order.
154  *
155  * uses a linear search; should be fine since the number of AVAs in
156  * a RDN should be limited.
157  */
158 static void
159 AVA_Sort( LDAPRDN *rdn, int iAVA )
160 {
161         int             i;
162         LDAPAVA         *ava_in = rdn[ 0 ][ iAVA ];
163
164         assert( rdn );
165         assert( ava_in );
166         
167         for ( i = 0; i < iAVA; i++ ) {
168                 LDAPAVA         *ava = rdn[ 0 ][ i ];
169                 int             a, j;
170
171                 assert( ava );
172
173                 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
174
175                 if ( a > 0 ) {
176                         break;
177                 }
178
179                 while ( a == 0 ) {
180                         int             v, d;
181
182                         d = ava_in->la_value.bv_len - ava->la_value.bv_len;
183
184                         v = memcmp( ava_in->la_value.bv_val, 
185                                         ava->la_value.bv_val,
186                                         d <= 0 ? ava_in->la_value.bv_len 
187                                                 : ava->la_value.bv_len );
188
189                         if ( v == 0 && d != 0 ) {
190                                 v = d;
191                         }
192
193                         if ( v <= 0 ) {
194                                 /* 
195                                  * got it!
196                                  */
197                                 break;
198                         }
199
200                         if ( ++i == iAVA ) {
201                                 /*
202                                  * already sorted
203                                  */
204                                 return;
205                         }
206
207                         ava = rdn[ 0 ][ i ];
208                         a = strcmp( ava_in->la_attr.bv_val, 
209                                         ava->la_attr.bv_val );
210                 }
211
212                 /*
213                  * move ahead
214                  */
215                 for ( j = iAVA; j > i; j-- ) {
216                         rdn[ 0 ][ j ] = rdn[ 0 ][ j - 1 ];
217                 }
218                 rdn[ 0 ][ i ] = ava_in;
219
220                 return;
221         }
222 }
223
224 /*
225  * In-place, schema-aware normalization / "pretty"ing of the
226  * structural representation of a distinguished name.
227  */
228 static int
229 LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
230 {
231         int             iRDN;
232         int             rc;
233
234         assert( dn );
235
236         for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
237                 LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
238                 int             iAVA;
239
240                 assert( rdn );
241
242                 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
243                         LDAPAVA                 *ava = rdn[ 0 ][ iAVA ];
244                         AttributeDescription    *ad;
245                         slap_syntax_validate_func *validf = NULL;
246                         slap_syntax_transform_func *transf = NULL;
247                         MatchingRule *mr;
248                         struct berval           bv = { 0, NULL };
249                         int                     do_sort = 0;
250
251                         assert( ava );
252
253                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
254                                 const char      *text = NULL;
255
256                                 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
257                                 if ( rc != LDAP_SUCCESS ) {
258                                         return LDAP_INVALID_SYNTAX;
259                                 }
260                                 
261                                 ava->la_private = ( void * )ad;
262                                 do_sort = 1;
263                         }
264
265                         /* 
266                          * Replace attr oid/name with the canonical name
267                          */
268                         ava->la_attr = ad->ad_cname;
269
270                         if( ava->la_flags & LDAP_AVA_BINARY ) {
271                                 /* AVA is binary encoded, don't muck with it */
272                                 validf = NULL;
273                                 transf = NULL;
274                                 mr = NULL;
275                         } else if( flags & SLAP_LDAPDN_PRETTY ) {
276                                 validf = NULL;
277                                 transf = ad->ad_type->sat_syntax->ssyn_pretty;
278                                 mr = NULL;
279                         } else {
280                                 validf = ad->ad_type->sat_syntax->ssyn_validate;
281                                 transf = ad->ad_type->sat_syntax->ssyn_normalize;
282                                 mr = ad->ad_type->sat_equality;
283                         }
284
285                         if ( validf ) {
286                                 /* validate value before normalization */
287                                 rc = ( *validf )( ad->ad_type->sat_syntax,
288                                         ava->la_value.bv_len
289                                                 ? &ava->la_value
290                                                 : (struct berval *) &slap_empty_bv );
291
292                                 if ( rc != LDAP_SUCCESS ) {
293                                         return LDAP_INVALID_SYNTAX;
294                                 }
295                         }
296
297                         if ( transf ) {
298                                 /*
299                                  * transform value by normalize/pretty function
300                                  *      if value is empty, use empty_bv
301                                  */
302                                 rc = ( *transf )( ad->ad_type->sat_syntax,
303                                         ava->la_value.bv_len
304                                                 ? &ava->la_value
305                                                 : (struct berval *) &slap_empty_bv,
306                                         &bv );
307                         
308                                 if ( rc != LDAP_SUCCESS ) {
309                                         return LDAP_INVALID_SYNTAX;
310                                 }
311                         }
312
313                         if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
314                                 char *s = bv.bv_val;
315
316                                 if ( UTF8bvnormalize( &bv, &bv, 
317                                                 LDAP_UTF8_CASEFOLD ) == NULL ) {
318                                         return LDAP_INVALID_SYNTAX;
319                                 }
320                                 free( s );
321                         }
322
323                         if( bv.bv_val ) {
324                                 free( ava->la_value.bv_val );
325                                 ava->la_value = bv;
326                         }
327
328                         if( do_sort ) AVA_Sort( rdn, iAVA );
329                 }
330         }
331
332         return LDAP_SUCCESS;
333 }
334
335 /*
336  * dn normalize routine
337  */
338 int
339 dnNormalize(
340         Syntax *syntax,
341         struct berval *val,
342         struct berval **normalized )
343 {
344         struct berval *out;
345         int rc;
346
347         assert( normalized && *normalized == NULL );
348
349         out = ch_malloc( sizeof( struct berval ) );
350         rc = dnNormalize2( syntax, val, out );
351         if ( rc != LDAP_SUCCESS )
352                 free( out );
353         else
354                 *normalized = out;
355         return rc;
356 }
357
358 int
359 dnNormalize2(
360         Syntax *syntax,
361         struct berval *val,
362         struct berval *out )
363 {
364         assert( val );
365         assert( out );
366
367         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
368
369         if ( val->bv_len != 0 ) {
370                 LDAPDN          *dn = NULL;
371                 int             rc;
372
373                 /*
374                  * Go to structural representation
375                  */
376                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
377                 if ( rc != LDAP_SUCCESS ) {
378                         return LDAP_INVALID_SYNTAX;
379                 }
380
381                 assert( strlen( val->bv_val ) == val->bv_len );
382
383                 /*
384                  * Schema-aware rewrite
385                  */
386                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
387                         ldap_dnfree( dn );
388                         return LDAP_INVALID_SYNTAX;
389                 }
390
391                 /*
392                  * Back to string representation
393                  */
394                 rc = ldap_dn2bv( dn, out, LDAP_DN_FORMAT_LDAPV3 );
395
396                 ldap_dnfree( dn );
397
398                 if ( rc != LDAP_SUCCESS ) {
399                         return LDAP_INVALID_SYNTAX;
400                 }
401         } else {
402                 ber_dupbv( out, val );
403         }
404
405         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
406
407         return LDAP_SUCCESS;
408 }
409
410 /*
411  * dn "pretty"ing routine
412  */
413 int
414 dnPretty(
415         Syntax *syntax,
416         struct berval *val,
417         struct berval **pretty)
418 {
419         struct berval *out;
420         int rc;
421
422         assert( pretty && *pretty == NULL );
423
424         out = ch_malloc( sizeof( struct berval ) );
425         rc = dnPretty2( syntax, val, out );
426         if ( rc != LDAP_SUCCESS )
427                 free( out );
428         else
429                 *pretty = out;
430         return rc;
431 }
432
433 int
434 dnPretty2(
435         Syntax *syntax,
436         struct berval *val,
437         struct berval *out)
438 {
439         assert( val );
440         assert( out );
441
442         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
443
444         if ( val->bv_len == 0 ) {
445                 ber_dupbv( out, val );
446
447         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
448                 return LDAP_INVALID_SYNTAX;
449
450         } else {
451                 LDAPDN          *dn = NULL;
452                 int             rc;
453
454                 /* FIXME: should be liberal in what we accept */
455                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
456                 if ( rc != LDAP_SUCCESS ) {
457                         return LDAP_INVALID_SYNTAX;
458                 }
459
460                 assert( strlen( val->bv_val ) == val->bv_len );
461
462                 /*
463                  * Schema-aware rewrite
464                  */
465                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
466                         ldap_dnfree( dn );
467                         return LDAP_INVALID_SYNTAX;
468                 }
469
470                 /* FIXME: not sure why the default isn't pretty */
471                 /* RE: the default is the form that is used as
472                  * an internal representation; the pretty form
473                  * is a variant */
474                 rc = ldap_dn2bv( dn, out,
475                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
476
477                 ldap_dnfree( dn );
478
479                 if ( rc != LDAP_SUCCESS ) {
480                         return LDAP_INVALID_SYNTAX;
481                 }
482         }
483
484         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
485
486         return LDAP_SUCCESS;
487 }
488
489 /*
490  * Combination of both dnPretty and dnNormalize
491  */
492 int
493 dnPrettyNormal(
494         Syntax *syntax,
495         struct berval *val,
496         struct berval *pretty,
497         struct berval *normal)
498 {
499         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
500
501         assert( val );
502         assert( pretty );
503         assert( normal );
504
505         if ( val->bv_len == 0 ) {
506                 ber_dupbv( pretty, val );
507                 ber_dupbv( normal, val );
508
509         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
510                 /* too big */
511                 return LDAP_INVALID_SYNTAX;
512
513         } else {
514                 LDAPDN          *dn = NULL;
515                 int             rc;
516
517                 pretty->bv_val = NULL;
518                 normal->bv_val = NULL;
519                 pretty->bv_len = 0;
520                 normal->bv_len = 0;
521
522                 /* FIXME: should be liberal in what we accept */
523                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
524                 if ( rc != LDAP_SUCCESS ) {
525                         return LDAP_INVALID_SYNTAX;
526                 }
527
528                 assert( strlen( val->bv_val ) == val->bv_len );
529
530                 /*
531                  * Schema-aware rewrite
532                  */
533                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
534                         ldap_dnfree( dn );
535                         return LDAP_INVALID_SYNTAX;
536                 }
537
538                 rc = ldap_dn2bv( dn, pretty,
539                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
540
541                 if ( rc != LDAP_SUCCESS ) {
542                         ldap_dnfree( dn );
543                         return LDAP_INVALID_SYNTAX;
544                 }
545
546                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
547                         ldap_dnfree( dn );
548                         free( pretty->bv_val );
549                         pretty->bv_val = NULL;
550                         pretty->bv_len = 0;
551                         return LDAP_INVALID_SYNTAX;
552                 }
553
554                 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
555
556                 ldap_dnfree( dn );
557                 if ( rc != LDAP_SUCCESS ) {
558                         free( pretty->bv_val );
559                         pretty->bv_val = NULL;
560                         pretty->bv_len = 0;
561                         return LDAP_INVALID_SYNTAX;
562                 }
563         }
564
565         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
566                 pretty->bv_val, normal->bv_val, 0 );
567
568         return LDAP_SUCCESS;
569 }
570
571 /*
572  * dnMatch routine
573  */
574 int
575 dnMatch(
576         int *matchp,
577         slap_mask_t flags,
578         Syntax *syntax,
579         MatchingRule *mr,
580         struct berval *value,
581         void *assertedValue )
582 {
583         int match;
584         struct berval *asserted = (struct berval *) assertedValue;
585
586         assert( matchp );
587         assert( value );
588         assert( assertedValue );
589         
590         match = value->bv_len - asserted->bv_len;
591
592         if ( match == 0 ) {
593                 match = memcmp( value->bv_val, asserted->bv_val, 
594                                 value->bv_len );
595         }
596
597 #ifdef NEW_LOGGING
598         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
599                 "dnMatch: %d\n    %s\n    %s\n", match,
600                 value->bv_val, asserted->bv_val ));
601 #else
602         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
603                 match, value->bv_val, asserted->bv_val );
604 #endif
605
606         *matchp = match;
607         return( LDAP_SUCCESS );
608 }
609
610 /*
611  * dnParent - dn's parent, in-place
612  *
613  * note: the incoming dn is assumed to be normalized/prettyfied,
614  * so that escaped rdn/ava separators are in '\'+hexpair form
615  */
616 void
617 dnParent( 
618         struct berval   *dn, 
619         struct berval   *pdn )
620 {
621         char    *p;
622
623         p = strchr( dn->bv_val, ',' );
624
625         /* one-level dn */
626         if ( p == NULL ) {
627                 *pdn = slap_empty_bv;
628                 return;
629         }
630
631         assert( DN_SEPARATOR( p[ 0 ] ) );
632         p++;
633
634         assert( ATTR_LEADCHAR( p[ 0 ] ) );
635         pdn->bv_val = p;
636         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
637
638         return;
639 }
640
641 int
642 dnExtractRdn( 
643         struct berval   *dn, 
644         struct berval   *rdn )
645 {
646         LDAPRDN         *tmpRDN;
647         const char      *p;
648         int             rc;
649
650         assert( dn );
651         assert( rdn );
652
653         if( dn->bv_len == 0 ) {
654                 return LDAP_OTHER;
655         }
656
657         rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
658         if ( rc != LDAP_SUCCESS ) {
659                 return rc;
660         }
661
662         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
663         ldap_rdnfree( tmpRDN );
664         if ( rc != LDAP_SUCCESS ) {
665                 return rc;
666         }
667
668         return LDAP_SUCCESS;
669 }
670
671 /*
672  * We can assume the input is a prettied or normalized DN
673  */
674 int 
675 dn_rdnlen(
676         Backend         *be,
677         struct berval   *dn_in )
678 {
679         const char      *p;
680
681         assert( dn_in );
682
683         if ( dn_in == NULL ) {
684                 return 0;
685         }
686
687         if ( !dn_in->bv_len ) {
688                 return 0;
689         }
690
691         if ( be != NULL && be_issuffix( be, dn_in ) ) {
692                 return 0;
693         }
694
695         p = strchr( dn_in->bv_val, ',' );
696
697         return p ? p - dn_in->bv_val : dn_in->bv_len;
698 }
699
700
701 /* rdnValidate:
702  *
703  * LDAP_SUCCESS if rdn is a legal rdn;
704  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
705  */
706 int
707 rdnValidate( struct berval *rdn )
708 {
709 #if 1
710         /* Major cheat!
711          * input is a pretty or normalized DN
712          * hence, we can just search for ','
713          */
714         if( rdn == NULL || rdn->bv_len == 0 ||
715                 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
716         {
717                 return LDAP_INVALID_SYNTAX;
718         }
719
720         return strchr( rdn->bv_val, ',' ) == NULL
721                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
722
723 #else
724         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
725         const char      *p;
726         int             rc;
727
728         /*
729          * must be non-empty
730          */
731         if ( rdn == NULL || rdn == '\0' ) {
732                 return 0;
733         }
734
735         /*
736          * must be parsable
737          */
738         rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
739         if ( rc != LDAP_SUCCESS ) {
740                 return 0;
741         }
742
743         /*
744          * Must be one-level
745          */
746         if ( p[ 0 ] != '\0' ) {
747                 return 0;
748         }
749
750         /*
751          * Schema-aware validate
752          */
753         if ( rc == LDAP_SUCCESS ) {
754                 rc = LDAPDN_validate( DN );
755         }
756         ldap_rdnfree( RDN );
757
758         /*
759          * Must validate (there's a repeated parsing ...)
760          */
761         return ( rc == LDAP_SUCCESS );
762 #endif
763 }
764
765
766 /* build_new_dn:
767  *
768  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
769  * renamed.
770  *
771  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
772  */
773
774 void
775 build_new_dn( struct berval * new_dn,
776         struct berval * parent_dn,
777         struct berval * newrdn )
778 {
779         char *ptr;
780
781         if ( parent_dn == NULL ) {
782                 ber_dupbv( new_dn, newrdn );
783                 return;
784         }
785
786         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
787         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
788
789         ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
790         *ptr++ = ',';
791         strcpy( ptr, parent_dn->bv_val );
792 }
793
794
795 /*
796  * dnIsSuffix - tells whether suffix is a suffix of dn.
797  * Both dn and suffix must be normalized.
798  */
799 int
800 dnIsSuffix(
801         const struct berval *dn,
802         const struct berval *suffix )
803 {
804         int     d = dn->bv_len - suffix->bv_len;
805
806         assert( dn );
807         assert( suffix );
808
809         /* empty suffix matches any dn */
810         if ( suffix->bv_len == 0 ) {
811                 return 1;
812         }
813
814         /* suffix longer than dn */
815         if ( d < 0 ) {
816                 return 0;
817         }
818
819         /* no rdn separator or escaped rdn separator */
820         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
821                 return 0;
822         }
823
824         /* no possible match or malformed dn */
825         if ( d == 1 ) {
826                 return 0;
827         }
828
829         /* compare */
830         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
831 }
832
833 /*
834  * Convert an X.509 DN into a normalized LDAP DN
835  */
836 int
837 dnX509normalize( void *x509_name, struct berval *out )
838 {
839         /* Invoke the LDAP library's converter with our schema-rewriter */
840         return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
841 }
842
843 /*
844  * Get the TLS session's peer's DN into a normalized LDAP DN
845  */
846 char *
847 dnX509peerNormalize( void *ssl )
848 {
849         return ldap_pvt_tls_get_peer_dn( ssl, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
850 }