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