]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
Fix pkiUser
[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->bv_len = 0;
628                 pdn->bv_val = dn->bv_val + dn->bv_len;
629                 return;
630         }
631
632         assert( DN_SEPARATOR( p[ 0 ] ) );
633         p++;
634
635         assert( ATTR_LEADCHAR( p[ 0 ] ) );
636         pdn->bv_val = p;
637         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
638
639         return;
640 }
641
642 int
643 dnExtractRdn( 
644         struct berval   *dn, 
645         struct berval   *rdn )
646 {
647         LDAPRDN         *tmpRDN;
648         const char      *p;
649         int             rc;
650
651         assert( dn );
652         assert( rdn );
653
654         if( dn->bv_len == 0 ) {
655                 return LDAP_OTHER;
656         }
657
658         rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
659         if ( rc != LDAP_SUCCESS ) {
660                 return rc;
661         }
662
663         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
664         ldap_rdnfree( tmpRDN );
665         if ( rc != LDAP_SUCCESS ) {
666                 return rc;
667         }
668
669         return LDAP_SUCCESS;
670 }
671
672 /*
673  * We can assume the input is a prettied or normalized DN
674  */
675 int 
676 dn_rdnlen(
677         Backend         *be,
678         struct berval   *dn_in )
679 {
680         const char      *p;
681
682         assert( dn_in );
683
684         if ( dn_in == NULL ) {
685                 return 0;
686         }
687
688         if ( !dn_in->bv_len ) {
689                 return 0;
690         }
691
692         if ( be != NULL && be_issuffix( be, dn_in ) ) {
693                 return 0;
694         }
695
696         p = strchr( dn_in->bv_val, ',' );
697
698         return p ? p - dn_in->bv_val : dn_in->bv_len;
699 }
700
701
702 /* rdnValidate:
703  *
704  * LDAP_SUCCESS if rdn is a legal rdn;
705  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
706  */
707 int
708 rdnValidate( struct berval *rdn )
709 {
710 #if 1
711         /* Major cheat!
712          * input is a pretty or normalized DN
713          * hence, we can just search for ','
714          */
715         if( rdn == NULL || rdn->bv_len == 0 ||
716                 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
717         {
718                 return LDAP_INVALID_SYNTAX;
719         }
720
721         return strchr( rdn->bv_val, ',' ) == NULL
722                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
723
724 #else
725         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
726         const char      *p;
727         int             rc;
728
729         /*
730          * must be non-empty
731          */
732         if ( rdn == NULL || rdn == '\0' ) {
733                 return 0;
734         }
735
736         /*
737          * must be parsable
738          */
739         rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
740         if ( rc != LDAP_SUCCESS ) {
741                 return 0;
742         }
743
744         /*
745          * Must be one-level
746          */
747         if ( p[ 0 ] != '\0' ) {
748                 return 0;
749         }
750
751         /*
752          * Schema-aware validate
753          */
754         if ( rc == LDAP_SUCCESS ) {
755                 rc = LDAPDN_validate( DN );
756         }
757         ldap_rdnfree( RDN );
758
759         /*
760          * Must validate (there's a repeated parsing ...)
761          */
762         return ( rc == LDAP_SUCCESS );
763 #endif
764 }
765
766
767 /* build_new_dn:
768  *
769  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
770  * renamed.
771  *
772  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
773  */
774
775 void
776 build_new_dn( struct berval * new_dn,
777         struct berval * parent_dn,
778         struct berval * newrdn )
779 {
780         char *ptr;
781
782         if ( parent_dn == NULL ) {
783                 ber_dupbv( new_dn, newrdn );
784                 return;
785         }
786
787         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
788         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
789
790         ptr = slap_strcopy( new_dn->bv_val, newrdn->bv_val );
791         *ptr++ = ',';
792         strcpy( ptr, parent_dn->bv_val );
793 }
794
795
796 /*
797  * dnIsSuffix - tells whether suffix is a suffix of dn.
798  * Both dn and suffix must be normalized.
799  */
800 int
801 dnIsSuffix(
802         const struct berval *dn,
803         const struct berval *suffix )
804 {
805         int     d = dn->bv_len - suffix->bv_len;
806
807         assert( dn );
808         assert( suffix );
809
810         /* empty suffix matches any dn */
811         if ( suffix->bv_len == 0 ) {
812                 return 1;
813         }
814
815         /* suffix longer than dn */
816         if ( d < 0 ) {
817                 return 0;
818         }
819
820         /* no rdn separator or escaped rdn separator */
821         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
822                 return 0;
823         }
824
825         /* no possible match or malformed dn */
826         if ( d == 1 ) {
827                 return 0;
828         }
829
830         /* compare */
831         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
832 }
833
834 #ifdef HAVE_TLS
835 /*
836  * Convert an X.509 DN into a normalized LDAP DN
837  */
838 int
839 dnX509normalize( void *x509_name, struct berval *out )
840 {
841         /* Invoke the LDAP library's converter with our schema-rewriter */
842         return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
843 }
844
845 /*
846  * Get the TLS session's peer's DN into a normalized LDAP DN
847  */
848 int
849 dnX509peerNormalize( void *ssl, struct berval *dn )
850 {
851
852         return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
853 }
854 #endif