]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
Import ITS#2007 and ITS#2009 bug fixes from HEAD
[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 #ifdef NEW_LOGGING
443         LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
444 #else
445         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
446 #endif
447
448         if ( val->bv_len == 0 ) {
449                 ber_dupbv( out, val );
450
451         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
452                 return LDAP_INVALID_SYNTAX;
453
454         } else {
455                 LDAPDN          *dn = NULL;
456                 int             rc;
457
458                 /* FIXME: should be liberal in what we accept */
459                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
460                 if ( rc != LDAP_SUCCESS ) {
461                         return LDAP_INVALID_SYNTAX;
462                 }
463
464                 assert( strlen( val->bv_val ) == val->bv_len );
465
466                 /*
467                  * Schema-aware rewrite
468                  */
469                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
470                         ldap_dnfree( dn );
471                         return LDAP_INVALID_SYNTAX;
472                 }
473
474                 /* FIXME: not sure why the default isn't pretty */
475                 /* RE: the default is the form that is used as
476                  * an internal representation; the pretty form
477                  * is a variant */
478                 rc = ldap_dn2bv( dn, out,
479                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
480
481                 ldap_dnfree( dn );
482
483                 if ( rc != LDAP_SUCCESS ) {
484                         return LDAP_INVALID_SYNTAX;
485                 }
486         }
487
488         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
489
490         return LDAP_SUCCESS;
491 }
492
493 /*
494  * Combination of both dnPretty and dnNormalize
495  */
496 int
497 dnPrettyNormal(
498         Syntax *syntax,
499         struct berval *val,
500         struct berval *pretty,
501         struct berval *normal)
502 {
503 #ifdef NEW_LOGGING
504         LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
505 #else
506         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
507 #endif
508
509         assert( val );
510         assert( pretty );
511         assert( normal );
512
513         if ( val->bv_len == 0 ) {
514                 ber_dupbv( pretty, val );
515                 ber_dupbv( normal, val );
516
517         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
518                 /* too big */
519                 return LDAP_INVALID_SYNTAX;
520
521         } else {
522                 LDAPDN          *dn = NULL;
523                 int             rc;
524
525                 pretty->bv_val = NULL;
526                 normal->bv_val = NULL;
527                 pretty->bv_len = 0;
528                 normal->bv_len = 0;
529
530                 /* FIXME: should be liberal in what we accept */
531                 rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP );
532                 if ( rc != LDAP_SUCCESS ) {
533                         return LDAP_INVALID_SYNTAX;
534                 }
535
536                 assert( strlen( val->bv_val ) == val->bv_len );
537
538                 /*
539                  * Schema-aware rewrite
540                  */
541                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
542                         ldap_dnfree( dn );
543                         return LDAP_INVALID_SYNTAX;
544                 }
545
546                 rc = ldap_dn2bv( dn, pretty,
547                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
548
549                 if ( rc != LDAP_SUCCESS ) {
550                         ldap_dnfree( dn );
551                         return LDAP_INVALID_SYNTAX;
552                 }
553
554                 if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
555                         ldap_dnfree( dn );
556                         free( pretty->bv_val );
557                         pretty->bv_val = NULL;
558                         pretty->bv_len = 0;
559                         return LDAP_INVALID_SYNTAX;
560                 }
561
562                 rc = ldap_dn2bv( dn, normal, LDAP_DN_FORMAT_LDAPV3 );
563
564                 ldap_dnfree( dn );
565                 if ( rc != LDAP_SUCCESS ) {
566                         free( pretty->bv_val );
567                         pretty->bv_val = NULL;
568                         pretty->bv_len = 0;
569                         return LDAP_INVALID_SYNTAX;
570                 }
571         }
572
573 #ifdef NEW_LOGGING
574         LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
575                 pretty->bv_val, normal->bv_val, 0  );
576 #else
577         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
578                 pretty->bv_val, normal->bv_val, 0 );
579 #endif
580
581         return LDAP_SUCCESS;
582 }
583
584 /*
585  * dnMatch routine
586  */
587 int
588 dnMatch(
589         int *matchp,
590         slap_mask_t flags,
591         Syntax *syntax,
592         MatchingRule *mr,
593         struct berval *value,
594         void *assertedValue )
595 {
596         int match;
597         struct berval *asserted = (struct berval *) assertedValue;
598
599         assert( matchp );
600         assert( value );
601         assert( assertedValue );
602         
603         match = value->bv_len - asserted->bv_len;
604
605         if ( match == 0 ) {
606                 match = memcmp( value->bv_val, asserted->bv_val, 
607                                 value->bv_len );
608         }
609
610 #ifdef NEW_LOGGING
611         LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n    %s\n    %s\n", 
612                 match, value->bv_val, asserted->bv_val  );
613 #else
614         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
615                 match, value->bv_val, asserted->bv_val );
616 #endif
617
618         *matchp = match;
619         return( LDAP_SUCCESS );
620 }
621
622 /*
623  * dnParent - dn's parent, in-place
624  *
625  * note: the incoming dn is assumed to be normalized/prettyfied,
626  * so that escaped rdn/ava separators are in '\'+hexpair form
627  */
628 void
629 dnParent( 
630         struct berval   *dn, 
631         struct berval   *pdn )
632 {
633         char    *p;
634
635         p = strchr( dn->bv_val, ',' );
636
637         /* one-level dn */
638         if ( p == NULL ) {
639                 pdn->bv_len = 0;
640                 pdn->bv_val = dn->bv_val + dn->bv_len;
641                 return;
642         }
643
644         assert( DN_SEPARATOR( p[ 0 ] ) );
645         p++;
646
647         assert( ATTR_LEADCHAR( p[ 0 ] ) );
648         pdn->bv_val = p;
649         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
650
651         return;
652 }
653
654 int
655 dnExtractRdn( 
656         struct berval   *dn, 
657         struct berval   *rdn )
658 {
659         LDAPRDN         *tmpRDN;
660         const char      *p;
661         int             rc;
662
663         assert( dn );
664         assert( rdn );
665
666         if( dn->bv_len == 0 ) {
667                 return LDAP_OTHER;
668         }
669
670         rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
671         if ( rc != LDAP_SUCCESS ) {
672                 return rc;
673         }
674
675         rc = ldap_rdn2bv( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 );
676         ldap_rdnfree( tmpRDN );
677         if ( rc != LDAP_SUCCESS ) {
678                 return rc;
679         }
680
681         return LDAP_SUCCESS;
682 }
683
684 /*
685  * We can assume the input is a prettied or normalized DN
686  */
687 int 
688 dn_rdnlen(
689         Backend         *be,
690         struct berval   *dn_in )
691 {
692         const char      *p;
693
694         assert( dn_in );
695
696         if ( dn_in == NULL ) {
697                 return 0;
698         }
699
700         if ( !dn_in->bv_len ) {
701                 return 0;
702         }
703
704         if ( be != NULL && be_issuffix( be, dn_in ) ) {
705                 return 0;
706         }
707
708         p = strchr( dn_in->bv_val, ',' );
709
710         return p ? p - dn_in->bv_val : dn_in->bv_len;
711 }
712
713
714 /* rdnValidate:
715  *
716  * LDAP_SUCCESS if rdn is a legal rdn;
717  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
718  */
719 int
720 rdnValidate( struct berval *rdn )
721 {
722 #if 1
723         /* Major cheat!
724          * input is a pretty or normalized DN
725          * hence, we can just search for ','
726          */
727         if( rdn == NULL || rdn->bv_len == 0 ||
728                 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
729         {
730                 return LDAP_INVALID_SYNTAX;
731         }
732
733         return strchr( rdn->bv_val, ',' ) == NULL
734                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
735
736 #else
737         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
738         const char      *p;
739         int             rc;
740
741         /*
742          * must be non-empty
743          */
744         if ( rdn == NULL || rdn == '\0' ) {
745                 return 0;
746         }
747
748         /*
749          * must be parsable
750          */
751         rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
752         if ( rc != LDAP_SUCCESS ) {
753                 return 0;
754         }
755
756         /*
757          * Must be one-level
758          */
759         if ( p[ 0 ] != '\0' ) {
760                 return 0;
761         }
762
763         /*
764          * Schema-aware validate
765          */
766         if ( rc == LDAP_SUCCESS ) {
767                 rc = LDAPDN_validate( DN );
768         }
769         ldap_rdnfree( RDN );
770
771         /*
772          * Must validate (there's a repeated parsing ...)
773          */
774         return ( rc == LDAP_SUCCESS );
775 #endif
776 }
777
778
779 /* build_new_dn:
780  *
781  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
782  * renamed.
783  *
784  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
785  */
786
787 void
788 build_new_dn( struct berval * new_dn,
789         struct berval * parent_dn,
790         struct berval * newrdn )
791 {
792         char *ptr;
793
794         if ( parent_dn == NULL ) {
795                 ber_dupbv( new_dn, newrdn );
796                 return;
797         }
798
799         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
800         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
801
802         ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
803         *ptr++ = ',';
804         strcpy( ptr, parent_dn->bv_val );
805 }
806
807
808 /*
809  * dnIsSuffix - tells whether suffix is a suffix of dn.
810  * Both dn and suffix must be normalized.
811  */
812 int
813 dnIsSuffix(
814         const struct berval *dn,
815         const struct berval *suffix )
816 {
817         int     d = dn->bv_len - suffix->bv_len;
818
819         assert( dn );
820         assert( suffix );
821
822         /* empty suffix matches any dn */
823         if ( suffix->bv_len == 0 ) {
824                 return 1;
825         }
826
827         /* suffix longer than dn */
828         if ( d < 0 ) {
829                 return 0;
830         }
831
832         /* no rdn separator or escaped rdn separator */
833         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
834                 return 0;
835         }
836
837         /* no possible match or malformed dn */
838         if ( d == 1 ) {
839                 return 0;
840         }
841
842         /* compare */
843         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
844 }
845
846 #ifdef HAVE_TLS
847 /*
848  * Convert an X.509 DN into a normalized LDAP DN
849  */
850 int
851 dnX509normalize( void *x509_name, struct berval *out )
852 {
853         /* Invoke the LDAP library's converter with our schema-rewriter */
854         return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
855 }
856
857 /*
858  * Get the TLS session's peer's DN into a normalized LDAP DN
859  */
860 int
861 dnX509peerNormalize( void *ssl, struct berval *dn )
862 {
863
864         return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
865 }
866 #endif